Page MenuHomec4science

model_type.hh
No OneTemporary

File Metadata

Created
Sat, May 11, 14:12

model_type.hh

/**
* @file
* @author Lucas Frérot <lucas.frerot@epfl.ch>
*
* @section LICENSE
*
* Copyright (©) 2016-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/>.
*
*/
/* -------------------------------------------------------------------------- */
#ifndef __MODEL_TYPE_HH__
#define __MODEL_TYPE_HH__
/* -------------------------------------------------------------------------- */
#include "grid.hh"
#include "grid_base.hh"
#include "tamaas.hh"
#include <boost/preprocessor/seq.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/stringize.hpp>
#include <memory>
#include <algorithm>
/* -------------------------------------------------------------------------- */
__BEGIN_TAMAAS__
// clang-format off
/// Models types **for printing only**
#define TAMAAS_MODEL_TYPES \
(basic_1d) \
(basic_2d) \
(surface_1d) \
(surface_2d) \
(volume_1d) \
(volume_2d)
// clang-format on
/// Types for grid dimensions and number of components
enum class model_type {
basic_1d, ///< one component line
basic_2d, ///< one component surface
surface_1d, ///< two components line
surface_2d, ///< three components surface
volume_1d, ///< two components volume
volume_2d ///< three components volume
};
inline std::ostream& operator<<(std::ostream& o, const model_type& val) {
switch (val) {
#define PRINT_MODEL_TYPE(r, data, type) \
case data::type: \
o << BOOST_PP_STRINGIZE(type); \
break;
BOOST_PP_SEQ_FOR_EACH(PRINT_MODEL_TYPE, model_type, TAMAAS_MODEL_TYPES);
#undef PRINT_MODEL_TYPE
}
return o;
}
/// Trait class to store physical dimension of domain, of boundary and
/// number of components
template <model_type type>
struct model_type_traits {};
#define MODEL_TYPE_TRAITS_MACRO(type, dim, comp, bdim) \
template <> \
struct model_type_traits<model_type::type> { \
static constexpr UInt dimension = dim; \
static constexpr UInt components = comp; \
static constexpr UInt boundary_dimension = bdim; \
static std::vector<UInt> indices; \
}
MODEL_TYPE_TRAITS_MACRO(basic_1d, 1, 1, 1);
MODEL_TYPE_TRAITS_MACRO(basic_2d, 2, 1, 2);
MODEL_TYPE_TRAITS_MACRO(surface_1d, 1, 2, 1);
MODEL_TYPE_TRAITS_MACRO(surface_2d, 2, 3, 2);
MODEL_TYPE_TRAITS_MACRO(volume_1d, 2, 2, 1);
MODEL_TYPE_TRAITS_MACRO(volume_2d, 3, 3, 2);
#undef MODEL_TYPE_TRAITS_MACRO
#define ALLOC_GRID_CASE_MACRO(a, data, type) \
case data::type: { \
constexpr UInt bdim = model_type_traits<data::type>::boundary_dimension; \
constexpr UInt dim = model_type_traits<data::type>::dimension; \
ptr = new Grid < T, \
(boundary) ? bdim \
: dim > (std::begin(n), std::end(n), \
model_type_traits<data::type>::components); \
break; \
}
/// Helper function for grid allocation
template <bool boundary, typename T, typename Container>
std::unique_ptr<GridBase<T>> allocateGrid(model_type type,
Container&& n) {
GridBase<T>* ptr = nullptr;
switch (type) {
BOOST_PP_SEQ_FOR_EACH(ALLOC_GRID_CASE_MACRO, model_type,
TAMAAS_MODEL_TYPES);
}
return std::unique_ptr<GridBase<T>>{ptr};
}
#undef ALLOC_GRID_CASE_MACRO
#define ALLOC_GRID_CASE_MACRO(a, data, type) \
case data::type: { \
constexpr UInt bdim = model_type_traits<data::type>::boundary_dimension; \
constexpr UInt dim = model_type_traits<data::type>::dimension; \
ptr = new Grid < T, \
(boundary) ? bdim : dim > (std::begin(n), std::end(n), nc); \
break; \
}
/// Helper function for grid allocation with non-standard components
template <bool boundary, typename T, typename Container>
std::unique_ptr<GridBase<T>> allocateGrid(model_type type,
Container&& n, UInt nc) {
GridBase<T>* ptr = nullptr;
switch (type) {
BOOST_PP_SEQ_FOR_EACH(ALLOC_GRID_CASE_MACRO, model_type,
TAMAAS_MODEL_TYPES);
}
return std::unique_ptr<GridBase<T>>{ptr};
}
#undef ALLOC_GRID_CASE_MACRO
// clang-format off
#ifdef TAMAAS_MODEL_TYPES
#undef TAMAAS_MODEL_TYPES
#endif
#define TAMAAS_MODEL_TYPES \
(model_type::basic_1d) \
(model_type::basic_2d) \
(model_type::surface_1d) \
(model_type::surface_2d) \
(model_type::volume_1d) \
(model_type::volume_2d)
// clang-format on
__END_TAMAAS__
/* -------------------------------------------------------------------------- */
#endif // __MODEL_TYPE_HH__

Event Timeline