Skip to content

Commit

Permalink
minor work on refactor tests and use pytest instead of unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
krande committed Oct 25, 2021
1 parent 13e53d6 commit 284354b
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 241 deletions.
5 changes: 3 additions & 2 deletions src/ada/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ def from_fem(
name: Union[str, list] = None,
enable_experimental_cache=False,
source_units="m",
fem_converter="default",
) -> Assembly:
a = Assembly(enable_experimental_cache=enable_experimental_cache, units=source_units)
if type(fem_file) in (str, pathlib.WindowsPath):
a.read_fem(fem_file, fem_format, name)
a.read_fem(fem_file, fem_format, name, fem_converter=fem_converter)
elif type(fem_file) is list:
for i, f in enumerate(fem_file):
fem_format_in = fem_format if fem_format is None else fem_format[i]
name_in = name if name is None else name[i]
a.read_fem(f, fem_format_in, name_in)
a.read_fem(f, fem_format_in, name_in, fem_converter=fem_converter)
else:
raise ValueError(f'fem_file must be either string or list. Passed type was "{type(fem_file)}"')

Expand Down
21 changes: 10 additions & 11 deletions tests/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pathlib
import unittest
from operator import attrgetter

from ada import FEM, Assembly, Beam, Part, Section
Expand All @@ -16,27 +15,27 @@ def build_test_beam_fem(geom_repr) -> Assembly:
return Assembly("MyAssembly") / (Part("MyPart", fem=bm.to_fem_obj(0.1, geom_repr)) / bm)


def compare_fem_objects(fem_a: FEM, fem_b: FEM, test_class: unittest.TestCase):
def compare_fem_objects(fem_a: FEM, fem_b: FEM):
for na, nb in zip(fem_a.elements, fem_b.elements):
test_class.assertEqual(na.id, nb.id)
assert na.id == nb.id
for nma, nmb in zip(na.nodes, nb.nodes):
test_class.assertEqual(nma, nmb)
assert nma == nmb

for na, nb in zip(fem_a.nodes, fem_b.nodes):
test_class.assertEqual(na.id, nb.id)
test_class.assertEqual(na.x, nb.x)
test_class.assertEqual(na.y, nb.y)
test_class.assertEqual(na.z, nb.z)
assert na.id == nb.id
assert na.x == nb.x
assert na.y == nb.y
assert na.z == nb.z

def assert_sets(s1, s2):
for m1, m2 in zip(
sorted(s1, key=attrgetter("name")),
sorted(s2, key=attrgetter("name")),
):
for ma, mb in zip(sorted(m1.members, key=attrgetter("id")), sorted(m2.members, key=attrgetter("id"))):
test_class.assertEqual(ma.id, mb.id)
assert ma.id == mb.id

test_class.assertEqual(len(s1), len(s2))
assert len(s1) == len(s2)

assert_sets(fem_a.sets.elements.values(), fem_b.sets.elements.values())
assert_sets(fem_a.sets.nodes.values(), fem_b.sets.nodes.values())
Expand All @@ -54,7 +53,7 @@ def dummy_display(ada_obj):
renderer.build_display()


def build_test_simplestru_fem(mesh_size=0.1, make_fem=True) -> Assembly:
def build_test_simplestru_fem(mesh_size=0.3, make_fem=True) -> Assembly:
p = SimpleStru("ParametricModel")

if make_fem:
Expand Down
82 changes: 37 additions & 45 deletions tests/concept_objects/test_stru_pipes.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,42 @@
import unittest

from common import dummy_display

from ada import Assembly, Part, Pipe, Section
from ada.config import Settings

test_folder = Settings.test_dir / "pipes"


class PipeIO(unittest.TestCase):
def test_pipe_straight(self):
a = Assembly("MyTest")

p = Part("MyPart")
a.add_part(p)
z = 3.2
y0 = -200e-3
pipe1 = Pipe("Pipe1", [(0, y0, 0), (0, y0, z)], Section("PSec", "PIPE", r=0.10, wt=5e-3))
p.add_pipe(pipe1)
a.to_ifc(test_folder / "pipe_straight.ifc")
dummy_display(a)

def test_pipe_bend(self):
a = Assembly("MyTest")
p = a.add_part(Part("MyPart"))

z = 3.2
y0 = -200e-3
x0 = -y0

pipe1 = Pipe(
"Pipe1",
[
(0, y0, z),
(5 + x0, y0, z),
(5 + x0, y0 + 5, z),
(10, y0 + 5, z + 2),
(10, y0 + 5, z + 10),
],
Section("PSec", "PIPE", r=0.10, wt=5e-3),
)
p.add_pipe(pipe1)
a.to_ifc(test_folder / "pipe_bend.ifc")
a.to_stp(test_folder / "pipe_bend.stp")
dummy_display(a)


if __name__ == "__main__":
unittest.main()
test_dir = Settings.test_dir / "pipes"


def test_pipe_straight():
a = Assembly("MyTest")

