OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

fvMeshAdderTemplates.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 
00028 #include "volFields.H"
00029 #include "surfaceFields.H"
00030 #include "emptyFvPatchField.H"
00031 
00032 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00033 
00034 template<class Type>
00035 void Foam::fvMeshAdder::map
00036 (
00037     const Field<Type>& oldFld,
00038     const labelList& oldToNew,
00039     Field<Type>& fld
00040 )
00041 {
00042     forAll(oldFld, cellI)
00043     {
00044         label newCellI = oldToNew[cellI];
00045 
00046         if (newCellI >= 0 && newCellI < fld.size())
00047         {
00048             fld[newCellI] = oldFld[cellI];
00049         }
00050     }
00051 }
00052 
00053 
00054 template<class Type>
00055 void Foam::fvMeshAdder::MapVolField
00056 (
00057     const mapAddedPolyMesh& meshMap,
00058 
00059     GeometricField<Type, fvPatchField, volMesh>& fld,
00060     const GeometricField<Type, fvPatchField, volMesh>& fldToAdd
00061 )
00062 {
00063     const fvMesh& mesh = fld.mesh();
00064 
00065     // Internal field
00066     // ~~~~~~~~~~~~~~
00067 
00068     {
00069         // Store old internal field
00070         Field<Type> oldInternalField(fld.internalField());
00071 
00072         // Modify internal field
00073         Field<Type>& intFld = fld.internalField();
00074 
00075         intFld.setSize(mesh.nCells());
00076 
00077         map(oldInternalField, meshMap.oldCellMap(), intFld);
00078         map(fldToAdd.internalField(), meshMap.addedCellMap(), intFld);
00079     }
00080 
00081 
00082     // Patch fields from old mesh
00083     // ~~~~~~~~~~~~~~~~~~~~~~~~~~
00084 
00085     {
00086         const labelList& oldPatchMap = meshMap.oldPatchMap();
00087         const labelList& oldPatchStarts = meshMap.oldPatchStarts();
00088         const labelList& oldPatchSizes = meshMap.oldPatchSizes();
00089 
00090         // Reorder old patches in order of new ones. Put removed patches at end.
00091 
00092         label unusedPatchI = 0;
00093 
00094         forAll(oldPatchMap, patchI)
00095         {
00096             label newPatchI = oldPatchMap[patchI];
00097 
00098             if (newPatchI != -1)
00099             {
00100                 unusedPatchI++;
00101             }
00102         }
00103 
00104         label nUsedPatches = unusedPatchI;
00105 
00106         // Reorder list for patchFields
00107         labelList oldToNew(oldPatchMap.size());
00108 
00109         forAll(oldPatchMap, patchI)
00110         {
00111             label newPatchI = oldPatchMap[patchI];
00112 
00113             if (newPatchI != -1)
00114             {
00115                 oldToNew[patchI] = newPatchI;
00116             }
00117             else
00118             {
00119                 oldToNew[patchI] = unusedPatchI++;
00120             }
00121         }
00122 
00123 
00124         // Sort deleted ones last so is now in newPatch ordering
00125         fld.boundaryField().reorder(oldToNew);
00126         // Extend to covers all patches
00127         fld.boundaryField().setSize(mesh.boundaryMesh().size());
00128         // Delete unused patches
00129         for
00130         (
00131             label newPatchI = nUsedPatches;
00132             newPatchI < fld.boundaryField().size();
00133             newPatchI++
00134         )
00135         {
00136             fld.boundaryField().set(newPatchI, NULL);
00137         }
00138 
00139 
00140         // Map old values
00141         // ~~~~~~~~~~~~~~
00142 
00143         forAll(oldPatchMap, patchI)
00144         {
00145             label newPatchI = oldPatchMap[patchI];
00146 
00147             if (newPatchI != -1)
00148             {
00149                 labelList newToOld
00150                 (   
00151                     calcPatchMap
00152                     (
00153                         oldPatchStarts[patchI],
00154                         oldPatchSizes[patchI],
00155                         meshMap.oldFaceMap(),
00156                         mesh.boundaryMesh()[newPatchI],
00157                         0                   // unmapped value
00158                     )
00159                 );
00160 
00161                 directFvPatchFieldMapper patchMapper(newToOld);
00162 
00163 
00164                 // Create new patchField with same type as existing one.
00165                 // Note:
00166                 // - boundaryField already in new order so access with newPatchI
00167                 // - fld.boundaryField()[newPatchI] both used for type and old
00168                 //   value
00169                 // - hope that field mapping allows aliasing since old and new
00170                 //   are same memory!
00171                 fld.boundaryField().set
00172                 (
00173                     newPatchI,
00174                     fvPatchField<Type>::New
00175                     (
00176                         fld.boundaryField()[newPatchI], // old field
00177                         mesh.boundary()[newPatchI],     // new fvPatch
00178                         fld.dimensionedInternalField(), // new internal field
00179                         patchMapper                     // mapper (new to old)
00180                     )
00181                 );
00182             }
00183         }
00184     }
00185 
00186 
00187 
00188     // Patch fields from added mesh
00189     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00190 
00191     {
00192         const labelList& addedPatchMap = meshMap.addedPatchMap();
00193 
00194         // Add addedMesh patches
00195         forAll(addedPatchMap, patchI)
00196         {
00197             label newPatchI = addedPatchMap[patchI];
00198 
00199             if (newPatchI != -1)
00200             {
00201                 const polyPatch& newPatch = mesh.boundaryMesh()[newPatchI];
00202                 const polyPatch& oldPatch =
00203                     fldToAdd.mesh().boundaryMesh()[patchI];
00204 
00205                 if (!fld.boundaryField()(newPatchI))
00206                 {
00207                     // First occurrence of newPatchI. Map from existing
00208                     // patchField
00209 
00210                     // From new patch faces to patch faces on added mesh.
00211                     labelList newToAdded
00212                     (   
00213                         calcPatchMap
00214                         (
00215                             oldPatch.start(),
00216                             oldPatch.size(),
00217                             meshMap.addedFaceMap(),
00218                             newPatch,
00219                             0                       // unmapped values
00220                         )
00221                     );
00222 
00223                     directFvPatchFieldMapper patchMapper(newToAdded);
00224 
00225                     fld.boundaryField().set
00226                     (
00227                         newPatchI,
00228                         fvPatchField<Type>::New
00229                         (
00230                             fldToAdd.boundaryField()[patchI], // added field
00231                             mesh.boundary()[newPatchI],       // new fvPatch
00232                             fld.dimensionedInternalField(),   // new int. field
00233                             patchMapper                       // mapper
00234                         )
00235                     );
00236                 }
00237                 else
00238                 {
00239                     // PatchField will have correct size already. Just slot in
00240                     // my elements.
00241 
00242                     // From new patch faces to patch faces on added mesh. This
00243                     // time keep unmapped elements -1.
00244                     labelList newToAdded
00245                     (   
00246                         calcPatchMap
00247                         (
00248                             oldPatch.start(),
00249                             oldPatch.size(),
00250                             meshMap.addedFaceMap(),
00251                             newPatch,
00252                             -1                      // unmapped values
00253                         )
00254                     );
00255 
00256                     const fvPatchField<Type>& addedFld =
00257                         fldToAdd.boundaryField()[patchI];
00258 
00259                     fvPatchField<Type>& newFld = fld.boundaryField()[newPatchI];
00260 
00261                     forAll(newFld, i)
00262                     {
00263                         label oldFaceI = newToAdded[i];
00264 
00265                         if (oldFaceI >= 0 && oldFaceI < addedFld.size())
00266                         {
00267                             newFld[i] = addedFld[oldFaceI];
00268                         }
00269                     }
00270                 }
00271             }
00272         }
00273     }
00274 }
00275 
00276 
00277 template<class Type>
00278 void Foam::fvMeshAdder::MapVolFields
00279 (
00280     const mapAddedPolyMesh& meshMap,
00281     const fvMesh& mesh,
00282     const fvMesh& meshToAdd
00283 )
00284 {
00285     HashTable<const GeometricField<Type, fvPatchField, volMesh>*> fields
00286     (
00287         mesh.objectRegistry::lookupClass
00288         <GeometricField<Type, fvPatchField, volMesh> >
00289         ()
00290     );
00291 
00292     HashTable<const GeometricField<Type, fvPatchField, volMesh>*> fieldsToAdd
00293     (
00294         meshToAdd.objectRegistry::lookupClass
00295         <GeometricField<Type, fvPatchField, volMesh> >
00296         ()
00297     );
00298 
00299     // It is necessary to enforce that all old-time fields are stored
00300     // before the mapping is performed.  Otherwise, if the
00301     // old-time-level field is mapped before the field itself, sizes
00302     // will not match.  
00303 
00304     for
00305     (
00306         typename HashTable<const GeometricField<Type, fvPatchField, volMesh>*>::
00307             iterator fieldIter = fields.begin();
00308         fieldIter != fields.end();
00309         ++fieldIter
00310     )
00311     {
00312         const_cast<GeometricField<Type, fvPatchField, volMesh>*>(fieldIter())
00313             ->storeOldTimes();
00314     }
00315 
00316 
00317     for
00318     (
00319         typename HashTable<const GeometricField<Type, fvPatchField, volMesh>*>::
00320             iterator fieldIter = fields.begin();
00321         fieldIter != fields.end();
00322         ++fieldIter
00323     )
00324     {
00325         GeometricField<Type, fvPatchField, volMesh>& fld =
00326             const_cast<GeometricField<Type, fvPatchField, volMesh>&>
00327             (
00328                 *fieldIter()
00329             );
00330 
00331         if (fieldsToAdd.found(fld.name()))
00332         {
00333             Pout<< "Mapping field " << fld.name() << endl;
00334 
00335             const GeometricField<Type, fvPatchField, volMesh>& fldToAdd =
00336                 *fieldsToAdd[fld.name()];
00337 
00338             MapVolField<Type>(meshMap, fld, fldToAdd);
00339         }
00340         else
00341         {
00342             WarningIn("fvMeshAdder::MapVolFields")
00343                 << "Not mapping field " << fld.name()
00344                 << " since not present on mesh to add"
00345                 << endl;
00346         }
00347     }
00348 }
00349 
00350 
00351 template<class Type>
00352 void Foam::fvMeshAdder::MapSurfaceField
00353 (
00354     const mapAddedPolyMesh& meshMap,
00355 
00356     GeometricField<Type, fvsPatchField, surfaceMesh>& fld,
00357     const GeometricField<Type, fvsPatchField, surfaceMesh>& fldToAdd
00358 )
00359 {
00360     const fvMesh& mesh = fld.mesh();
00361     const labelList& oldPatchStarts = meshMap.oldPatchStarts();
00362 
00363     // Internal field
00364     // ~~~~~~~~~~~~~~
00365 
00366     // Store old internal field
00367     {
00368         Field<Type> oldField(fld);
00369 
00370         // Modify internal field
00371         Field<Type>& intFld = fld.internalField();
00372 
00373         intFld.setSize(mesh.nInternalFaces());
00374 
00375         map(oldField, meshMap.oldFaceMap(), intFld);
00376         map(fldToAdd, meshMap.addedFaceMap(), intFld);
00377 
00378         // Faces that were boundary faces but are not anymore.
00379         // Use owner value (so lowest numbered cell, i.e. from 'old' not 'added'
00380         // mesh)
00381         forAll(fld.boundaryField(), patchI)
00382         {
00383             const fvsPatchField<Type>& pf = fld.boundaryField()[patchI];
00384 
00385             label start = oldPatchStarts[patchI];
00386 
00387             forAll(pf, i)
00388             {
00389                 label newFaceI = meshMap.oldFaceMap()[start + i];
00390 
00391                 if (newFaceI >= 0 && newFaceI < mesh.nInternalFaces())
00392                 {
00393                     intFld[newFaceI] = pf[i];
00394                 }
00395             }
00396         }
00397     }
00398 
00399 
00400     // Patch fields from old mesh
00401     // ~~~~~~~~~~~~~~~~~~~~~~~~~~
00402 
00403     {
00404         const labelList& oldPatchMap = meshMap.oldPatchMap();
00405         const labelList& oldPatchSizes = meshMap.oldPatchSizes();
00406 
00407         // Reorder old patches in order of new ones. Put removed patches at end.
00408 
00409         label unusedPatchI = 0;
00410 
00411         forAll(oldPatchMap, patchI)
00412         {
00413             label newPatchI = oldPatchMap[patchI];
00414 
00415             if (newPatchI != -1)
00416             {
00417                 unusedPatchI++;
00418             }
00419         }
00420 
00421         label nUsedPatches = unusedPatchI;
00422 
00423         // Reorder list for patchFields
00424         labelList oldToNew(oldPatchMap.size());
00425 
00426         forAll(oldPatchMap, patchI)
00427         {
00428             label newPatchI = oldPatchMap[patchI];
00429 
00430             if (newPatchI != -1)
00431             {
00432                 oldToNew[patchI] = newPatchI;
00433             }
00434             else
00435             {
00436                 oldToNew[patchI] = unusedPatchI++;
00437             }
00438         }
00439 
00440 
00441         // Sort deleted ones last so is now in newPatch ordering
00442         fld.boundaryField().reorder(oldToNew);
00443         // Extend to covers all patches
00444         fld.boundaryField().setSize(mesh.boundaryMesh().size());
00445         // Delete unused patches
00446         for
00447         (
00448             label newPatchI = nUsedPatches;
00449             newPatchI < fld.boundaryField().size();
00450             newPatchI++
00451         )
00452         {
00453             fld.boundaryField().set(newPatchI, NULL);
00454         }
00455 
00456 
00457         // Map old values
00458         // ~~~~~~~~~~~~~~
00459 
00460         forAll(oldPatchMap, patchI)
00461         {
00462             label newPatchI = oldPatchMap[patchI];
00463 
00464             if (newPatchI != -1)
00465             {
00466                 labelList newToOld
00467                 (   
00468                     calcPatchMap
00469                     (
00470                         oldPatchStarts[patchI],
00471                         oldPatchSizes[patchI],
00472                         meshMap.oldFaceMap(),
00473                         mesh.boundaryMesh()[newPatchI],
00474                         0                   // unmapped value
00475                     )
00476                 );
00477 
00478                 directFvPatchFieldMapper patchMapper(newToOld);
00479 
00480 
00481                 // Create new patchField with same type as existing one.
00482                 // Note:
00483                 // - boundaryField already in new order so access with newPatchI
00484                 // - fld.boundaryField()[newPatchI] both used for type and old
00485                 //   value
00486                 // - hope that field mapping allows aliasing since old and new
00487                 //   are same memory!
00488                 fld.boundaryField().set
00489                 (
00490                     newPatchI,
00491                     fvsPatchField<Type>::New
00492                     (
00493                         fld.boundaryField()[newPatchI], // old field
00494                         mesh.boundary()[newPatchI],     // new fvPatch
00495                         fld.dimensionedInternalField(), // new internal field
00496                         patchMapper                     // mapper (new to old)
00497                     )
00498                 );
00499             }
00500         }
00501     }
00502 
00503 
00504 
00505     // Patch fields from added mesh
00506     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00507 
00508     {
00509         const labelList& addedPatchMap = meshMap.addedPatchMap();
00510 
00511         // Add addedMesh patches
00512         forAll(addedPatchMap, patchI)
00513         {
00514             label newPatchI = addedPatchMap[patchI];
00515 
00516             if (newPatchI != -1)
00517             {
00518                 const polyPatch& newPatch = mesh.boundaryMesh()[newPatchI];
00519                 const polyPatch& oldPatch =
00520                     fldToAdd.mesh().boundaryMesh()[patchI];
00521 
00522                 if (!fld.boundaryField()(newPatchI))
00523                 {
00524                     // First occurrence of newPatchI. Map from existing
00525                     // patchField
00526 
00527                     // From new patch faces to patch faces on added mesh.
00528                     labelList newToAdded
00529                     (   
00530                         calcPatchMap
00531                         (
00532                             oldPatch.start(),
00533                             oldPatch.size(),
00534                             meshMap.addedFaceMap(),
00535                             newPatch,
00536                             0                       // unmapped values
00537                         )
00538                     );
00539 
00540                     directFvPatchFieldMapper patchMapper(newToAdded);
00541 
00542                     fld.boundaryField().set
00543                     (
00544                         newPatchI,
00545                         fvsPatchField<Type>::New
00546                         (
00547                             fldToAdd.boundaryField()[patchI],// added field
00548                             mesh.boundary()[newPatchI],      // new fvPatch
00549                             fld.dimensionedInternalField(),  // new int. field
00550                             patchMapper                      // mapper
00551                         )
00552                     );
00553                 }
00554                 else
00555                 {
00556                     // PatchField will have correct size already. Just slot in
00557                     // my elements.
00558 
00559                     // From new patch faces to patch faces on added mesh. This
00560                     // time keep unmapped elements -1.
00561                     labelList newToAdded
00562                     (   
00563                         calcPatchMap
00564                         (
00565                             oldPatch.start(),
00566                             oldPatch.size(),
00567                             meshMap.addedFaceMap(),
00568                             newPatch,
00569                             -1                      // unmapped values
00570                         )
00571                     );
00572 
00573                     const fvsPatchField<Type>& addedFld =
00574                         fldToAdd.boundaryField()[patchI];
00575 
00576                     fvsPatchField<Type>& newFld =
00577                         fld.boundaryField()[newPatchI];
00578 
00579                     forAll(newFld, i)
00580                     {
00581                         label oldFaceI = newToAdded[i];
00582 
00583                         if (oldFaceI >= 0 && oldFaceI < addedFld.size())
00584                         {
00585                             newFld[i] = addedFld[oldFaceI];
00586                         }
00587                     }
00588                 }
00589             }
00590         }
00591     }
00592 }
00593 
00594 
00595 template<class Type>
00596 void Foam::fvMeshAdder::MapSurfaceFields
00597 (
00598     const mapAddedPolyMesh& meshMap,
00599     const fvMesh& mesh,
00600     const fvMesh& meshToAdd
00601 )
00602 {
00603     typedef GeometricField<Type, fvsPatchField, surfaceMesh> fldType;
00604 
00605     HashTable<const fldType*> fields
00606     (
00607         mesh.objectRegistry::lookupClass<fldType>()
00608     );
00609 
00610     HashTable<const fldType*> fieldsToAdd
00611     (
00612         meshToAdd.objectRegistry::lookupClass<fldType>()
00613     );
00614 
00615     // It is necessary to enforce that all old-time fields are stored
00616     // before the mapping is performed.  Otherwise, if the
00617     // old-time-level field is mapped before the field itself, sizes
00618     // will not match.  
00619 
00620     for
00621     (
00622         typename HashTable<const fldType*>::
00623             iterator fieldIter = fields.begin();
00624         fieldIter != fields.end();
00625         ++fieldIter
00626     )
00627     {
00628         const_cast<fldType*>(fieldIter())
00629             ->storeOldTimes();
00630     }
00631 
00632 
00633     for
00634     (
00635         typename HashTable<const fldType*>::
00636             iterator fieldIter = fields.begin();
00637         fieldIter != fields.end();
00638         ++fieldIter
00639     )
00640     {
00641         fldType& fld = const_cast<fldType&>(*fieldIter());
00642 
00643         if (fieldsToAdd.found(fld.name()))
00644         {
00645             Pout<< "Mapping field " << fld.name() << endl;
00646 
00647             const fldType& fldToAdd = *fieldsToAdd[fld.name()];
00648 
00649             MapSurfaceField<Type>(meshMap, fld, fldToAdd);
00650         }
00651         else
00652         {
00653             WarningIn("fvMeshAdder::MapSurfaceFields")
00654                 << "Not mapping field " << fld.name()
00655                 << " since not present on mesh to add"
00656                 << endl;
00657         }
00658     }
00659 }
00660 
00661 
00662 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd