Page MenuHomec4science

model_type.hh
No OneTemporary

File Metadata

Created
Wed, May 1, 07:25

model_type.hh

/**
* @file
* @section LICENSE
*
* Copyright (©) 2016-2020 EPFL (École Polytechnique Fédérale de Lausanne),
* Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides)
*
* 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/>.
*
*/
/* -------------------------------------------------------------------------- */
#ifndef __MODEL_TYPE_HH__
#define __MODEL_TYPE_HH__
/* -------------------------------------------------------------------------- */
#include "grid.hh"
#include "grid_base.hh"
#include "static_types.hh"
#include "tamaas.hh"
#include <algorithm>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/seq.hpp>
#include <boost/preprocessor/stringize.hpp>
#include <memory>
/* -------------------------------------------------------------------------- */
namespace 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 constexpr UInt voigt = voigt_size<comp>::value; \
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
namespace detail {
template <model_type type, bool boundary>
struct dim_choice
: std::integral_constant<
UInt, (boundary) ? model_type_traits<type>::boundary_dimension
: model_type_traits<type>::dimension> {};
} // namespace detail
template <model_type type, bool boundary, typename T, typename Container>
std::unique_ptr<Grid<T, detail::dim_choice<type, boundary>::value>>
allocateGrid(Container&& n, UInt nc) {
return std::make_unique<Grid<T, detail::dim_choice<type, boundary>::value>>(
std::begin(n), std::end(n), nc);
}
#define ALLOC_GRID_CASE_MACRO(a, data, type) \
case data::type: { \
ptr = allocateGrid<data::type, boundary, T>( \
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) {
std::unique_ptr<GridBase<T>> ptr = nullptr;
switch (type) {
BOOST_PP_SEQ_FOR_EACH(ALLOC_GRID_CASE_MACRO, model_type,
TAMAAS_MODEL_TYPES);
}
return ptr;
}
#undef ALLOC_GRID_CASE_MACRO
#define ALLOC_GRID_CASE_MACRO(a, data, type) \
case data::type: { \
ptr = allocateGrid<data::type, boundary, T>(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) {
std::unique_ptr<GridBase<T>> ptr = nullptr;
switch (type) {
BOOST_PP_SEQ_FOR_EACH(ALLOC_GRID_CASE_MACRO, model_type,
TAMAAS_MODEL_TYPES);
}
return 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
} // namespace tamaas
/* --------------------------------------------------------------------------
*/
#endif // __MODEL_TYPE_HH__

Event Timeline