Skip to content

Instantly share code, notes, and snippets.

@quantshah
Created November 14, 2016 20:38
Show Gist options
  • Save quantshah/9cdd5d2ee09f41bf1f78d80d3b4d65f8 to your computer and use it in GitHub Desktop.
Save quantshah/9cdd5d2ee09f41bf1f78d80d3b4d65f8 to your computer and use it in GitHub Desktop.
A parser to update OpenFoam mesh files using values from GUI
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import re"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# If you want to download the mesh file then use this\n",
"# Otherwise just put the file in the same folder or give the path to file in filename\n",
"mesh_file_url = 'https://raw.githubusercontent.com/OpenFOAM/OpenFOAM-2.1.x/ccbaf836072aeff5696c682572b442b7588e3d1f/tutorials/incompressible/simpleFoam/motorBike/system/snappyHexMeshDict'"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"filename = 'SnappyHexMeshDict'\n",
"\n",
"with open(filename) as f:\n",
" data = f.read()\n",
"f.close()"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"maxLocalCells_name = 'maxLocalCells'\n",
"maxLocalCells_value = '232323'\n",
"\n",
"level_name = 'level' #line 115 with braces in value\n",
"level_value = '(5000 600)' #Convert values to string before using.\n",
"\n",
"levels_name = \"levels\"\n",
"levels_value = '(('+str('1E15EEEEEE')+'))'"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# make a dictionary to make things easier. Keys are the name of the object and values are strings\n",
"\n",
"attributes = dict()\n",
"\n",
"attributes['maxLocalCells'] = maxLocalCells_value\n",
"attributes['level'] = level_value\n",
"attributes['levels'] = levels_value"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def replace(attribute_name, attribute_value, input_string):\n",
" \"\"\"\n",
" A function to replace `attribute_name` with the `attribute value` in the Hex file\n",
" \n",
" Parameters\n",
" ----------\n",
" attribute_name : str\n",
" Attribute name such as `maxLocalCells`\n",
" Use this from the keys of the defined dictionary\n",
" \n",
" attribute_value : str\n",
" Attribute value to fill. \n",
" Use this from the dictionary or get it like object.Plain_text.\n",
" Make sure that the format is exactly what you want to put after the space\n",
" after attribute name and the ';' including the brackets and spaces etc\n",
" Such as maxLocalCells 10203;\n",
" Or, levels ((1E15EEEEEE));\n",
" \n",
" input_string : str\n",
" The input string or the file after reading the contents as\n",
" ```with open(filename) as f:\n",
" input_string = f.read()\n",
" Returns\n",
" -------\n",
" output : str\n",
" A string with the filled attribute values which can be written to a file\n",
" \"\"\"\n",
" output = re.sub(attribute_name+\".*?;\", attribute_name+\" \"+attribute_value+\";\", input_string)\n",
" \n",
" return(output)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/*--------------------------------*- C++ -*----------------------------------*\\\n",
"| ========= | |\n",
"| \\\\ / F ield | OpenFOAM: The Open Source CFD Toolbox |\n",
"| \\\\ / O peration | Version: 2.1.x |\n",
"| \\\\ / A nd | Web: www.OpenFOAM.org |\n",
"| \\\\/ M anipulation | |\n",
"\\*---------------------------------------------------------------------------*/\n",
"FoamFile\n",
"{\n",
" version 2.0;\n",
" format ascii;\n",
" class dictionary;\n",
" object snappyHexMeshDict;\n",
"}\n",
"// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n",
"\n",
"// Which of the steps to run\n",
"castellatedMesh true;\n",
"snap true;\n",
"addLayers true;\n",
"\n",
"\n",
"// Geometry. Definition of all surfaces. All surfaces are of class\n",
"// searchableSurface.\n",
"// Surfaces are used\n",
"// - to specify refinement for any mesh cell intersecting it\n",
"// - to specify refinement for any mesh cell inside/outside/near\n",
"// - to 'snap' the mesh boundary to the surface\n",
"geometry\n",
"{\n",
" motorBike.obj\n",
" {\n",
" type triSurfaceMesh;\n",
" name motorBike;\n",
" }\n",
"\n",
" refinementBox\n",
" {\n",
" type searchableBox;\n",
" min (-1.0 -0.7 0.0);\n",
" max ( 8.0 0.7 2.5);\n",
" }\n",
"};\n",
"\n",
"\n",
"\n",
"// Settings for the castellatedMesh generation.\n",
"castellatedMeshControls\n",
"{\n",
"\n",
" // Refinement parameters\n",
" // ~~~~~~~~~~~~~~~~~~~~~\n",
"\n",
" // If local number of cells is >= maxLocalCells on any processor\n",
" // switches from from refinement followed by balancing\n",
" // (current method) to (weighted) balancing before refinement.\n",
" maxLocalCells 23232123;\n",
"\n",
" // Overall cell limit (approximately). Refinement will stop immediately\n",
" // upon reaching this number so a refinement level might not complete.\n",
" // Note that this is the number of cells before removing the part which\n",
" // is not 'visible' from the keepPoint. The final number of cells might\n",
" // actually be a lot less.\n",
" maxGlobalCells 2000000;\n",
"\n",
" // The surface refinement loop might spend lots of iterations refining just a\n",
" // few cells. This setting will cause refinement to stop if <= minimumRefine\n",
" // are selected for refinement. Note: it will at least do one iteration\n",
" // (unless the number of cells to refine is 0)\n",
" minRefinementCells 10;\n",
"\n",
" // Allow a certain level of imbalance during refining\n",
" // (since balancing is quite expensive)\n",
" // Expressed as fraction of perfect balance (= overall number of cells /\n",
" // nProcs). 0=balance always.\n",
" maxLoadUnbalance 0.10;\n",
"\n",
"\n",
" // Number of buffer layers between different levels.\n",
" // 1 means normal 2:1 refinement restriction, larger means slower\n",
" // refinement.\n",
" nCellsBetweenLevels 3;\n",
"\n",
"\n",
"\n",
" // Explicit feature edge refinement\n",
" // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
"\n",
" // Specifies a level for any cell intersected by its edges.\n",
" // This is a featureEdgeMesh, read from constant/triSurface for now.\n",
" features\n",
" (\n",
" //{\n",
" // file \"someLine.eMesh\";\n",
" // level 2;\n",
" //}\n",
" );\n",
"\n",
"\n",
"\n",
" // Surface based refinement\n",
" // ~~~~~~~~~~~~~~~~~~~~~~~~\n",
"\n",
" // Specifies two levels for every surface. The first is the minimum level,\n",
" // every cell intersecting a surface gets refined up to the minimum level.\n",
" // The second level is the maximum level. Cells that 'see' multiple\n",
" // intersections where the intersections make an\n",
" // angle > resolveFeatureAngle get refined up to the maximum level.\n",
"\n",
" refinementSurfaces\n",
" {\n",
" motorBike\n",
" {\n",
" // Surface-wise min and max refinement level\n",
" level (5 6);\n",
" }\n",
" }\n",
"\n",
" // Resolve sharp angles\n",
" resolveFeatureAngle 30;\n",
"\n",
"\n",
" // Region-wise refinement\n",
" // ~~~~~~~~~~~~~~~~~~~~~~\n",
"\n",
" // Specifies refinement level for cells in relation to a surface. One of\n",
" // three modes\n",
" // - distance. 'levels' specifies per distance to the surface the\n",
" // wanted refinement level. The distances need to be specified in\n",
" // descending order.\n",
" // - inside. 'levels' is only one entry and only the level is used. All\n",
" // cells inside the surface get refined up to the level. The surface\n",
" // needs to be closed for this to be possible.\n",
" // - outside. Same but cells outside.\n",
"\n",
" refinementRegions\n",
" {\n",
" refinementBox\n",
" {\n",
" mode inside;\n",
" levels ((1E15 4));\n",
" }\n",
" }\n",
"\n",
"\n",
" // Mesh selection\n",
" // ~~~~~~~~~~~~~~\n",
"\n",
" // After refinement patches get added for all refinementSurfaces and\n",
" // all cells intersecting the surfaces get put into these patches. The\n",
" // section reachable from the locationInMesh is kept.\n",
" // NOTE: This point should never be on a face, always inside a cell, even\n",
" // after refinement.\n",
" locationInMesh (3 3 0.43);\n",
"\n",
"\n",
" // Whether any faceZones (as specified in the refinementSurfaces)\n",
" // are only on the boundary of corresponding cellZones or also allow\n",
" // free-standing zone faces. Not used if there are no faceZones.\n",
" allowFreeStandingZoneFaces true;\n",
"}\n",
"\n",
"\n",
"\n",
"// Settings for the snapping.\n",
"snapControls\n",
"{\n",
" //- Number of patch smoothing iterations before finding correspondence\n",
" // to surface\n",
" nSmoothPatch 3;\n",
"\n",
" //- Relative distance for points to be attracted by surface feature point\n",
" // or edge. True distance is this factor times local\n",
" // maximum edge length.\n",
" tolerance 4.0;\n",
"\n",
" //- Number of mesh displacement relaxation iterations.\n",
" nSolveIter 0;\n",
"\n",
" //- Maximum number of snapping relaxation iterations. Should stop\n",
" // before upon reaching a correct mesh.\n",
" nRelaxIter 5;\n",
"\n",
" //- Highly experimental and wip: number of feature edge snapping\n",
" // iterations. Leave out altogether to disable.\n",
" // Do not use here since mesh resolution too low and baffles present\n",
" //nFeatureSnapIter 10;\n",
"}\n",
"\n",
"\n",
"\n",
"// Settings for the layer addition.\n",
"addLayersControls\n",
"{\n",
" // Are the thickness parameters below relative to the undistorted\n",
" // size of the refined cell outside layer (true) or absolute sizes (false).\n",
" relativeSizes true;\n",
"\n",
" // Per final patch (so not geometry!) the layer information\n",
" layers\n",
" {\n",
" \"(lowerWall|motorBike).*\"\n",
" {\n",
" nSurfaceLayers 1;\n",
" }\n",
" }\n",
"\n",
" // Expansion factor for layer mesh\n",
" expansionRatio 1.0;\n",
"\n",
" //- Wanted thickness of final added cell layer. If multiple layers\n",
" // is the\n",
" // thickness of the layer furthest away from the wall.\n",
" // Relative to undistorted size of cell outside layer.\n",
" // is the thickness of the layer furthest away from the wall.\n",
" // See relativeSizes parameter.\n",
" finalLayerThickness 0.3;\n",
"\n",
" //- Minimum thickness of cell layer. If for any reason layer\n",
" // cannot be above minThickness do not add layer.\n",
" // Relative to undistorted size of cell outside layer.\n",
" minThickness 0.1;\n",
"\n",
" //- If points get not extruded do nGrow layers of connected faces that are\n",
" // also not grown. This helps convergence of the layer addition process\n",
" // close to features.\n",
" // Note: changed(corrected) w.r.t 17x! (didn't do anything in 17x)\n",
" nGrow 0;\n",
"\n",
" // Advanced settings\n",
"\n",
" //- When not to extrude surface. 0 is flat surface, 90 is when two faces\n",
" // are perpendicular\n",
" featureAngle 30;\n",
"\n",
" //- Maximum number of snapping relaxation iterations. Should stop\n",
" // before upon reaching a correct mesh.\n",
" nRelaxIter 3;\n",
"\n",
" // Number of smoothing iterations of surface normals\n",
" nSmoothSurfaceNormals 1;\n",
"\n",
" // Number of smoothing iterations of interior mesh movement direction\n",
" nSmoothNormals 3;\n",
"\n",
" // Smooth layer thickness over surface patches\n",
" nSmoothThickness 10;\n",
"\n",
" // Stop layer growth on highly warped cells\n",
" maxFaceThicknessRatio 0.5;\n",
"\n",
" // Reduce layer growth where ratio thickness to medial\n",
" // distance is large\n",
" maxThicknessToMedialRatio 0.3;\n",
"\n",
" // Angle used to pick up medial axis points\n",
" // Note: changed(corrected) w.r.t 17x! 90 degrees corresponds to 130 in 17x.\n",
" minMedianAxisAngle 90;\n",
"\n",
"\n",
" // Create buffer region for new layer terminations\n",
" nBufferCellsNoExtrude 0;\n",
"\n",
"\n",
" // Overall max number of layer addition iterations. The mesher will exit\n",
" // if it reaches this number of iterations; possibly with an illegal\n",
" // mesh.\n",
" nLayerIter 50;\n",
"}\n",
"\n",
"\n",
"\n",
"// Generic mesh quality settings. At any undoable phase these determine\n",
"// where to undo.\n",
"meshQualityControls\n",
"{\n",
" //- Maximum non-orthogonality allowed. Set to 180 to disable.\n",
" maxNonOrtho 65;\n",
"\n",
" //- Max skewness allowed. Set to <0 to disable.\n",
" maxBoundarySkewness 20;\n",
" maxInternalSkewness 4;\n",
"\n",
" //- Max concaveness allowed. Is angle (in degrees) below which concavity\n",
" // is allowed. 0 is straight face, <0 would be convex face.\n",
" // Set to 180 to disable.\n",
" maxConcave 80;\n",
"\n",
" //- Minimum pyramid volume. Is absolute volume of cell pyramid.\n",
" // Set to a sensible fraction of the smallest cell volume expected.\n",
" // Set to very negative number (e.g. -1E30) to disable.\n",
" minVol 1e-13;\n",
"\n",
" //- Minimum quality of the tet formed by the face-centre\n",
" // and variable base point minimum decomposition triangles and\n",
" // the cell centre. This has to be a positive number for tracking\n",
" // to work. Set to very negative number (e.g. -1E30) to\n",
" // disable.\n",
" // <0 = inside out tet,\n",
" // 0 = flat tet\n",
" // 1 = regular tet\n",
" minTetQuality 1e-30;\n",
"\n",
" //- Minimum face area. Set to <0 to disable.\n",
" minArea -1;\n",
"\n",
" //- Minimum face twist. Set to <-1 to disable. dot product of face normal\n",
" //- and face centre triangles normal\n",
" minTwist 0.02;\n",
"\n",
" //- minimum normalised cell determinant\n",
" //- 1 = hex, <= 0 = folded or flattened illegal cell\n",
" minDeterminant 0.001;\n",
"\n",
" //- minFaceWeight (0 -> 0.5)\n",
" minFaceWeight 0.02;\n",
"\n",
" //- minVolRatio (0 -> 1)\n",
" minVolRatio 0.01;\n",
"\n",
" //must be >0 for Fluent compatibility\n",
" minTriangleTwist -1;\n",
"\n",
"\n",
" // Advanced\n",
"\n",
" //- Number of error distribution iterations\n",
" nSmoothScale 4;\n",
" //- amount to scale back displacement at error points\n",
" errorReduction 0.75;\n",
"}\n",
"\n",
"\n",
"// Advanced\n",
"\n",
"// Flags for optional output\n",
"// 0 : only write final meshes\n",
"// 1 : write intermediate meshes\n",
"// 2 : write volScalarField with cellLevel for postprocessing\n",
"// 4 : write current intersections as .obj files\n",
"debug 0;\n",
"\n",
"\n",
"// Merge tolerance. Is fraction of overall bounding box of initial mesh.\n",
"// Note: the write tolerance needs to be higher than this.\n",
"mergeTolerance 1e-6;\n",
"\n",
"\n",
"// ************************************************************************* //\n",
"\n"
]
}
],
"source": [
"print(replace('maxLocalCells', str(23232123), data))\n",
"\n",
"replaced_maxLocalCells = replace('maxLocalCells', str(23232123), data)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/*--------------------------------*- C++ -*----------------------------------*\\\n",
"| ========= | |\n",
"| \\\\ / F ield | OpenFOAM: The Open Source CFD Toolbox |\n",
"| \\\\ / O peration | Version: 2.1.x |\n",
"| \\\\ / A nd | Web: www.OpenFOAM.org |\n",
"| \\\\/ M anipulation | |\n",
"\\*---------------------------------------------------------------------------*/\n",
"FoamFile\n",
"{\n",
" version 2.0;\n",
" format ascii;\n",
" class dictionary;\n",
" object snappyHexMeshDict;\n",
"}\n",
"// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n",
"\n",
"// Which of the steps to run\n",
"castellatedMesh true;\n",
"snap true;\n",
"addLayers true;\n",
"\n",
"\n",
"// Geometry. Definition of all surfaces. All surfaces are of class\n",
"// searchableSurface.\n",
"// Surfaces are used\n",
"// - to specify refinement for any mesh cell intersecting it\n",
"// - to specify refinement for any mesh cell inside/outside/near\n",
"// - to 'snap' the mesh boundary to the surface\n",
"geometry\n",
"{\n",
" motorBike.obj\n",
" {\n",
" type triSurfaceMesh;\n",
" name motorBike;\n",
" }\n",
"\n",
" refinementBox\n",
" {\n",
" type searchableBox;\n",
" min (-1.0 -0.7 0.0);\n",
" max ( 8.0 0.7 2.5);\n",
" }\n",
"};\n",
"\n",
"\n",
"\n",
"// Settings for the castellatedMesh generation.\n",
"castellatedMeshControls\n",
"{\n",
"\n",
" // Refinement parameters\n",
" // ~~~~~~~~~~~~~~~~~~~~~\n",
"\n",
" // If local number of cells is >= maxLocalCells on any processor\n",
" // switches from from refinement followed by balancing\n",
" // (current method) to (weighted) balancing before refinement.\n",
" maxLocalCells 23232123;\n",
"\n",
" // Overall cell limit (approximately). Refinement will stop immediately\n",
" // upon reaching this number so a refinement level might not complete.\n",
" // Note that this is the number of cells before removing the part which\n",
" // is not 'visible' from the keepPoint. The final number of cells might\n",
" // actually be a lot less.\n",
" maxGlobalCells 2000000;\n",
"\n",
" // The surface refinement loop might spend lots of iterations refining just a\n",
" // few cells. This setting will cause refinement to stop if <= minimumRefine\n",
" // are selected for refinement. Note: it will at least do one iteration\n",
" // (unless the number of cells to refine is 0)\n",
" minRefinementCells 10;\n",
"\n",
" // Allow a certain level of imbalance during refining\n",
" // (since balancing is quite expensive)\n",
" // Expressed as fraction of perfect balance (= overall number of cells /\n",
" // nProcs). 0=balance always.\n",
" maxLoadUnbalance 0.10;\n",
"\n",
"\n",
" // Number of buffer layers between different levels.\n",
" // 1 means normal 2:1 refinement restriction, larger means slower\n",
" // refinement.\n",
" nCellsBetweenLevels 3;\n",
"\n",
"\n",
"\n",
" // Explicit feature edge refinement\n",
" // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
"\n",
" // Specifies a level for any cell intersected by its edges.\n",
" // This is a featureEdgeMesh, read from constant/triSurface for now.\n",
" features\n",
" (\n",
" //{\n",
" // file \"someLine.eMesh\";\n",
" // level (5000 600);\n",
" //}\n",
" );\n",
"\n",
"\n",
"\n",
" // Surface based refinement\n",
" // ~~~~~~~~~~~~~~~~~~~~~~~~\n",
"\n",
" // Specifies two levels for every surface. The first is the minimum level,\n",
" // every cell intersecting a surface gets refined up to the minimum level.\n",
" // The second level is the maximum level. Cells that 'see' multiple\n",
" // intersections where the intersections make an\n",
" // angle > resolveFeatureAngle get refined up to the maximum level.\n",
"\n",
" refinementSurfaces\n",
" {\n",
" motorBike\n",
" {\n",
" // Surface-wise min and max refinement level\n",
" level (5000 600);\n",
" }\n",
" }\n",
"\n",
" // Resolve sharp angles\n",
" resolveFeatureAngle 30;\n",
"\n",
"\n",
" // Region-wise refinement\n",
" // ~~~~~~~~~~~~~~~~~~~~~~\n",
"\n",
" // Specifies refinement level for cells in relation to a surface. One of\n",
" // three modes\n",
" // - distance. 'levels' specifies per distance to the surface the\n",
" // wanted refinement level. The distances need to be specified in\n",
" // descending order.\n",
" // - inside. 'levels' is only one entry and only the level is used. All\n",
" // cells inside the surface get refined up to the level. The surface\n",
" // needs to be closed for this to be possible.\n",
" // - outside. Same but cells outside.\n",
"\n",
" refinementRegions\n",
" {\n",
" refinementBox\n",
" {\n",
" mode inside;\n",
" level (5000 600);\n",
" }\n",
" }\n",
"\n",
"\n",
" // Mesh selection\n",
" // ~~~~~~~~~~~~~~\n",
"\n",
" // After refinement patches get added for all refinementSurfaces and\n",
" // all cells intersecting the surfaces get put into these patches. The\n",
" // section reachable from the locationInMesh is kept.\n",
" // NOTE: This point should never be on a face, always inside a cell, even\n",
" // after refinement.\n",
" locationInMesh (3 3 0.43);\n",
"\n",
"\n",
" // Whether any faceZones (as specified in the refinementSurfaces)\n",
" // are only on the boundary of corresponding cellZones or also allow\n",
" // free-standing zone faces. Not used if there are no faceZones.\n",
" allowFreeStandingZoneFaces true;\n",
"}\n",
"\n",
"\n",
"\n",
"// Settings for the snapping.\n",
"snapControls\n",
"{\n",
" //- Number of patch smoothing iterations before finding correspondence\n",
" // to surface\n",
" nSmoothPatch 3;\n",
"\n",
" //- Relative distance for points to be attracted by surface feature point\n",
" // or edge. True distance is this factor times local\n",
" // maximum edge length.\n",
" tolerance 4.0;\n",
"\n",
" //- Number of mesh displacement relaxation iterations.\n",
" nSolveIter 0;\n",
"\n",
" //- Maximum number of snapping relaxation iterations. Should stop\n",
" // before upon reaching a correct mesh.\n",
" nRelaxIter 5;\n",
"\n",
" //- Highly experimental and wip: number of feature edge snapping\n",
" // iterations. Leave out altogether to disable.\n",
" // Do not use here since mesh resolution too low and baffles present\n",
" //nFeatureSnapIter 10;\n",
"}\n",
"\n",
"\n",
"\n",
"// Settings for the layer addition.\n",
"addLayersControls\n",
"{\n",
" // Are the thickness parameters below relative to the undistorted\n",
" // size of the refined cell outside layer (true) or absolute sizes (false).\n",
" relativeSizes true;\n",
"\n",
" // Per final patch (so not geometry!) the layer information\n",
" layers\n",
" {\n",
" \"(lowerWall|motorBike).*\"\n",
" {\n",
" nSurfaceLayers 1;\n",
" }\n",
" }\n",
"\n",
" // Expansion factor for layer mesh\n",
" expansionRatio 1.0;\n",
"\n",
" //- Wanted thickness of final added cell layer. If multiple layers\n",
" // is the\n",
" // thickness of the layer furthest away from the wall.\n",
" // Relative to undistorted size of cell outside layer.\n",
" // is the thickness of the layer furthest away from the wall.\n",
" // See relativeSizes parameter.\n",
" finalLayerThickness 0.3;\n",
"\n",
" //- Minimum thickness of cell layer. If for any reason layer\n",
" // cannot be above minThickness do not add layer.\n",
" // Relative to undistorted size of cell outside layer.\n",
" minThickness 0.1;\n",
"\n",
" //- If points get not extruded do nGrow layers of connected faces that are\n",
" // also not grown. This helps convergence of the layer addition process\n",
" // close to features.\n",
" // Note: changed(corrected) w.r.t 17x! (didn't do anything in 17x)\n",
" nGrow 0;\n",
"\n",
" // Advanced settings\n",
"\n",
" //- When not to extrude surface. 0 is flat surface, 90 is when two faces\n",
" // are perpendicular\n",
" featureAngle 30;\n",
"\n",
" //- Maximum number of snapping relaxation iterations. Should stop\n",
" // before upon reaching a correct mesh.\n",
" nRelaxIter 3;\n",
"\n",
" // Number of smoothing iterations of surface normals\n",
" nSmoothSurfaceNormals 1;\n",
"\n",
" // Number of smoothing iterations of interior mesh movement direction\n",
" nSmoothNormals 3;\n",
"\n",
" // Smooth layer thickness over surface patches\n",
" nSmoothThickness 10;\n",
"\n",
" // Stop layer growth on highly warped cells\n",
" maxFaceThicknessRatio 0.5;\n",
"\n",
" // Reduce layer growth where ratio thickness to medial\n",
" // distance is large\n",
" maxThicknessToMedialRatio 0.3;\n",
"\n",
" // Angle used to pick up medial axis points\n",
" // Note: changed(corrected) w.r.t 17x! 90 degrees corresponds to 130 in 17x.\n",
" minMedianAxisAngle 90;\n",
"\n",
"\n",
" // Create buffer region for new layer terminations\n",
" nBufferCellsNoExtrude 0;\n",
"\n",
"\n",
" // Overall max number of layer addition iterations. The mesher will exit\n",
" // if it reaches this number of iterations; possibly with an illegal\n",
" // mesh.\n",
" nLayerIter 50;\n",
"}\n",
"\n",
"\n",
"\n",
"// Generic mesh quality settings. At any undoable phase these determine\n",
"// where to undo.\n",
"meshQualityControls\n",
"{\n",
" //- Maximum non-orthogonality allowed. Set to 180 to disable.\n",
" maxNonOrtho 65;\n",
"\n",
" //- Max skewness allowed. Set to <0 to disable.\n",
" maxBoundarySkewness 20;\n",
" maxInternalSkewness 4;\n",
"\n",
" //- Max concaveness allowed. Is angle (in degrees) below which concavity\n",
" // is allowed. 0 is straight face, <0 would be convex face.\n",
" // Set to 180 to disable.\n",
" maxConcave 80;\n",
"\n",
" //- Minimum pyramid volume. Is absolute volume of cell pyramid.\n",
" // Set to a sensible fraction of the smallest cell volume expected.\n",
" // Set to very negative number (e.g. -1E30) to disable.\n",
" minVol 1e-13;\n",
"\n",
" //- Minimum quality of the tet formed by the face-centre\n",
" // and variable base point minimum decomposition triangles and\n",
" // the cell centre. This has to be a positive number for tracking\n",
" // to work. Set to very negative number (e.g. -1E30) to\n",
" // disable.\n",
" // <0 = inside out tet,\n",
" // 0 = flat tet\n",
" // 1 = regular tet\n",
" minTetQuality 1e-30;\n",
"\n",
" //- Minimum face area. Set to <0 to disable.\n",
" minArea -1;\n",
"\n",
" //- Minimum face twist. Set to <-1 to disable. dot product of face normal\n",
" //- and face centre triangles normal\n",
" minTwist 0.02;\n",
"\n",
" //- minimum normalised cell determinant\n",
" //- 1 = hex, <= 0 = folded or flattened illegal cell\n",
" minDeterminant 0.001;\n",
"\n",
" //- minFaceWeight (0 -> 0.5)\n",
" minFaceWeight 0.02;\n",
"\n",
" //- minVolRatio (0 -> 1)\n",
" minVolRatio 0.01;\n",
"\n",
" //must be >0 for Fluent compatibility\n",
" minTriangleTwist -1;\n",
"\n",
"\n",
" // Advanced\n",
"\n",
" //- Number of error distribution iterations\n",
" nSmoothScale 4;\n",
" //- amount to scale back displacement at error points\n",
" errorReduction 0.75;\n",
"}\n",
"\n",
"\n",
"// Advanced\n",
"\n",
"// Flags for optional output\n",
"// 0 : only write final meshes\n",
"// 1 : write intermediate meshes\n",
"// 2 : write volScalarField with cellLevel for postprocessing\n",
"// 4 : write current intersections as .obj files\n",
"debug 0;\n",
"\n",
"\n",
"// Merge tolerance. Is fraction of overall bounding box of initial mesh.\n",
"// Note: the write tolerance needs to be higher than this.\n",
"mergeTolerance 1e-6;\n",
"\n",
"\n",
"// ************************************************************************* //\n",
"\n"
]
}
],
"source": [
"print(replace(level_name, level_value, replaced_maxLocalCells))"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Finally, automate things from the dict\n",
"\n",
"replaced_all = data\n",
"for key in attributes:\n",
" replaced_all = replace(key, attributes[key], replaced_all)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/*--------------------------------*- C++ -*----------------------------------*\\\n",
"| ========= | |\n",
"| \\\\ / F ield | OpenFOAM: The Open Source CFD Toolbox |\n",
"| \\\\ / O peration | Version: 2.1.x |\n",
"| \\\\ / A nd | Web: www.OpenFOAM.org |\n",
"| \\\\/ M anipulation | |\n",
"\\*---------------------------------------------------------------------------*/\n",
"FoamFile\n",
"{\n",
" version 2.0;\n",
" format ascii;\n",
" class dictionary;\n",
" object snappyHexMeshDict;\n",
"}\n",
"// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n",
"\n",
"// Which of the steps to run\n",
"castellatedMesh true;\n",
"snap true;\n",
"addLayers true;\n",
"\n",
"\n",
"// Geometry. Definition of all surfaces. All surfaces are of class\n",
"// searchableSurface.\n",
"// Surfaces are used\n",
"// - to specify refinement for any mesh cell intersecting it\n",
"// - to specify refinement for any mesh cell inside/outside/near\n",
"// - to 'snap' the mesh boundary to the surface\n",
"geometry\n",
"{\n",
" motorBike.obj\n",
" {\n",
" type triSurfaceMesh;\n",
" name motorBike;\n",
" }\n",
"\n",
" refinementBox\n",
" {\n",
" type searchableBox;\n",
" min (-1.0 -0.7 0.0);\n",
" max ( 8.0 0.7 2.5);\n",
" }\n",
"};\n",
"\n",
"\n",
"\n",
"// Settings for the castellatedMesh generation.\n",
"castellatedMeshControls\n",
"{\n",
"\n",
" // Refinement parameters\n",
" // ~~~~~~~~~~~~~~~~~~~~~\n",
"\n",
" // If local number of cells is >= maxLocalCells on any processor\n",
" // switches from from refinement followed by balancing\n",
" // (current method) to (weighted) balancing before refinement.\n",
" maxLocalCells 232323;\n",
"\n",
" // Overall cell limit (approximately). Refinement will stop immediately\n",
" // upon reaching this number so a refinement level might not complete.\n",
" // Note that this is the number of cells before removing the part which\n",
" // is not 'visible' from the keepPoint. The final number of cells might\n",
" // actually be a lot less.\n",
" maxGlobalCells 2000000;\n",
"\n",
" // The surface refinement loop might spend lots of iterations refining just a\n",
" // few cells. This setting will cause refinement to stop if <= minimumRefine\n",
" // are selected for refinement. Note: it will at least do one iteration\n",
" // (unless the number of cells to refine is 0)\n",
" minRefinementCells 10;\n",
"\n",
" // Allow a certain level of imbalance during refining\n",
" // (since balancing is quite expensive)\n",
" // Expressed as fraction of perfect balance (= overall number of cells /\n",
" // nProcs). 0=balance always.\n",
" maxLoadUnbalance 0.10;\n",
"\n",
"\n",
" // Number of buffer layers between different levels.\n",
" // 1 means normal 2:1 refinement restriction, larger means slower\n",
" // refinement.\n",
" nCellsBetweenLevels 3;\n",
"\n",
"\n",
"\n",
" // Explicit feature edge refinement\n",
" // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
"\n",
" // Specifies a level for any cell intersected by its edges.\n",
" // This is a featureEdgeMesh, read from constant/triSurface for now.\n",
" features\n",
" (\n",
" //{\n",
" // file \"someLine.eMesh\";\n",
" // level (5000 600);\n",
" //}\n",
" );\n",
"\n",
"\n",
"\n",
" // Surface based refinement\n",
" // ~~~~~~~~~~~~~~~~~~~~~~~~\n",
"\n",
" // Specifies two levels for every surface. The first is the minimum level,\n",
" // every cell intersecting a surface gets refined up to the minimum level.\n",
" // The second level is the maximum level. Cells that 'see' multiple\n",
" // intersections where the intersections make an\n",
" // angle > resolveFeatureAngle get refined up to the maximum level.\n",
"\n",
" refinementSurfaces\n",
" {\n",
" motorBike\n",
" {\n",
" // Surface-wise min and max refinement level\n",
" level (5000 600);\n",
" }\n",
" }\n",
"\n",
" // Resolve sharp angles\n",
" resolveFeatureAngle 30;\n",
"\n",
"\n",
" // Region-wise refinement\n",
" // ~~~~~~~~~~~~~~~~~~~~~~\n",
"\n",
" // Specifies refinement level for cells in relation to a surface. One of\n",
" // three modes\n",
" // - distance. 'levels' specifies per distance to the surface the\n",
" // wanted refinement level. The distances need to be specified in\n",
" // descending order.\n",
" // - inside. 'levels' is only one entry and only the level is used. All\n",
" // cells inside the surface get refined up to the level. The surface\n",
" // needs to be closed for this to be possible.\n",
" // - outside. Same but cells outside.\n",
"\n",
" refinementRegions\n",
" {\n",
" refinementBox\n",
" {\n",
" mode inside;\n",
" level (5000 600);\n",
" }\n",
" }\n",
"\n",
"\n",
" // Mesh selection\n",
" // ~~~~~~~~~~~~~~\n",
"\n",
" // After refinement patches get added for all refinementSurfaces and\n",
" // all cells intersecting the surfaces get put into these patches. The\n",
" // section reachable from the locationInMesh is kept.\n",
" // NOTE: This point should never be on a face, always inside a cell, even\n",
" // after refinement.\n",
" locationInMesh (3 3 0.43);\n",
"\n",
"\n",
" // Whether any faceZones (as specified in the refinementSurfaces)\n",
" // are only on the boundary of corresponding cellZones or also allow\n",
" // free-standing zone faces. Not used if there are no faceZones.\n",
" allowFreeStandingZoneFaces true;\n",
"}\n",
"\n",
"\n",
"\n",
"// Settings for the snapping.\n",
"snapControls\n",
"{\n",
" //- Number of patch smoothing iterations before finding correspondence\n",
" // to surface\n",
" nSmoothPatch 3;\n",
"\n",
" //- Relative distance for points to be attracted by surface feature point\n",
" // or edge. True distance is this factor times local\n",
" // maximum edge length.\n",
" tolerance 4.0;\n",
"\n",
" //- Number of mesh displacement relaxation iterations.\n",
" nSolveIter 0;\n",
"\n",
" //- Maximum number of snapping relaxation iterations. Should stop\n",
" // before upon reaching a correct mesh.\n",
" nRelaxIter 5;\n",
"\n",
" //- Highly experimental and wip: number of feature edge snapping\n",
" // iterations. Leave out altogether to disable.\n",
" // Do not use here since mesh resolution too low and baffles present\n",
" //nFeatureSnapIter 10;\n",
"}\n",
"\n",
"\n",
"\n",
"// Settings for the layer addition.\n",
"addLayersControls\n",
"{\n",
" // Are the thickness parameters below relative to the undistorted\n",
" // size of the refined cell outside layer (true) or absolute sizes (false).\n",
" relativeSizes true;\n",
"\n",
" // Per final patch (so not geometry!) the layer information\n",
" layers\n",
" {\n",
" \"(lowerWall|motorBike).*\"\n",
" {\n",
" nSurfaceLayers 1;\n",
" }\n",
" }\n",
"\n",
" // Expansion factor for layer mesh\n",
" expansionRatio 1.0;\n",
"\n",
" //- Wanted thickness of final added cell layer. If multiple layers\n",
" // is the\n",
" // thickness of the layer furthest away from the wall.\n",
" // Relative to undistorted size of cell outside layer.\n",
" // is the thickness of the layer furthest away from the wall.\n",
" // See relativeSizes parameter.\n",
" finalLayerThickness 0.3;\n",
"\n",
" //- Minimum thickness of cell layer. If for any reason layer\n",
" // cannot be above minThickness do not add layer.\n",
" // Relative to undistorted size of cell outside layer.\n",
" minThickness 0.1;\n",
"\n",
" //- If points get not extruded do nGrow layers of connected faces that are\n",
" // also not grown. This helps convergence of the layer addition process\n",
" // close to features.\n",
" // Note: changed(corrected) w.r.t 17x! (didn't do anything in 17x)\n",
" nGrow 0;\n",
"\n",
" // Advanced settings\n",
"\n",
" //- When not to extrude surface. 0 is flat surface, 90 is when two faces\n",
" // are perpendicular\n",
" featureAngle 30;\n",
"\n",
" //- Maximum number of snapping relaxation iterations. Should stop\n",
" // before upon reaching a correct mesh.\n",
" nRelaxIter 3;\n",
"\n",
" // Number of smoothing iterations of surface normals\n",
" nSmoothSurfaceNormals 1;\n",
"\n",
" // Number of smoothing iterations of interior mesh movement direction\n",
" nSmoothNormals 3;\n",
"\n",
" // Smooth layer thickness over surface patches\n",
" nSmoothThickness 10;\n",
"\n",
" // Stop layer growth on highly warped cells\n",
" maxFaceThicknessRatio 0.5;\n",
"\n",
" // Reduce layer growth where ratio thickness to medial\n",
" // distance is large\n",
" maxThicknessToMedialRatio 0.3;\n",
"\n",
" // Angle used to pick up medial axis points\n",
" // Note: changed(corrected) w.r.t 17x! 90 degrees corresponds to 130 in 17x.\n",
" minMedianAxisAngle 90;\n",
"\n",
"\n",
" // Create buffer region for new layer terminations\n",
" nBufferCellsNoExtrude 0;\n",
"\n",
"\n",
" // Overall max number of layer addition iterations. The mesher will exit\n",
" // if it reaches this number of iterations; possibly with an illegal\n",
" // mesh.\n",
" nLayerIter 50;\n",
"}\n",
"\n",
"\n",
"\n",
"// Generic mesh quality settings. At any undoable phase these determine\n",
"// where to undo.\n",
"meshQualityControls\n",
"{\n",
" //- Maximum non-orthogonality allowed. Set to 180 to disable.\n",
" maxNonOrtho 65;\n",
"\n",
" //- Max skewness allowed. Set to <0 to disable.\n",
" maxBoundarySkewness 20;\n",
" maxInternalSkewness 4;\n",
"\n",
" //- Max concaveness allowed. Is angle (in degrees) below which concavity\n",
" // is allowed. 0 is straight face, <0 would be convex face.\n",
" // Set to 180 to disable.\n",
" maxConcave 80;\n",
"\n",
" //- Minimum pyramid volume. Is absolute volume of cell pyramid.\n",
" // Set to a sensible fraction of the smallest cell volume expected.\n",
" // Set to very negative number (e.g. -1E30) to disable.\n",
" minVol 1e-13;\n",
"\n",
" //- Minimum quality of the tet formed by the face-centre\n",
" // and variable base point minimum decomposition triangles and\n",
" // the cell centre. This has to be a positive number for tracking\n",
" // to work. Set to very negative number (e.g. -1E30) to\n",
" // disable.\n",
" // <0 = inside out tet,\n",
" // 0 = flat tet\n",
" // 1 = regular tet\n",
" minTetQuality 1e-30;\n",
"\n",
" //- Minimum face area. Set to <0 to disable.\n",
" minArea -1;\n",
"\n",
" //- Minimum face twist. Set to <-1 to disable. dot product of face normal\n",
" //- and face centre triangles normal\n",
" minTwist 0.02;\n",
"\n",
" //- minimum normalised cell determinant\n",
" //- 1 = hex, <= 0 = folded or flattened illegal cell\n",
" minDeterminant 0.001;\n",
"\n",
" //- minFaceWeight (0 -> 0.5)\n",
" minFaceWeight 0.02;\n",
"\n",
" //- minVolRatio (0 -> 1)\n",
" minVolRatio 0.01;\n",
"\n",
" //must be >0 for Fluent compatibility\n",
" minTriangleTwist -1;\n",
"\n",
"\n",
" // Advanced\n",
"\n",
" //- Number of error distribution iterations\n",
" nSmoothScale 4;\n",
" //- amount to scale back displacement at error points\n",
" errorReduction 0.75;\n",
"}\n",
"\n",
"\n",
"// Advanced\n",
"\n",
"// Flags for optional output\n",
"// 0 : only write final meshes\n",
"// 1 : write intermediate meshes\n",
"// 2 : write volScalarField with cellLevel for postprocessing\n",
"// 4 : write current intersections as .obj files\n",
"debug 0;\n",
"\n",
"\n",
"// Merge tolerance. Is fraction of overall bounding box of initial mesh.\n",
"// Note: the write tolerance needs to be higher than this.\n",
"mergeTolerance 1e-6;\n",
"\n",
"\n",
"// ************************************************************************* //\n",
"\n"
]
}
],
"source": [
"print(replaced_all)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# write output_file\n",
"\n",
"output_filename = 'Snappy'\n",
"with open(output_filename, 'w') as f:\n",
" f.write(replaced_all)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"f.close()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment