From 3071a81d1b2e475eec8f1bfacd6e99a2a16edfcd Mon Sep 17 00:00:00 2001 From: L-Nafaryus Date: Thu, 21 Oct 2021 16:04:19 +0500 Subject: [PATCH] Mod: foamfile presets --- anisotropy/openfoam/foamcase.py | 94 +++++++++---- anisotropy/openfoam/foamfile.py | 18 ++- anisotropy/openfoam/presets.py | 236 ++++++++++++++++++++++++++++++++ anisotropy/samples/simple.py | 2 +- 4 files changed, 319 insertions(+), 31 deletions(-) create mode 100644 anisotropy/openfoam/presets.py diff --git a/anisotropy/openfoam/foamcase.py b/anisotropy/openfoam/foamcase.py index 566c4c1..4898cb6 100644 --- a/anisotropy/openfoam/foamcase.py +++ b/anisotropy/openfoam/foamcase.py @@ -3,28 +3,74 @@ # License: GNU GPL version 3, see the file "LICENSE" for details. from anisotropy.openfoam.foamfile import FoamFile +import os, shutil +import re + +class FoamCase(object): + def __init__(self, foamfiles: list = None, path: str = None): + + self.path = path or os.path.abspath("") + + if foamfiles: + self.extend(foamfiles) + + def __enter__(self): + self.__curpath = os.path.abspath("") + os.chdir(self.path) + return + + def __exit__(self, exc_type, exc_val, exc_tb): + os.chdir(self.__curpath) + self.__curpath = None + + def append(self, foamfile: FoamFile): + if isinstance(foamfile, FoamFile): + setattr(self, foamfile.header["object"], foamfile) + + else: + raise Exception("Trying to append not a FoamFile.") + + def extend(self, foamfiles: list): + for ff in foamfiles: + self.append(ff) + + def write(self): + for v in self.__dict__.values(): + if isinstance(v, FoamFile): + v.write(self.path) + + def read(self): + for v in self.__dict__.values(): + if isinstance(v, FoamFile): + v.read() + + def clean(self): + regxs = [ + r"^\d+.\d+$", + r"^\d+$", + r"^processor\d+$", + r"^log..+$", + r"^.+.log$", + r"^logs$", + r"^postProcessing$", + r"^polyMesh$" + ] + included = ["0", "constant", "system"] + excluded = [] + + for root, dirs, files in os.walk(os.path.abspath("")): + for _dir in dirs: + excluded += [ os.path.join(root, _dir) for regx in regxs if re.match(regx, _dir) ] + + for file in files: + excluded += [ os.path.join(root, file) for regx in regxs if re.match(regx, file) ] + + for file in excluded: + if os.path.split(file)[1] not in included and os.path.exists(file): + if os.path.isdir(file): + shutil.rmtree(file) + + if os.path.isfile(file): + os.remove(file) + -class ControlDict(FoamFile): - def __init__(self): - ff = FoamFile( - "system/controlDict", - _location = "system" - ) - self.header = ff.header - self.content = { - "application": "simpleFoam", - "startFrom": "startTime", - "startTime": 0, - "stopAt": "endTime", - "endTime": 2000, - "deltaT": 1, - "writeControl": "timeStep", - "writeInterval": 100, - "purgeWrite": 0, - "writeFormat": "ascii", - "writePrecision": 6, - "writeCompression": "off", - "timeFormat": "general", - "timePrecision": 6, - "runTimeModifiable": True - } diff --git a/anisotropy/openfoam/foamfile.py b/anisotropy/openfoam/foamfile.py index f7b6e19..6f1099c 100644 --- a/anisotropy/openfoam/foamfile.py +++ b/anisotropy/openfoam/foamfile.py @@ -17,12 +17,12 @@ class FoamFile(object): _object = None ): - self.path = os.path.abspath(filename) + self.filename = filename self.header = { "version": _version, "format": _format, "class": _class, - "object": _object or os.path.split(filename)[1] + "object": _object or os.path.split(self.filename)[1] } self.content = {} @@ -46,7 +46,7 @@ class FoamFile(object): yield key def read(self): - ppf = ParsedParameterFile(self.path) + ppf = ParsedParameterFile(os.path.abspath(self.filename)) self.header = ppf.header self.content = ppf.content @@ -70,7 +70,7 @@ class FoamFile(object): return "\n".join([*desc, header, afterheader, content, endfile]) - def write(self): + def write(self, casepath: str = None): header = FoamFileGenerator({}, header = self.header) header = header.makeString()[ :-2] header = header.replace("\n ", "\n" + 4 * " ") @@ -82,9 +82,15 @@ class FoamFile(object): prepared = self._template(header, content) - os.makedirs(os.path.split(self.path)[0], exist_ok = True) + if casepath: + path = os.path.join(casepath, self.filename) - with open(self.path, "w") as io: + else: + path = os.path.abspath(self.filename) + + os.makedirs(os.path.split(path)[0], exist_ok = True) + + with open(path, "w") as io: _ = io.write(prepared) diff --git a/anisotropy/openfoam/presets.py b/anisotropy/openfoam/presets.py new file mode 100644 index 0000000..5f026cd --- /dev/null +++ b/anisotropy/openfoam/presets.py @@ -0,0 +1,236 @@ +# -*- coding: utf-8 -*- +# This file is part of anisotropy. +# License: GNU GPL version 3, see the file "LICENSE" for details. + +from anisotropy.openfoam.foamfile import FoamFile + +class ControlDict(FoamFile): + def __init__(self): + ff = FoamFile( + "system/controlDict", + _location = "system" + ) + self.header = ff.header + self.content = { + "application": "simpleFoam", + "startFrom": "startTime", + "startTime": 0, + "stopAt": "endTime", + "endTime": 2000, + "deltaT": 1, + "writeControl": "timeStep", + "writeInterval": 100, + "purgeWrite": 0, + "writeFormat": "ascii", + "writePrecision": 6, + "writeCompression": "off", + "timeFormat": "general", + "timePrecision": 6, + "runTimeModifiable": True + } + +class FvSolution(FoamFile): + def __init__(self): + ff = FoamFile( + "system/fvSolution", + _location = "system" + ) + self.header = ff.header + self.content = { + "solvers": { + "p": { + "solver": "GAMG", + "tolerance": 1e-06, + "relTol": 0.1, + "smoother": "GaussSeidel" + }, + "U": { + "solver": "smoothSolver", + "smoother": "symGaussSeidel", + "tolerance": 1e-05, + "relTol": 0.1 + } + }, + "SIMPLE": { + "nNonOrthogonalCorrectors": 0, + "consistent": "yes", + "residualControl": { + "p": 1e-02, + "U": 1e-03 + } + }, + "relaxationFactors": { + "fields": { + "p": 0.3 + }, + "equations": { + "U": 0.7 + } + } + } + +class FvSchemes(FoamFile): + def __init__(self): + ff = FoamFile( + "system/fvSchemes", + _location = "system" + ) + self.header = ff.header + self.content = { + "ddtSchemes": { + "default": "steadyState" + }, + "gradSchemes": { + "default": ["Gauss", "linear"] + }, + "divSchemes": { + "default": "none", + "div(phi,U)": ["bounded", "Gauss", "linearUpwind", "grad(U)"], + "div((nuEff*dev2(T(grad(U)))))": ["Gauss", "linear"], + "div(nonlinearStress)": ["Gauss", "linear"] + }, + "laplacianSchemes": { + "default": ["Gauss", "linear", "corrected"] + }, + "interpolationSchemes": { + "default": "linear" + }, + "snGradSchemes": { + "default": "corrected" + } + } + +class TransportProperties(FoamFile): + def __init__(self): + ff = FoamFile( + "constant/transportProperties", + _location = "constant" + ) + self.header = ff.header + self.content = { + "transportModel": "Newtonian", + "nu": 1e-05 + } + +class TurbulenceProperties(FoamFile): + def __init__(self): + ff = FoamFile( + "constant/turbulenceProperties", + _location = "constant" + ) + self.header = ff.header + self.content = { + "simulationType": "RAS", + "RAS": { + "RASModel": "kEpsilon", + "turbulence": "on", + "printCoeffs": "on" + } + } + +class P(FoamFile): + def __init__(self): + ff = FoamFile( + "0/p", + _location = "0", + _class = "volScalarField" + ) + self.header = ff.header + self.content = { + "dimensions": "[0 2 -2 0 0 0 0]", + "internalField": "uniform 0", + "boundaryField": { + "inlet": { + "type": "fixedValue", + "value": "uniform 0.001" + }, + "outlet": { + "type": "fixedValue", + "value": "uniform 0" + }, + "wall": { + "type": "zeroGradient" + } + } + } + +class U(FoamFile): + def __init__(self): + ff = FoamFile( + "0/U", + _location = "0", + _class = "volVectorField" + ) + self.header = ff.header + self.content = { + "dimensions": "[0 1 -1 0 0 0 0]", + "internalField": "uniform (0 0 0)", + "boundaryField": { + "inlet": { + "type": "fixedValue", + "value": "uniform (0 0 -6e-5)" + }, + "outlet": { + "type": "zeroGradient", + }, + "wall": { + "type": "fixedValue", + "value": "uniform (0 0 0)" + } + } + } + +class CreatePatchDict(FoamFile): + def __init__(self): + ff = FoamFile( + "system/createPatchDict", + _location = "system", + ) + self.header = ff.header + self.content = { + "pointSync": False, + "patches": [ + { + "name": "inlet", + "patchInfo": { + "type": "patch", + "inGroups": ["inlet"] + }, + "constructFrom": "patches", + "patches": ["some_inlet"] + }, + { + "name": "output", + "patchInfo": { + "type": "patch", + "inGroups": ["outlet"] + }, + "constructFrom": "patches", + "patches": ["some_outlet"] + }, + { + "name": "wall", + "patchInfo": { + "type": "wall", + "inGroups": ["wall"] + }, + "constructFrom": "patches", + "patches": ["some_wall"] + } + ] + } + +class DecomposeParDict(FoamFile): + def __init__(self): + ff = FoamFile( + "system/decomposeParDict", + _location = "system", + ) + self.header = ff.header + self.content = { + "numberOfSubdomains": 4, + "method": "simple", + "coeffs": { + "n": [2, 2, 2] + } + } diff --git a/anisotropy/samples/simple.py b/anisotropy/samples/simple.py index 4ee8a75..427fe8b 100644 --- a/anisotropy/samples/simple.py +++ b/anisotropy/samples/simple.py @@ -236,7 +236,7 @@ class SimpleMesh(Mesh): #hypo3dVL = algo3d.ViscousLayers(...) -from anisotropy.openfoam.foamcase import ControlDict +from anisotropy.openfoam.presets import ControlDict class SimpleFlow(object): # FoamCase def __init__(self):