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,31 +128,68 @@ 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 """
from math import sqrt def loadScratch(self):
if not os.path.exists(self.env["DEFAULT_CONFIG"]):
logger.error("Missed default configuration file")
return
structures = deepcopy(self.env["structures"]) buf = toml.load(self.env["DEFAULT_CONFIG"]).get("structures")
self.params = [] paramsAll = []
for structure in structures: # TODO: custom config and merge
_theta = structure["geometry"]["theta"]
for entry in buf:
# Shortcuts
_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 = {
"structure": dict(
type = entry["structure"]["type"],
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)
self.setupDB()
for entry in paramsAll:
self.updateDB(entry)
def evalParams(self):
structure = self.params.get("structure")
if not structure:
logger.error("Trying to eval empty parameters")
return
if structure["type"] == "simple":
thetaMin = 0.01
thetaMax = 0.28
r0 = 1 r0 = 1
L = 2 * r0 L = 2 * r0
radius = r0 / (1 - theta) radius = r0 / (1 - theta)
@ -166,7 +199,10 @@ class Anisotropy(object):
delta = 0.2 delta = 0.2
fillets = delta - Cf * (radius - r0) fillets = delta - Cf * (radius - r0)
elif structure["name"] == "faceCentered": elif structure["type"] == "faceCentered":
thetaMin = 0.01
thetaMax = 0.13
L = 1.0 L = 1.0
r0 = L * sqrt(2) / 4 r0 = L * sqrt(2) / 4
radius = r0 / (1 - theta) radius = r0 / (1 - theta)
@ -176,7 +212,10 @@ class Anisotropy(object):
delta = 0.012 delta = 0.012
fillets = delta - Cf * (radius - r0) fillets = delta - Cf * (radius - r0)
elif structure["name"] == "bodyCentered": elif structure["type"] == "bodyCentered":
thetaMin = 0.01
thetaMax = 0.18
L = 1.0 L = 1.0
r0 = L * sqrt(3) / 4 r0 = L * sqrt(3) / 4
radius = r0 / (1 - theta) radius = r0 / (1 - theta)
@ -186,34 +225,31 @@ class Anisotropy(object):
delta = 0.02 delta = 0.02
fillets = delta - Cf * (radius - r0) fillets = delta - Cf * (radius - r0)
self.params["structure"] = dict(
path = os.path.join( **structure,
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, L = L,
r0 = r0,
radius = radius, radius = radius,
filletsEnabled = structure["geometry"]["filletsEnabled"],
fillets = fillets 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'] }"
) )
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:
params = src
elif self.params:
params = self.params
else:
logger.error("Trying to update db from empty parameters")
return
# TODO: query for every table
query = ( query = (
Structure Structure
.select(Structure, Mesh) .select(Structure, Mesh)
.join(Mesh, JOIN.INNER, on = (Mesh.structure_id == Structure.id)) .join(
Mesh,
JOIN.INNER,
on = (Mesh.structure_id == Structure.structure_id)
)
.where( .where(
Structure.name == entry["name"], Structure.type == params["structure"]["type"],
Structure.direction == str(entry["geometry"]["direction"]), Structure.direction == str(params["structure"]["direction"]),
Structure.theta == entry["geometry"]["theta"] Structure.theta == params["structure"]["theta"]
) )
) )
structureID = self._updateStructure(entry, query) structureID = self._updateStructure(entry["structure"], query)
meshID = self._updateMesh(entry["mesh"], query, structureID) meshID = self._updateMesh(entry["mesh"], query, structureID)
self._updateSubMesh(entry.get("submesh", []), query, meshID) self._updateSubMesh(entry.get("submesh", []), query, meshID)
self._updateMeshResult(entry.get("meshresults", {}), 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)