Mod: forcing db

This commit is contained in:
L-Nafaryus 2021-07-22 15:27:26 +05:00
parent a962a6d64f
commit 92a232fd3d
4 changed files with 196 additions and 163 deletions

View File

@ -46,8 +46,12 @@ import os
import toml import toml
from copy import deepcopy from copy import deepcopy
from anisotropy.models import db, Structure, Mesh from anisotropy.models import db, Structure, Mesh
from anisotropy.utils import struct
import salomepl import salomepl
###
# Environment variables and config
##
env = { "ROOT": os.path.abspath(".") } env = { "ROOT": os.path.abspath(".") }
env.update({ env.update({
"BUILD": os.path.join(env["ROOT"], "build"), "BUILD": os.path.join(env["ROOT"], "build"),
@ -68,9 +72,12 @@ if os.path.exists(env["CONFIG"]):
if config.get(restricted): if config.get(restricted):
config.pop(restricted) config.pop(restricted)
env.update(config) # TODO: dict replacing
#env.update(config)
###
# Logger
##
logger_env = env.get("logger", {}) logger_env = env.get("logger", {})
logging.basicConfig( logging.basicConfig(
level = logging.INFO, level = logging.INFO,
@ -99,9 +106,9 @@ def timer(func):
class Anisotropy(object): class Anisotropy(object):
def __init__(self): def __init__(self):
self.db = self._setupDB() self.db = self._setupDB()
self.structures = self._expandConfigParams(env["structures"]) self.params = []
#self.updateDB() self.evalParameters(env)
@staticmethod @staticmethod
def version(): def version():
@ -130,126 +137,120 @@ class Anisotropy(object):
return db return db
@staticmethod def evalParameters(self, _env: dict):
def _expandConfigParams(params):
structures = deepcopy(params)
for structure in structures:
theta = structure["geometry"]["theta"]
start, end = int(theta[0] / theta[2]), int(theta[1] / theta[2]) + 1
structure["geometry"]["theta"] = list(
map(lambda n: n * theta[2], range(start, end))
)
thickness = structure["mesh"]["thickness"]
count = len(structure["geometry"]["theta"])
structure["mesh"]["thickness"] = list(
map(lambda n: thickness[0] + n * (thickness[1] - thickness[0]) / (count - 1), range(0, count))
)
return structures
@staticmethod
def _setupQueue(params):
structures = deepcopy(params)
queue = []
for structure in structures:
for direction in structure["geometry"]["directions"]:
for n, theta in enumerate(structure["geometry"]["theta"]):
prequeue = deepcopy(structure)
del prequeue["geometry"]["directions"]
prequeue["geometry"]["direction"] = direction
prequeue["geometry"]["theta"] = theta
prequeue["path"] = os.path.join(
env["BUILD"],
structure["name"],
"direction-{}{}{}".format(*direction),
"theta-{}".format(theta)
)
prequeue["mesh"]["thickness"] = structure["mesh"]["thickness"][n]
queue.append(prequeue)
return queue
def updateDB(self):
queue = self._setupQueue(self.structures)
for structure in queue:
s = Structure.create(
name = structure["name"],
path = structure["path"],
**structure["geometry"]
)
Mesh.create(
structure = s,
**structure["mesh"]
)
def computeGeometryParams(self):
from math import sqrt from math import sqrt
structures = self._setupQueue(self.structures) structures = deepcopy(_env["structures"])
for s in structures: for structure in structures:
theta = s["geometry"]["theta"] _theta = structure["geometry"]["theta"]
thetaMin = int(_theta[0] / _theta[2])
if s["name"] == "simple": thetaMax = int(_theta[1] / _theta[2]) + 1
r0 = 1 thetaList = list(
L = 2 * r0 map(lambda n: n * _theta[2], range(thetaMin, thetaMax))
radius = r0 / (1 - theta)
C1, C2 = 0.8, 0.5
theta1, theta2 = 0.01, 0.28
Cf = C1 + (C2 - C1) / (theta2 - theta1) * (theta - theta1)
delta = 0.2
fillets = delta - Cf * (radius - r0)
elif s["name"] == "faceCentered":
L = 1.0
r0 = L * sqrt(2) / 4
radius = r0 / (1 - theta)
C1, C2 = 0.3, 0.2
theta1, theta2 = 0.01, 0.13
Cf = C1 + (C2 - C1) / (theta2 - theta1) * (theta - theta1)
delta = 0.012
fillets = delta - Cf * (radius - r0)
elif s["name"] == "bodyCentered":
L = 1.0
r0 = L * sqrt(3) / 4
radius = r0 / (1 - theta)
C1, C2 = 0.3, 0.2
theta1, theta2 = 0.01, 0.18
Cf = C1 + (C2 - C1) / (theta2 - theta1) * (theta - theta1)
delta = 0.02
fillets = delta - Cf * (radius - r0)
buf = {} #deepcopy(s)
buf.update(
{"name": s["name"],
"path": s["path"]},
**s["geometry"]
)
buf.update({
"r0": r0,
"L": L,
"radius": radius,
"fillets": fillets
})
stable = Structure.create(**buf)
Mesh.create(
structure = stable,
**s["mesh"]
) )
_thickness = structure["mesh"]["thickness"]
count = len(thetaList)
thicknessList = list(
map(lambda n: _thickness[0] + n * (_thickness[1] - _thickness[0]) / (count - 1), range(0, count))
)
for direction in structure["geometry"]["directions"]:
for n, theta in enumerate(thetaList):
if structure["name"] == "simple":
r0 = 1
L = 2 * r0
radius = r0 / (1 - theta)
C1, C2 = 0.8, 0.5
Cf = C1 + (C2 - C1) / (thetaMax - thetaMin) * (theta - thetaMin)
delta = 0.2
fillets = delta - Cf * (radius - r0)
elif structure["name"] == "faceCentered":
L = 1.0
r0 = L * sqrt(2) / 4
radius = r0 / (1 - theta)
C1, C2 = 0.3, 0.2
Cf = C1 + (C2 - C1) / (thetaMax - thetaMin) * (theta - thetaMin)
delta = 0.012
fillets = delta - Cf * (radius - r0)
elif structure["name"] == "bodyCentered":
L = 1.0
r0 = L * sqrt(3) / 4
radius = r0 / (1 - theta)
C1, C2 = 0.3, 0.2
Cf = C1 + (C2 - C1) / (thetaMax - thetaMin) * (theta - thetaMin)
delta = 0.02
fillets = delta - Cf * (radius - r0)
path = os.path.join(
_env["BUILD"],
structure["name"],
f"direction-{ direction }",
f"theta-{ theta }"
)
geometry = dict(
theta = theta,
direction = direction,
r0 = r0,
L = L,
radius = radius,
filletsEnabled = structure["geometry"]["filletsEnabled"],
fillets = fillets
)
mesh = deepcopy(structure["mesh"])
mesh.update(
thickness = thicknessList[n]
)
self.params.append({
"name": structure["name"],
"path": path,
"geometry": geometry,
"mesh": mesh,
"submesh": deepcopy(structure["submesh"])
})
# SELECT * FROM structure LEFT OUTER JOIN mesh ON mesh.structure_id = structure.id WHERE name = "faceCentered" AND direction = "[1, 1, 1]" AND theta = 0.12;
def updateDB(self):
for entry in self.params:
query = (Structure
.select()
.where(
Structure.name == entry["name"],
Structure.direction == str(entry["geometry"]["direction"]),
Structure.theta == entry["geometry"]["theta"]
)
)
s = deepcopy(entry["geometry"])
s.update(
name = entry["name"],
path = entry["path"]
)
m = deepcopy(entry["mesh"])
if not query.exists():
with self.db.atomic():
stab = Structure.create(**s)
m.update(structure = stab)
mtab = Mesh.create(**m)
else:
with self.db.atomic():
stab = Structure.update(**s)
m.update(structure = stab)
mtab = Mesh.update(**m)
@timer @timer
def computeMesh(self): def computeMesh(self):
@ -258,9 +259,6 @@ class Anisotropy(object):
out, err, returncode = salomepl.runSalome(port, scriptpath, env["ROOT"], case) out, err, returncode = salomepl.runSalome(port, scriptpath, env["ROOT"], case)
def computeFlow(self): def computeFlow(self):
pass pass

View File

@ -17,7 +17,7 @@ faceCentered = true
[0, 0, 1], [0, 0, 1],
[1, 1, 1] [1, 1, 1]
] ]
fillet = true filletsEnabled = true
[structures.mesh] [structures.mesh]
viscousLayers = true viscousLayers = true
@ -38,7 +38,7 @@ faceCentered = true
[0, 0, 1], [0, 0, 1],
[1, 1, 1] [1, 1, 1]
] ]
fillet = true filletsEnabled = true
[structures.mesh] [structures.mesh]
viscousLayers = true viscousLayers = true
@ -59,7 +59,7 @@ faceCentered = true
[0, 0, 1], [0, 0, 1],
[1, 1, 1] [1, 1, 1]
] ]
fillet = true filletsEnabled = true
[structures.mesh] [structures.mesh]
viscousLayers = true viscousLayers = true

