diff --git a/anisotropy/openfoam/foamcase.py b/anisotropy/openfoam/foamcase.py index 4898cb6..5618d5d 100644 --- a/anisotropy/openfoam/foamcase.py +++ b/anisotropy/openfoam/foamcase.py @@ -23,12 +23,12 @@ class FoamCase(object): os.chdir(self.__curpath) self.__curpath = None - def append(self, foamfile: FoamFile): - if isinstance(foamfile, FoamFile): - setattr(self, foamfile.header["object"], foamfile) + def append(self, ff: FoamFile): + #if isinstance(foamfile, FoamFile): + setattr(self, ff.header["object"], ff) - else: - raise Exception("Trying to append not a FoamFile.") + #else: + # raise Exception("Trying to append not a FoamFile.") def extend(self, foamfiles: list): for ff in foamfiles: diff --git a/anisotropy/openfoam/foamfile.py b/anisotropy/openfoam/foamfile.py index 6f1099c..143ee4f 100644 --- a/anisotropy/openfoam/foamfile.py +++ b/anisotropy/openfoam/foamfile.py @@ -38,6 +38,9 @@ class FoamFile(object): def __delitem__(self, key): del self.content[key] + def update(self, **kwargs): + self.content.update(**kwargs) + def __len__(self): return len(self.content) @@ -73,7 +76,7 @@ class FoamFile(object): def write(self, casepath: str = None): header = FoamFileGenerator({}, header = self.header) header = header.makeString()[ :-2] - header = header.replace("\n ", "\n" + 4 * " ") + header = header.replace("\n ", "\n" + 4 * " ") content = FoamFileGenerator(self.content) content = content.makeString()[ :-1] diff --git a/anisotropy/openfoam/presets.py b/anisotropy/openfoam/presets.py index 5f026cd..067c337 100644 --- a/anisotropy/openfoam/presets.py +++ b/anisotropy/openfoam/presets.py @@ -6,11 +6,10 @@ from anisotropy.openfoam.foamfile import FoamFile class ControlDict(FoamFile): def __init__(self): - ff = FoamFile( + FoamFile.__init__(self, "system/controlDict", _location = "system" ) - self.header = ff.header self.content = { "application": "simpleFoam", "startFrom": "startTime", @@ -31,11 +30,10 @@ class ControlDict(FoamFile): class FvSolution(FoamFile): def __init__(self): - ff = FoamFile( + FoamFile.__init__(self, "system/fvSolution", _location = "system" ) - self.header = ff.header self.content = { "solvers": { "p": { @@ -71,26 +69,25 @@ class FvSolution(FoamFile): class FvSchemes(FoamFile): def __init__(self): - ff = FoamFile( + FoamFile.__init__(self, "system/fvSchemes", _location = "system" ) - self.header = ff.header self.content = { "ddtSchemes": { "default": "steadyState" }, "gradSchemes": { - "default": ["Gauss", "linear"] + "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"] + "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"] + "default": ("Gauss", "linear", "corrected") }, "interpolationSchemes": { "default": "linear" @@ -102,11 +99,10 @@ class FvSchemes(FoamFile): class TransportProperties(FoamFile): def __init__(self): - ff = FoamFile( + FoamFile.__init__(self, "constant/transportProperties", _location = "constant" ) - self.header = ff.header self.content = { "transportModel": "Newtonian", "nu": 1e-05 @@ -114,11 +110,10 @@ class TransportProperties(FoamFile): class TurbulenceProperties(FoamFile): def __init__(self): - ff = FoamFile( + FoamFile.__init__(self, "constant/turbulenceProperties", _location = "constant" ) - self.header = ff.header self.content = { "simulationType": "RAS", "RAS": { @@ -130,12 +125,11 @@ class TurbulenceProperties(FoamFile): class P(FoamFile): def __init__(self): - ff = FoamFile( + FoamFile.__init__(self, "0/p", _location = "0", _class = "volScalarField" ) - self.header = ff.header self.content = { "dimensions": "[0 2 -2 0 0 0 0]", "internalField": "uniform 0", @@ -156,12 +150,11 @@ class P(FoamFile): class U(FoamFile): def __init__(self): - ff = FoamFile( + FoamFile.__init__(self, "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)", @@ -182,11 +175,10 @@ class U(FoamFile): class CreatePatchDict(FoamFile): def __init__(self): - ff = FoamFile( + FoamFile.__init__(self, "system/createPatchDict", _location = "system", ) - self.header = ff.header self.content = { "pointSync": False, "patches": [ @@ -222,11 +214,10 @@ class CreatePatchDict(FoamFile): class DecomposeParDict(FoamFile): def __init__(self): - ff = FoamFile( + FoamFile.__init__(self, "system/decomposeParDict", _location = "system", ) - self.header = ff.header self.content = { "numberOfSubdomains": 4, "method": "simple", diff --git a/anisotropy/samples/simple.py b/anisotropy/samples/simple.py index 427fe8b..1b4f874 100644 --- a/anisotropy/samples/simple.py +++ b/anisotropy/samples/simple.py @@ -236,12 +236,132 @@ class SimpleMesh(Mesh): #hypo3dVL = algo3d.ViscousLayers(...) -from anisotropy.openfoam.presets import ControlDict +from anisotropy.openfoam.presets import ( + ControlDict, FvSchemes, FvSolution, + TransportProperties, TurbulenceProperties, CreatePatchDict, + P, U +) +from anisotropy.openfoam.foamcase import FoamCase class SimpleFlow(object): # FoamCase def __init__(self): controlDict = ControlDict() - controlDict["startFrom"] = "latestTime" - controlDict["endTime"] = 5000 - controlDict["writeInterval"] = 100 + controlDict.update( + startFrom = "latestTime", + endTime = 5000, + writeInterval = 100 + ) + fvSchemes = FvSchemes() + + fvSolution = FvSolution() + fvSolution["solvers"]["U"].update( + nSweeps = 2, + tolerance = 1e-08 + ) + fvSolution["solvers"]["Phi"] = dict( + solver = "GAMG", + smoother = "DIC", + cacheAgglomeration = "yes", + agglomerator = "faceAreaPair", + nCellsInCoarsestLevel = 10, + mergeLevels = 1, + tolerance = 1e-06, + relTol = 0.01 + ) + fvSolution["potentialFlow"] = dict( + nNonOrthogonalCorrectors = 20, + PhiRefCell = 0, + PhiRefPoint = 0, + PhiRefValue = 0, + Phi = 0 + ) + fvSolution["cache"] = { "grad(U)": None } + fvSolution["SIMPLE"].update( + nNonOrthogonalCorrectors = 10, + residualControl = dict( + p = 1e-05, + U = 1e-05 + ) + ) + fvSolution["relaxationFactors"]["equations"]["U"] = 0.5 + + transportProperties = TransportProperties() + transportProperties.update( + nu = 1e-06 + ) + + turbulenceProperties = TurbulenceProperties() + turbulenceProperties.content = dict( + simulationType = "laminar" + ) + + createPatchDict = CreatePatchDict() + createPatchDict["patches"] = [] + + for patch in ["inlet", "outlet", "wall", "strips", *[ f"symetry{ n }" for n in range(6) ]]: + newPatch = dict( + name = patch, + patchInfo = dict( + type = "patch", + inGroups = [patch] + ), + constructFrom = "patches", + patches = [ f"smesh_{ patch }" ] + ) + + match patch: + case "wall" | "strips": + newPatch["patchInfo"].update( + type = "wall", + inGroups = [ "wall" ] + ) + + case patch if patch.find("symetry") == 0: + newPatch["patchInfo"]["inGroups"] = [ "symetryPlane" ] + + createPatchDict["patches"].append(newPatch) + + + boundaries = [ "inlet", "outlet", "symetryPlane", "wall", "strips" ] + p = P() + p["boundaryField"] = {} + u = U() + u["boundaryField"] = {} + + for boundary in boundaries: + match boundary: + case "inlet": + p["boundaryField"][boundary] = dict( + type = "fixedValue", + value = "uniform 0.001" + ) + u["boundaryField"][boundary] = dict( + type = "fixedValue", + value = "uniform (0 0 -6e-5)" + ) + + case "outlet": + p["boundaryField"][boundary] = dict( + type = "fixedValue", + value = "uniform 0" + ) + u["boundaryField"][boundary] = dict( + type = "zeroGradient", + ) + + case _: + p["boundaryField"][boundary] = dict( + type = "zeroGradient" + ) + u["boundaryField"][boundary] = dict( + type = "fixedValue", + value = "uniform (0 0 0)" + ) + + + self.approximation = FoamCase([ + controlDict, fvSchemes, fvSolution, createPatchDict, + transportProperties, turbulenceProperties, + p, u + ])