Skip to content

Commit

Permalink
QOL updates
Browse files Browse the repository at this point in the history
  • Loading branch information
pshriwise committed Oct 22, 2024
1 parent aacb8f6 commit b7d328a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 27 deletions.
66 changes: 43 additions & 23 deletions src/openmc_cad_adapter/surfaces.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC, abstractclassmethod, abstractmethod

from abc import ABC, abstractmethod
import sys
import math
import warnings

import numpy as np
import openmc
Expand All @@ -15,12 +15,31 @@ def indent(indent_size):

class CADSurface(ABC):

@abstractmethod
def to_cubit_surface(self, ent_type, node, extents, inner_world=None, hex=False):
ids, cmds = self.to_cubit_surface_inner(ent_type, node, extents, inner_world, hex)
cmds += self.boundary_condition(ids)
return ids, cmds

@abstractmethod
def to_cubit_surface_inner(self, ent_type, node, extents, inner_world=None, hex=False):
raise NotImplementedError

@abstractclassmethod
def boundary_condition(self, cad_surface_ids):
if self.boundary_type == 'transmission':
return []
cmds = []
cmds.append(f'group \"boundary:{self.boundary_type}\" add surface {cad_surface_ids[2:]}')
return cmds

@classmethod
def from_openmc_surface(cls, surface):
with warnings.catch_warnings() as w:
warnings.simplefilter("ignore")
return cls.from_openmc_surface_inner(surface)

@classmethod
@abstractmethod
def from_openmc_surface_inner(cls, surface):
raise NotImplementedError


Expand All @@ -30,7 +49,7 @@ class CADPlane(CADSurface, openmc.Plane):
def lreverse(node):
return "" if node.side == '-' else "reverse"

def to_cubit_surface(self, ent_type, node, extents, inner_world=None, hex=False):
def to_cubit_surface_inner(self, ent_type, node, extents, inner_world=None, hex=False):
cmds = []

n = np.array([self.coefficients[k] for k in ('a', 'b', 'c')])
Expand Down Expand Up @@ -65,10 +84,11 @@ def to_cubit_surface(self, ent_type, node, extents, inner_world=None, hex=False)
cmds.append(f"section body {{ {ids} }} with surface {{ {sur} }} {self.lreverse(node)}")
cmds.append(f"del surface {{ {sur} }}")

cmds += self.boundary_condition(ids)
return ids, cmds

@classmethod
def from_openmc_surface(cls, plane):
def from_openmc_surface_inner(cls, plane):
return cls(plane.a, plane.b, plane.c, plane.d, plane.boundary_type, plane.albedo, plane.name, plane.id)


Expand All @@ -78,15 +98,15 @@ class CADXPlane(openmc.XPlane):
def reverse(node):
return "reverse" if node.side == '-' else ""

def to_cubit_surface(self, ent_type, node, extents, inner_world=None, hex=False):
def to_cubit_surface_inner(self, ent_type, node, extents, inner_world=None, hex=False):
cad_cmds = []
cad_cmds.append(f"brick x {extents[0]} y {extents[1]} z {extents[2]}")
ids = emit_get_last_id( ent_type, cad_cmds)
cad_cmds.append(f"section body {{ {ids} }} with xplane offset {self.coefficients['x0']} {self.reverse(node)}")
return ids, cad_cmds

@classmethod
def from_openmc_surface(cls, plane):
def from_openmc_surface_inner(cls, plane):
return cls(x0=plane.x0, boundary_type=plane.boundary_type, albedo=plane.albedo, name=plane.name, surface_id=plane.id)


Expand All @@ -96,38 +116,38 @@ class CADYPlane(openmc.YPlane):
def reverse(node):
return "reverse" if node.side == '-' else ""

def to_cubit_surface(self, ent_type, node, extents, inner_world=None, hex=False):
def to_cubit_surface_inner(self, ent_type, node, extents, inner_world=None, hex=False):
cad_cmds = []
cad_cmds.append(f"brick x {extents[0]} y {extents[1]} z {extents[2]}")
ids = emit_get_last_id( ent_type, cad_cmds)
cad_cmds.append(f"section body {{ {ids} }} with yplane offset {self.coefficients['y0']} {self.reverse(node)}")
return ids, cad_cmds

@classmethod
def from_openmc_surface(cls, plane):
def from_openmc_surface_inner(cls, plane):
return cls(y0=plane.y0, boundary_type=plane.boundary_type, albedo=plane.albedo, name=plane.name, surface_id=plane.id)


class CADZPlane(openmc.ZPlane):
class CADZPlane(CADSurface, openmc.ZPlane):

@staticmethod
def reverse(node):
return "reverse" if node.side == '-' else ""

def to_cubit_surface(self, ent_type, node, extents, inner_world=None, hex=False):
def to_cubit_surface_inner(self, ent_type, node, extents, inner_world=None, hex=False):
cad_cmds = []
cad_cmds.append(f"brick x {extents[0]} y {extents[1]} z {extents[2]}")
ids = emit_get_last_id( ent_type, cad_cmds)
cad_cmds.append(f"section body {{ {ids} }} with zplane offset {self.coefficients['z0']} {self.reverse(node)}")
return ids, cad_cmds

@classmethod
def from_openmc_surface(cls, plane):
def from_openmc_surface_inner(cls, plane):
return cls(z0=plane.z0, boundary_type=plane.boundary_type, albedo=plane.albedo, name=plane.name, surface_id=plane.id)

class CADCylinder(CADSurface, openmc.Cylinder):

def to_cubit_surface(self, ent_type, node, extents, inner_world=None, hex=False):
def to_cubit_surface_inner(self, ent_type, node, extents, inner_world=None, hex=False):
print('XCADCylinder to cubit surface')
cad_cmds = []
h = inner_world[2] if inner_world else extents[2]
Expand Down Expand Up @@ -155,13 +175,13 @@ def to_cubit_surface(self, ent_type, node, extents, inner_world=None, hex=False)
return ids, cad_cmds

