Skip to content

Commit

Permalink
refactor: using test classes in test_inline tests (#3524)
Browse files Browse the repository at this point in the history
* refactor: creating test classes

* chore: adding changelog file 3524.added.md [dependabot-skip]

---------

Co-authored-by: pyansys-ci-bot <[email protected]>
  • Loading branch information
germa89 and pyansys-ci-bot authored Oct 29, 2024
1 parent df9cb3c commit 11694ed
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 81 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/3524.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
refactor: using test classes in test_inline tests
64 changes: 0 additions & 64 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,44 +750,6 @@ def cube_solve(cleared, mapdl, cube_geom_and_mesh):
out = mapdl.modal_analysis(nmode=10, freqb=1)


@pytest.fixture
def box_with_fields(cleared, mapdl):
mapdl.prep7()
mapdl.mp("kxx", 1, 45)
mapdl.mp("ex", 1, 2e10)
mapdl.mp("perx", 1, 1)
mapdl.mp("murx", 1, 1)
if mapdl.version >= 25.1:
mapdl.tb("pm", 1, "", "", "perm")
mapdl.tbdata("", 0)

mapdl.et(1, "SOLID70")
mapdl.et(2, "CPT215")
mapdl.keyopt(2, 12, 1) # Activating PRES DOF
mapdl.et(3, "SOLID122")
mapdl.et(4, "SOLID96")
mapdl.block(0, 1, 0, 1, 0, 1)
mapdl.esize(0.5)
return mapdl


@pytest.fixture
def box_geometry(mapdl, cleared):
areas, keypoints = create_geometry(mapdl)
q = mapdl.queries
return q, keypoints, areas, get_details_of_nodes(mapdl)


@pytest.fixture
def line_geometry(mapdl, cleared):
mapdl.prep7(mute=True)
k0 = mapdl.k(1, 0, 0, 0)
k1 = mapdl.k(2, 1, 2, 2)
l0 = mapdl.l(k0, k1)
q = mapdl.queries
return q, [k0, k1], l0


@pytest.fixture
def query(mapdl, cleared):
return mapdl.queries
Expand Down Expand Up @@ -844,32 +806,6 @@ def selection_test_geometry(mapdl, cleared):
return mapdl.queries


@pytest.fixture
def twisted_sheet(mapdl, cleared):
mapdl.prep7()
mapdl.et(1, "SHELL181")
mapdl.mp("EX", 1, 2e5)
mapdl.mp("PRXY", 1, 0.3) # Poisson's Ratio
mapdl.rectng(0, 1, 0, 1)
mapdl.sectype(1, "SHELL")
mapdl.secdata(0.1)
mapdl.esize(0.5)
mapdl.amesh("all")
mapdl.run("/SOLU")
mapdl.antype("STATIC")
mapdl.nsel("s", "loc", "x", 0)
mapdl.d("all", "all")
mapdl.nsel("s", "loc", "x", 1)
mapdl.d("all", "ux", -0.1)
mapdl.d("all", "uy", -0.1)
mapdl.d("all", "uz", -0.1)
mapdl.allsel("all")
mapdl.solve()
mapdl.finish()
q = mapdl.queries
return q, get_details_of_nodes(mapdl)


def create_geometry(mapdl):
mapdl.prep7()
k0 = mapdl.k(1, 0, 0, 0)
Expand Down
107 changes: 90 additions & 17 deletions tests/test_inline_functions/test_component_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,78 +22,120 @@

import pytest

from conftest import create_geometry, get_details_of_nodes


class TestCentroidGetter:
def test_x(self, box_geometry):

@pytest.fixture(scope="class")
def box_geometry(self, mapdl):
mapdl.finish(mute=True)
mapdl.clear("NOSTART", mute=True)
mapdl.prep7(mute=True)
areas, keypoints = create_geometry(mapdl)
q = mapdl.queries
return q, keypoints, areas, get_details_of_nodes(mapdl)

def test_centroid_getter_x(self, box_geometry):
q, kps, areas, nodes = box_geometry
x = q.centrx(1)
assert x is not None

def test_y(self, box_geometry):
def test_centroid_getter_y(self, box_geometry):
q, kps, areas, nodes = box_geometry
y = q.centry(1)
assert y is not None

def test_z(self, box_geometry):
def test_centroid_getter_z(self, box_geometry):
q, kps, areas, nodes = box_geometry
z = q.centrz(1)
assert z is not None


class TestComponentQueries:
def test_nx(self, box_geometry):
def test_component_query_nx(self, box_geometry):
q, kps, areas, nodes = box_geometry
x = q.nx(1)
assert x == nodes[1].x

def test_ny(self, box_geometry):
def test_component_query_ny(self, box_geometry):
q, kps, areas, nodes = box_geometry
y = q.ny(1)
assert y == nodes[1].y

def test_nz(self, box_geometry):
def test_component_query_nz(self, box_geometry):
q, kps, areas, nodes = box_geometry
z = q.nz(1)
assert z == nodes[1].z

def test_kx(self, box_geometry):
def test_component_query_kx(self, box_geometry):
q, kps, areas, nodes = box_geometry
# first kp at (0, 0, 0)
x = q.kx(kps[0])
assert x == 0.0

def test_ky(self, box_geometry):
def test_component_query_ky(self, box_geometry):
q, kps, areas, nodes = box_geometry
y = q.ky(kps[0])
assert y == 0.0

def test_kz(self, box_geometry):
def test_component_query_kz(self, box_geometry):
q, kps, areas, nodes = box_geometry
z = q.kz(kps[0])
assert z == 0.0


class TestInverseGetFunctions:
@pytest.mark.parametrize("coords", [(0, 0, 0), (0.5, 0.5, 0.5), (100, 100, 100)])
def test_get_node_at_coordinates(self, box_geometry, coords):
def test_inverse_get_node_at_coordinates(self, box_geometry, coords):
q, kps, areas, nodes = box_geometry
node = q.node(*coords)
assert node in nodes

def test_get_node_matches_known_node(self, box_geometry):
def test_inverse_get_node_matches_known_node(self, box_geometry):
q, kps, areas, nodes = box_geometry
for number, node in nodes.items():
calculated_node = q.node(node.x, node.y, node.z)
assert number == calculated_node

@pytest.mark.parametrize("coords", [(0, 0, 0), (0.5, 0.5, 0.5), (100, 100, 100)])
def test_get_keypoints_at_coordinates(self, box_geometry, coords):
def test_inverse_get_keypoints_at_coordinates(self, box_geometry, coords):
q, kps, areas, nodes = box_geometry
kp = q.kp(*coords)
assert kp in kps


class TestDisplacementComponentQueries:
class TestDisplacementComponentQueriesBox:

@pytest.fixture(scope="class")
def solved_box(self, mapdl):
mapdl.mute = True # improve stability
mapdl.finish(mute=True)
mapdl.clear("NOSTART", mute=True)

mapdl.prep7()
mapdl.et(1, "SOLID5")
mapdl.block(0, 10, 0, 20, 0, 30)
mapdl.esize(10)
mapdl.vmesh("ALL")
mapdl.units("SI") # SI - International system (m, kg, s, K).
# Define a material (nominal steel in SI)
mapdl.mp("EX", 1, 210e9) # Elastic moduli in Pa (kg/(m*s**2))
mapdl.mp("DENS", 1, 7800) # Density in kg/m3
mapdl.mp("PRXY", 1, 0.3) # Poisson's Ratio
# Fix the left-hand side.
mapdl.nsel("S", "LOC", "Z", 0)
mapdl.d("ALL", "UX")
mapdl.d("ALL", "UY")
mapdl.d("ALL", "UZ")

mapdl.nsel("S", "LOC", "Z", 30)
mapdl.f("ALL", "FX", 1000)
mapdl.run("/SOLU")
mapdl.antype("STATIC")
mapdl.solve()
mapdl.finish()
mapdl.mute = False

q = mapdl.queries
return q, get_details_of_nodes(mapdl)

def test_ux(self, solved_box):
q, nodes = solved_box
displaced_nodes = [node for node in nodes if abs(q.ux(node)) > 0]
Expand All @@ -109,6 +151,37 @@ def test_uz(self, solved_box):
displaced_nodes = [node for node in nodes if abs(q.uz(node)) > 0]
assert len(displaced_nodes) > 0


class TestDisplacementComponentQueriesSheet:

@pytest.fixture(scope="class")
def twisted_sheet(self, mapdl):
mapdl.finish(mute=True)
mapdl.clear("NOSTART", mute=True)

mapdl.prep7()
mapdl.et(1, "SHELL181")
mapdl.mp("EX", 1, 2e5)
mapdl.mp("PRXY", 1, 0.3) # Poisson's Ratio
mapdl.rectng(0, 1, 0, 1)
mapdl.sectype(1, "SHELL")
mapdl.secdata(0.1)
mapdl.esize(0.5)
mapdl.amesh("all")
mapdl.run("/SOLU")
mapdl.antype("STATIC")
mapdl.nsel("s", "loc", "x", 0)
mapdl.d("all", "all")
mapdl.nsel("s", "loc", "x", 1)
mapdl.d("all", "ux", -0.1)
mapdl.d("all", "uy", -0.1)
mapdl.d("all", "uz", -0.1)
mapdl.allsel("all")
mapdl.solve()
mapdl.finish()
q = mapdl.queries
return q, get_details_of_nodes(mapdl)

def test_rotx(self, twisted_sheet):
q, nodes = twisted_sheet
displaced_nodes = [node for node in nodes if abs(q.rotx(node)) > 0]
Expand Down
14 changes: 14 additions & 0 deletions tests/test_inline_functions/test_connectivity_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,22 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import pytest

from conftest import create_geometry, get_details_of_nodes


class TestConnectivityQueries:

@pytest.fixture(scope="function")
def box_geometry(self, mapdl):
mapdl.finish(mute=True)
mapdl.clear("NOSTART", mute=True)
mapdl.prep7(mute=True)
areas, keypoints = create_geometry(mapdl)
q = mapdl.queries
return q, keypoints, areas, get_details_of_nodes(mapdl)

def test_nelem(self, box_geometry):
q, kps, areas, nodes = box_geometry
ns = [q.nelem(1, i) for i in range(1, 21)]
Expand Down
31 changes: 31 additions & 0 deletions tests/test_inline_functions/test_field_component_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,38 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import pytest


class TestFieldComponentValueGetter:

# The tests change the mesh so this fixture must be function scoped.
@pytest.fixture(scope="function")
def box_with_fields(self, mapdl):
mapdl.finish(mute=True)
mapdl.clear("NOSTART", mute=True)

mapdl.prep7()
mapdl.mp("kxx", 1, 45)
mapdl.mp("ex", 1, 2e10)
mapdl.mp("perx", 1, 1)
mapdl.mp("murx", 1, 1)
if mapdl.version >= 25.1:
mapdl.tb("pm", 1, "", "", "perm")
mapdl.tbdata("", 0)

mapdl.et(1, "SOLID70")
mapdl.et(2, "CPT215")
mapdl.keyopt(2, 12, 1) # Activating PRES DOF
mapdl.et(3, "SOLID122")
mapdl.et(4, "SOLID96")
mapdl.block(0, 1, 0, 1, 0, 1)
mapdl.esize(0.5)
return mapdl

def test_temp(self, box_with_fields):
mapdl = box_with_fields
mapdl.prep7()
mapdl.type(1)
mapdl.vmesh(1)
mapdl.d("all", "temp", 5.0)
Expand All @@ -34,6 +62,7 @@ def test_temp(self, box_with_fields):

def test_pressure(self, box_with_fields):
mapdl = box_with_fields
mapdl.prep7()
mapdl.type(2)
mapdl.vmesh(1)
mapdl.d("all", "pres", 5.0)
Expand All @@ -45,6 +74,7 @@ def test_pressure(self, box_with_fields):

def test_volt(self, box_with_fields):
mapdl = box_with_fields
mapdl.prep7()
mapdl.type(3)
mapdl.vmesh(1)
mapdl.d("all", "volt", 5.0)
Expand All @@ -55,6 +85,7 @@ def test_volt(self, box_with_fields):

def test_mag(self, box_with_fields):
mapdl = box_with_fields
mapdl.prep7()
mapdl.type(4)
mapdl.vmesh(1)
mapdl.d("all", "mag", 5.0)
Expand Down
27 changes: 27 additions & 0 deletions tests/test_inline_functions/test_line_component_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,23 @@

from math import isclose

import pytest


class TestLineCoordinateQueries:

@pytest.fixture(scope="class")
def line_geometry(self, mapdl):
mapdl.finish(mute=True)
mapdl.clear("NOSTART", mute=True)

mapdl.prep7(mute=True)
k0 = mapdl.k(1, 0, 0, 0)
k1 = mapdl.k(2, 1, 2, 2)
l0 = mapdl.l(k0, k1)
q = mapdl.queries
return q, [k0, k1], l0

def test_lx(self, line_geometry):
q, kps, line = line_geometry
x = q.lx(line, 0.5)
Expand All @@ -41,6 +56,18 @@ def test_lz(self, line_geometry):


class TestLineSlopeQueries:
@pytest.fixture(scope="class")
def line_geometry(self, mapdl):
mapdl.finish(mute=True)
mapdl.clear("NOSTART", mute=True)

mapdl.prep7(mute=True)
k0 = mapdl.k(1, 0, 0, 0)
k1 = mapdl.k(2, 1, 2, 2)
l0 = mapdl.l(k0, k1)
q = mapdl.queries
return q, [k0, k1], l0

def test_lsx(self, line_geometry):
q, kps, line = line_geometry
sx = q.lsx(line, 0.5)
Expand Down
Loading

0 comments on commit 11694ed

Please sign in to comment.