View File

@ -1,11 +1,37 @@
from peewee import * from peewee import *
db = SqliteDatabase(None) class ListField(Field):
field_type = "list"
def db_value(self, value):
return str(value)
def python_value(self, value):
pval = []
for ch in value[1 : -1].split(","):
try:
pval.append(float(ch))
except:
pass
finally:
pval.append(ch.strip())
return pval
db = SqliteDatabase(
None,
pragmas = { "foreign_keys": 1 },
field_types = { "list": "text" }
)
class BaseModel(Model): class BaseModel(Model):
class Meta: class Meta:
database = db database = db
class Structure(BaseModel): class Structure(BaseModel):
name = TextField() name = TextField()
direction = TextField() direction = TextField()
@ -15,39 +41,48 @@ class Structure(BaseModel):
L = FloatField() L = FloatField()
radius = FloatField() radius = FloatField()
fillet = BooleanField() filletsEnabled = BooleanField()
fillets = FloatField() fillets = FloatField()
path = TextField() path = TextField()
class Mesh(BaseModel): class Mesh(BaseModel):
structure = ForeignKeyField(Structure, backref = "meshes") structure_id = ForeignKeyField(Structure, backref = "meshes")
maxSize = FloatField() maxSize = FloatField(null = True)
minSize = FloatField() minSize = FloatField(null = True)
fineness = IntegerField() fineness = IntegerField(null = True)
growthRate = FloatField() growthRate = FloatField(null = True)
nbSegPerEdge = FloatField() nbSegPerEdge = FloatField(null = True)
nbSegPerRadius = FloatField() nbSegPerRadius = FloatField(null = True)
chordalErrorEnabled = BooleanField() chordalErrorEnabled = BooleanField(null = True)
chordalError = FloatField() chordalError = FloatField(null = True)
secondOrder = BooleanField() secondOrder = BooleanField(null = True)
optimize = BooleanField() optimize = BooleanField(null = True)
quadAllowed = BooleanField() quadAllowed = BooleanField(null = True)
useSurfaceCurvature = BooleanField() useSurfaceCurvature = BooleanField(null = True)
fuseEdges = BooleanField() fuseEdges = BooleanField(null = True)
checkChartBoundary = BooleanField() checkChartBoundary = BooleanField(null = True)
viscousLayers = BooleanField() viscousLayers = BooleanField(null = True)
thickness = FloatField() thickness = FloatField(null = True)
numberOfLayers = IntegerField() numberOfLayers = IntegerField(null = True)
stretchFactor = FloatField() stretchFactor = FloatField(null = True)
isFacesToIgnore = BooleanField() isFacesToIgnore = BooleanField(null = True)
#facesToIgnore = ["inlet", "outlet"] #facesToIgnore = ["inlet", "outlet"]
#faces = [] #faces = []
#extrusionMethod = ExtrusionMethod.SURF_OFFSET_SMOOTH #extrusionMethod = ExtrusionMethod.SURF_OFFSET_SMOOTH
calculationTime = TimeField()
class SubMesh(Mesh):
mesh_id = ForeignKeyField(Mesh, backref = "submeshes")
class MeshResult(BaseModel):
mesh_id = ForeignKeyField(Mesh, backref = "meshresults")
calculationTime = TimeField(null = True)

