Skip to content

Commit

Permalink
Start to add make new gmshsession api the default for handling of mes…
Browse files Browse the repository at this point in the history
…hing.

 *Removed old gmsh class.
 * Created a shorthand method on backendgeom called to_fem_obj to make any physical object (for simple fem meshes only) to a fem mesh using minimal lines of code.
  • Loading branch information
Krande committed Sep 18, 2021
1 parent 0774444 commit e915280
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 62 deletions.
13 changes: 13 additions & 0 deletions src/ada/base/physical_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ def add_penetration(self, pen):

return pen

def to_fem_obj(self, mesh_size, geom_repr, options=None, silent=True):
"""
:type options: ada.fem.meshing.GmshOptions
:rtype: ada.FEM
"""
from ada.fem.meshing import GmshOptions, GmshSession

options = GmshOptions(Mesh_Algorithm=8) if options is None else options
with GmshSession(silent=silent, options=options) as gs:
gs.add_obj(self, geom_repr=geom_repr)
gs.mesh(mesh_size)
return gs.get_fem()

def to_stp(self, destination_file, geom_repr=None, schema="AP242", silent=False, fuse_piping=False):
from ada.fem.shapes import ElemType
from ada.occ.writer import StepExporter
Expand Down
28 changes: 20 additions & 8 deletions src/ada/concepts/levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
Surface,
)
from ada.fem.containers import FemElements, FemSections, FemSets
from ada.fem.elements import ElemType
from ada.ifc.utils import create_guid


Expand All @@ -73,8 +74,6 @@ def __init__(
guid=None,
):
super().__init__(name, guid=guid, metadata=metadata, units=units, parent=parent, ifc_elem=ifc_elem)
from ada.fem.meshing import GMesh

