Page MenuHomec4science

model_factory.cpp
No OneTemporary

File Metadata

Created
Wed, May 15, 17:58

model_factory.cpp

/*
* SPDX-License-Indentifier: AGPL-3.0-or-later
*
* Copyright (©) 2016-2022 EPFL (École Polytechnique Fédérale de Lausanne),
* Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides)
* Copyright (©) 2020-2022 Lucas Frérot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/* -------------------------------------------------------------------------- */
#include "model_factory.hh"
#include "model_template.hh"
#include <boost/preprocessor/seq.hpp>
/* -------------------------------------------------------------------------- */
namespace tamaas {
/// \cond HIDDEN_SYMBOLS
#define MODEL_TYPE_CASE_MACRO(truc, arguments, type) \
case type: { \
result = std::make_unique<Concrete<type>>(std::forward<Args>(args)...); \
break; \
}
/// \endcond
#define IMPLEMENTED_MODEL_TYPES TAMAAS_MODEL_TYPES
template <typename Abstract, template <model_type> class Concrete,
class... Args>
decltype(auto) createFromModelType(model_type type, Args&&... args) {
std::unique_ptr<Abstract> result = nullptr;
switch (type) {
BOOST_PP_SEQ_FOR_EACH(MODEL_TYPE_CASE_MACRO, ~, IMPLEMENTED_MODEL_TYPES);
default:
TAMAAS_EXCEPTION("Model type not implemented");
}
return result;
}
#undef MODEL_TYPE_CASE_MACRO
#undef IMPLEMENTED_MODEL_TYPES
std::unique_ptr<Model>
ModelFactory::createModel(model_type type, const std::vector<Real>& system_size,
const std::vector<UInt>& discretization) {
return createFromModelType<Model, ModelTemplate>(type, system_size,
discretization);
}
std::unique_ptr<Model> ModelFactory::createModel(const Model& model) {
auto copy = createModel(model.getType(), model.getSystemSize(),
model.getDiscretization());
copy->setElasticity(model.getYoungModulus(), model.getPoissonRatio());
#define FIELD_COPY_CASE(truc, arguments, dim) \
case dim: { \
const auto* ptr = dynamic_cast<const Grid<Real, dim>*>(&f); \
if (ptr != nullptr) { \
auto new_f = std::make_shared<Grid<Real, dim>>(ptr->sizes(), \
ptr->getNbComponents()); \
*new_f = *ptr; \
copy->registerField(k, std::move(new_f)); \
} \
break; \
}
// Register and copy all fields
for (auto&& k : model.getFields()) {
auto&& f = model[k];
switch (f.getDimension()) {
BOOST_PP_SEQ_FOR_EACH(FIELD_COPY_CASE, ~, (1)(2)(3));
}
}
#undef FIELD_COPY_CASE
// TODO: Registrer and copy integral operators
return copy;
}
std::unique_ptr<Residual>
ModelFactory::createResidual(Model* model, Real sigma_y, Real hardening) {
if (model->getType() != model_type::volume_2d)
TAMAAS_EXCEPTION("Cannot instanciate model: " << model);
return std::make_unique<ResidualTemplate<model_type::volume_2d>>(
model, sigma_y, hardening);
}
#define MODEL_TYPE_CASE_MACRO(truc, arguments, type) \
case type: { \
m.registerIntegralOperator<Mindlin<type, 2>>("mindlin_gradient"); \
m.registerIntegralOperator<Boussinesq<type, 1>>("boussinesq_gradient"); \
m.registerIntegralOperator<Mindlin<type, 1>>("mindlin"); \
m.registerIntegralOperator<Boussinesq<type, 0>>("boussinesq"); \
break; \
}
void ModelFactory::registerVolumeOperators(Model& m) {
switch (m.getType()) {
BOOST_PP_SEQ_FOR_EACH(MODEL_TYPE_CASE_MACRO, ~, (model_type::volume_2d));
default:
TAMAAS_EXCEPTION("Registering volume operators not supported on " << m);
}
}
#undef MODEL_TYPE_CASE_MACRO
} // namespace tamaas

Event Timeline