Mod: formatting openfoam submodule

This commit is contained in:
L-Nafaryus 2022-01-28 21:16:35 +05:00
parent 6bfbb83b77
commit 33ff54bd3b
No known key found for this signature in database
GPG Key ID: C76D8DCD2727DBB7
7 changed files with 325 additions and 307 deletions

View File

@ -1,9 +1,22 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from .utils import version, uniform, datReader from . import utils
from .foamfile import FoamFile from . import conversion
from .foamcase import FoamCase
from .file import FoamFile
from .runner import FoamRunner from .runner import FoamRunner
from .case import FoamCase
from . import presets from . import presets
from . import runnerPresets from . import commands
__all__ = [
"utils",
"conversion",
"FoamFile",
"FoamRunner",
"FoamCase",
"presets",
"commands"
]

View File

@ -1,10 +1,12 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from anisotropy.openfoam.foamfile import FoamFile
import os, shutil import os, shutil
import re import re
from copy import deepcopy from copy import deepcopy
from . import FoamFile
class FoamCase(object): class FoamCase(object):
def __init__(self, foamfiles: list = None, path: str = None): def __init__(self, foamfiles: list = None, path: str = None):

104
anisotropy/openfoam/file.py Normal file
View File

@ -0,0 +1,104 @@
# -*- coding: utf-8 -*-
import pathlib
from . import conversion
class FoamFile(object):
def __init__(
self,
_version: float = 2.0,
_format: str = "ascii",
_class: str = "dictionary",
_object: str = None,
_location: str = None,
filename: str = None
):
"""A FoamFile object.
:param _version:
Version of the file format, current is 2.0.
:param _format:
ASCII or binary representation, currently ascii only
supported.
:param _class:
Class of the file.
:param _object:
Usually contains name of the file.
:param _location:
Path to the parent directory of the file according
to the case root.
:param filename:
Can be used as shortcut to set _location and _object,
_location and _object parameters will be ignored.
"""
if filename:
splitted = filename.split("/")
_object = splitted[-1]
_location = "/".join(splitted[ :-1])
self.header = {
"version": _version,
"format": _format,
"class": _class,
"object": _object
}
self.content = {}
if _location:
self.header["location"] = f'"{ _location }"'
def __getitem__(self, key):
return self.content[key]
def __setitem__(self, key, value):
self.content[key] = value
def __delitem__(self, key):
del self.content[key]
def update(self, **kwargs):
self.content.update(**kwargs)
def __len__(self):
return len(self.content)
def __iter__(self):
for key in self.content:
yield key
def __repr__(self) -> str:
return "<FoamFile: {}>".format(self.header["object"] or None)
def read(self, filename: str = None):
"""Read a FoamFile.
:param filename:
Path to the file. If None, use location from header with
current working directory.
:return:
Self.
"""
path = pathlib.Path(filename or self.header["location"]).resolve()
header, content = conversion.read_foamfile(path)
self.header = header
if not header.get("object"):
self.header["object"] = path.parts[-1]
self.content = content
return self
def write(self, filename: str = None):
"""Write a FoamFile to the file.
:param filename:
Path to the file. If None, use location from header with
current working directory..
"""
filename = pathlib.Path(filename or self.header["location"]).resolve()
conversion.write_foamfile(self.header, self.content, filename)

View File

@ -1,98 +0,0 @@
# -*- coding: utf-8 -*-
from anisotropy.openfoam.utils import version
from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile
from PyFoam.Basics.FoamFileGenerator import FoamFileGenerator
import os
class FoamFile(object):
def __init__(self,
filename,
_version = 2.0,
_format = "ascii",
_class = "dictionary",
_location = None,
_object = None
):
self.filename = filename
self.header = {
"version": _version,
"format": _format,
"class": _class,
"object": _object or os.path.split(self.filename)[1]
}
self.content = {}
if _location:
self.header["location"] = f'"{ _location }"'
def __getitem__(self, key):
return self.content[key]
def __setitem__(self, key, value):
self.content[key] = value
def __delitem__(self, key):
del self.content[key]
def update(self, **kwargs):
self.content.update(**kwargs)
def __len__(self):
return len(self.content)
def __iter__(self):
for key in self.content:
yield key
def read(self):
ppf = ParsedParameterFile(os.path.abspath(self.filename))
self.header = ppf.header
self.content = ppf.content
def _template(self, header, content):
limit = 78
desc = [
"/*--------------------------------*- C++ -*----------------------------------*\\",
"| ========= | |",
"| \\\\ / F ield | OpenFOAM: The Open Source CFD Toolbox |",
"| \\\\ / O peration |",
"| \\\\ / A nd | |",
"| \\\\/ M anipulation | |",
"\\*---------------------------------------------------------------------------*/"
]
desc[3] += " Version: {}".format(version() or "missed")
desc[3] += " " * (limit - len(desc[3])) + "|"
afterheader = "// " + 37 * "* " + "//"
endfile = "// " + 73 * "*" + " //"
return "\n".join([*desc, header, afterheader, content, endfile])
def write(self, casepath: str = None):
header = FoamFileGenerator({}, header = self.header)
header = header.makeString()[ :-2]
header = header.replace("\n ", "\n" + 4 * " ")
content = FoamFileGenerator(self.content)
content = content.makeString()[ :-1]
content = content.replace("\n ", "\n" + 4 * " ").replace(" \t// " + 73 * "*" + " //", "")
content = content.replace(" /* empty */ ", "")
prepared = self._template(header, content)
if casepath:
path = os.path.join(casepath, self.filename)
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)

