diff --git a/test/test_model/patch_tests/CMakeLists.txt b/test/test_model/patch_tests/CMakeLists.txt index bcb5329d1..8f109bdb8 100644 --- a/test/test_model/patch_tests/CMakeLists.txt +++ b/test/test_model/patch_tests/CMakeLists.txt @@ -1,113 +1,113 @@ #=============================================================================== # @file CMakeLists.txt # # @author Guillaume Anciaux # @author David Simon Kammer # @author Nicolas Richart # # @date creation: Fri Oct 22 2010 # @date last modification: Thu Feb 08 2018 # # @brief configuration for patch tests # # @section LICENSE # # Copyright (©) 2010-2018 EPFL (Ecole Polytechnique Fédérale de Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides) # # Akantu is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. # # Akantu is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License along with Akantu. If not, see . # # @section DESCRIPTION # #=============================================================================== add_subdirectory(data) register_gtest_sources(SOURCES patch_test_linear_elastic_explicit.cc PACKAGE solid_mechanics FILES_TO_COPY data/material_check_stress_plane_strain.dat data/material_check_stress_plane_stress.dat) register_gtest_sources(SOURCES patch_test_linear_elastic_implicit.cc PACKAGE solid_mechanics implicit FILES_TO_COPY data/material_check_stress_plane_strain.dat data/material_check_stress_plane_stress.dat) register_gtest_sources(SOURCES patch_test_linear_anisotropic_explicit.cc PACKAGE solid_mechanics lapack UNSTABLE FILES_TO_COPY data/material_anisotropic.dat) register_gtest_sources(SOURCES test_lumped_mass.cc PACKAGE solid_mechanics FILES_TO_COPY data/material_lumped.dat) register_gtest_sources( SOURCES patch_test_linear_heat_transfer_explicit.cc FILES_TO_COPY data/heat_transfer_input.dat PACKAGE heat_transfer) register_gtest_sources( SOURCES patch_test_linear_heat_transfer_static.cc FILES_TO_COPY data/heat_transfer_input.dat PACKAGE heat_transfer implicit) register_gtest_sources( SOURCES patch_test_linear_heat_transfer_implicit.cc FILES_TO_COPY data/heat_transfer_input.dat PACKAGE heat_transfer implicit ) register_gtest_test(patch_test_linear FILES_TO_COPY ${PATCH_TEST_MESHES}) register_test(test_linear_elastic_explicit_python - SCRIPT patch_test_linear_elastic_explicit.py + SCRIPT test_patch_linear_elastic_explicit.py PYTHON PACKAGE python_interface FILES_TO_COPY patch_test_linear_fixture.py FILES_TO_COPY patch_test_linear_solid_mechanics_fixture.py FILES_TO_COPY ${PATCH_TEST_MESHES}) register_test(test_linear_elastic_static_python - SCRIPT patch_test_linear_elastic_static.py + SCRIPT test_patch_linear_elastic_static.py PYTHON PACKAGE python_interface implicit FILES_TO_COPY patch_test_linear_fixture.py FILES_TO_COPY patch_test_linear_solid_mechanics_fixture.py) register_test(test_linear_anisotropic_explicit_python - SCRIPT patch_test_linear_anisotropic_explicit.py + SCRIPT test_patch_linear_anisotropic_explicit.py PYTHON UNSTABLE PACKAGE python_interface lapack FILES_TO_COPY patch_test_linear_fixture.py FILES_TO_COPY patch_test_linear_solid_mechanics_fixture.py FILES_TO_COPY data/material_anisotropic.dat) register_test(patch_test_linear_heat_transfer_explicit_python - SCRIPT patch_test_linear_heat_transfer_explicit.py + SCRIPT test_patch_linear_heat_transfer_explicit.py PYTHON PACKAGE python_interface heat_transfer FILES_TO_COPY patch_test_linear_fixture.py FILES_TO_COPY patch_test_linear_heat_transfer_fixture.py FILES_TO_COPY data/heat_transfer_input.dat) register_test(patch_test_linear_heat_transfer_static_python - SCRIPT patch_test_linear_heat_transfer_static.py + SCRIPT test_patch_linear_heat_transfer_static.py PYTHON PACKAGE python_interface heat_transfer implicit FILES_TO_COPY patch_test_linear_fixture.py FILES_TO_COPY patch_test_linear_heat_transfer_fixture.py FILES_TO_COPY data/heat_transfer_input.dat) register_test(patch_test_linear_heat_transfer_implicit_python - SCRIPT patch_test_linear_heat_transfer_implicit.py + SCRIPT test_patch_linear_heat_transfer_implicit.py PYTHON PACKAGE python_interface heat_transfer implicit FILES_TO_COPY patch_test_linear_fixture.py FILES_TO_COPY patch_test_linear_heat_transfer_fixture.py FILES_TO_COPY data/heat_transfer_input.dat) diff --git a/test/test_model/patch_tests/patch_test_linear_fixture.py b/test/test_model/patch_tests/patch_test_linear_fixture.py index c86b32dcf..08f062f57 100644 --- a/test/test_model/patch_tests/patch_test_linear_fixture.py +++ b/test/test_model/patch_tests/patch_test_linear_fixture.py @@ -1,147 +1,146 @@ #!/usr/bin/env python3 # ------------------------------------------------------------------------------ __author__ = "Guillaume Anciaux" __copyright__ = "Copyright (C) 2016-2018, EPFL (Ecole Polytechnique Fédérale" \ " de Lausanne) Laboratory (LSMS - Laboratoire de Simulation" \ " en Mécanique des Solides)" __credits__ = ["Guillaume Anciaux"] __license__ = "L-GPLv3" __maintainer__ = "Guillaume Anciaux" __email__ = "guillaume.anciaux@epfl.ch" # ------------------------------------------------------------------------------ import akantu import unittest import numpy as np -from local_elastic import LocalElastic class TestPatchTestLinear(unittest.TestCase): alpha = np.array([[0.01, 0.02, 0.03, 0.04], [0.05, 0.06, 0.07, 0.08], [0.09, 0.10, 0.11, 0.12]]) gradient_tolerance = 1e-13 result_tolerance = 1e-13 dofs_tolerance = 1e-15 def __init__(self, test_name, elem_type_str, functor=None): self.test_name = test_name self.functor = functor self.elem_type = akantu.getElementTypes()[elem_type_str] self.elem_type_str = elem_type_str self.dim = akantu.Mesh.getSpatialDimension(self.elem_type) super().__init__(test_name) def _internal_call(self): self.functor(self) def __getattr__(self, key): if key == self.test_name: return self._internal_call def setUp(self): self.mesh = akantu.Mesh(self.dim, self.elem_type_str) self.mesh.read(str(self.elem_type_str) + ".msh") akantu.MeshUtils.buildFacets(self.mesh) self.mesh.createBoundaryGroupFromGeometry() # mat_factory = akantu.MaterialFactory.getInstance() # # def allocator(_dim, unused, model, _id): # return LocalElastic(model, _id) # # try: # mat_factory.registerAllocator("local_elastic", allocator) # except Exception: # pass self.model = self.model_type(self.mesh) def tearDown(self): del self.model del self.mesh def initModel(self, method, material_file): akantu.parseInput(material_file) akantu.setDebugLevel(akantu.dblError) self.model.initFull(method) self.applyBC() if method != akantu._static: self.model.setTimeStep(0.8 * self.model.getStableTimeStep()) def applyBC(self): boundary = self.model.getBlockedDOFs() for name, eg in self.mesh.getElementGroups().items(): nodes = eg.getNodes() boundary[nodes, :] = True def applyBConDOFs(self, dofs): coordinates = self.mesh.getNodes() for name, eg in self.mesh.getElementGroups().items(): nodes = eg.getNodes().flatten() dofs[nodes] = self.setLinearDOF(dofs[nodes], coordinates[nodes]) def prescribed_gradient(self, dof): gradient = self.alpha[:dof.shape[1], 1:self.dim + 1] return gradient def checkGradient(self, gradient, dofs): pgrad = self.prescribed_gradient(dofs).T gradient = gradient.reshape(gradient.shape[0], *pgrad.shape) diff = gradient[:] - pgrad norm = np.abs(pgrad).max() gradient_error = np.abs(diff).max() / norm self.assertAlmostEqual(0, gradient_error, delta=self.gradient_tolerance) def checkResults(self, presult_func, results, dofs): presult = presult_func(self.prescribed_gradient(dofs)).flatten() remaining_size = np.prod(np.array(results.shape[1:])) results = results.reshape((results.shape[0], remaining_size)) for result in results: diff = result - presult norm = np.abs(result).max() if norm == 0: result_error = np.abs(diff).max() else: result_error = np.abs(diff).max() / norm self.assertAlmostEqual(0., result_error, delta=self.result_tolerance) def setLinearDOF(self, dof, coord): nb_dofs = dof.shape[1] dof[:] = np.einsum('ik,ak->ai', self.alpha[:nb_dofs, 1:self.dim + 1], coord) for i in range(0, nb_dofs): dof[:, i] += self.alpha[i, 0] return dof def checkDOFs(self, dofs): coordinates = self.mesh.getNodes() ref_dofs = np.zeros_like(dofs) self.setLinearDOF(ref_dofs, coordinates) diff = dofs - ref_dofs dofs_error = np.abs(diff).max() self.assertAlmostEqual(0., dofs_error, delta=self.dofs_tolerance) @classmethod def TYPED_TEST(cls, functor, label): for type_name, _type in akantu.getElementTypes().items(): if type_name == "_point_1": continue method_name = type_name + '_' + label test_case = cls(method_name, type_name, functor) test_case.setUp() functor(test_case) test_case.tearDown() diff --git a/test/test_model/patch_tests/test_patch_linear_elastic_explicit.py b/test/test_model/patch_tests/test_patch_linear_elastic_explicit.py index 6f0c37fc9..85a204f98 100755 --- a/test/test_model/patch_tests/test_patch_linear_elastic_explicit.py +++ b/test/test_model/patch_tests/test_patch_linear_elastic_explicit.py @@ -1,40 +1,45 @@ #!/usr/bin/env python3 # ------------------------------------------------------------------------------ __author__ = "Guillaume Anciaux" __copyright__ = "Copyright (C) 2016-2018, EPFL (Ecole Polytechnique Fédérale" \ " de Lausanne) Laboratory (LSMS - Laboratoire de Simulation" \ " en Mécanique des Solides)" __credits__ = ["Guillaume Anciaux"] __license__ = "L-GPLv3" __maintainer__ = "Guillaume Anciaux" __email__ = "guillaume.anciaux@epfl.ch" # ------------------------------------------------------------------------------ from patch_test_linear_solid_mechanics_fixture import TestPatchTestSMMLinear import akantu +import sys def foo(self): filename = "material_check_stress_plane_stress.dat" if self.plane_strain: filename = "material_check_stress_plane_strain.dat" self.initModel(akantu._explicit_lumped_mass, filename) coordinates = self.mesh.getNodes() displacement = self.model.getDisplacement() # set the position of all nodes to the static solution self.setLinearDOF(displacement, coordinates) for s in range(0, 100): self.model.solveStep() ekin = self.model.getEnergy("kinetic") self.assertAlmostEqual(0, ekin, delta=1e-16) self.checkAll() def test(): TestPatchTestSMMLinear.TYPED_TEST(foo, "Explicit") + + +if 'pytest' not in sys.modules: + test() diff --git a/test/test_model/patch_tests/test_patch_linear_elastic_static.py b/test/test_model/patch_tests/test_patch_linear_elastic_static.py index 5ca885166..e7f48bad3 100755 --- a/test/test_model/patch_tests/test_patch_linear_elastic_static.py +++ b/test/test_model/patch_tests/test_patch_linear_elastic_static.py @@ -1,36 +1,41 @@ #!/usr/bin/env python3 # ------------------------------------------------------------------------------ __author__ = "Guillaume Anciaux" __copyright__ = "Copyright (C) 2016-2018, EPFL (Ecole Polytechnique Fédérale" \ " de Lausanne) Laboratory (LSMS - Laboratoire de Simulation" \ " en Mécanique des Solides)" __credits__ = ["Guillaume Anciaux"] __license__ = "L-GPLv3" __maintainer__ = "Guillaume Anciaux" __email__ = "guillaume.anciaux@epfl.ch" # ------------------------------------------------------------------------------ from patch_test_linear_solid_mechanics_fixture import TestPatchTestSMMLinear import akantu +import sys def foo(self): filename = "material_check_stress_plane_stress.dat" if self.plane_strain: filename = "material_check_stress_plane_strain.dat" self.initModel(akantu._static, filename) solver = self.model.getNonLinearSolver() solver.set("max_iterations", 2) solver.set("threshold", 2e-4) solver.set("convergence_type", akantu._scc_residual) self.model.solveStep() self.checkAll() def test(): TestPatchTestSMMLinear.TYPED_TEST(foo, "Static") + + +if 'pytest' not in sys.modules: + test() diff --git a/test/test_model/patch_tests/patch_test_linear_heat_transfer_explicit.py b/test/test_model/patch_tests/test_patch_linear_heat_transfer_explicit.py similarity index 82% rename from test/test_model/patch_tests/patch_test_linear_heat_transfer_explicit.py rename to test/test_model/patch_tests/test_patch_linear_heat_transfer_explicit.py index 999fc03d7..567c1ebb6 100644 --- a/test/test_model/patch_tests/patch_test_linear_heat_transfer_explicit.py +++ b/test/test_model/patch_tests/test_patch_linear_heat_transfer_explicit.py @@ -1,35 +1,39 @@ #!/usr/bin/env python3 # ------------------------------------------------------------------------------ __author__ = "Guillaume Anciaux" __copyright__ = "Copyright (C) 2016-2018, EPFL (Ecole Polytechnique Fédérale" \ " de Lausanne) Laboratory (LSMS - Laboratoire de Simulation" \ " en Mécanique des Solides)" __credits__ = ["Guillaume Anciaux"] __license__ = "L-GPLv3" __maintainer__ = "Guillaume Anciaux" __email__ = "guillaume.anciaux@epfl.ch" # ------------------------------------------------------------------------------ from patch_test_linear_heat_transfer_fixture import TestPatchTestHTMLinear import akantu +import sys def foo(self): - self.initModel( - akantu.HeatTransferModelOptions(akantu._explicit_lumped_mass), - "heat_transfer_input.dat") + self.initModel(akantu._explicit_lumped_mass, "heat_transfer_input.dat") coordinates = self.mesh.getNodes() temperature = self.model.getTemperature() # set the position of all nodes to the static solution self.setLinearDOF(temperature, coordinates) for s in range(0, 100): self.model.solveStep() self.checkAll() -TestPatchTestHTMLinear.TYPED_TEST(foo, "Explicit") +def test(): + TestPatchTestHTMLinear.TYPED_TEST(foo, "Explicit") + + +if 'pytest' not in sys.modules: + test() diff --git a/test/test_model/patch_tests/patch_test_linear_heat_transfer_implicit.py b/test/test_model/patch_tests/test_patch_linear_heat_transfer_implicit.py similarity index 100% rename from test/test_model/patch_tests/patch_test_linear_heat_transfer_implicit.py rename to test/test_model/patch_tests/test_patch_linear_heat_transfer_implicit.py diff --git a/test/test_model/patch_tests/patch_test_linear_heat_transfer_static.py b/test/test_model/patch_tests/test_patch_linear_heat_transfer_static.py similarity index 83% rename from test/test_model/patch_tests/patch_test_linear_heat_transfer_static.py rename to test/test_model/patch_tests/test_patch_linear_heat_transfer_static.py index 58e009d54..63f5a3259 100644 --- a/test/test_model/patch_tests/patch_test_linear_heat_transfer_static.py +++ b/test/test_model/patch_tests/test_patch_linear_heat_transfer_static.py @@ -1,32 +1,37 @@ #!/usr/bin/env python3 # ------------------------------------------------------------------------------ __author__ = "Guillaume Anciaux" __copyright__ = "Copyright (C) 2016-2018, EPFL (Ecole Polytechnique Fédérale" \ " de Lausanne) Laboratory (LSMS - Laboratoire de Simulation" \ " en Mécanique des Solides)" __credits__ = ["Guillaume Anciaux"] __license__ = "L-GPLv3" __maintainer__ = "Guillaume Anciaux" __email__ = "guillaume.anciaux@epfl.ch" # ------------------------------------------------------------------------------ from patch_test_linear_heat_transfer_fixture import TestPatchTestHTMLinear import akantu +import sys def foo(self): - self.initModel(akantu.HeatTransferModelOptions(akantu._static), - "heat_transfer_input.dat") + self.initModel(akantu._static, "heat_transfer_input.dat") solver = self.model.getNonLinearSolver() solver.set("max_iterations", 2) solver.set("threshold", 2e-4) solver.set("convergence_type", akantu._scc_residual) self.model.solveStep() self.checkAll() -TestPatchTestHTMLinear.TYPED_TEST(foo, "Static") +def test(): + TestPatchTestHTMLinear.TYPED_TEST(foo, "Static") + + +if 'pytest' not in sys.modules: + test()