self._nodes = Nodes(parent=self)
self._beams = Beams(parent=self)
self._plates = Plates(parent=self)
Expand All @@ -83,7 +82,6 @@ def __init__(
self._connections = Connections(parent=self)
self._materials = Materials(parent=self)
self._sections = Sections(parent=self)
self._gmsh = GMesh(self)
self._colour = colour
self._origin = origin
self._instances = []
Expand Down Expand Up @@ -463,6 +461,25 @@ def _import_part_from_ifc(self, ifc_elem):
pr_type = ifc_elem.is_a()
return opposite[pr_type]

def to_fem_obj(
self, mesh_size: float, bm_repr=ElemType.LINE, pl_repr=ElemType.SHELL, options=None, silent=True
) -> FEM:
""":type options: ada.fem.meshing.GmshOptions"""
from ada.fem.meshing import GmshOptions, GmshSession

options = GmshOptions(Mesh_Algorithm=8) if options is None else options
with GmshSession(silent=silent, options=options) as gs:
# TODO: Beam and plate nodes (and nodes at intersecting beams) are still not properly represented
for obj in self.get_all_physical_objects():
if type(obj) is Beam:
gs.add_obj(obj, geom_repr=bm_repr, build_native_lines=False)
elif type(obj) is Plate:
gs.add_obj(obj, geom_repr=pl_repr)
else:
logging.error(f'Unsupported object type "{obj}". Should be either plate or beam objects')
gs.mesh(mesh_size)
return gs.get_fem()

@property
def parts(self) -> dict[str, Part]:
return self._parts
Expand Down Expand Up @@ -500,11 +517,6 @@ def fem(self, value: FEM):
value.parent = self
self._fem = value

@property
def gmsh(self):
""":rtype: ada.fem.meshing.GMesh"""
return self._gmsh

@property
def connections(self) -> Connections:
return self._connections
Expand Down
3 changes: 1 addition & 2 deletions src/ada/fem/meshing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from .gmshapi import GMesh
from .gmshapiv2 import GmshData, GmshOptions, GmshSession, GmshTask

__all__ = ["GMesh", "GmshSession", "GmshOptions", "GmshData", "GmshTask"]
__all__ = ["GmshSession", "GmshOptions", "GmshData", "GmshTask"]
9 changes: 3 additions & 6 deletions src/ada/fem/meshing/gmshapiv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
import gmsh
import numpy as np

from ada import FEM
from ada import FEM, Beam, Node, Pipe, Plate, Shape
from ada.base.physical_objects import BackendGeom
from ada.concepts.containers import Nodes
from ada.concepts.piping import Pipe
from ada.concepts.points import Node
from ada.concepts.primitives import Shape
from ada.concepts.structural import Beam, Plate
from ada.config import Settings
from ada.core.utils import make_name_fem_ready
from ada.fem import Elem, FemSection, FemSet
Expand Down Expand Up @@ -88,7 +85,7 @@ def __init__(self, silent=False, persist=True, options: GmshOptions = GmshOption

def add_obj(
self,
obj: Union[Shape, Beam, Plate, Pipe],
obj: Union[BackendGeom, Shape, Beam, Plate, Pipe],
geom_repr=ElemType.SOLID,
el_order=1,
silent=True,
Expand Down
8 changes: 1 addition & 7 deletions src/ada/param_models/fem_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from ada import Assembly, Beam, Material, Part, PrimBox, PrimCyl, PrimExtrude, User
from ada.fem import Bc, FemSet, Load, Step
from ada.fem.meshing.gmshapiv2 import GmshSession
from ada.fem.shapes import ElemType
from ada.fem.utils import get_beam_end_nodes
from ada.materials.metals import CarbonSteel, DnvGl16Mat
Expand Down Expand Up @@ -50,12 +49,7 @@ def beam_ex1(p1=(0, 0, 0), p2=(1.5, 0, 0), profile="IPE400", geom_repr=ElemType.
add_random_cutouts(bm)
# Create a FEM analysis of the beam as a cantilever subjected to gravity loads
p = a.get_part("MyPart")

with GmshSession(silent=True) as gs:
gs.add_obj(bm, geom_repr)
gs.mesh(0.1)
p.fem = gs.get_fem()

p.fem = bm.to_fem_obj(0.1, geom_repr)
# Add a set containing ALL elements (necessary for Calculix loads).
fs = p.fem.add_set(FemSet("Eall", [el for el in p.fem.elements], FemSet.TYPES.ELSET))

Expand Down
21 changes: 9 additions & 12 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@


def dummy_display(ada_obj):

if type(ada_obj) is Section:
sec_render = SectionRenderer()
_, _ = sec_render.build_display(ada_obj)
Expand All @@ -22,22 +21,20 @@ def dummy_display(ada_obj):
renderer.build_display()


def build_test_model():
param_model = SimpleStru("ParametricModel")
a = Assembly("ParametricSite")
a.add_part(param_model)
param_model.gmsh.mesh(max_dim=2, interactive=False, gmsh_silent=True)
param_model.add_bcs()
def build_test_simplestru_fem(mesh_size=0.1):
p = SimpleStru("ParametricModel")
a = Assembly("ParametricSite") / p
p.fem = p.to_fem_obj(mesh_size)
p.add_bcs()

return a


def build_test_beam():
def build_test_beam_fem(geom_repr):
a = Assembly("MyAssembly")
p = Part("MyPart")
p.add_beam(Beam("Bm", (0, 0, 0), (1, 0, 0), "IPE300"))
p.gmsh.mesh(0.5)
a.add_part(p)
p = a.add_part(Part("MyPart"))
bm = p.add_beam(Beam("Bm", (0, 0, 0), (1, 0, 0), "IPE300"))
p.fem = bm.to_fem_obj(0.5, geom_repr)
return a


Expand Down
1 change: 1 addition & 0 deletions tests/test_gmsh_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def test_beam(self):
dummy_display(a)

def test_beam_hex(self):
# TODO: this test is not yet producing HEX elements.
with GmshSession(silent=True, options=GmshOptions(Mesh_ElementOrder=2)) as gs:
solid_bm = gs.add_obj(self.bm1, "solid")

Expand Down
2 changes: 1 addition & 1 deletion tests/test_io_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_simplestru_fem_cache(self):
a = Assembly(model_name, clear_cache=True, enable_experimental_cache=True) / SimpleStru("ParamModel")

pfem = a.get_by_name("ParamModel")
pfem.gmsh.mesh()
pfem.fem = pfem.to_fem_obj(0.1)
time1 = time.time() - start

a.update_cache()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_io_fem.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import unittest

from common import build_test_model
from common import build_test_simplestru_fem

from ada.core.utils import roundoff


class TestFemProperties(unittest.TestCase):
def test_calc_cog(self):

a = build_test_model()
a = build_test_simplestru_fem()
p = a.parts["ParametricModel"]
cog = p.fem.elements.calc_cog()
tol = 0.01
Expand Down
6 changes: 3 additions & 3 deletions tests/test_io_fem_abaqus.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pprint
import unittest

from common import build_test_beam, build_test_model, example_files
from common import build_test_beam_fem, build_test_simplestru_fem, example_files

from ada import Assembly
from ada.fem.io.abaqus.common import AbaCards
Expand Down Expand Up @@ -185,11 +185,11 @@ def test_contact_general(self):

class TestAbaqus(unittest.TestCase):
def test_write_bm(self):
a = build_test_beam()
a = build_test_beam_fem("line")
a.to_fem("my_beam", fem_format="abaqus", overwrite=True)

def test_write_test_model(self):
a = build_test_model()
a = build_test_simplestru_fem()
a.to_fem("my_abaqus", fem_format="abaqus", overwrite=True)

def test_read_C3D20(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_io_fem_calculix.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from common import build_test_model, example_files
from common import build_test_simplestru_fem, example_files

from ada import Assembly
from ada.fem import FemSet, Load, Step
Expand All @@ -15,7 +15,7 @@ def test_read_C3D20(self):
assert vol == (0.49999999627471, 1.2499999925494, 3.9999999701977)

def test_write_test_model(self):
a = build_test_model()
a = build_test_simplestru_fem()
fs = a.fem.add_set(
FemSet("Eall", [el for el in a.get_by_name("ParametricModel").fem.elements.elements], "elset")
)
Expand Down
11 changes: 8 additions & 3 deletions tests/test_io_fem_code_aster.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import unittest

from common import build_test_beam, build_test_model, compare_fem_objects, example_files
from common import (
build_test_beam_fem,
build_test_simplestru_fem,
compare_fem_objects,
example_files,
)

from ada import Assembly
from ada.config import Settings
Expand Down Expand Up @@ -74,11 +79,11 @@ def test_write_cantilever(self):
compare_fem_objects(p_a.fem, p_b.fem, self)

def test_write_bm(self):
a = build_test_beam()
a = build_test_beam_fem("line")
a.to_fem("my_code_aster_bm", fem_format="code_aster", overwrite=True)

def test_write_test_model(self):
a = build_test_model()
a = build_test_simplestru_fem()
a.to_fem("simple_stru", fem_format="code_aster", overwrite=True)


Expand Down
4 changes: 2 additions & 2 deletions tests/test_io_fem_sesam.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from common import build_test_model
from common import build_test_simplestru_fem

from ada import Assembly, Beam, Plate

Expand Down Expand Up @@ -51,7 +51,7 @@ def test_write_ff(self):

class TestUsfos(unittest.TestCase):
def test_write_usfos(self):
a = build_test_model()
a = build_test_simplestru_fem()
a.to_fem("my_usfos", fem_format="usfos", overwrite=True)


Expand Down
13 changes: 7 additions & 6 deletions tests/test_param_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import unittest

from common import build_test_simplestru_fem

from ada import Assembly
from ada.config import Settings
from ada.fem import Load, Step
Expand All @@ -24,12 +26,11 @@ def test_basic_module_to_step(self):
# a.to_stp('param1', geom_type='solid')

def test_to_fem(self):
param_model = SimpleStru("ParametricModel")
param_model.gmsh.mesh(order=1, size=0.1, max_dim=2, interactive=False)
param_model.add_bcs()
self.assertEqual(len(param_model.fem.bcs), 4)
self.assertEqual(len(param_model.fem.elements), 10420)
self.assertEqual(len(param_model.fem.nodes), 5318)
a = build_test_simplestru_fem()
param_model = a.get_by_name("ParametricModel")
self.assertEqual(len(param_model.fem.bcs), 1)
self.assertEqual(len(param_model.fem.elements), 12920)
self.assertEqual(len(param_model.fem.nodes), 5331)

a = Assembly("ParametricSite")
a.add_part(param_model)
Expand Down
11 changes: 3 additions & 8 deletions tests/test_viz_geometry.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import unittest

from common import dummy_display
from common import build_test_simplestru_fem, dummy_display

from ada import Assembly, Beam, Plate
from ada.param_models.basic_module import SimpleStru


class VisualizeTests(unittest.TestCase):
Expand All @@ -25,12 +24,8 @@ def test_viz(self):
dummy_display(a)

def test_module(self):
param_model = SimpleStru("ParametricModel")
param_model.gmsh.mesh(size=0.1, max_dim=2)
param_model.add_bcs()
a = Assembly("ParametricSite")
a.add_part(param_model)

param_model = build_test_simplestru_fem()
a = Assembly("ParametricSite") / param_model
dummy_display(a)


Expand Down

0 comments on commit e915280

Please sign in to comment.