p = Part("MyPart")
a.add_part(p)
z = 3.2
y0 = -200e-3
pipe1 = Pipe("Pipe1", [(0, y0, 0), (0, y0, z)], Section("PSec", "PIPE", r=0.10, wt=5e-3))
p.add_pipe(pipe1)
a.to_ifc(test_dir / "pipe_straight.ifc")
dummy_display(a)


def test_pipe_bend():

z = 3.2
y0 = -200e-3
x0 = -y0

pipe1 = Pipe(
"Pipe1",
[
(0, y0, z),
(5 + x0, y0, z),
(5 + x0, y0 + 5, z),
(10, y0 + 5, z + 2),
(10, y0 + 5, z + 10),
],
Section("PSec", "PIPE", r=0.10, wt=5e-3),
)
a = Assembly("MyTest") / (Part("MyPart") / pipe1)
a.to_ifc(test_dir / "pipe_bend.ifc")
a.to_stp(test_dir / "pipe_bend.stp")
dummy_display(a)
30 changes: 14 additions & 16 deletions tests/fem/formats/test_io_fem_calculix.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import unittest

from common import build_test_beam_fem, example_files

from ada import Assembly
from ada.fem import FemSet, Load, StepImplicit


class TestCalculix(unittest.TestCase):
def test_read_C3D20(self):
a = Assembly()
a.read_fem(example_files / "fem_files/calculix/contact2e.inp")
beams = list(a.parts.values())[0]
vol = beams.fem.nodes.vol_cog
assert vol == (0.49999999627471, 1.2499999925494, 3.9999999701977)
def test_read_C3D20():
a = Assembly()
a.read_fem(example_files / "fem_files/calculix/contact2e.inp")
beams = list(a.parts.values())[0]
vol = beams.fem.nodes.vol_cog
assert vol == (0.49999999627471, 1.2499999925494, 3.9999999701977)


def test_write_test_model(self):
a = build_test_beam_fem("shell")
fs = a.fem.add_set(FemSet("Eall", [el for el in a.get_by_name("MyPart").fem.elements.elements], "elset"))
my_step = StepImplicit("static", total_time=1, max_incr=1, init_incr=1, nl_geom=True)
my_step.add_load(Load("Gravity", "gravity", -9.81, fem_set=fs))
a.fem.add_step(my_step)
def test_write_test_model():
a = build_test_beam_fem("shell")
fs = a.fem.add_set(FemSet("Eall", [el for el in a.get_by_name("MyPart").fem.elements.elements], "elset"))
my_step = StepImplicit("static", total_time=1, max_incr=1, init_incr=1, nl_geom=True)
my_step.add_load(Load("Gravity", "gravity", -9.81, fem_set=fs))
a.fem.add_step(my_step)

a.to_fem("my_calculix", fem_format="calculix", overwrite=True) # , execute=True, exit_on_complete=False)
a.to_fem("my_calculix", fem_format="calculix", overwrite=True) # , execute=True, exit_on_complete=False)
89 changes: 35 additions & 54 deletions tests/fem/formats/test_io_fem_code_aster.py
Original file line number Diff line number Diff line change
@@ -1,86 +1,67 @@
import unittest
from common import compare_fem_objects, example_files

from common import build_test_beam_fem, compare_fem_objects, example_files

from ada import Assembly
import ada
from ada.config import Settings
from ada.param_models.fem_models import beam_ex1


class TestCodeAster(unittest.TestCase):
def test_read_write_cylinder(self):

name = "cylinder"
def test_read_write_cylinder():

a = Assembly()
a.read_fem(example_files / "fem_files/meshes/med/cylinder.med", "code_aster", name="cylinder_rewritten")
a.to_fem(name, "code_aster", overwrite=True)
name = "cylinder"

b = Assembly()
b.read_fem((Settings.scratch_dir / name / name).with_suffix(".med"), fem_format="code_aster")
a = ada.from_fem(example_files / "fem_files/meshes/med/cylinder.med", "code_aster", name="cylinder_rewritten")
a.to_fem(name, "code_aster", overwrite=True)

p_a = a.parts["cylinder_rewritten"]
p_b = b.parts["cylinder"]
b = ada.from_fem((Settings.scratch_dir / name / name).with_suffix(".med"), fem_format="code_aster")

compare_fem_objects(p_a.fem, p_b.fem, self)
p_a = a.parts["cylinder_rewritten"]
p_b = b.parts["cylinder"]

def test_read_write_box(self):
compare_fem_objects(p_a.fem, p_b.fem)

name = "box"

a = Assembly()
a.read_fem(example_files / "fem_files/meshes/med/box.med", "code_aster", name="box")
a.to_fem(name, "code_aster", overwrite=True)
def test_read_write_box():

b = Assembly()
b.read_fem((Settings.scratch_dir / name / name).with_suffix(".med"), fem_format="code_aster")
name = "box"

p_a = a.parts["box"]
p_b = b.parts["box"]
a = ada.from_fem(example_files / "fem_files/meshes/med/box.med", "code_aster", name="box")
a.to_fem(name, "code_aster", overwrite=True)

