├── .gitignore ├── LICENSE ├── README.md ├── src ├── Make │ ├── files │ ├── linux64GccDPInt32Debug │ │ ├── options │ │ ├── sourceFiles │ │ └── variables │ ├── linux64GccDPInt32Opt │ │ ├── options │ │ ├── sourceFiles │ │ └── variables │ └── options ├── dynamicRefine2DFvMesh │ ├── dynamicRefine2DFvMesh.C │ └── dynamicRefine2DFvMesh.H └── hexRef2D │ ├── hexRef2D.C │ └── hexRef2D.H └── tutorial └── interFoam └── damBreak ├── 0 ├── U ├── alpha.water ├── alpha.water.orig └── p_rgh ├── .gitignore ├── Allrun ├── Screenshot from 2019-04-18 14-46-42.png ├── alpha-with-refinement.gif ├── constant ├── dynamicMeshDict ├── g ├── polyMesh │ ├── boundary │ ├── faces │ ├── neighbour │ ├── owner │ └── points ├── transportProperties └── turbulenceProperties ├── meshRefinement-2d.gif ├── meshRefinement-3d.gif └── system ├── blockMeshDict ├── controlDict ├── decomposeParDict ├── fvSchemes ├── fvSolution ├── sampling └── setFieldsDict /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.dep 3 | */linux64* 4 | *lnInclude* 5 | 6 | */0.[0-9]* 7 | */[1-9].[0-9]* 8 | */[1-9]* 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Ajit Kumar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Adaptive mesh refinement in OpenFoam-v1812 for 2-dimensional problems 2 | 3 | Standard OpenFOAM-v1812 has a module called dynamicRefineFvMesh. This adaptive mesh refinement tool does not seem to be 4 | applicable for two dimensional problems. [Shonibare](https://www.academia.edu/16217705/Two-dimensional_adaptive_meshing_in_OpenFOAM) and [Ahmad Baniabedalruhman](https://digitalcommons.mtu.edu/cgi/viewcontent.cgi?referer=&httpsredir=1&article=2008&context=etds) 5 | recently described how to adapt this adaptive mesh tools for two dimensional problems. However, 6 | CFD community struggled to implement their ideas. See this [cfd-online forum](https://www.cfd-online.com/Forums/openfoam-community-contributions/118870-2d-adaptive-mesh-refinement.html). 7 | 8 | Eventually, [Luca Cornolti](https://www.cfd-online.com/Forums/openfoam-community-contributions/118870-2d-adaptive-mesh-refinement.html) shared a working for code 9 | for adaptive mesh refinement for 2d problems. His initial code were for foam-extend. This repository is simple adaptation Luca's work 10 | for OpenFOAM-v1812. The animated screenshot clearly shows that it works. 11 | 12 | [Disclaimer: It is to be noted that in the same forum discussion, Luca Cornolti also shared his adaptive mesh refinement 13 | for OpenFOAM-v1816. This repository is not a copy of that. Instead, this codes is built on top of Luca's foam-extend codes] 14 | 15 | 16 | 17 | 18 | 21 | 24 | 27 | 28 |
19 | Adaptive mesh refinement in OpenFoam-v1812 20 | 22 | Adaptive mesh refinement in OpenFoam-v1812 23 | 25 | Adaptive mesh refinement in OpenFoam-v1812 26 |
29 | 30 | ### Compilation 31 | 32 | Switch to src directory and run 33 |
34 | wmake libso
35 | 
36 | 37 | ### Example 38 | To run the a dam-break example, switch to tutorial/interFoam/dambreak and then run 39 |
40 | ./Allrun
41 | 
42 | -------------------------------------------------------------------------------- /src/Make/files: -------------------------------------------------------------------------------- 1 | hexRef2D/hexRef2D.C 2 | dynamicRefine2DFvMesh/dynamicRefine2DFvMesh.C 3 | 4 | 5 | LIB = $(FOAM_USER_LIBBIN)/libdynamicRefine2D 6 | -------------------------------------------------------------------------------- /src/Make/linux64GccDPInt32Debug/options: -------------------------------------------------------------------------------- 1 | # 1 "Make/options" 2 | # 1 "" 3 | # 1 "" 4 | # 10 "" 5 | # 1 "/usr/include/stdc-predef.h" 1 3 4 6 | 7 | # 17 "/usr/include/stdc-predef.h" 3 4 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 | # 10 "" 2 55 | # 1 "Make/options" 56 | EXE_INC = -I$(LIB_SRC)/finiteVolume/lnInclude -I$(LIB_SRC)/surfMesh/lnInclude -I$(LIB_SRC)/meshTools/lnInclude -I$(LIB_SRC)/mesh/extrudeModel/lnInclude -I$(LIB_SRC)/dynamicMesh/lnInclude -I$(LIB_SRC)/dynamicFvMesh/lnInclude 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | LIB_LIBS = -lfiniteVolume -lsurfMesh -lextrudeModel -ldynamicMesh -ldynamicFvMesh 65 | -------------------------------------------------------------------------------- /src/Make/linux64GccDPInt32Debug/sourceFiles: -------------------------------------------------------------------------------- 1 | SOURCE = \ 2 | hexRef2D.C \ 3 | dynamicRefineFvMesh2D.C \ 4 | 5 | # sources 6 | -------------------------------------------------------------------------------- /src/Make/linux64GccDPInt32Debug/variables: -------------------------------------------------------------------------------- 1 | LIB = $(FOAM_USER_LIBBIN)/libTest2dDynamicRefine 2 | -------------------------------------------------------------------------------- /src/Make/linux64GccDPInt32Opt/options: -------------------------------------------------------------------------------- 1 | # 1 "Make/options" 2 | # 1 "" 3 | # 1 "" 4 | # 10 "" 5 | # 1 "/usr/include/stdc-predef.h" 1 3 4 6 | 7 | # 17 "/usr/include/stdc-predef.h" 3 4 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 | # 10 "" 2 55 | # 1 "Make/options" 56 | EXE_INC = -I$(LIB_SRC)/finiteVolume/lnInclude -I$(LIB_SRC)/surfMesh/lnInclude -I$(LIB_SRC)/meshTools/lnInclude -I$(LIB_SRC)/mesh/extrudeModel/lnInclude -I$(LIB_SRC)/dynamicMesh/lnInclude -I$(LIB_SRC)/dynamicFvMesh/lnInclude 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | LIB_LIBS = -lfiniteVolume -lsurfMesh -lextrudeModel -ldynamicMesh -ldynamicFvMesh 65 | -------------------------------------------------------------------------------- /src/Make/linux64GccDPInt32Opt/sourceFiles: -------------------------------------------------------------------------------- 1 | SOURCE = \ 2 | hexRef2D/hexRef2D.C \ 3 | dynamicRefine2DFvMesh/dynamicRefine2DFvMesh.C \ 4 | 5 | # sources 6 | -------------------------------------------------------------------------------- /src/Make/linux64GccDPInt32Opt/variables: -------------------------------------------------------------------------------- 1 | LIB = $(FOAM_USER_LIBBIN)/libdynamicRefine2D 2 | -------------------------------------------------------------------------------- /src/Make/options: -------------------------------------------------------------------------------- 1 | EXE_INC = \ 2 | -I$(LIB_SRC)/finiteVolume/lnInclude \ 3 | -I$(LIB_SRC)/surfMesh/lnInclude \ 4 | -I$(LIB_SRC)/meshTools/lnInclude \ 5 | -I$(LIB_SRC)/mesh/extrudeModel/lnInclude \ 6 | -I$(LIB_SRC)/dynamicMesh/lnInclude \ 7 | -I$(LIB_SRC)/dynamicFvMesh/lnInclude 8 | 9 | LIB_LIBS = \ 10 | -lfiniteVolume \ 11 | -lsurfMesh \ 12 | -lextrudeModel \ 13 | -ldynamicMesh \ 14 | -ldynamicFvMesh 15 | -------------------------------------------------------------------------------- /src/dynamicRefine2DFvMesh/dynamicRefine2DFvMesh.C: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------*\ 2 | ========= | 3 | \\ / F ield | foam-extend: Open Source CFD 4 | \\ / O peration | Version: 3.2 5 | \\ / A nd | Web: http://www.foam-extend.org 6 | \\/ M anipulation | For copyright notice see file Copyright 7 | ------------------------------------------------------------------------------- 8 | License 9 | This file is part of OpenFOAM-v1806. 10 | 11 | Author 12 | Luca Cornolti - written for foam-extend 13 | 14 | Modified for OpenFOAM-v1806 by 15 | Ajit Kumar, Shiv Nadar University, India 16 | 17 | 18 | \*---------------------------------------------------------------------------*/ 19 | 20 | #include "dynamicRefine2DFvMesh.H" 21 | #include "addToRunTimeSelectionTable.H" 22 | #include "volFields.H" 23 | #include "surfaceFields.H" 24 | #include "fvCFD.H" 25 | #include "syncTools.H" 26 | #include "pointFields.H" 27 | #include "polyTopoChange.H" 28 | 29 | // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 30 | 31 | namespace Foam 32 | { 33 | 34 | // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // 35 | 36 | defineTypeNameAndDebug(dynamicRefine2DFvMesh, 0); 37 | 38 | addToRunTimeSelectionTable(dynamicFvMesh, dynamicRefine2DFvMesh, IOobject); 39 | 40 | 41 | // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // 42 | 43 | label dynamicRefine2DFvMesh::count 44 | ( 45 | const PackedBoolList& l, 46 | const unsigned int val 47 | ) 48 | { 49 | label n = 0; 50 | forAll(l, i) 51 | { 52 | if (l.get(i) == val) 53 | { 54 | n++; 55 | } 56 | } 57 | return n; 58 | } 59 | 60 | 61 | void dynamicRefine2DFvMesh::calculateProtectedCells 62 | ( 63 | PackedBoolList& unrefineableCell 64 | ) const 65 | { 66 | if (protectedCell_.empty()) 67 | { 68 | unrefineableCell.clear(); 69 | return; 70 | } 71 | 72 | const labelList& cellLevel = meshCutter_.cellLevel(); 73 | 74 | unrefineableCell = protectedCell_; 75 | 76 | // Get neighbouring cell level 77 | labelList neiLevel(nFaces()-nInternalFaces()); 78 | 79 | for (label faceI = nInternalFaces(); faceI < nFaces(); faceI++) 80 | { 81 | neiLevel[faceI-nInternalFaces()] = cellLevel[faceOwner()[faceI]]; 82 | } 83 | syncTools::swapBoundaryFaceList(*this, neiLevel); // Ajit: , false); 84 | 85 | 86 | while (true) 87 | { 88 | // Pick up faces on border of protected cells 89 | boolList seedFace(nFaces(), false); 90 | 91 | forAll(faceNeighbour(), faceI) 92 | { 93 | label own = faceOwner()[faceI]; 94 | bool ownProtected = (unrefineableCell.get(own) == 1); 95 | label nei = faceNeighbour()[faceI]; 96 | bool neiProtected = (unrefineableCell.get(nei) == 1); 97 | 98 | if (ownProtected && (cellLevel[nei] > cellLevel[own])) 99 | { 100 | seedFace[faceI] = true; 101 | } 102 | else if (neiProtected && (cellLevel[own] > cellLevel[nei])) 103 | { 104 | seedFace[faceI] = true; 105 | } 106 | } 107 | for (label faceI = nInternalFaces(); faceI < nFaces(); faceI++) 108 | { 109 | label own = faceOwner()[faceI]; 110 | bool ownProtected = (unrefineableCell.get(own) == 1); 111 | if 112 | ( 113 | ownProtected 114 | && (neiLevel[faceI-nInternalFaces()] > cellLevel[own]) 115 | ) 116 | { 117 | seedFace[faceI] = true; 118 | } 119 | } 120 | 121 | syncTools::syncFaceList(*this, seedFace, orEqOp()); //, false); Ajit: Change here 122 | 123 | 124 | // Extend unrefineableCell 125 | bool hasExtended = false; 126 | 127 | for (label faceI = 0; faceI < nInternalFaces(); faceI++) 128 | { 129 | if (seedFace[faceI]) 130 | { 131 | label own = faceOwner()[faceI]; 132 | if (unrefineableCell.get(own) == 0) 133 | { 134 | unrefineableCell.set(own, 1); 135 | hasExtended = true; 136 | } 137 | 138 | label nei = faceNeighbour()[faceI]; 139 | if (unrefineableCell.get(nei) == 0) 140 | { 141 | unrefineableCell.set(nei, 1); 142 | hasExtended = true; 143 | } 144 | } 145 | } 146 | for (label faceI = nInternalFaces(); faceI < nFaces(); faceI++) 147 | { 148 | if (seedFace[faceI]) 149 | { 150 | label own = faceOwner()[faceI]; 151 | if (unrefineableCell.get(own) == 0) 152 | { 153 | unrefineableCell.set(own, 1); 154 | hasExtended = true; 155 | } 156 | } 157 | } 158 | 159 | if (!returnReduce(hasExtended, orOp())) 160 | { 161 | break; 162 | } 163 | } 164 | } 165 | 166 | 167 | void dynamicRefine2DFvMesh::readDict() 168 | { 169 | dictionary refineDict 170 | ( 171 | IOdictionary 172 | ( 173 | IOobject 174 | ( 175 | "dynamicMeshDict", 176 | time().constant(), 177 | *this, 178 | IOobject::MUST_READ, 179 | IOobject::NO_WRITE, 180 | false 181 | ) 182 | ).subDict(typeName + "Coeffs") 183 | ); 184 | 185 | correctFluxes_ = List >(refineDict.lookup("correctFluxes")); 186 | 187 | dumpLevel_ = Switch(refineDict.lookup("dumpLevel")); 188 | } 189 | 190 | 191 | // Refines cells, maps fields and recalculates (an approximate) flux 192 | autoPtr dynamicRefine2DFvMesh::refine 193 | ( 194 | const labelList& cellsToRefine 195 | ) 196 | { 197 | // Mesh changing engine. 198 | polyTopoChange meshMod(*this); 199 | 200 | // Play refinement commands into mesh changer. 201 | meshCutter_.setRefinement(cellsToRefine, meshMod); 202 | 203 | // Create mesh (with inflation), return map from old to new mesh. 204 | //autoPtr map = meshMod.changeMesh(*this, true); 205 | autoPtr map = meshMod.changeMesh(*this, false); 206 | 207 | Info<< "Refined from " 208 | << returnReduce(map().nOldCells(), sumOp