Skip to content

Commit

Permalink
fix for openmsr#52
Browse files Browse the repository at this point in the history
make return of lits of stls from mesher subclasses explicit.
the implicit return proved unreliable
  • Loading branch information
ebknudsen committed Oct 4, 2023
1 parent 987f4d9 commit ae2da8c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
6 changes: 4 additions & 2 deletions src/CAD_to_OpenMC/assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,14 @@ def solids_to_h5m(self,brep_filename: str = None, h5m_filename:str="dagmc.h5m",
meshgen=am.meshers.get(backend,**mesher_config)
meshgen.set_verbosity(self.verbose)
stl_list=meshgen.generate_stls()
for e,s in zip(self.entities,stl_list):
e.stl=s
if(backend=='stl2'):
self.stl2h5m_byface(h5m_filename,True)
if (self.verbose):
print(f'SUMMARY: {"solid_id":8} {"material_tag":16} {"stl-file":16}')
for i,a in zip(range(len(self.entities)),self.entities):
print(f'SUMMARY: {i+1:8} {a.tag:16} ' + " ".join( [f'{stl[0]:16}' for stl in a.stls] ) )
print(f'SUMMARY: {i+1:8} {a.tag:16} ' + " ".join( [f'{stl[0]:16}' for stl in a.stl] ) )
else:
if(heal):
stl_list=self.heal_stls(stl_list)
Expand Down Expand Up @@ -545,7 +547,7 @@ def add_entities_to_moab_core(self, mbcore:core.Core, mbtags:dict):
sid=0
gid=0
for i,e in enumerate(self.entities):
for j,T in enumerate(e.stls):
for j,T in enumerate(e.stl):
f,sense=T
if f not in faces_added:
fset= mbcore.create_meshset()
Expand Down
18 changes: 5 additions & 13 deletions src/CAD_to_OpenMC/assemblymesher_cq.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,8 @@ def _set_entities(cls,entities):
cls.cq_mesher_entities=entities

def generate_stls(self):
self._mesh_surfaces()
return
#created a cq-compund from list of entities
for i,e in enumerate(self.cq_mesher_entities):
j=i+1
filename=f"volume_{j}.stl"
e.solid.exportStl(filename,ascii=True,tolerance=self.tolerance,angularTolerance=self.angular_tolerance)
if(self.verbosity_level>1):
print(f"INFO: cq export to file {filename}")
e.stl=filename
if(self.refine):
self._refine_stls(e.stl)
stls=self._mesh_surfaces()
return stls

@classmethod
def _refine_stls(cls,stl,refine):
Expand Down Expand Up @@ -115,6 +105,7 @@ def _mmgs_refine_stl(cls,stl):
def _mesh_surfaces(self):
#loop over all surfaces in all entities
#and generate meshes (refined or otherwise)
stls=[]
cwd=pl.Path.cwd()
#create a workplace in tmp
with cdtemp() as mngr:
Expand All @@ -134,9 +125,10 @@ def _mesh_surfaces(self):
volumefaces.append(o[1])
#merge the stls to a single .stl in the working directory
merge_stl(str(cwd / volname), volumefaces,of='bin')
e.stl=volname
stls.append(volname)
# clear the hash table
self._clear_face_hashtable()
return stls

@classmethod
def _mesh_single(cls, fid, vid, refine):
Expand Down
9 changes: 6 additions & 3 deletions src/CAD_to_OpenMC/assemblymesher_cq2.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _set_entities(cls,entities):
cls.cq_mesher_entities=entities

def generate_stls(self):
self._mesh_surfaces()
return self._mesh_surfaces()

def _mesh_surfaces(self):
#loop over all surfaces in all entities
Expand All @@ -90,13 +90,16 @@ def _mesh_surfaces(self):
output=pool.starmap(self._mesh_single, mpargs)

#process the list of meshed faces.
stls=[]
for i,e in enumerate(self.cq_mesher_entities):
e.stls=[]
face_stls=[]
for k,v in face_hash_table.items():
vids=v[1] # the volumes that this face belongs to
if i in vids:
#this face is part of this volume
e.stls.append(v)
face_stls.append(v)
stls.append(face_stls)
return stls

@classmethod
def _mesh_single(cls, fid, vid, refine, hh, faceHash):
Expand Down
25 changes: 17 additions & 8 deletions src/CAD_to_OpenMC/assemblymesher_gmsh.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,28 @@
from .assemblymesher_base import assemblymesher

class MesherGMSH(assemblymesher):
gmsh_mesher_entities=None


def __init__(self, min_mesh_size, max_mesh_size, curve_samples, default, mesh_algorithm, vetoed, threads, radial_threshold, refine, entities):
self.IntermediateLayer='brep'
self.min_mesh_size=min_mesh_size
self.max_mesh_size=max_mesh_size
self.curve_samples=curve_samples
self.mesh_algorithm=mesh_algorithm
self.default=default
self.entities=entities
self._set_entities(entities)
self.vetoed=vetoed
self.threads=threads
self.radial_threshold=radial_threshold
self.refine=refine
self._gmsh_init()
self._cq_solids_to_gmsh()

@classmethod
def _set_entities(cls,entities):
cls.gmsh_mesher_entities=entities

@property
def refine(self):
return self._refine
Expand Down Expand Up @@ -88,7 +95,7 @@ def _set_radial_field(self):
def _cq_solids_to_gmsh(self):
import glob
#create a compund cq solid from entities
solids=[e.solid for e in self.entities]
solids=[e.solid for e in self.gmsh_mesher_entities]
compound=cq.Compound.makeCompound(solids)
with tempfile.TemporaryDirectory() as td:
outpath=os.path.join(td,'export.'+self.IntermediateLayer)
Expand All @@ -111,12 +118,12 @@ def _reorder(self):
#tag_idx is now the reordered list of indices
#use that to reorder the entities to match the newly imported
#list of volumes in the gmsh geometry representation
self.entities=[self.entities[i] for i in tag_idx if i!=-1]
self.gmsh_mesher_entities=[self.gmsh_mesher_entities[i] for i in tag_idx if i!=-1]

def _find_similar(self,cms,bb,vol):
closest=1e12
iclose=-1
for i,e in enumerate(self.entities):
for i,e in enumerate(self.gmsh_mesher_entities):
simi=e.similarity(cms,bb,vol,tolerance=1e5)
if (simi<closest):
iclose=i
Expand All @@ -139,9 +146,9 @@ def generate_stls(self):
self._generate_mesh()
stls=[]
vols=gmsh.model.getEntities(3)
for i,e in enumerate(self.entities):
for k,e in enumerate(self.gmsh_mesher_entities):
#gmsh volume ids run from 1
dim,vid=vols[i]
dim,vid=vols[k]

if (dim!=3):
#appears not to be a volume - skip
Expand All @@ -157,11 +164,12 @@ def generate_stls(self):
filename=f'volume_{vid}.stl'
try:
gmsh.write(filename)
e.stl=filename
stls.append(filename)
except:
e.stl=None
stls.append('None')
print(f'WARNING: Could not write volume {vid}. Skipping')
gmsh.model.removePhysicalGroups([]) # remove group again
return stls

class MesherGMSHBuilder:
def __init__(self):
Expand All @@ -172,6 +180,7 @@ def __call__(self, min_mesh_size, max_mesh_size, curve_samples, default, mesh_al
self._instance = MesherGMSH(min_mesh_size, max_mesh_size, curve_samples, default, mesh_algorithm, vetoed, threads, radial_threshold, refine, entities)
else:
#need to do it this way since gmsh needs to be reinitialized
self._instance._set_entities(entities)
self._instance._set_pars(min_mesh_size,max_mesh_size, curve_samples, default, mesh_algorithm, vetoed, threads, radial_threshold, refine)
return self._instance

Expand Down

0 comments on commit ae2da8c

Please sign in to comment.