Mod: foamfile presets

This commit is contained in:
L-Nafaryus 2021-10-21 16:04:19 +05:00
parent b45954d153
commit 3071a81d1b
No known key found for this signature in database
GPG Key ID: EF672A5303B2FA96
4 changed files with 319 additions and 31 deletions

View File

@ -3,28 +3,74 @@
# License: GNU GPL version 3, see the file "LICENSE" for details. # License: GNU GPL version 3, see the file "LICENSE" for details.
from anisotropy.openfoam.foamfile import FoamFile 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
}

View File

@ -17,12 +17,12 @@ class FoamFile(object):
_object = None _object = None
): ):
self.path = os.path.abspath(filename) self.filename = filename
self.header = { self.header = {
"version": _version, "version": _version,
"format": _format, "format": _format,
"class": _class, "class": _class,
"object": _object or os.path.split(filename)[1] "object": _object or os.path.split(self.filename)[1]
} }
self.content = {} self.content = {}
@ -46,7 +46,7 @@ class FoamFile(object):
yield key yield key
def read(self): def read(self):
ppf = ParsedParameterFile(self.path) ppf = ParsedParameterFile(os.path.abspath(self.filename))
self.header = ppf.header self.header = ppf.header
self.content = ppf.content self.content = ppf.content
@ -70,7 +70,7 @@ class FoamFile(object):
return "\n".join([*desc, header, afterheader, content, endfile]) return "\n".join([*desc, header, afterheader, content, endfile])
def write(self): def write(self, casepath: str = None):
header = FoamFileGenerator({}, header = self.header) header = FoamFileGenerator({}, header = self.header)
header = header.makeString()[ :-2] header = header.makeString()[ :-2]
header = header.replace("\n ", "\n" + 4 * " ") header = header.replace("\n ", "\n" + 4 * " ")
@ -82,9 +82,15 @@ class FoamFile(object):
prepared = self._template(header, content) 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) _ = io.write(prepared)

View File

@ -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]
}
}

View File

@ -236,7 +236,7 @@ class SimpleMesh(Mesh):
#hypo3dVL = algo3d.ViscousLayers(...) #hypo3dVL = algo3d.ViscousLayers(...)
from anisotropy.openfoam.foamcase import ControlDict from anisotropy.openfoam.presets import ControlDict
class SimpleFlow(object): # FoamCase class SimpleFlow(object): # FoamCase
def __init__(self): def __init__(self):