Fix: db updating and creating
This commit is contained in:
parent
e20130f35f
commit
513fd1be52
@ -41,9 +41,7 @@ import toml
|
|||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from anisotropy.models import db, JOIN, Structure, Mesh, SubMesh, MeshResult
|
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 openfoam
|
|
||||||
from math import sqrt
|
|
||||||
|
|
||||||
###
|
###
|
||||||
# Environment variables and config
|
# Environment variables and config
|
||||||
@ -79,18 +77,45 @@ env["db_path"] = env["BUILD"]
|
|||||||
# Logger
|
# Logger
|
||||||
##
|
##
|
||||||
logger_env = env.get("logger", {})
|
logger_env = env.get("logger", {})
|
||||||
logging.basicConfig(
|
|
||||||
level = logging.INFO,
|
|
||||||
format = logger_env.get("format", "%(levelname)s: %(message)s"),
|
|
||||||
handlers = [
|
|
||||||
logging.StreamHandler(),
|
|
||||||
logging.FileHandler(
|
|
||||||
os.path.join(env["LOG"], logger_env.get("name", "anisotropy"))
|
|
||||||
)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
logger = logging.getLogger(logger_env.get("name", "anisotropy"))
|
|
||||||
|
|
||||||
|
class CustomFormatter(logging.Formatter):
|
||||||
|
grey = "\x1b[38;21m"
|
||||||
|
yellow = "\x1b[33;21m"
|
||||||
|
red = "\x1b[31;21m"
|
||||||
|
bold_red = "\x1b[31;1m"
|
||||||
|
reset = "\x1b[0m"
|
||||||
|
format = logger_env.get("format", "[ %(asctime)s ] [ %(levelname)s ] %(message)s")
|
||||||
|
|
||||||
|
FORMATS = {
|
||||||
|
logging.DEBUG: grey + format + reset,
|
||||||
|
logging.INFO: grey + format + reset,
|
||||||
|
logging.WARNING: yellow + format + reset,
|
||||||
|
logging.ERROR: red + format + reset,
|
||||||
|
logging.CRITICAL: bold_red + format + reset
|
||||||
|
}
|
||||||
|
|
||||||
|
def format(self, record):
|
||||||
|
log_fmt = self.FORMATS.get(record.levelno)
|
||||||
|
formatter = logging.Formatter(log_fmt)
|
||||||
|
|
||||||
|
return formatter.format(record)
|
||||||
|
|
||||||
|
logger = logging.getLogger(logger_env.get("name", "anisotropy"))
|
||||||
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
|
sh = logging.StreamHandler()
|
||||||
|
sh.setLevel(logging.DEBUG)
|
||||||
|
sh.setFormatter(CustomFormatter())
|
||||||
|
|
||||||
|
fh = logging.FileHandler(os.path.join(env["LOG"], logger_env.get("name", "anisotropy")))
|
||||||
|
fh.setLevel(logging.DEBUG)
|
||||||
|
fh.setFormatter(CustomFormatter())
|
||||||
|
|
||||||
|
logger.addHandler(sh)
|
||||||
|
logger.addHandler(fh)
|
||||||
|
|
||||||
|
peeweeLogger = logging.getLogger("peewee")
|
||||||
|
peeweeLogger.setLevel(logging.INFO)
|
||||||
|
|
||||||
def timer(func):
|
def timer(func):
|
||||||
def inner(*args, **kwargs):
|
def inner(*args, **kwargs):
|
||||||
@ -102,6 +127,9 @@ def timer(func):
|
|||||||
|
|
||||||
return inner
|
return inner
|
||||||
|
|
||||||
|
import salomepl
|
||||||
|
import openfoam
|
||||||
|
from math import sqrt
|
||||||
|
|
||||||
class Anisotropy(object):
|
class Anisotropy(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -156,19 +184,18 @@ class Anisotropy(object):
|
|||||||
|
|
||||||
for direction in entry["structure"]["directions"]:
|
for direction in entry["structure"]["directions"]:
|
||||||
for n, theta in enumerate(thetaList):
|
for n, theta in enumerate(thetaList):
|
||||||
|
mesh = deepcopy(entry["mesh"])
|
||||||
|
mesh["thickness"] = thicknessList[n]
|
||||||
|
|
||||||
entryNew = {
|
entryNew = {
|
||||||
"structure": dict(
|
"structure": dict(
|
||||||
type = entry["structure"]["type"],
|
type = entry["structure"]["type"],
|
||||||
theta = theta,
|
theta = theta,
|
||||||
direction = [ float(num) for num in direction ],
|
direction = [ float(num) for num in direction ],
|
||||||
|
filletsEnabled = entry["structure"]["filletsEnabled"]
|
||||||
),
|
),
|
||||||
"mesh": dict(
|
"mesh": mesh,
|
||||||
**deepcopy(entry["mesh"]),
|
"submesh": deepcopy(entry["submesh"])
|
||||||
thickness = thicknessList[n]
|
|
||||||
),
|
|
||||||
"submesh": dict(
|
|
||||||
**deepcopy(entry["submesh"])
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsAll.append(entryNew)
|
paramsAll.append(entryNew)
|
||||||
@ -192,10 +219,10 @@ class Anisotropy(object):
|
|||||||
|
|
||||||
r0 = 1
|
r0 = 1
|
||||||
L = 2 * r0
|
L = 2 * r0
|
||||||
radius = r0 / (1 - theta)
|
radius = r0 / (1 - structure["theta"])
|
||||||
|
|
||||||
C1, C2 = 0.8, 0.5
|
C1, C2 = 0.8, 0.5
|
||||||
Cf = C1 + (C2 - C1) / (thetaMax - thetaMin) * (theta - thetaMin)
|
Cf = C1 + (C2 - C1) / (thetaMax - thetaMin) * (structure["theta"] - thetaMin)
|
||||||
delta = 0.2
|
delta = 0.2
|
||||||
fillets = delta - Cf * (radius - r0)
|
fillets = delta - Cf * (radius - r0)
|
||||||
|
|
||||||
@ -205,10 +232,10 @@ class Anisotropy(object):
|
|||||||
|
|
||||||
L = 1.0
|
L = 1.0
|
||||||
r0 = L * sqrt(2) / 4
|
r0 = L * sqrt(2) / 4
|
||||||
radius = r0 / (1 - theta)
|
radius = r0 / (1 - structure["theta"])
|
||||||
|
|
||||||
C1, C2 = 0.3, 0.2
|
C1, C2 = 0.3, 0.2
|
||||||
Cf = C1 + (C2 - C1) / (thetaMax - thetaMin) * (theta - thetaMin)
|
Cf = C1 + (C2 - C1) / (thetaMax - thetaMin) * (structure["theta"] - thetaMin)
|
||||||
delta = 0.012
|
delta = 0.012
|
||||||
fillets = delta - Cf * (radius - r0)
|
fillets = delta - Cf * (radius - r0)
|
||||||
|
|
||||||
@ -218,15 +245,15 @@ class Anisotropy(object):
|
|||||||
|
|
||||||
L = 1.0
|
L = 1.0
|
||||||
r0 = L * sqrt(3) / 4
|
r0 = L * sqrt(3) / 4
|
||||||
radius = r0 / (1 - theta)
|
radius = r0 / (1 - structure["theta"])
|
||||||
|
|
||||||
C1, C2 = 0.3, 0.2
|
C1, C2 = 0.3, 0.2
|
||||||
Cf = C1 + (C2 - C1) / (thetaMax - thetaMin) * (theta - thetaMin)
|
Cf = C1 + (C2 - C1) / (thetaMax - thetaMin) * (structure["theta"] - thetaMin)
|
||||||
delta = 0.02
|
delta = 0.02
|
||||||
fillets = delta - Cf * (radius - r0)
|
fillets = delta - Cf * (radius - r0)
|
||||||
|
|
||||||
self.params["structure"] = dict(
|
self.params["structure"].update(
|
||||||
**structure,
|
#**structure,
|
||||||
L = L,
|
L = L,
|
||||||
r0 = r0,
|
r0 = r0,
|
||||||
radius = radius,
|
radius = radius,
|
||||||
@ -263,10 +290,11 @@ class Anisotropy(object):
|
|||||||
def setupDB(self):
|
def setupDB(self):
|
||||||
os.makedirs(self.env["db_path"], exist_ok = True)
|
os.makedirs(self.env["db_path"], exist_ok = True)
|
||||||
|
|
||||||
|
dbname = os.path.join(self.env["db_path"], "anisotropy.db")
|
||||||
self.db = db
|
self.db = db
|
||||||
self.db.init(os.path.join(self.env["db_path"], "anisotropy.db"))
|
self.db.init(dbname)
|
||||||
|
|
||||||
if not os.path.exists(self.env["db_path"]):
|
if not os.path.exists(dbname):
|
||||||
self.db.create_tables([
|
self.db.create_tables([
|
||||||
Structure,
|
Structure,
|
||||||
Mesh,
|
Mesh,
|
||||||
@ -315,39 +343,39 @@ class Anisotropy(object):
|
|||||||
query = (
|
query = (
|
||||||
Mesh.update(**raw)
|
Mesh.update(**raw)
|
||||||
.where(
|
.where(
|
||||||
Mesh.structure_id == req["structure_id"]
|
Mesh.structure_id == structureID #req["structure_id"]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
query.execute()
|
query.execute()
|
||||||
|
|
||||||
return tabID
|
return tabID
|
||||||
|
|
||||||
def _updateSubMesh(self, srcs: list, queryMain, meshID) -> None:
|
def _updateSubMesh(self, src: dict, queryMain, meshID) -> None:
|
||||||
if not srcs:
|
if not src:
|
||||||
return
|
return
|
||||||
|
|
||||||
for src in srcs:
|
raw = deepcopy(src)
|
||||||
raw = deepcopy(src)
|
|
||||||
|
with self.db.atomic():
|
||||||
|
if not SubMesh.select().where(SubMesh.mesh_id == meshID).exists():
|
||||||
|
tabID = SubMesh.create(
|
||||||
|
mesh_id = meshID,
|
||||||
|
**raw
|
||||||
|
)
|
||||||
|
logger.debug(f"[ DB ] Created SubMesh entry { tabID }")
|
||||||
|
|
||||||
with self.db.atomic():
|
else:
|
||||||
if not queryMain.exists():
|
#req = queryMain.dicts().get()
|
||||||
tabID = SubMesh.create(
|
#tabID = req["mesh_id"]
|
||||||
mesh_id = meshID,
|
|
||||||
**raw
|
query = (
|
||||||
|
SubMesh.update(**raw)
|
||||||
|
.where(
|
||||||
|
SubMesh.mesh_id == meshID, #req["mesh_id"],
|
||||||
|
SubMesh.name == src["name"]
|
||||||
)
|
)
|
||||||
|
)
|
||||||
else:
|
query.execute()
|
||||||
req = queryMain.dicts().get()
|
|
||||||
tabID = req["mesh_id"]
|
|
||||||
|
|
||||||
query = (
|
|
||||||
SubMesh.update(**raw)
|
|
||||||
.where(
|
|
||||||
SubMesh.mesh_id == req["mesh_id"],
|
|
||||||
SubMesh.name == src["name"]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
query.execute()
|
|
||||||
|
|
||||||
def _updateMeshResult(self, src: dict, queryMain, meshID) -> None:
|
def _updateMeshResult(self, src: dict, queryMain, meshID) -> None:
|
||||||
if not src:
|
if not src:
|
||||||
@ -356,20 +384,21 @@ class Anisotropy(object):
|
|||||||
raw = deepcopy(src)
|
raw = deepcopy(src)
|
||||||
|
|
||||||
with self.db.atomic():
|
with self.db.atomic():
|
||||||
if not queryMain.exists():
|
if not MeshResult.select().where(MeshResult.mesh_id == meshID).exists():
|
||||||
tabID = MeshResult.create(
|
tabID = MeshResult.create(
|
||||||
mesh_id = meshID,
|
mesh_id = meshID,
|
||||||
**raw
|
**raw
|
||||||
)
|
)
|
||||||
|
logger.debug(f"[ DB ] Created MeshResult entry { tabID }")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
req = queryMain.dicts().get()
|
#req = queryMain.dicts().get()
|
||||||
tabID = req["mesh_id"]
|
#tabID = req["mesh_id"]
|
||||||
|
|
||||||
query = (
|
query = (
|
||||||
Mesh.update(**raw)
|
MeshResult.update(**raw)
|
||||||
.where(
|
.where(
|
||||||
Mesh.mesh_id == req["mesh_id"]
|
MeshResult.mesh_id == meshID #req["mesh_id"]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
query.execute()
|
query.execute()
|
||||||
@ -386,7 +415,6 @@ class Anisotropy(object):
|
|||||||
logger.error("Trying to update db from empty parameters")
|
logger.error("Trying to update db from empty parameters")
|
||||||
return
|
return
|
||||||
|
|
||||||
# TODO: query for every table
|
|
||||||
query = (
|
query = (
|
||||||
Structure
|
Structure
|
||||||
.select(Structure, Mesh)
|
.select(Structure, Mesh)
|
||||||
@ -402,30 +430,47 @@ class Anisotropy(object):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
structureID = self._updateStructure(entry["structure"], query)
|
structureID = self._updateStructure(params["structure"], query)
|
||||||
|
|
||||||
meshID = self._updateMesh(entry["mesh"], query, structureID)
|
meshID = self._updateMesh(params["mesh"], query, structureID)
|
||||||
self._updateSubMesh(entry.get("submesh", []), query, meshID)
|
|
||||||
self._updateMeshResult(entry.get("meshresults", {}), query, meshID)
|
for submeshParams in params.get("submesh", []):
|
||||||
|
self._updateSubMesh(submeshParams, query, meshID)
|
||||||
|
|
||||||
|
self._updateMeshResult(params.get("meshresults", {}), query, meshID)
|
||||||
|
|
||||||
|
|
||||||
def loadDB(self, structure_type: str, structure_direction: list, structure_theta: float):
|
def loadDB(self, structure_type: str, structure_direction: list, structure_theta: float):
|
||||||
query = (
|
structureQuery = (
|
||||||
Structure
|
Structure
|
||||||
.select(Structure, Mesh)
|
.select()
|
||||||
.join(
|
|
||||||
Mesh,
|
|
||||||
JOIN.INNER,
|
|
||||||
on = (Mesh.structure_id == Structure.structure_id)
|
|
||||||
)
|
|
||||||
.where(
|
.where(
|
||||||
Structure.type == structure_type,
|
Structure.type == structure_type,
|
||||||
Structure.direction == str(structure_direction),
|
Structure.direction == str(structure_direction),
|
||||||
Structure.theta == structure_theta
|
Structure.theta == structure_theta
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.params = {}
|
||||||
|
|
||||||
self.params = query.dicts().get()
|
with self.db.atomic():
|
||||||
|
if structureQuery.exists():
|
||||||
|
self.params["structure"] = structureQuery.dicts().get()
|
||||||
|
|
||||||
|
meshQuery = structureQuery.get().meshes
|
||||||
|
|
||||||
|
if meshQuery.exists():
|
||||||
|
self.params["mesh"] = meshQuery.dicts().get()
|
||||||
|
|
||||||
|
submeshQuery = meshQuery.get().submeshes
|
||||||
|
|
||||||
|
if submeshQuery.exists():
|
||||||
|
self.params["submesh"] = [ entry for entry in submeshQuery.dicts() ]
|
||||||
|
|
||||||
|
meshresultQuery = meshQuery.get().meshresults
|
||||||
|
|
||||||
|
if meshresultQuery.exists():
|
||||||
|
self.params["meshresult"] = meshresultQuery.dicts().get()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[logger]
|
[logger]
|
||||||
name = "anisotropy"
|
name = "anisotropy"
|
||||||
format = "%(levelname)s: %(message)s"
|
format = "[ %(levelname)s ] %(message)s"
|
||||||
|
|
||||||
[base]
|
[base]
|
||||||
simple = true
|
simple = true
|
||||||
|
@ -48,9 +48,10 @@ def genmesh(root, name, direction, theta):
|
|||||||
##
|
##
|
||||||
model = Anisotropy()
|
model = Anisotropy()
|
||||||
model.setupDB()
|
model.setupDB()
|
||||||
model.updateFromDB()
|
model.loadDB(name, direction, theta)
|
||||||
|
model.evalParams()
|
||||||
|
|
||||||
p = model.getParams(name, direction, theta)
|
p = model.params
|
||||||
|
|
||||||
|
|
||||||
###
|
###
|
||||||
@ -58,10 +59,10 @@ def genmesh(root, name, direction, theta):
|
|||||||
##
|
##
|
||||||
logger.info("\n".join([
|
logger.info("\n".join([
|
||||||
"genmesh:",
|
"genmesh:",
|
||||||
f"structure type:\t{ p['name'] }",
|
f"structure type:\t{ p['structure']['type'] }",
|
||||||
f"coefficient:\t{ p['geometry']['theta'] }",
|
f"coefficient:\t{ p['structure']['theta'] }",
|
||||||
f"fillet:\t{ p['geometry']['fillets'] }",
|
f"fillet:\t{ p['structure']['fillets'] }",
|
||||||
f"flow direction:\t{ p['geometry']['direction'] }"
|
f"flow direction:\t{ p['structure']['direction'] }"
|
||||||
]))
|
]))
|
||||||
|
|
||||||
salome.salome_init()
|
salome.salome_init()
|
||||||
@ -75,13 +76,13 @@ def genmesh(root, name, direction, theta):
|
|||||||
simple = Simple,
|
simple = Simple,
|
||||||
bodyCentered = BodyCentered,
|
bodyCentered = BodyCentered,
|
||||||
faceCentered = FaceCentered
|
faceCentered = FaceCentered
|
||||||
)[p["name"]]
|
)[p["structure"]["type"]]
|
||||||
shape, groups = structure(**p["geometry"]).build()
|
shape, groups = structure(**p["structure"]).build()
|
||||||
|
|
||||||
[length, surfaceArea, volume] = geompy.BasicProperties(shape, theTolerance = 1e-06)
|
[length, surfaceArea, volume] = geompy.BasicProperties(shape, theTolerance = 1e-06)
|
||||||
|
|
||||||
logger.info("\n".join([
|
logger.info("\n".join([
|
||||||
"shape:"
|
"shape:",
|
||||||
f"edges length:\t{ length }",
|
f"edges length:\t{ length }",
|
||||||
f"surface area:\t{ surfaceArea }",
|
f"surface area:\t{ surfaceArea }",
|
||||||
f"volume:\t{ volume }"
|
f"volume:\t{ volume }"
|
||||||
@ -141,19 +142,22 @@ def genmesh(root, name, direction, theta):
|
|||||||
mesh.removePyramids()
|
mesh.removePyramids()
|
||||||
mesh.assignGroups()
|
mesh.assignGroups()
|
||||||
|
|
||||||
mesh.exportUNV(os.path.join(p["path"], "mesh.unv"))
|
casePath = model.getCasePath()
|
||||||
|
os.makedirs(casePath, exist_ok = True)
|
||||||
|
mesh.exportUNV(os.path.join(casePath, "mesh.unv"))
|
||||||
|
|
||||||
meshStats = mesh.stats()
|
meshStats = mesh.stats()
|
||||||
p["meshResult"] = dict(
|
p["meshresults"] = dict(
|
||||||
mesh_id = p["mesh"]["id"],
|
#mesh_id = p["mesh"]["mesh_id"],
|
||||||
surfaceArea = surfaceArea,
|
surfaceArea = surfaceArea,
|
||||||
volume = volume,
|
volume = volume,
|
||||||
**meshStats
|
**meshStats
|
||||||
)
|
)
|
||||||
model.updateDB()
|
model.updateDB()
|
||||||
|
|
||||||
statstr = "\n".join(map(lambda k, v: f"{ k }:\t{ v }", meshStats))
|
logger.info("mesh stats:\n{}".format(
|
||||||
logger.info(f"mesh stats:\n{ statsstr }")
|
"\n".join(map(lambda v: f"{ v[0] }:\t{ v[1] }", meshStats.items()))
|
||||||
|
))
|
||||||
|
|
||||||
salome.salome_close()
|
salome.salome_close()
|
||||||
|
|
||||||
|
@ -32,16 +32,16 @@ class BaseModel(Model):
|
|||||||
class Structure(BaseModel):
|
class Structure(BaseModel):
|
||||||
structure_id = PrimaryKeyField()
|
structure_id = PrimaryKeyField()
|
||||||
|
|
||||||
name = TextField()
|
type = TextField()
|
||||||
direction = ListField()
|
direction = ListField()
|
||||||
theta = FloatField()
|
theta = FloatField()
|
||||||
|
|
||||||
r0 = FloatField()
|
r0 = FloatField(null = True)
|
||||||
L = FloatField()
|
L = FloatField(null = True)
|
||||||
radius = FloatField()
|
radius = FloatField(null = True)
|
||||||
|
|
||||||
filletsEnabled = BooleanField()
|
filletsEnabled = BooleanField(null = True)
|
||||||
fillets = FloatField()
|
fillets = FloatField(null = True)
|
||||||
#path = TextField()
|
#path = TextField()
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger("anisotropy")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import GEOM
|
import GEOM
|
||||||
from salome.geom import geomBuilder
|
from salome.geom import geomBuilder
|
||||||
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
print("[Warning] Trying to get SALOME geometry modules outside SALOME environment. Modules won't be imported.")
|
logger.warning("Trying to get SALOME geometry modules outside SALOME environment. Modules won't be imported.")
|
||||||
|
|
||||||
if globals().get("geomBuilder"):
|
if globals().get("geomBuilder"):
|
||||||
geompy = geomBuilder.New()
|
geompy = geomBuilder.New()
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger("anisotropy")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import SMESH
|
import SMESH
|
||||||
from salome.smesh import smeshBuilder
|
from salome.smesh import smeshBuilder
|
||||||
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
print("[Warning] Trying to get SALOME mesh modules outside SALOME environment. Modules won't be imported.")
|
logger.warning("[Warning] Trying to get SALOME mesh modules outside SALOME environment. Modules won't be imported.")
|
||||||
|
|
||||||
if globals().get("smeshBuilder"):
|
if globals().get("smeshBuilder"):
|
||||||
smesh = smeshBuilder.New()
|
smesh = smeshBuilder.New()
|
||||||
|
Loading…
Reference in New Issue
Block a user