diff --git a/tests/SConscript b/tests/SConscript index d2d65a0..ddba72c 100644 --- a/tests/SConscript +++ b/tests/SConscript @@ -1,114 +1,115 @@ from __future__ import print_function import os from os.path import join, abspath, basename # ------------------------------------------------------------------------------ def copyComStr(env, main): if 'SHCXXCOMSTR' in main: env['CXXCOMSTR'] = main['SHCXXCOMSTR'] if 'SHLINKCOMSTR' in main: env['LINKCOMSTR'] = main['SHLINKCOMSTR'] # ------------------------------------------------------------------------------ def make_python_tests(env): """Copy python tests to build directory""" test_env = env.Clone( PRINT_CMD_LINE_FUNC=main_env['gen_print']("Copying", "red", main_env)) test_files = Split(""" run_tests.sh test_hertz.py test_westergaard.py test_patch_westergaard.py test_surface.py test_autocorrelation.py test_hertz_disp.py test_hertz_kato.py test_saturated_pressure.py test_bem_grid.py test_flood_fill.py test_gtest.py + test_tangential.py fftfreq.py conftest.py """) src_dir = "#/tests" build_dir = 'build-' + main_env['build_type'] + '/tests' for file in test_files: source = join(src_dir, file) test_env.Command(file, source, Copy("$TARGET", "$SOURCE")) # ------------------------------------------------------------------------------ def compile_google_test(env, gtest_path): gtest_obj = env.Object('gtest.o', [join(gtest_path, "src/gtest-all.cc")]) env.StaticLibrary('gtest', gtest_obj) # ------------------------------------------------------------------------------ def make_google_tests(env): gtest_dir = Dir('#/third-party/googletest/googletest') gtest_env = env.Clone(CPPPATH=[gtest_dir], CXXFLAGS=['-pthread', '-isystem', join(str(gtest_dir), "include")]) colors = env['COLOR_DICT'] if not env['verbose']: gtest_env['ARCOMSTR'] = u'{}[Ar]{} $TARGET'.format(colors['purple'], colors['end']) gtest_env['RANLIBCOMSTR'] = u'{}[Randlib]{} $TARGET'.format(colors['purple'], colors['end']) gtest_env.Tool(gtest) gtest_path = str(gtest_dir) compile_google_test(gtest_env, gtest_path) env.AppendUnique(LIBS=['Tamaas'], CXXFLAGS=gtest_env['CXXFLAGS']) google_test_files = Split(""" test_fft.cpp test_grid.cpp test_loop.cpp test_model.cpp """) google_test_files += ['libgtest.a'] gtest_main = env.Object("gtest_main.o", 'tamaas_gtest_main.cc') env.Program('test_gtest_all', google_test_files + [gtest_main]) # ------------------------------------------------------------------------------ def make_bare_tests(env): env.Program("test_rough_surface.cpp") # ------------------------------------------------------------------------------ Import('main_env') # Setup of test environment test_env = main_env.Clone() test_env.AppendUnique(LIBS=['Tamaas'], LIBPATH=[abspath('build-' + main_env['build_type'] + '/src')]) copyComStr(test_env, main_env) # Building tests that do not require any third party make_bare_tests(test_env) if test_env['build_python']: test_env.Tool(pybind11) #test_env.AppendUnique(LIBS=['python3.6m']) test_env.ParseConfig("{}-config --ldflags".format(test_env['py_exec'])) test_env['CCFLAGS'] = [] make_python_tests(test_env) # Building google tests if test_env['use_googletest']: make_google_tests(test_env) diff --git a/tests/test_tangential.py b/tests/test_tangential.py new file mode 100644 index 0000000..f2f49c3 --- /dev/null +++ b/tests/test_tangential.py @@ -0,0 +1,108 @@ +#!/usr/bin/env python +# coding: utf-8 +# ----------------------------------------------------------------------------- +# @author Son Pham-Ba +# +# @section LICENSE +# +# Copyright (©) 2018 EPFL (Ecole Polytechnique Fédérale de +# Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des +# Solides) +# +# Tamaas 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. +# +# Tamaas 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 Tamaas. If not, see . +# ----------------------------------------------------------------------------- +from __future__ import division, print_function + +import numpy as np +from numpy.linalg import norm +import tamaas as tm + + +def test_pressure(): + tm.initialize() + + E = 1.0 + nu = 0.5 + mu = 0.5 + n = 81 + L = 1.0 + + p_target = np.array([0.5e-4, 0, 2e-4]); + g_target = np.array([-0.000223, 0, 9.99911]); + + x = np.linspace(-L/2, L/2, n) + y = np.copy(x) + x, y = np.meshgrid(x, y, indexing="ij") + r_sqrd = (x**2 + y**2) + # Spherical contact + R = 10.0 + surface = R * np.sqrt((r_sqrd < R**2) * (1 - r_sqrd / R**2)) + + # Creating model + model = tm.ModelFactory.createModel(tm.model_type_surface_2d, [L, L], [n, n]) + model.setElasticity(E, nu) + p = model.getTraction(); + + # Theoretical solution + Estar = E / (1.0 - nu**2) + Fn = p_target[2] * L**2 + Fx = p_target[0] * L**2 + d_theory = np.power(3 / 4 * Fn / Estar / np.sqrt(R), 2/3) + a_theory = np.sqrt(R * d_theory) + c_theory = a_theory * (1 - Fx / (mu * Fn)) ** (1/3) + p0_theory = np.power(6 * Fn * Estar**2 / np.pi**3 / R**2, 1/3) + t1_theory = mu * p0_theory + t2_theory = t1_theory * c_theory / a_theory + # p_theory = p0_theory * np.sqrt((r_sqrd < a_theory**2) * (1 - r_sqrd / a_theory**2)) + t_theory = t1_theory * np.sqrt((r_sqrd < a_theory**2) * (1 - r_sqrd / a_theory**2)) + t_theory = t_theory - t2_theory * np.sqrt((r_sqrd < c_theory**2) * (1 - r_sqrd / c_theory**2)) + + def assert_error(err): + error = norm(p[..., 0] - t_theory) / norm(t_theory); + print (error) + assert error < err + + + # Test Kato solver + solver = tm.Kato(model, surface, 1e-12, mu) + solver.setMaxIterations(1000) + solver.solve(p_target, 100) + assert_error(4e-2) + + # solver.solveRelaxed(g_target) + # assert_error(1e-2) + + # # Test BeckTeboulle solver + # solver = tm.BeckTeboulle(model, surface, 1e-12, mu) + # solver.solve(g_target) + # assert_error(1e-2) + + # Test Condat solver + solver = tm.Condat(model, surface, 1e-12, mu) + solver.setMaxIterations(5000) # or 10000 + solver.solve(p_target) + assert_error(7e-2) # 4e-2 for 10000 iterations + + # Test tangential Polonsky Keer solver + solver = tm.PolonskyKeerTan(model, surface, 1e-12, mu) + solver.setMaxIterations(1000) + solver.solve(p_target) + assert_error(4e-2) + + + tm.finalize() + + +if __name__ == "__main__": + test_pressure()