View File

@ -66,23 +66,23 @@ def defaultParameters(**configParameters):
return p return p
def updateParams(old, new: dict): def updateParams(old, new: dict):
old.SetMaxSize(new.get("maxSize") if new.get("maxSize") else old.GetMaxSize()) old.SetMaxSize(new.get("maxSize", old.GetMaxSize()))
old.SetMinSize(new.get("minSize") if new.get("minSize") else old.GetMinSize()) old.SetMinSize(new.get("minSize", old.GetMinSize()))
old.SetFineness(new.get("fineness") if new.get("fineness") else old.GetFineness()) old.SetFineness(new.get("fineness", old.GetFineness()))
old.SetGrowthRate(new.get("growthRate") if new.get("growthRate") else old.GetGrowthRate()) old.SetGrowthRate(new.get("growthRate", old.GetGrowthRate()))
old.SetNbSegPerEdge(new.get("nbSegPerEdge") if new.get("nbSegPerEdge") else old.GetNbSegPerEdge()) old.SetNbSegPerEdge(new.get("nbSegPerEdge", old.GetNbSegPerEdge()))
old.SetNbSegPerRadius(new.get("nbSegPerRadius") if new.get("nbSegPerRadius") else old.GetNbSegPerRadius()) old.SetNbSegPerRadius(new.get("nbSegPerRadius", old.GetNbSegPerRadius()))
old.SetChordalErrorEnabled(new.get("chordalErrorEnabled") if new.get("chordalErrorEnabled") else old.GetChordalErrorEnabled()) old.SetChordalErrorEnabled(new.get("chordalErrorEnabled", old.GetChordalErrorEnabled()))
old.SetChordalError(new.get("chordalError") if new.get("chordalError") else old.GetChordalError()) old.SetChordalError(new.get("chordalError", old.GetChordalError()))
old.SetSecondOrder(new.get("secondOrder") if new.get("secondOrder") else old.GetSecondOrder()) old.SetSecondOrder(new.get("secondOrder", old.GetSecondOrder()))
old.SetOptimize(new.get("optimize") if new.get("optimize") else old.GetOptimize()) old.SetOptimize(new.get("optimize", old.GetOptimize()))
old.SetQuadAllowed(new.get("quadAllowed") if new.get("quadAllowed") else old.GetQuadAllowed()) old.SetQuadAllowed(new.get("quadAllowed", old.GetQuadAllowed()))
old.SetUseSurfaceCurvature(new.get("useSurfaceCurvature") if new.get("useSurfaceCurvature") else old.GetUseSurfaceCurvature()) old.SetUseSurfaceCurvature(new.get("useSurfaceCurvature", old.GetUseSurfaceCurvature()))
old.SetFuseEdges(new.get("fuseEdges") if new.get("fuseEdges") else old.GetFuseEdges()) old.SetFuseEdges(new.get("fuseEdges", old.GetFuseEdges()))
old.SetCheckChartBoundary(new.get("checkChartBoundary") if new.get("checkChartBoundary") else old.GetCheckChartBoundary()) old.SetCheckChartBoundary(new.get("checkChartBoundary", old.GetCheckChartBoundary()))
class Mesh(object): class Mesh(object):