@classmethod
def from_openmc_surface(cls, cyl):
def from_openmc_surface_inner(cls, cyl):
return cls(r=cyl.r, x0=cyl.x0, y0=cyl.y0, z0=cyl.z0, dx=cyl.dx, dy=cyl.dy, dz=cyl.dz,
boundary_type=cyl.boundary_type, albedo=cyl.albedo, name=cyl.name, surface_id=cyl.id)

class CADXCylinder(CADSurface, openmc.XCylinder):

def to_cubit_surface(self, ent_type, node, extents, inner_world=None, hex=False):
def to_cubit_surface_inner(self, ent_type, node, extents, inner_world=None, hex=False):
cad_cmds = []
h = inner_world[0] if inner_world else extents[0]
cad_cmds.append( f"cylinder height {h} radius {self.r}")
Expand All @@ -188,13 +208,13 @@ def to_cubit_surface(self, ent_type, node, extents, inner_world=None, hex=False)
return ids, cad_cmds

@classmethod
def from_openmc_surface(cls, cyl):
def from_openmc_surface_inner(cls, cyl):
return cls(r=cyl.r, y0=cyl.y0, z0=cyl.z0, boundary_type=cyl.boundary_type, albedo=cyl.albedo, name=cyl.name, surface_id=cyl.id)


class CADYCylinder(CADSurface, openmc.YCylinder):

def to_cubit_surface(self, ent_type, node, extents, inner_world=None, hex=False):
def to_cubit_surface_inner(self, ent_type, node, extents, inner_world=None, hex=False):
cad_cmds = []
h = inner_world[1] if inner_world else extents[1]
cad_cmds.append( f"cylinder height {h} radius {self.r}")
Expand All @@ -221,13 +241,13 @@ def to_cubit_surface(self, ent_type, node, extents, inner_world=None, hex=False)
return ids, cad_cmds

@classmethod
def from_openmc_surface(cls, cyl):
def from_openmc_surface_inner(cls, cyl):
return cls(r=cyl.r, x0=cyl.x0, z0=cyl.z0, boundary_type=cyl.boundary_type, albedo=cyl.albedo, name=cyl.name, surface_id=cyl.id)


class CADZCylinder(CADSurface, openmc.ZCylinder):

def to_cubit_surface(self, ent_type, node, extents, inner_world=None, hex=False):
def to_cubit_surface_inner(self, ent_type, node, extents, inner_world=None, hex=False):
cad_cmds = []
h = inner_world[2] if inner_world else extents[2]
cad_cmds.append( f"cylinder height {h} radius {self.r}")
Expand All @@ -252,19 +272,19 @@ def to_cubit_surface(self, ent_type, node, extents, inner_world=None, hex=False)
return ids, cad_cmds

@classmethod
def from_openmc_surface(cls, cyl):
def from_openmc_surface_inner(cls, cyl):
return cls(r=cyl.r, x0=cyl.x0, y0=cyl.y0, boundary_type=cyl.boundary_type, albedo=cyl.albedo, name=cyl.name, surface_id=cyl.id)


class CADSphere(openmc.Sphere):

def to_cubit_surface(self, ent_type, node, extents, inner_world=None, hex=False):
def to_cubit_surface_inner(self, ent_type, node, extents, inner_world=None, hex=False):
cad_cmds = []
cad_cmds.append( f"sphere redius {self.r}")
ids = emit_get_last_id(ent_type, cad_cmds)
move(ids, self.x0, self.y0, self.z0, cad_cmds)
return ids, cad_cmds

@classmethod
def from_openmc_surface(cls, sphere):
def from_openmc_surface_inner(cls, sphere):
return cls(r=sphere.r, x0=sphere.x0, y0=sphere.y0, z0=sphere.z0, boundary_type=sphere.boundary_type, albedo=sphere.albedo, name=sphere.name, surface_id=sphere.id)
14 changes: 10 additions & 4 deletions src/openmc_cad_adapter/to_cubit_journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ def reverse():
return "reverse" if node.side == '-' else ""

if surface._type in _SURFACE_DICTIONARY:
CADSurface = _SURFACE_DICTIONARY[surface._type].from_openmc_surface(surface)
ids, cad_cmds = CADSurface.to_cubit_surface(ent_type, node, w, inner_world, hex)
cad_surface = _SURFACE_DICTIONARY[surface._type].from_openmc_surface(surface)
ids, cad_cmds = cad_surface.to_cubit_surface(ent_type, node, w, inner_world, hex)
cmds += cad_cmds
return ids
elif surface._type == "sphere":
Expand Down Expand Up @@ -610,8 +610,14 @@ def do_cell(cell, cell_ids: Iterable[int] = None):
before = len( cmds )
cmds.append( f"#CELL {cell.id}" )
vol_or_body = process_node_or_fill( cell, w )
if cell.fill_type == "material":
cmds.append( f'group \"Material_{cell.fill.id}\" add body {{ { vol_or_body[0] } }} ' )
if cell.fill is None:
cmds.append(f'group "mat:void" add body {{ { vol_or_body[0] } }} ')
elif cell.fill_type == "material":
mat_identifier = f"mat:{cell.fill.id}"
# use material names when possible
if cell.fill.name is not None and cell.fill.name:
mat_identifier = f"mat:{cell.fill.name}"
cmds.append( f'group \"{mat_identifier}\" add body {{ { vol_or_body[0] } }} ' )
after = len( cmds )

if cell_ids is not None and cell.id in cell_ids:
Expand Down

0 comments on commit b7d328a

Please sign in to comment.