Skip to content

Commit

Permalink
Resolve "create test for amfe.ui"
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferdinand Elhardt authored and c-meyer committed May 15, 2020
1 parent cf36263 commit 613adfe
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 8 deletions.
50 changes: 43 additions & 7 deletions amfe/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,15 @@ def set_neumann_by_group(component, group_name, direction, following=False, neum
f : function
pointer to function with signature float func(float: t)
"""
if direction == 'normal':
if following:
neumann = NormalFollowingNeumann(time_func=f)
if isinstance(direction, str):
if direction == 'normal':
if following:
neumann = NormalFollowingNeumann(time_func=f)
else:
raise NotImplementedError('There is no implementation for normal direction that is fixed with inertial'
'frame')
else:
raise NotImplementedError('There is no implementation for normal direction that is fixed with inertial'
'frame')
raise ValueError('direction {} not supported'.format(direction))
else:
direction = np.array(direction, dtype=float) # This would raise an error if cannot be transformed to array
if following:
Expand All @@ -212,9 +215,42 @@ def set_neumann_by_group(component, group_name, direction, following=False, neum
component.assign_neumann(neumann_name, neumann, [group_name], '_groups')


def set_neumann_by_elementids(component, elementids, direction_vector=np.array([0, 1]), neumann_name='Neumann0',
def set_neumann_by_elementids(component, elementids, direction, following=False, neumann_name='Neumann0',
f=constant_force(1.0)):
neumann = FixedDirectionNeumann(direction_vector, time_func=f)
"""
Sets a neumann condition on a component by addressing elementids in the mesh.
Parameters
----------
component : component
component the neumann condition should be added
elementids : list or ndarray of integers
elementids to which the condition is assigned to
direction : array_like
direction of the force.
following : bool
Use following = True to keep the direction relative to the body frame.
If following is set to false (default), the direction is fixed.
neumann_name : str
name of the condition, defined by user
f : function
pointer to function with signature float func(float: t)
"""
if isinstance(direction, str):
if direction == 'normal':
if following:
neumann = NormalFollowingNeumann(time_func=f)
else:
raise NotImplementedError('There is no implementation for normal direction that is fixed with inertial'
'frame')
else:
raise ValueError('direction {} not supported'.format(direction))
else:
direction = np.array(direction, dtype=float) # This would raise an error if cannot be transformed to array
if following:
raise NotImplementedError('There is no implementation for forces that follow the body frame')
else:
neumann = FixedDirectionNeumann(direction, time_func=f)
component.assign_neumann(neumann_name, neumann, elementids, '_eleids')


Expand Down
3 changes: 2 additions & 1 deletion docs/release/1.1.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ amfe.tools improvements
amfe.ui improvements
--------------------
- added method to compute the modes of a system

- UI exports strains and stresses by default
- created tests for methods in amfe.ui
- in all solvers, changed argument from taking a Component to MechanicalSystem
- added option 'normal' to set_neumann_by_group in order to set a neumann condition with a following normal direction
to surface
Expand Down
1 change: 1 addition & 0 deletions tests/meshes/not_a_mesh_for_testing.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is not a mesh.
182 changes: 182 additions & 0 deletions tests/test_ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
# Copyright (c) 2018, Lehrstuhl fuer Angewandte Mechanik, Technische Universitaet Muenchen.
#
# Distributed under BSD-3-Clause License. See LICENSE-File for more information
#

from unittest import TestCase
import numpy as np
from numpy.testing import assert_array_equal

from amfe.ui import *
from amfe.component import MeshComponent
from amfe.mesh import Mesh
from amfe.material import Material
from amfe.io.tools import amfe_dir
from amfe.forces import *
from amfe.neumann.structural_neumann import FixedDirectionNeumann, NormalFollowingNeumann
from amfe.solver.translators import create_constrained_mechanical_system_from_component
from amfe.solver import AmfeSolution


class TestUi(TestCase):
def setUp(self):
self.input_file_1 = amfe_dir('tests/meshes/2_partitions_2quad_mesh.msh')
self.input_file_2 = amfe_dir('tests/meshes/gid_json_4_tets.json')

class DummyMaterial:
def __init__(self, name):
self.name = name

self.testmaterial = DummyMaterial('steel')

def tearDown(self):
pass

def test_import_mesh_from_file(self):
input_file_3 = amfe_dir('tests/meshes/not_a_mesh_for_testing.txt')
testmesh_1 = import_mesh_from_file(self.input_file_1)
testmesh_2 = import_mesh_from_file(self.input_file_2)
# check if raises error for no valid mesh file
self.assertRaises(ValueError, import_mesh_from_file, input_file_3)
# check for correct import
self.assertIsInstance(testmesh_1, Mesh)
self.assertIsInstance(testmesh_2, Mesh)
self.assertEqual(testmesh_1.no_of_elements, 2)
self.assertEqual(testmesh_2.no_of_elements, 4)

def test_create_structural_component(self):
testmesh_1 = import_mesh_from_file(self.input_file_1)
testcomponent = create_structural_component(testmesh_1)
self.assertIsInstance(testcomponent, MeshComponent)

def test_create_material(self):
testmaterial_1 = create_material('Kirchhoff', E=210E9, nu=0.3, rho=7.86E3,
plane_stress=True, thickness=0.1)
testmaterial_2 = create_material('Kirchhoff')
testmaterial_3 = create_material('MooneyRivlin', A10=0.3E3, A01=0.2E3, kappa=2E5, rho=0.95E3,
plane_stress=False, thickness=2.0)
testmaterial_4 = create_material('MooneyRivlin')
self.assertIsInstance(testmaterial_1, Material)
self.assertIsInstance(testmaterial_2, Material)
self.assertIsInstance(testmaterial_3, Material)
self.assertIsInstance(testmaterial_4, Material)
self.assertRaises(ValueError, create_material, 'Not_a_material', E=210E9, nu=0.3, rho=7.86E3,
plane_stress=True, thickness=0.1)

def test_assign_material_by_group(self):
testmesh = import_mesh_from_file(self.input_file_1)
testcomponent = create_structural_component(testmesh)
# test for error-message when using wrong group-name
self.assertRaises(ValueError, assign_material_by_group, testcomponent, self.testmaterial, 'not_a_group')
# check if material successfully assigned
assign_material_by_group(testcomponent, self.testmaterial, '1')
material = testcomponent.get_materials()
self.assertEqual(material[0], self.testmaterial)

def test_assign_material_by_elementids(self):
testmesh = import_mesh_from_file(self.input_file_1)
testcomponent = create_structural_component(testmesh)
eleids1 = np.array([1, 2], dtype=int)
assign_material_by_elementids(testcomponent, self.testmaterial, eleids1)
# check if material successfully assigned
eleids1_real = testcomponent.get_elementids_by_materials(self.testmaterial)
assert_array_equal(eleids1, eleids1_real)

def test_set_dirichlet_by_group(self):
testmesh = import_mesh_from_file(self.input_file_1)
testcomponent = create_structural_component(testmesh)
assign_material_by_group(testcomponent, self.testmaterial, '1')
assign_material_by_group(testcomponent, self.testmaterial, '2')
assign_material_by_group(testcomponent, self.testmaterial, '3')

# assign Dirichlet_ux and test
group_name = '1'
direction = 'ux'
constraint_name = 'Dirichlet_ux'
set_dirichlet_by_group(testcomponent, group_name, direction, constraint_name)
self.assertEqual(testcomponent.constraints.no_of_constraints, 2)

# assign Dirichlet_uy and test
group_name = '2'
direction = 'uy'
constraint_name = 'Dirichlet_uy'
set_dirichlet_by_group(testcomponent, group_name, direction, constraint_name)
self.assertEqual(testcomponent.constraints.no_of_constraints, 4)

def test_set_dirichlet_by_nodeids(self):
testmesh = import_mesh_from_file(self.input_file_1)
testcomponent = create_structural_component(testmesh)
assign_material_by_group(testcomponent, self.testmaterial, '1')
assign_material_by_group(testcomponent, self.testmaterial, '2')
assign_material_by_group(testcomponent, self.testmaterial, '3')

# assign Dirichlet_ux
nodeids = np.array([1, 2, 3])
direction = 'ux'
constraint_name = 'Dirichlet_ux'
set_dirichlet_by_nodeids(testcomponent, nodeids, direction, constraint_name)
self.assertEqual(testcomponent.constraints.no_of_constraints, 3)

# assign Dirichlet_uy
nodeids = np.array([4, 5, 6])
direction = 'ux'
constraint_name = 'Dirichlet_uy'
set_dirichlet_by_nodeids(testcomponent, nodeids, direction, constraint_name)
self.assertEqual(testcomponent.constraints.no_of_constraints, 6)

def test_set_neumann_by_group(self):
testmesh = import_mesh_from_file(self.input_file_1)
testcomponent = create_structural_component(testmesh)
assign_material_by_group(testcomponent, self.testmaterial, '1')
assign_material_by_group(testcomponent, self.testmaterial, '2')
assign_material_by_group(testcomponent, self.testmaterial, '3')

# assign Neumann-condition
group_name = '1'
direction_vector = np.array([0, 1])
neumann_name = 'Neumann0'
force = constant_force(1.0)
set_neumann_by_group(testcomponent, group_name, direction_vector, neumann_name=neumann_name, f=force)
# test
neumann_obj, fk_mesh, fk_mapping = testcomponent.neumann.get_ele_obj_fk_mesh_and_fk_mapping()
self.assertIsInstance(neumann_obj[0], FixedDirectionNeumann)

def test_set_neumann_by_elementids(self):
testmesh = import_mesh_from_file(self.input_file_1)
testcomponent = create_structural_component(testmesh)
assign_material_by_group(testcomponent, self.testmaterial, '1')
assign_material_by_group(testcomponent, self.testmaterial, '2')
assign_material_by_group(testcomponent, self.testmaterial, '3')

# assign Neumann-condition
eleids1 = np.array([1, 2], dtype=int)
direction_vector = np.array([0, 1])
neumann_name = 'Neumann0'
force = constant_force(1.0)
set_neumann_by_elementids(testcomponent, eleids1, direction_vector, neumann_name=neumann_name, f=force)

# assign Neumann-condition
direction_vector = 'normal'
neumann_name = 'Neumann1'
force = constant_force(1.0)
set_neumann_by_elementids(testcomponent, eleids1, direction_vector, following=True, neumann_name=neumann_name,
f=force)

# test
neumann_obj, fk_mesh, fk_mapping = testcomponent.neumann.get_ele_obj_fk_mesh_and_fk_mapping()
self.assertIsInstance(neumann_obj[0], FixedDirectionNeumann)
self.assertIsInstance(neumann_obj[2], NormalFollowingNeumann)

def test_solve_modes(self):
testmesh_1 = import_mesh_from_file(self.input_file_1)
testcomponent = create_structural_component(testmesh_1)
testmaterial_1 = create_material('Kirchhoff', E=210E9, nu=0.3, rho=7.86E3,
plane_stress=True, thickness=0.1)
assign_material_by_group(testcomponent, testmaterial_1, '3')
testsystem, testformulation = create_constrained_mechanical_system_from_component(testcomponent)
modes = solve_modes(testsystem, testformulation, 10)
self.assertIsInstance(modes, AmfeSolution)
self.assertEqual(len(modes.t), 10)
self.assertEqual(len(modes.ddq), 10)
self.assertEqual(len(modes.dq), 10)
self.assertEqual(len(modes.q), 10)

0 comments on commit 613adfe

Please sign in to comment.