From 33ff54bd3bc376417af83c825ba6a23aa24a5100 Mon Sep 17 00:00:00 2001 From: L-Nafaryus Date: Fri, 28 Jan 2022 21:16:35 +0500 Subject: [PATCH] Mod: formatting openfoam submodule --- anisotropy/openfoam/__init__.py | 21 +- anisotropy/openfoam/{foamcase.py => case.py} | 4 +- .../{runnerPresets.py => commands.py} | 0 anisotropy/openfoam/file.py | 104 +++++ anisotropy/openfoam/foamfile.py | 98 ----- anisotropy/openfoam/presets.py | 402 +++++++++--------- anisotropy/openfoam/utils.py | 3 +- 7 files changed, 325 insertions(+), 307 deletions(-) rename anisotropy/openfoam/{foamcase.py => case.py} (97%) rename anisotropy/openfoam/{runnerPresets.py => commands.py} (100%) create mode 100644 anisotropy/openfoam/file.py delete mode 100644 anisotropy/openfoam/foamfile.py diff --git a/anisotropy/openfoam/__init__.py b/anisotropy/openfoam/__init__.py index 07ca9f8..245435d 100644 --- a/anisotropy/openfoam/__init__.py +++ b/anisotropy/openfoam/__init__.py @@ -1,9 +1,22 @@ # -*- coding: utf-8 -*- -from .utils import version, uniform, datReader -from .foamfile import FoamFile -from .foamcase import FoamCase +from . import utils +from . import conversion + +from .file import FoamFile from .runner import FoamRunner +from .case import FoamCase from . import presets -from . import runnerPresets +from . import commands + + +__all__ = [ + "utils", + "conversion", + "FoamFile", + "FoamRunner", + "FoamCase", + "presets", + "commands" +] diff --git a/anisotropy/openfoam/foamcase.py b/anisotropy/openfoam/case.py similarity index 97% rename from anisotropy/openfoam/foamcase.py rename to anisotropy/openfoam/case.py index 191ec90..42540cf 100644 --- a/anisotropy/openfoam/foamcase.py +++ b/anisotropy/openfoam/case.py @@ -1,10 +1,12 @@ # -*- coding: utf-8 -*- -from anisotropy.openfoam.foamfile import FoamFile import os, shutil import re from copy import deepcopy +from . import FoamFile + + class FoamCase(object): def __init__(self, foamfiles: list = None, path: str = None): diff --git a/anisotropy/openfoam/runnerPresets.py b/anisotropy/openfoam/commands.py similarity index 100% rename from anisotropy/openfoam/runnerPresets.py rename to anisotropy/openfoam/commands.py diff --git a/anisotropy/openfoam/file.py b/anisotropy/openfoam/file.py new file mode 100644 index 0000000..05cb839 --- /dev/null +++ b/anisotropy/openfoam/file.py @@ -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 "".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) diff --git a/anisotropy/openfoam/foamfile.py b/anisotropy/openfoam/foamfile.py deleted file mode 100644 index 94d346f..0000000 --- a/anisotropy/openfoam/foamfile.py +++ /dev/null @@ -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) - - - diff --git a/anisotropy/openfoam/presets.py b/anisotropy/openfoam/presets.py index d2cd900..1a067ba 100644 --- a/anisotropy/openfoam/presets.py +++ b/anisotropy/openfoam/presets.py @@ -1,227 +1,223 @@ # -*- 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 __init__(self): - FoamFile.__init__(self, - "system/fvSolution", - _location = "system" - ) - 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 - } +def controlDict() -> FoamFile: + ff = FoamFile(filename = "system/controlDict") + + ff.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" + } + + 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": { - "nNonOrthogonalCorrectors": 0, - "consistent": "yes", - "residualControl": { - "p": 1e-02, - "U": 1e-03 - } + "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 }, - "relaxationFactors": { - "fields": { - "p": 0.3 - }, - "equations": { - "U": 0.7 - } + "equations": { + "U": 0.7 } } + } -class FvSchemes(FoamFile): - def __init__(self): - FoamFile.__init__(self, - "system/fvSchemes", - _location = "system" - ) - 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" - } + return ff + + +def fvSchemes() -> FoamFile: + ff = FoamFile(filename = "system/fvSchemes") + + ff.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" + } + } + + 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): - def __init__(self): - FoamFile.__init__(self, - "constant/transportProperties", - _location = "constant" - ) - self.content = { - "transportModel": "Newtonian", - "nu": 1e-05 - } + return ff -class TurbulenceProperties(FoamFile): - def __init__(self): - FoamFile.__init__(self, - "constant/turbulenceProperties", - _location = "constant" - ) - self.content = { - "simulationType": "RAS", - "RAS": { - "RASModel": "kEpsilon", - "turbulence": "on", - "printCoeffs": "on" + +def p() -> FoamFile: + ff = FoamFile("0/p", _class = "volScalarField") + + ff.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 P(FoamFile): - def __init__(self): - FoamFile.__init__(self, - "0/p", - _location = "0", - _class = "volScalarField" - ) - 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" - } + return ff + + +def U() -> FoamFile: + ff = FoamFile("0/U", _class = "volVectorField") + + ff.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 U(FoamFile): - def __init__(self): - FoamFile.__init__(self, - "0/U", - _location = "0", - _class = "volVectorField" - ) - 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)" + return ff + + +def createPatchDict() -> FoamFile: + ff = FoamFile("system/createPatchDict") + + ff.content = { + "pointSync": False, + "patches": [ + { + "name": "inlet", + "patchInfo": { + "type": "patch", + "inGroups": ["inlet"] }, - "outlet": { - "type": "zeroGradient", + "constructFrom": "patches", + "patches": ["some_inlet"] + }, + { + "name": "output", + "patchInfo": { + "type": "patch", + "inGroups": ["outlet"] }, - "wall": { - "type": "fixedValue", - "value": "uniform (0 0 0)" - } + "constructFrom": "patches", + "patches": ["some_outlet"] + }, + { + "name": "wall", + "patchInfo": { + "type": "wall", + "inGroups": ["wall"] + }, + "constructFrom": "patches", + "patches": ["some_wall"] } - } + ] + } -class CreatePatchDict(FoamFile): - 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"] - } - ] - } + return ff -class DecomposeParDict(FoamFile): - def __init__(self): - FoamFile.__init__(self, - "system/decomposeParDict", - _location = "system", - ) - self.content = { - "numberOfSubdomains": 4, - "method": "simple", - "coeffs": { - "n": [2, 2, 2] - } + +def decomposeParDict() -> FoamFile: + ff = FoamFile("system/decomposeParDict") + + ff.content = { + "numberOfSubdomains": 4, + "method": "simple", + "coeffs": { + "n": [2, 2, 2] } + } + + return ff diff --git a/anisotropy/openfoam/utils.py b/anisotropy/openfoam/utils.py index 1dea920..ba9112d 100644 --- a/anisotropy/openfoam/utils.py +++ b/anisotropy/openfoam/utils.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- from __future__ import annotations -from numpy.typing import ndarray, ArrayLike +from numpy.typing import ArrayLike +from numpy import ndarray import os