compare_fem_objects(p_a.fem, p_b.fem, self)
b = ada.from_fem((Settings.scratch_dir / name / name).with_suffix(".med"), fem_format="code_aster")

def test_read_write_portal_frame(self):
p_a = a.parts["box"]
p_b = b.parts["box"]

name = "portal"
compare_fem_objects(p_a.fem, p_b.fem)

a = Assembly()
a.read_fem(example_files / "fem_files/code_aster/portal_01.med", "code_aster", name=name)
a.to_fem(name, "code_aster", overwrite=True)

b = Assembly()
b.read_fem((Settings.scratch_dir / name / name).with_suffix(".med"), fem_format="code_aster")
def test_read_write_portal_frame():

p_a = a.parts[name]
p_b = b.parts[name]
name = "portal"

compare_fem_objects(p_a.fem, p_b.fem, self)
a = ada.from_fem(example_files / "fem_files/code_aster/portal_01.med", "code_aster", name=name)
a.to_fem(name, "code_aster", overwrite=True)

def test_write_cantilever(self):
b = ada.from_fem((Settings.scratch_dir / name / name).with_suffix(".med"), fem_format="code_aster")

name = "cantilever_code_aster"
p_a = a.parts[name]
p_b = b.parts[name]

a = beam_ex1()
compare_fem_objects(p_a.fem, p_b.fem)

a.to_fem(name, fem_format="code_aster", overwrite=True)
dest_file = (Settings.scratch_dir / name / name).with_suffix(".med")

b = Assembly()
b.read_fem(dest_file, fem_format="code_aster")
def test_write_cantilever():

p_a = a.parts["MyPart"]
p_b = b.parts["cantilever_code_aster"]
name = "cantilever_code_aster"

compare_fem_objects(p_a.fem, p_b.fem, self)
a = beam_ex1()

def test_write_bm(self):
a = build_test_beam_fem("line")
a.to_fem("my_code_aster_bm", fem_format="code_aster", overwrite=True)
a.to_fem(name, fem_format="code_aster", overwrite=True)
dest_file = (Settings.scratch_dir / name / name).with_suffix(".med")

def test_write_bm_shell(self):
a = build_test_beam_fem("shell")
a.to_fem("my_code_aster_bm_shell", fem_format="code_aster", overwrite=True)
b = ada.from_fem(dest_file, fem_format="code_aster")

p_a = a.parts["MyPart"]
p_b = b.parts["cantilever_code_aster"]

if __name__ == "__main__":
unittest.main()
compare_fem_objects(p_a.fem, p_b.fem)
41 changes: 18 additions & 23 deletions tests/fem/formats/test_io_fem_meshio.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
import unittest

from common import example_files

from ada import Assembly
import ada


def test_read_write_code_aster_to_xdmf():
a = ada.from_fem(example_files / "fem_files/meshes/med/box.med", fem_converter="meshio")
a.to_fem("box_analysis_xdmf", fem_format="xdmf", fem_converter="meshio", overwrite=True)


class TestMeshio(unittest.TestCase):
def test_read_write_code_aster_to_xdmf(self):
a = Assembly("meshio_from_ca", "temp")
a.read_fem(example_files / "fem_files/meshes/med/box.med", fem_converter="meshio")
a.to_fem("box_analysis_xdmf", fem_format="xdmf", fem_converter="meshio", overwrite=True)
def test_read_write_code_aster_to_abaqus():
a = ada.from_fem(example_files / "fem_files/meshes/med/box.med", fem_converter="meshio")
a.to_fem("box_analysis_abaqus", fem_format="abaqus", fem_converter="meshio", overwrite=True)

def test_read_write_code_aster_to_abaqus(self):
a = Assembly("meshio_from_ca", "temp")
a.read_fem(example_files / "fem_files/meshes/med/box.med", fem_converter="meshio")
a.to_fem("box_analysis_abaqus", fem_format="abaqus", fem_converter="meshio", overwrite=True)

def test_read_C3D20(self):
a = Assembly("my_assembly", "temp")
a.read_fem(example_files / "fem_files/calculix/contact2e.inp", fem_converter="meshio")
def test_read_C3D20():
a = ada.from_fem(example_files / "fem_files/calculix/contact2e.inp", fem_converter="meshio")
print(a)

def test_read_abaqus(self):
b = Assembly("my_assembly", "temp")
b.read_fem(example_files / "fem_files/meshes/abaqus/element_elset.inp", fem_converter="meshio")

def test_read_code_aster(self):
a = Assembly("meshio_from_ca", "temp")
a.read_fem(example_files / "fem_files/meshes/med/box.med", fem_converter="meshio")
def test_read_abaqus():
b = ada.from_fem(example_files / "fem_files/meshes/abaqus/element_elset.inp", fem_converter="meshio")
print(b)


if __name__ == "__main__":
unittest.main()
def test_read_code_aster():
a = ada.from_fem(example_files / "fem_files/meshes/med/box.med", fem_converter="meshio")
print(a)
Loading

0 comments on commit 284354b

Please sign in to comment.