Page MenuHomec4science

test_model.cpp
No OneTemporary

File Metadata

Created
Fri, Jul 5, 06:41

test_model.cpp

/**
*
* @author Lucas Frérot <lucas.frerot@epfl.ch>
*
* @section LICENSE
*
* Copyright (©) 2017 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 <http://www.gnu.org/licenses/>.
*
*/
/* -------------------------------------------------------------------------- */
#include "model_factory.hh"
#include "test.hh"
#include <random>
/* -------------------------------------------------------------------------- */
using namespace tamaas;
TEST(TestModel, applyElasticity) {
auto model =
ModelFactory::createModel(model_type::volume_2d, {1., 1., 1.}, {1, 1, 1});
Grid<Real, 3> gradient({1, 1, 1}, 9), stress({1, 1, 1}, 9);
// Random data objects
std::random_device rnd;
std::mt19937 mt(rnd());
std::normal_distribution<> dis(0, 1);
// Filling gradient with random data (sequential)
for (auto& du : gradient)
du = dis(mt);
std::uniform_real_distribution<> unif(0, 0.5);
model->setElasticity(std::abs(dis(mt)) * unif(mt), unif(mt));
// Computing stresses with seperate array
model->applyElasticity(stress, gradient);
// Checking correct isotropic elasticity
auto mu = model->getShearModulus(), nu = model->getPoissonRatio();
auto lambda = 2 * mu * nu / (1 - 2 * nu);
MatrixProxy<Real, 3, 3> grad(gradient(0));
auto trace = grad.trace();
Matrix<Real, 3, 3> sigma;
for (UInt i = 0; i < 3; ++i)
for (UInt j = 0; j < 3; ++j)
sigma(i, j) = (i == j) * lambda * trace + mu * (grad(i, j) + grad(j, i));
EXPECT_TRUE(compare(stress, sigma, AreFloatEqual())) << "Elasticity fail";
// Computing stress with same array
model->applyElasticity(gradient, gradient);
EXPECT_TRUE(compare(gradient, stress, AreFloatEqual()))
<< "Applying elasticity in-place fail";
}
TEST(TestModel, applyElasticitySym) {
auto model =
ModelFactory::createModel(model_type::volume_2d, {1., 1., 1.}, {1, 1, 1});
Grid<Real, 3> gradient({1, 1, 1}, 6), stress({1, 1, 1}, 6);
// Random data objects
std::random_device rnd;
std::mt19937 mt(rnd());
std::normal_distribution<> dis(0, 1);
// Filling gradient with random data (sequential)
for (auto& du : gradient)
du = dis(mt);
std::uniform_real_distribution<> unif(0, 0.5);
model->setElasticity(std::abs(dis(mt)) * unif(mt), unif(mt));
// Computing stresses with seperate array
model->applyElasticity(stress, gradient);
// Checking correct isotropic elasticity
auto mu = model->getShearModulus(), nu = model->getPoissonRatio();
auto lambda = 2 * mu * nu / (1 - 2 * nu);
SymMatrixProxy<Real, 3> sym_grad(gradient(0));
auto grad = dense(sym_grad);
auto trace = grad.trace();
Matrix<Real, 3, 3> sigma;
for (UInt i = 0; i < 3; ++i)
for (UInt j = 0; j < 3; ++j)
sigma(i, j) = (i == j) * lambda * trace + mu * (grad(i, j) + grad(j, i));
EXPECT_TRUE(compare(stress, symmetrize(sigma), AreFloatEqual()))
<< "Elasticity fail";
// Computing stress with same array
model->applyElasticity(gradient, gradient);
EXPECT_TRUE(compare(gradient, stress, AreFloatEqual()))
<< "Applying elasticity in-place fail";
}

Event Timeline