View File

@ -1,227 +1,223 @@
# -*- coding: utf-8 -*- # -*- 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 from . import FoamFile
class ControlDict(FoamFile):
def __init__(self):
FoamFile.__init__(self,
"system/controlDict",
_location = "system"
)
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 controlDict() -> FoamFile:
def __init__(self): ff = FoamFile(filename = "system/controlDict")
FoamFile.__init__(self,
"system/fvSolution", ff.content = {
_location = "system" "application": "simpleFoam",
) "startFrom": "startTime",
self.content = { "startTime": 0,
"solvers": { "stopAt": "endTime",
"p": { "endTime": 2000,
"solver": "GAMG", "deltaT": 1,
"tolerance": 1e-06, "writeControl": "timeStep",
"relTol": 0.1, "writeInterval": 100,
"smoother": "GaussSeidel" "purgeWrite": 0,
}, "writeFormat": "ascii",
"U": { "writePrecision": 6,
"solver": "smoothSolver", "writeCompression": "off",
"smoother": "symGaussSeidel", "timeFormat": "general",
"tolerance": 1e-05, "timePrecision": 6,
"relTol": 0.1 "runTimeModifiable": "true"
} }
return ff
def fvSolution() -> FoamFile:
ff = FoamFile(filename = "system/fvSolution")
ff.content = {
"solvers": {
"p": {
"solver": "GAMG",
"tolerance": 1e-06,
"relTol": 0.1,
"smoother": "GaussSeidel"
}, },
"SIMPLE": { "U": {
"nNonOrthogonalCorrectors": 0, "solver": "smoothSolver",
"consistent": "yes", "smoother": "symGaussSeidel",
"residualControl": { "tolerance": 1e-05,
"p": 1e-02, "relTol": 0.1
"U": 1e-03 }
} },
"SIMPLE": {
"nNonOrthogonalCorrectors": 0,
"consistent": "yes",
"residualControl": {
"p": 1e-02,
"U": 1e-03
}
},
"relaxationFactors": {
"fields": {
"p": 0.3
}, },
"relaxationFactors": { "equations": {
"fields": { "U": 0.7
"p": 0.3
},
"equations": {
"U": 0.7
}
} }
} }
}
class FvSchemes(FoamFile): return ff
def __init__(self):
FoamFile.__init__(self,
"system/fvSchemes", def fvSchemes() -> FoamFile:
_location = "system" ff = FoamFile(filename = "system/fvSchemes")
)
self.content = { ff.content = {
"ddtSchemes": { "ddtSchemes": {
"default": "steadyState" "default": "steadyState"
}, },
"gradSchemes": { "gradSchemes": {
"default": ("Gauss", "linear") "default": ("Gauss", "linear")
}, },
"divSchemes": { "divSchemes": {
"default": "none", "default": "none",
"div(phi,U)": ("bounded", "Gauss", "linearUpwind", "grad(U)"), "div(phi,U)": ("bounded", "Gauss", "linearUpwind", "grad(U)"),
"div((nuEff*dev2(T(grad(U)))))": ("Gauss", "linear"), "div((nuEff*dev2(T(grad(U)))))": ("Gauss", "linear"),
"div(nonlinearStress)": ("Gauss", "linear") "div(nonlinearStress)": ("Gauss", "linear")
}, },
"laplacianSchemes": { "laplacianSchemes": {
"default": ("Gauss", "linear", "corrected") "default": ("Gauss", "linear", "corrected")
}, },
"interpolationSchemes": { "interpolationSchemes": {
"default": "linear" "default": "linear"
}, },
"snGradSchemes": { "snGradSchemes": {
"default": "corrected" "default": "corrected"
} }
}
return ff
def transportProperties() -> FoamFile:
ff = FoamFile(filename = "constant/transportProperties")
ff.content = {
"transportModel": "Newtonian",
"nu": 1e-05
}
return ff
def turbulenceProperties() -> FoamFile:
ff = FoamFile(filename = "constant/turbulenceProperties")
ff.content = {
"simulationType": "RAS",
"RAS": {
"RASModel": "kEpsilon",
"turbulence": "on",
"printCoeffs": "on"
} }
}
class TransportProperties(FoamFile): return ff
def __init__(self):
FoamFile.__init__(self,
"constant/transportProperties",
_location = "constant"
)
self.content = {
"transportModel": "Newtonian",
"nu": 1e-05
}
class TurbulenceProperties(FoamFile):
def __init__(self): def p() -> FoamFile:
FoamFile.__init__(self, ff = FoamFile("0/p", _class = "volScalarField")
"constant/turbulenceProperties",
_location = "constant" ff.content = {
) "dimensions": "[0 2 -2 0 0 0 0]",
self.content = { "internalField": "uniform 0",
"simulationType": "RAS", "boundaryField": {
"RAS": { "inlet": {
"RASModel": "kEpsilon", "type": "fixedValue",
"turbulence": "on", "value": "uniform 0.001"
"printCoeffs": "on" },
"outlet": {
"type": "fixedValue",
"value": "uniform 0"
},
"wall": {
"type": "zeroGradient"
} }
} }
}
class P(FoamFile): return ff
def __init__(self):
FoamFile.__init__(self,
"0/p", def U() -> FoamFile:
_location = "0", ff = FoamFile("0/U", _class = "volVectorField")
_class = "volScalarField"
) ff.content = {
self.content = { "dimensions": "[0 1 -1 0 0 0 0]",
"dimensions": "[0 2 -2 0 0 0 0]", "internalField": "uniform (0 0 0)",
"internalField": "uniform 0", "boundaryField": {
"boundaryField": { "inlet": {
"inlet": { "type": "fixedValue",
"type": "fixedValue", "value": "uniform (0 0 -6e-5)"
"value": "uniform 0.001" },
}, "outlet": {
"outlet": { "type": "zeroGradient",
"type": "fixedValue", },
"value": "uniform 0" "wall": {
}, "type": "fixedValue",
"wall": { "value": "uniform (0 0 0)"
"type": "zeroGradient"
}
} }
} }
}
class U(FoamFile): return ff
def __init__(self):
FoamFile.__init__(self,
"0/U", def createPatchDict() -> FoamFile:
_location = "0", ff = FoamFile("system/createPatchDict")
_class = "volVectorField"
) ff.content = {
self.content = { "pointSync": False,
"dimensions": "[0 1 -1 0 0 0 0]", "patches": [
"internalField": "uniform (0 0 0)", {
"boundaryField": { "name": "inlet",
"inlet": { "patchInfo": {
"type": "fixedValue", "type": "patch",
"value": "uniform (0 0 -6e-5)" "inGroups": ["inlet"]
}, },
"outlet": { "constructFrom": "patches",
"type": "zeroGradient", "patches": ["some_inlet"]
},
{
"name": "output",
"patchInfo": {
"type": "patch",
"inGroups": ["outlet"]
}, },
"wall": { "constructFrom": "patches",
"type": "fixedValue", "patches": ["some_outlet"]
"value": "uniform (0 0 0)" },
} {
"name": "wall",
"patchInfo": {
"type": "wall",
"inGroups": ["wall"]
},
"constructFrom": "patches",
"patches": ["some_wall"]
} }
} ]
}
class CreatePatchDict(FoamFile): return ff
def __init__(self):
FoamFile.__init__(self,
"system/createPatchDict",
_location = "system",
)
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): def decomposeParDict() -> FoamFile:
FoamFile.__init__(self, ff = FoamFile("system/decomposeParDict")
"system/decomposeParDict",
_location = "system", ff.content = {
) "numberOfSubdomains": 4,
self.content = { "method": "simple",
"numberOfSubdomains": 4, "coeffs": {
"method": "simple", "n": [2, 2, 2]
"coeffs": {
"n": [2, 2, 2]
}
} }
}
return ff

View File

@ -1,7 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import annotations from __future__ import annotations
from numpy.typing import ndarray, ArrayLike from numpy.typing import ArrayLike
from numpy import ndarray
import os import os