netgen/tests/pytest/test_tutorials.py

119 lines
4.8 KiB
Python
Raw Normal View History

2019-08-08 13:12:33 +05:00
import os, pytest
from netgen.meshing import meshsize, MeshingParameters, SetMessageImportance
2019-08-08 13:12:33 +05:00
import netgen.csg as csg
import netgen.stl as stl
2019-10-04 15:25:14 +05:00
from pyngcore import TaskManager
try:
import netgen.occ as occ
has_occ = True
except ImportError:
has_occ = False
2019-08-08 13:12:33 +05:00
from results import *
SetMessageImportance(0)
2019-08-08 13:12:33 +05:00
def getFiles(fileEnding):
r, d, files = next(os.walk(os.path.join("..","..","tutorials")))
return (f for f in files if f.endswith(fileEnding))
def getCheckFunc(filename):
2019-10-04 15:25:14 +05:00
def func(mesh,mp,i):
2019-08-08 13:12:33 +05:00
if filename in number_elements:
# number of elements should be in 2% range of expected value
2019-10-04 15:25:14 +05:00
assert mesh.ne == number_elements[filename][i]
badness = mesh.CalcTotalBadness(mp)
qual_classes = list(mesh.GetQualityHistogram())
assert badness == pytest.approx(total_badness[filename][i], rel=1e-6)
assert qual_classes == quality_histogram[filename][i]
return func
2019-08-08 13:12:33 +05:00
def getResultFunc(filename):
2019-10-04 15:25:14 +05:00
def resultFunc(mesh, mp):
2019-08-08 13:12:33 +05:00
results = {}
results["number_elements"] = mesh.ne
2019-10-04 15:25:14 +05:00
results["total_badness"] = mesh.CalcTotalBadness(mp)
results["quality_histogram"] = list(mesh.GetQualityHistogram())
2019-08-08 13:12:33 +05:00
return results
return resultFunc
def getMeshingparameters(filename):
2019-10-04 15:25:14 +05:00
standard = [MeshingParameters()] + [MeshingParameters(ms) for ms in (meshsize.very_coarse, meshsize.coarse, meshsize.moderate, meshsize.fine, meshsize.very_fine)]
2019-08-08 13:12:33 +05:00
if filename == "shell.geo":
return [] # do not test this example cause it needs so long...
if filename == "extrusion.geo":
return [] # this segfaults right now
if filename == "manyholes2.geo":
return [standard[1]] # this gets too big for finer meshsizes
if filename in ("manyholes.geo", "frame.step"):
return standard[:3] # this gets too big for finer meshsizes
if filename == "screw.step":
return standard[3:] # coarser meshes don't work here
2019-08-08 13:12:33 +05:00
return standard
_geofiles = [f for f in getFiles(".geo")] + [f for f in getFiles(".stl")]
if has_occ:
_geofiles += [f for f in getFiles(".step")]
2019-10-04 15:25:14 +05:00
_geofiles.sort()
2019-08-08 13:12:33 +05:00
def generateMesh(filename, mp):
if filename.endswith(".geo"):
geo = csg.CSGeometry(os.path.join("..","..","tutorials", filename))
elif filename.endswith(".stl"):
geo = stl.STLGeometry(os.path.join("..","..","tutorials", filename))
elif filename.endswith(".step"):
geo = occ.OCCGeometry(os.path.join("..","..","tutorials", filename))
2019-10-04 15:25:14 +05:00
return geo.GenerateMesh(mp)
2019-08-08 13:12:33 +05:00
2019-08-29 20:56:47 +05:00
def isSlowTest(filename):
return filename in ["cubemcyl.geo", "frame.step", "revolution.geo", "manyholes.geo", "torus.geo",
"cubemsphere.geo", "manyholes2.geo", "matrix.geo", "trafo.geo", "ellipticcone.geo",
"period.geo", "shaft.geo", "cubeandring.geo", "ellipticcyl.geo",
"ellipsoid.geo", "cone.geo"]
def getParamForTest(filename):
return pytest.param(filename, getCheckFunc(filename), marks=pytest.mark.slow) if isSlowTest(filename) \
else (filename, getCheckFunc(filename))
@pytest.mark.parametrize(("filename, checkFunc"), [getParamForTest(f) for f in _geofiles])
2019-08-08 13:12:33 +05:00
def test_geoFiles(filename, checkFunc):
2019-10-04 15:25:14 +05:00
import filecmp
for i, mp_ in enumerate(getMeshingparameters(filename)):
2019-08-08 13:12:33 +05:00
print("load geo", filename)
2019-10-04 15:25:14 +05:00
mp = MeshingParameters(mp_, parallel_meshing=False)
2019-08-08 13:12:33 +05:00
mesh = generateMesh(filename, mp)
if checkFunc is not None:
2019-10-04 15:25:14 +05:00
checkFunc(mesh,mp,i)
2019-09-09 20:05:06 +05:00
mesh.Save(filename+'_seq.vol.gz')
2019-10-04 15:25:14 +05:00
with TaskManager():
2019-09-09 20:05:06 +05:00
mesh_par = generateMesh(filename, mp)
mesh_par.Save(filename+'_par.vol.gz')
assert filecmp.cmp(filename+'_seq.vol.gz', filename+'_par.vol.gz')
2019-08-08 13:12:33 +05:00
import time
def generateResultFile():
2019-10-04 15:25:14 +05:00
with TaskManager():
2019-08-08 13:12:33 +05:00
with open("results.py", "w") as f:
print("number_elements = {}", file=f)
2019-10-04 15:25:14 +05:00
print("total_badness = {}", file=f)
print("quality_histogram = {}", file=f)
2019-08-08 13:12:33 +05:00
for _file, _func in ((gf, getResultFunc(gf)) for gf in _geofiles):
start = time.time()
print("write", _file)
mps = getMeshingparameters(_file)
if not mps:
continue
2019-10-04 15:25:14 +05:00
results = [_func(generateMesh(_file, mp), mp) for mp in mps]
2019-08-08 13:12:33 +05:00
print("number_elements['{}'] = {}".format(_file, "(" + ",".join((str(r["number_elements"]) for r in results)) + ")"), file=f)
2019-10-04 15:25:14 +05:00
print("total_badness['{}'] = {}".format(_file, "(" + ",".join((str(r["total_badness"]) for r in results)) + ")"), file=f)
print("quality_histogram['{}'] = {}".format(_file, "(" + ",".join((str(r["quality_histogram"]) for r in results)) + ")"), file=f)
2019-08-08 13:12:33 +05:00
print("needed", time.time() - start, "seconds")
if __name__ == "__main__":
generateResultFile()