Mod: lazy commit

This commit is contained in:
L-Nafaryus 2021-08-05 14:21:43 +05:00
parent 8f038439ba
commit e20130f35f
3 changed files with 179 additions and 111 deletions

View File

@ -43,22 +43,20 @@ from anisotropy.models import db, JOIN, Structure, Mesh, SubMesh, MeshResult
from anisotropy.utils import struct, deepupdate from anisotropy.utils import struct, deepupdate
import salomepl import salomepl
import openfoam import openfoam
from math import sqrt
### ###
# Environment variables and config # Environment variables and config
## ##
env = { "ROOT": os.path.abspath(".") } env = { "ROOT": os.path.abspath(".") }
env.update({ env.update(dict(
"BUILD": os.path.join(env["ROOT"], "build"), BUILD = os.path.join(env["ROOT"], "build"),
"LOG": os.path.join(env["ROOT"], "logs"), LOG = os.path.join(env["ROOT"], "logs"),
"CONFIG": os.path.join(env["ROOT"], "conf/config.toml") DEFAULT_CONFIG = os.path.join(env["ROOT"], "anisotropy/default.toml"),
}) CONFIG = os.path.join(env["ROOT"], "conf/config.toml")
env["db_path"] = os.path.join(env["BUILD"], "anisotropy.db") ))
env["db_path"] = env["BUILD"]
_defaultConfig = os.path.join(env["ROOT"], "anisotropy/default.toml")
if os.path.exists(_defaultConfig):
env.update(toml.load(_defaultConfig))
#if os.path.exists(env["CONFIG"]): #if os.path.exists(env["CONFIG"]):
# config = toml.load(env["CONFIG"]) # config = toml.load(env["CONFIG"])
@ -111,8 +109,6 @@ class Anisotropy(object):
self.db = None self.db = None
self.params = [] self.params = []
#self.evalParameters(env)
#self.setupDB()
@staticmethod @staticmethod
def version(): def version():
@ -132,88 +128,128 @@ class Anisotropy(object):
return "\n".join([ f"{ k }: { v }" for k, v in versions.items() ]) return "\n".join([ f"{ k }: { v }" for k, v in versions.items() ])
def evalEnvParameters(self):
""" 'Uncompress' and eval environment parameters """ def loadScratch(self):
if not os.path.exists(self.env["DEFAULT_CONFIG"]):
logger.error("Missed default configuration file")
return
from math import sqrt buf = toml.load(self.env["DEFAULT_CONFIG"]).get("structures")
paramsAll = []
structures = deepcopy(self.env["structures"]) # TODO: custom config and merge
self.params = []
for entry in buf:
for structure in structures: # Shortcuts
_theta = structure["geometry"]["theta"] _theta = entry["structure"]["theta"]
thetaMin = int(_theta[0] / _theta[2]) thetaMin = int(_theta[0] / _theta[2])
thetaMax = int(_theta[1] / _theta[2]) + 1 thetaMax = int(_theta[1] / _theta[2]) + 1
thetaList = list( thetaList = list(
map(lambda n: n * _theta[2], range(thetaMin, thetaMax)) map(lambda n: n * _theta[2], range(thetaMin, thetaMax))
) )
_thickness = structure["mesh"]["thickness"] _thickness = entry["mesh"]["thickness"]
count = len(thetaList) count = len(thetaList)
thicknessList = list( thicknessList = list(
map(lambda n: _thickness[0] + n * (_thickness[1] - _thickness[0]) / (count - 1), range(0, count)) map(lambda n: _thickness[0] + n * (_thickness[1] - _thickness[0]) / (count - 1), range(0, count))
) )
for direction in structure["geometry"]["directions"]: for direction in entry["structure"]["directions"]:
for n, theta in enumerate(thetaList): for n, theta in enumerate(thetaList):
if structure["name"] == "simple": entryNew = {
r0 = 1 "structure": dict(
L = 2 * r0 type = entry["structure"]["type"],
radius = r0 / (1 - theta) theta = theta,
direction = [ float(num) for num in direction ],
),
"mesh": dict(
**deepcopy(entry["mesh"]),
thickness = thicknessList[n]
),
"submesh": dict(
**deepcopy(entry["submesh"])
)
}
paramsAll.append(entryNew)
C1, C2 = 0.8, 0.5 self.setupDB()
Cf = C1 + (C2 - C1) / (thetaMax - thetaMin) * (theta - thetaMin)
delta = 0.2 for entry in paramsAll:
fillets = delta - Cf * (radius - r0) self.updateDB(entry)
elif structure["name"] == "faceCentered":
L = 1.0 def evalParams(self):
r0 = L * sqrt(2) / 4 structure = self.params.get("structure")
radius = r0 / (1 - theta)
C1, C2 = 0.3, 0.2 if not structure:
Cf = C1 + (C2 - C1) / (thetaMax - thetaMin) * (theta - thetaMin) logger.error("Trying to eval empty parameters")
delta = 0.012 return
fillets = delta - Cf * (radius - r0)
if structure["type"] == "simple":
thetaMin = 0.01
thetaMax = 0.28
elif structure["name"] == "bodyCentered": r0 = 1
L = 1.0 L = 2 * r0
r0 = L * sqrt(3) / 4 radius = r0 / (1 - theta)
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["type"] == "faceCentered":
thetaMin = 0.01
thetaMax = 0.13
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["type"] == "bodyCentered":
thetaMin = 0.01
thetaMax = 0.18
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)
self.params["structure"] = dict(
**structure,
L = L,
r0 = r0,
radius = radius,
fillets = fillets
)
def getCasePath(self):
structure = self.params.get("structure")
if not structure:
logger.error("Trying to use empty parameters")
return
return os.path.join(
self.env["BUILD"],
structure["type"],
f"direction-{ structure['direction'] }",
f"theta-{ structure['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(
self.env["BUILD"],
structure["name"],
f"direction-{ direction }",
f"theta-{ theta }"
)
geometry = dict(
theta = theta,
direction = [ float(num) for num in 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(dict(
name = structure["name"],
path = path,
geometry = geometry,
mesh = mesh,
submesh = deepcopy(structure["submesh"])
))
def getParams(self, structure: str, direction: list, theta: float): def getParams(self, structure: str, direction: list, theta: float):
@ -225,8 +261,10 @@ class Anisotropy(object):
def setupDB(self): def setupDB(self):
os.makedirs(self.env["db_path"], exist_ok = True)
self.db = db self.db = db
self.db.init(self.env["db_path"]) self.db.init(os.path.join(self.env["db_path"], "anisotropy.db"))
if not os.path.exists(self.env["db_path"]): if not os.path.exists(self.env["db_path"]):
self.db.create_tables([ self.db.create_tables([
@ -238,11 +276,7 @@ class Anisotropy(object):
def _updateStructure(self, src: dict, queryMain) -> int: def _updateStructure(self, src: dict, queryMain) -> int:
raw = deepcopy(src["geometry"]) raw = deepcopy(src)
raw.update(
name = src["name"],
path = src["path"]
)
with self.db.atomic(): with self.db.atomic():
if not queryMain.exists(): if not queryMain.exists():
@ -250,12 +284,12 @@ class Anisotropy(object):
else: else:
req = queryMain.dicts().get() req = queryMain.dicts().get()
tabID = req["id"] tabID = req["structure_id"]
query = ( query = (
Structure.update(**raw) Structure.update(**raw)
.where( .where(
Structure.name == req["name"], Structure.type == req["type"],
Structure.direction == str(req["direction"]), Structure.direction == str(req["direction"]),
Structure.theta == req["theta"] Structure.theta == req["theta"]
) )
@ -281,7 +315,7 @@ class Anisotropy(object):
query = ( query = (
Mesh.update(**raw) Mesh.update(**raw)
.where( .where(
Mesh.structure_id == req["id"] Mesh.structure_id == req["structure_id"]
) )
) )
query.execute() query.execute()
@ -341,28 +375,64 @@ class Anisotropy(object):
query.execute() query.execute()
@timer @timer
def updateDB(self): def updateDB(self, src: dict = None):
for entry in self.params: if src:
query = ( params = src
Structure
.select(Structure, Mesh) elif self.params:
.join(Mesh, JOIN.INNER, on = (Mesh.structure_id == Structure.id)) params = self.params
.where(
Structure.name == entry["name"], else:
Structure.direction == str(entry["geometry"]["direction"]), logger.error("Trying to update db from empty parameters")
Structure.theta == entry["geometry"]["theta"] return
)
# TODO: query for every table
query = (
Structure
.select(Structure, Mesh)
.join(
Mesh,
JOIN.INNER,
on = (Mesh.structure_id == Structure.structure_id)
) )
.where(
structureID = self._updateStructure(entry, query) Structure.type == params["structure"]["type"],
meshID = self._updateMesh(entry["mesh"], query, structureID) Structure.direction == str(params["structure"]["direction"]),
self._updateSubMesh(entry.get("submesh", []), query, meshID) Structure.theta == params["structure"]["theta"]
self._updateMeshResult(entry.get("meshresults", {}), query, meshID) )
)
structureID = self._updateStructure(entry["structure"], query)
meshID = self._updateMesh(entry["mesh"], query, structureID)
self._updateSubMesh(entry.get("submesh", []), query, meshID)
self._updateMeshResult(entry.get("meshresults", {}), query, meshID)
def loadDB(self, structure_type: str, structure_direction: list, structure_theta: float):
query = (
Structure
.select(Structure, Mesh)
.join(
Mesh,
JOIN.INNER,
on = (Mesh.structure_id == Structure.structure_id)
)
.where(
Structure.type == structure_type,
Structure.direction == str(structure_direction),
Structure.theta == structure_theta
)
)
self.params = query.dicts().get()
# TODO: loadDB (one model), loadsDB (all models) # TODO: loadDB (one model), loadsDB (all models)
@timer @timer
def updateFromDB(self): def updateFromDB(self):
squery = Structure.select().order_by(Structure.id) squery = Structure.select().order_by(Structure.structure_id)
mquery = Mesh.select().order_by(Mesh.structure_id) mquery = Mesh.select().order_by(Mesh.structure_id)
smquery = SubMesh.select() smquery = SubMesh.select()
mrquery = MeshResult.select().order_dy(MeshResult.mesh_id) mrquery = MeshResult.select().order_dy(MeshResult.mesh_id)

View File

@ -8,10 +8,8 @@ bodyCentered = true
faceCentered = true faceCentered = true
[[structures]] [[structures]]
name = "simple" [structures.structure]
# auto # path: str type = "simple"
[structures.geometry]
# auto # from theta: list # theta: float # auto # from theta: list # theta: float
theta = [0.01, 0.28, 0.01] # [min, max, step] theta = [0.01, 0.28, 0.01] # [min, max, step]
# auto # from directions:list # direction: list # auto # from directions:list # direction: list
@ -77,10 +75,8 @@ faceCentered = true
checkChartBoundary = false checkChartBoundary = false
[[structures]] [[structures]]
name = "bodyCentered" [structures.structure]
# auto # path: str type = "bodyCentered"
[structures.geometry]
# auto # from theta: list # theta: float # auto # from theta: list # theta: float
theta = [0.01, 0.17, 0.01] # [min, max, step] theta = [0.01, 0.17, 0.01] # [min, max, step]
# auto # from directions:list # direction: list # auto # from directions:list # direction: list
@ -146,10 +142,8 @@ faceCentered = true
checkChartBoundary = false checkChartBoundary = false
[[structures]] [[structures]]
name = "faceCentered" [structures.structure]
# auto # path: str type = "faceCentered"
[structures.geometry]
# auto # from theta: list # theta: float # auto # from theta: list # theta: float
theta = [0.01, 0.18, 0.01] # [min, max, step] theta = [0.01, 0.18, 0.01] # [min, max, step]
# auto # from directions:list # direction: list # auto # from directions:list # direction: list

View File

@ -30,6 +30,8 @@ class BaseModel(Model):
class Structure(BaseModel): class Structure(BaseModel):
structure_id = PrimaryKeyField()
name = TextField() name = TextField()
direction = ListField() direction = ListField()
theta = FloatField() theta = FloatField()
@ -40,7 +42,7 @@ class Structure(BaseModel):
filletsEnabled = BooleanField() filletsEnabled = BooleanField()
fillets = FloatField() fillets = FloatField()
path = TextField() #path = TextField()
class Mesh(BaseModel): class Mesh(BaseModel):
@ -76,6 +78,7 @@ class Mesh(BaseModel):
class SubMesh(BaseModel): class SubMesh(BaseModel):
submesh_id = PrimaryKeyField()
mesh_id = ForeignKeyField(Mesh, backref = "submeshes") mesh_id = ForeignKeyField(Mesh, backref = "submeshes")
name = TextField() name = TextField()
@ -99,6 +102,7 @@ class SubMesh(BaseModel):
class MeshResult(BaseModel): class MeshResult(BaseModel):
meshresult_id = PrimaryKeyField()
mesh_id = ForeignKeyField(Mesh, backref = "meshresults") mesh_id = ForeignKeyField(Mesh, backref = "meshresults")
surfaceArea = FloatField(null = True) surfaceArea = FloatField(null = True)