File size: 7,252 Bytes
5a600cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Copyright (C) 2020-2022 OpenFOAM Foundation
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "MPLICface.H"


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::MPLICface::MPLICface(const bool unweighted)
:
    unweighted_(unweighted)
{};


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

Foam::label Foam::MPLICface::cutFace
(
    const labelList& f,
    const labelList& faceEdges,
    const pointField& points,
    const boolList& isEdgeCutOld,
    boolList& isEdgeCut,
    label& faceEdgei,
    const UList<scalar>& pointsAlpha,
    const UList<vector>& pointsU,
    const label facei,
    const scalar target,
    const bool ow
)
{
    // Clear all the storage
    cutPoints_.clear();
    subPoints_.clear();
    cutEdges_.clear();
    subPointsU_.clear();

    // Direction of face circulators
    flipped_ = false;

    label fp;
    if (faceEdgei == -1)
    {
        const UIndirectList<scalar> fAlphas(pointsAlpha, f);

        // Starting from this point on the face
        fp = findMin(fAlphas);
        flipped_ = ow ? false : true;
    }
    else
    {
        // Finding first index of the edge in the face point list
        const label startPoint = faceEdgei;

        // Pick up starting point and decide direction of circulators
        const label fwd = f.fcIndex(startPoint);
        const label back = f.rcIndex(startPoint);
        const label lEdgeP = f[fwd] == f[f.fcIndex(faceEdgei)] ? fwd : back;

        // Starting from this point on the face
        fp = pointsAlpha[f[startPoint]] < pointsAlpha[f[lEdgeP]]
            ? startPoint : lEdgeP;

        if
        (
            !(
                (
                    lEdgeP == fwd
                 && pointsAlpha[f[startPoint]] < pointsAlpha[f[lEdgeP]]
                )
             || (
                    lEdgeP == back
                 && pointsAlpha[f[startPoint]] > pointsAlpha[f[lEdgeP]]
                )
             )
        )
        {
            flipped_ = true;
        }
    }

    label nextFp, edgei;
    if (flipped_)
    {
        nextFp = f.rcIndex(fp);
        edgei = nextFp;
    }
    else
    {
        nextFp = f.fcIndex(fp);
        edgei = fp;
    }

    forAll(f, i)
    {
        // Edge points field value
        const scalar& fl = pointsAlpha[f[fp]];
        const scalar& fk = pointsAlpha[f[nextFp]];

        const point& pL = points[f[fp]];
        const point& pK = points[f[nextFp]];

        const label edg = faceEdges[edgei];

        // Collect sub-point if the iso-value is bigger than target value
        if (fl >= target && cutPoints_.size() > 0)
        {
            subPoints_.append(pL);
            if (!unweighted_)
            {
                subPointsU_.append(pointsU[f[fp]]);
            }
        }

        // Cut the edge
        if
        (
            !isEdgeCutOld[edg]
         && ((fl < target && fk >= target) || (fl >= target && fk < target))
        )
        {
            const scalar coeff = (target - fk)/(fl - fk);
            const point cut = pK + coeff*(pL - pK);
            if (!unweighted_)
            {
                subPointsU_.append
                (
                    pointsU[f[nextFp]]
                  + coeff*(pointsU[f[fp]] - pointsU[f[nextFp]])
                );
            }
            cutPoints_.append(cut);
            subPoints_.append(cut);
            cutEdges_.append(edg);
            isEdgeCut[edg] =  true;
        }

        // Termination control
        if (cutPoints_.size() == 2)
        {
            faceEdgei = edgei;
            return 1;
        }

        if (flipped_)
        {
            fp = f.rcIndex(fp);
            nextFp = f.rcIndex(fp);
            edgei = nextFp;
        }
        else
        {
            fp = f.fcIndex(fp);
            nextFp = f.fcIndex(fp);
            edgei = fp;
        }
    }

    return 0;
}


Foam::label Foam::MPLICface::cutFace
(
    const UList<label>& f,
    const UList<point>& points,
    const UList<scalar>& pointsAlpha,
    const UList<vector>& pointsU,
    const scalar target,
    const bool ow
)
{
    // Clear all the storage
    cutPoints_.clear();
    subPoints_.clear();
    subPointsU_.clear();

    // Direction of face circulators
    flipped_ = ow ? false : true;

    const UIndirectList<scalar> fAlphas(pointsAlpha, f);

    // Starting from point with minimum alpha value
    label fp = findMin(fAlphas);

    // Second point index
    label nextFp = flipped_  ? f.rcIndex(fp) : f.fcIndex(fp);

    forAll(f, i)
    {
        // Edge points field value
        const scalar& fl = pointsAlpha[f[fp]];
        const scalar& fk = pointsAlpha[f[nextFp]];

        const point& pL = points[f[fp]];
        const point& pK = points[f[nextFp]];

        // Collect sub-point if the iso-value is bigger than target value
        if (fl >= target && cutPoints_.size() > 0)
        {
            subPoints_.append(pL);
            if (!unweighted_)
            {
                subPointsU_.append(pointsU[f[fp]]);
            }
        }

        // Cut the edge
        if ((fl < target && fk >= target) || (fl >= target && fk < target))
        {
            const scalar coeff = (target - fk)/(fl - fk);
            const point cut = pK + coeff*(pL - pK);
            if (!unweighted_)
            {
                subPointsU_.append
                (
                    pointsU[f[nextFp]]
                  + coeff*(pointsU[f[fp]] - pointsU[f[nextFp]])
                );
            }
            cutPoints_.append(cut);
            subPoints_.append(cut);
        }

        if (flipped_)
        {
            fp = f.rcIndex(fp);
            nextFp = f.rcIndex(fp);
        }
        else
        {
            fp = f.fcIndex(fp);
            nextFp = f.fcIndex(fp);
        }
    }

    // Simple cut
    if (cutPoints_.size() == 2)
    {
        return 1;
    }

    // Multiple cuts through the face
    else if (cutPoints_.size() > 2)
    {
        return -1;
    }

    // No cut
    else
    {
        return 0;
    }
}


// ************************************************************************* //