Created
November 20, 2015 14:57
-
-
Save rjw57/dda2326e0d6ecac5e1a7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Example which re-creates the OpenFOAM cavity tutorial case. | |
FIXME: test that it runs(!) | |
>>> import os | |
>>> case_dir = os.path.join(getfixture('tmpdir').strpath, 'cavity') | |
>>> main(case_dir) | |
>>> os.path.isdir(os.path.join(case_dir, '0.5')) | |
True | |
""" | |
import os | |
from PyFoam.Execution.BasicRunner import BasicRunner | |
from PyFoam.Basics.DataStructures import Dimension | |
from cusfsim.case import Case, KnownDict | |
def main(output_dir='cavity'): | |
# Check that the specified case directory does not already exist | |
if os.path.exists(output_dir): | |
raise RuntimeError( | |
'Refusing to write to existing path: {}'.format(output_dir) | |
) | |
# Create the case | |
case = Case(output_dir) | |
# Write the control dict | |
with case.mutable_dict(KnownDict.CONTROL) as control: | |
control.update(CONTROL_DICT) | |
# Write the block mesh dict | |
with case.mutable_dict(KnownDict.BLOCK_MESH) as bm: | |
bm.update(BLOCK_MESH_DICT) | |
# At this point there is enough to run blockMesh | |
assert run_on_case(case, 'blockMesh') | |
# Update the physical properties | |
with case.mutable_dict(KnownDict.TRANSPORT_PROPERTIES) as tp: | |
tp['nu'] = ( | |
Dimension(0, 2, -1, 0, 0, 0, 0), | |
0.01, | |
) | |
# Update the solver and scheme configuration | |
with case.mutable_dict(KnownDict.FV_SOLUTION) as d: | |
d.update(FV_SOLUTION_DICT) | |
with case.mutable_dict(KnownDict.FV_SCHEMES) as d: | |
d.update(FV_SCHEMES_DICT) | |
# Create the p initial conditions | |
with case.mutable_dict('0/p') as p: | |
p.update({ | |
'dimensions': Dimension(0, 2, -2, 0, 0, 0, 0), | |
'internalField': ('uniform', 0), | |
'boundaryField': { | |
'movingWall': { 'type': 'zeroGradient' }, | |
'fixedWalls': { 'type': 'zeroGradient' }, | |
'frontAndBack': { 'type': 'empty' }, | |
}, | |
}) | |
# Create the U initial conditions | |
with case.mutable_dict('0/U') as U: | |
U.update({ | |
'dimensions': Dimension(0, 1, -1, 0, 0, 0, 0), | |
'internalField': ('uniform', [0, 0, 0]), | |
'boundaryField': { | |
'movingWall': { | |
'type': 'fixedValue', 'value': ('uniform', [1, 0, 0]) | |
}, | |
'fixedWalls': { | |
'type': 'fixedValue', 'value': ('uniform', [0, 0, 0]) | |
}, | |
'frontAndBack': { 'type': 'empty' }, | |
}, | |
}) | |
# Now we can run the main application | |
assert run_on_case(case, 'icoFoam') | |
# Control dict from tutorial | |
CONTROL_DICT = { | |
'application': 'icoFoam', | |
'startFrom': 'startTime', | |
'startTime': 0, | |
'stopAt': 'endTime', | |
'endTime': 0.5, | |
'deltaT': 0.005, | |
'writeControl': 'timeStep', | |
'writeInterval': 20, | |
'purgeWrite': 0, | |
'writeFormat': 'ascii', | |
'writePrecision': 6, | |
'writeCompression': 'off', | |
'timeFormat': 'general', | |
'timePrecision': 6, | |
'runTimeModifiable': True, | |
} | |
# Block mesh dict from tutorial | |
BLOCK_MESH_DICT ={ | |
'convertToMeters': 0.1, | |
'vertices': [ | |
[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], | |
[0, 0, 0.1], [1, 0, 0.1], [1, 1, 0.1], [0, 1, 0.1], | |
], | |
'blocks': [ | |
( | |
'hex', [0, 1, 2, 3, 4, 5, 6, 7], [20, 20, 1], | |
'simpleGrading', [1, 1, 1], | |
) | |
], | |
'edges': [], | |
# Note the odd way in which boundary is defined here as a | |
# list of tuples. | |
'boundary': [ | |
('movingWall', { | |
'type': 'wall', | |
'faces': [ [3, 7, 6, 2] ], | |
}), | |
('fixedWalls', { | |
'type': 'wall', | |
'faces': [ | |
[0, 4, 7, 3], | |
[2, 6, 5, 1], | |
[1, 5, 4, 0], | |
], | |
}), | |
('frontAndBack', { | |
'type': 'empty', | |
'faces': [ | |
[0, 3, 2, 1], | |
[4, 5, 6, 7], | |
], | |
}), | |
], | |
'mergePatchPairs': [], | |
} | |
# fvSolution dictionary | |
FV_SOLUTION_DICT = { | |
'solvers': { | |
'p': { | |
'solver': 'PCG', | |
'preconditioner': 'DIC', | |
'tolerance': 1e-6, | |
'relTol': 0, | |
}, | |
'U': { | |
'solver': 'smoothSolver', | |
'smoother': 'symGaussSeidel', | |
'tolerance': 1e-5, | |
'relTol': 0, | |
}, | |
}, | |
'PISO': { | |
'nCorrectors': 2, | |
'nNonOrthogonalCorrectors': 0, | |
'pRefCell': 0, | |
'pRefValue': 0, | |
} | |
} | |
# fvSchemes dict | |
FV_SCHEMES_DICT = { | |
'ddtSchemes': { 'default': 'Euler' }, | |
'gradSchemes': { 'default': 'Gauss linear', 'grad(p)': 'Gauss linear' }, | |
'divSchemes': { 'div(phi,U)': 'Gauss linear', 'default': 'none' }, | |
'laplacianSchemes': { 'default': 'Gauss linear orthogonal' }, | |
'interpolationSchemes': { 'default': 'linear' }, | |
'snGradSchemes': { 'default': 'orthogonal' }, | |
} | |
def run_on_case(case, command): | |
"""Utility function to run an OpenFOAM application on a given case.""" | |
runner = BasicRunner([command, '-case', case.root_dir_path]) | |
runner.start() | |
return runner.runOK() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment