pointConstraints.C 12.6 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
OpenFOAM bot's avatar
OpenFOAM bot committed
6 7
     \\/     M anipulation  |
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8 9
    Copyright (C) 2013-2016 OpenFOAM Foundation
    Copyright (C) 2017-2019 OpenCFD Ltd.
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
-------------------------------------------------------------------------------
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 "pointConstraints.H"
#include "emptyPointPatch.H"
#include "polyMesh.H"
#include "pointMesh.H"
#include "globalMeshData.H"
#include "twoDPointCorrector.H"

36
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 38 39

namespace Foam
{
40 41
    defineTypeNameAndDebug(pointConstraints, 0);
}
42 43 44 45


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

46
void Foam::pointConstraints::makePatchPatchAddressing()
47 48 49 50 51
{
    if (debug)
    {
        Pout<< "pointConstraints::makePatchPatchAddressing() : "
            << "constructing boundary addressing"
52
            << endl << incrIndent;
53 54
    }

55 56
    const pointMesh& pMesh = mesh();
    const polyMesh& mesh = pMesh();
57 58

    const pointBoundaryMesh& pbm = pMesh.boundary();
59
    const polyBoundaryMesh& bm = mesh.boundaryMesh();
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75


    // first count the total number of patch-patch points

    label nPatchPatchPoints = 0;

    forAll(pbm, patchi)
    {
        if (!isA<emptyPointPatch>(pbm[patchi]) && !pbm[patchi].coupled())
        {
            const labelList& bp = bm[patchi].boundaryPoints();

            nPatchPatchPoints += bp.size();

            if (debug)
            {
76
                Pout<< indent << "On patch:" << pbm[patchi].name()
77 78 79 80 81 82 83
                    << " nBoundaryPoints:" << bp.size() << endl;
            }
        }
    }

    if (debug)
    {
84 85
        Pout<< indent << "Found nPatchPatchPoints:" << nPatchPatchPoints
            << endl;
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
    }


    // Go through all patches and mark up the external edge points
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    // From meshpoint to index in patchPatchPointConstraints_.
    Map<label> patchPatchPointSet(2*nPatchPatchPoints);

    // Constraints (initialised to unconstrained)
    patchPatchPointConstraints_.setSize(nPatchPatchPoints);
    patchPatchPointConstraints_ = pointConstraint();

    // From constraint index to mesh point
    labelList patchPatchPoints(nPatchPatchPoints);

    label pppi = 0;

    forAll(pbm, patchi)
    {
        if (!isA<emptyPointPatch>(pbm[patchi]) && !pbm[patchi].coupled())
        {
            const labelList& bp = bm[patchi].boundaryPoints();
            const labelList& meshPoints = pbm[patchi].meshPoints();

            forAll(bp, pointi)
            {
113
                const label ppp = meshPoints[bp[pointi]];
114

115
                const auto iter = patchPatchPointSet.cfind(ppp);
116 117 118

                label constraintI = -1;

119
                if (iter.found())
120
                {
121
                    constraintI = iter.val();
122 123 124
                }
                else
                {
125 126 127
                    patchPatchPointSet.insert(ppp, pppi);
                    patchPatchPoints[pppi] = ppp;
                    constraintI = pppi++;
128 129 130 131 132 133 134 135 136 137 138 139 140 141
                }

                // Apply to patch constraints
                pbm[patchi].applyConstraint
                (
                    bp[pointi],
                    patchPatchPointConstraints_[constraintI]
                );
            }
        }
    }

    if (debug)
    {
142
        Pout<< indent << "Have (local) constrained points:"
143 144 145 146 147 148 149 150
            << nPatchPatchPoints << endl;
    }


    // Extend set with constraints across coupled points
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    {
151
        const globalMeshData& gd = mesh.globalData();
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
        const labelListList& globalPointSlaves = gd.globalPointSlaves();
        const mapDistribute& globalPointSlavesMap = gd.globalPointSlavesMap();
        const Map<label>& cpPointMap = gd.coupledPatch().meshPointMap();
        const labelList& cpMeshPoints = gd.coupledPatch().meshPoints();

        // Constraints on coupled points
        List<pointConstraint> constraints
        (
            globalPointSlavesMap.constructSize()
        );

        // Copy from patchPatch constraints into coupledConstraints.
        forAll(pbm, patchi)
        {
            if (!isA<emptyPointPatch>(pbm[patchi]) && !pbm[patchi].coupled())
            {
                const labelList& bp = bm[patchi].boundaryPoints();
                const labelList& meshPoints = pbm[patchi].meshPoints();

                forAll(bp, pointi)
                {
173 174 175
                    const label ppp = meshPoints[bp[pointi]];

                    const auto iter = cpPointMap.cfind(ppp);
176

177
                    if (iter.found())
178 179 180
                    {
                        // Can just copy (instead of apply) constraint
                        // will already be consistent across multiple patches.
181
                        constraints[iter.val()] = patchPatchPointConstraints_
182 183 184 185 186 187 188 189 190 191 192 193
                        [
                            patchPatchPointSet[ppp]
                        ];
                    }
                }
            }
        }

        // Exchange data
        globalPointSlavesMap.distribute(constraints);

        // Combine master with slave constraints
194
        forAll(globalPointSlaves, pointi)
195
        {
196
            const labelList& slaves = globalPointSlaves[pointi];
197 198 199 200

            // Combine master constraint with slave constraints
            forAll(slaves, i)
            {
201
                constraints[pointi].combine(constraints[slaves[i]]);
202 203 204 205
            }
            // Duplicate master constraint into slave slots
            forAll(slaves, i)
            {
206
                constraints[slaves[i]] = constraints[pointi];
207 208 209 210 211 212 213 214 215 216 217
            }
        }

        // Send back
        globalPointSlavesMap.reverseDistribute
        (
            cpMeshPoints.size(),
            constraints
        );

        // Add back into patchPatch constraints
218
        forAll(constraints, coupledPointi)
219
        {
220
            if (constraints[coupledPointi].first() != 0)
221
            {
222
                label meshPointi = cpMeshPoints[coupledPointi];
223

224
                const auto iter = patchPatchPointSet.cfind(meshPointi);
225 226 227

                label constraintI = -1;

228 229 230 231 232 233 234 235 236 237 238 239
                if (iter.found())
                {
                    //Pout<< indent << "on meshpoint:" << meshPointi
                    //    << " coupled:" << coupledPointi
                    //    << " at:" << mesh.points()[meshPointi]
                    //    << " have possibly extended constraint:"
                    //    << constraints[coupledPointi]
                    //    << endl;

                    constraintI = iter.val();
                }
                else
240
                {
241 242 243
                    //Pout<< indent << "on meshpoint:" << meshPointi
                    //    << " coupled:" << coupledPointi
                    //    << " at:" << mesh.points()[meshPointi]
244
                    //    << " have new constraint:"
245
                    //    << constraints[coupledPointi]
246 247 248 249 250
                    //    << endl;

                    // Allocate new constraint
                    if (patchPatchPoints.size() <= pppi)
                    {
251 252 253 254
                        // Check if not enough space. This
                        // can occasionally happen if -coupled points connect
                        // to the inside of a patch -these coupled points also
                        // carry a constraint
255
                        patchPatchPoints.setSize(pppi+100);
256 257 258 259 260
                        patchPatchPointConstraints_.setSize
                        (
                            pppi+100,
                            pointConstraint()
                        );
261
                    }
262 263
                    patchPatchPointSet.insert(meshPointi, pppi);
                    patchPatchPoints[pppi] = meshPointi;
264 265 266 267 268 269 270
                    constraintI = pppi++;
                }

                // Combine (new or existing) constraint with one
                // on coupled.
                patchPatchPointConstraints_[constraintI].combine
                (
271
                    constraints[coupledPointi]
272 273 274 275 276 277 278 279 280 281 282 283 284 285
                );
            }
        }
    }



    nPatchPatchPoints = pppi;
    patchPatchPoints.setSize(nPatchPatchPoints);
    patchPatchPointConstraints_.setSize(nPatchPatchPoints);


    if (debug)
    {
286
        Pout<< indent << "Have (global) constrained points:"
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
            << nPatchPatchPoints << endl;
    }


    // Copy out all non-trivial constraints
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    patchPatchPointConstraintPoints_.setSize(nPatchPatchPoints);
    patchPatchPointConstraintTensors_.setSize(nPatchPatchPoints);

    label nConstraints = 0;

    forAll(patchPatchPointConstraints_, i)
    {
        // Note: check for more than zero constraints. (could check for
        //       more than one constraint but what about coupled points that
        //       inherit the constraintness)
        if (patchPatchPointConstraints_[i].first() != 0)
        {
            patchPatchPointConstraintPoints_[nConstraints] =
                patchPatchPoints[i];

            patchPatchPointConstraintTensors_[nConstraints] =
                patchPatchPointConstraints_[i].constraintTransformation();

            nConstraints++;
        }
    }

    if (debug)
    {
318
        Pout<< indent << "Have non-trivial constrained points:"
319 320 321 322 323 324 325 326 327 328
            << nConstraints << endl;
    }

    patchPatchPointConstraints_.setSize(nConstraints);
    patchPatchPointConstraintPoints_.setSize(nConstraints);
    patchPatchPointConstraintTensors_.setSize(nConstraints);


    if (debug)
    {
329 330
        Pout<< decrIndent
            << "pointConstraints::makePatchPatchAddressing() : "
331 332 333 334 335 336 337 338
            << "finished constructing boundary addressing"
            << endl;
    }
}


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

339
Foam::pointConstraints::pointConstraints(const pointMesh& pm)
340
:
341
    MeshObject<pointMesh, Foam::UpdateableMeshObject, pointConstraints>(pm)
342
{
343 344 345 346 347 348 349
    if (debug)
    {
        Pout<< "pointConstraints::pointConstraints(const pointMesh&): "
            << "Constructing from pointMesh " << pm.name()
            << endl;
    }

350 351 352 353 354 355
    makePatchPatchAddressing();
}


// * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * * //

356
Foam::pointConstraints::~pointConstraints()
357 358 359 360 361 362
{
    if (debug)
    {
        Pout<< "pointConstraints::~pointConstraints()" << endl;
    }
}
363 364 365 366


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

367
void Foam::pointConstraints::updateMesh(const mapPolyMesh&)
368 369 370 371 372
{
    makePatchPatchAddressing();
}


373
bool Foam::pointConstraints::movePoints()
374 375 376 377 378
{
    return true;
}


379
void Foam::pointConstraints::constrainDisplacement
380 381 382 383 384 385 386 387 388 389 390 391 392 393
(
    pointVectorField& pf,
    const bool overrideFixedValue
) const
{
    // Override constrained pointPatchField types with the constraint value.
    // This relies on only constrained pointPatchField implementing the evaluate
    // function
    pf.correctBoundaryConditions();

    // Sync any dangling points
    syncUntransformedData
    (
        pf.mesh()(),
394
        pf.primitiveFieldRef(),
395 396 397 398 399 400 401 402
        maxMagSqrEqOp<vector>()
    );

    // Apply multiple constraints on edge/corner points
    constrainCorners(pf);

    // Apply any 2D motion constraints (or should they go before
    // corner constraints?)
403
    twoDPointCorrector::New(mesh()()).correctDisplacement
404
    (
405
        mesh()().points(),
406
        pf.primitiveFieldRef()
407 408 409 410 411 412 413 414 415 416
    );

    if (overrideFixedValue)
    {
        setPatchFields(pf);
    }
}


template<>
417
void Foam::pointConstraints::constrainCorners<Foam::scalar>
418 419 420 421 422 423 424
(
    GeometricField<scalar, pointPatchField, pointMesh>& pf
) const
{}


template<>
425
void Foam::pointConstraints::constrainCorners<Foam::label>
426 427 428 429 430 431 432
(
    GeometricField<label, pointPatchField, pointMesh>& pf
) const
{}


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