Page MenuHomec4science

grid.cpp
No OneTemporary

File Metadata

Created
Sun, May 12, 05:17

grid.cpp

/**
*
* @author Lucas Frérot <lucas.frerot@epfl.ch>
*
* @section LICENSE
*
* Copyright (©) 2016 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 "tamaas.hh"
#include "grid.hh"
#include <cstring>
#include <complex>
#include <algorithm>
/* -------------------------------------------------------------------------- */
__BEGIN_TAMAAS__
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
Grid<T, dim>::Grid():
GridBase<T>() {
this->n.fill(0);
this->strides.fill(1);
this->nb_components = 1;
}
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
Grid<T, dim>::Grid(const std::array<UInt, dim> & n,
UInt nb_components):
GridBase<T>() {
this->init(n, nb_components);
}
template <typename T, UInt dim>
Grid<T, dim>::Grid(const std::vector<UInt> & n,
UInt nb_components):
GridBase<T>() {
if (n.size() != dim)
TAMAAS_EXCEPTION("Provided sizes for grid do not match dimension");
this->init(n, nb_components);
}
template <typename T, UInt dim>
Grid<T, dim>::Grid(const std::initializer_list<UInt> & n,
UInt nb_components):
GridBase<T>() {
if (n.size() != dim)
TAMAAS_EXCEPTION("Provided sizes for grid do not match dimension");
this->init(n, nb_components);
}
template <typename T, UInt dim>
Grid<T, dim>::Grid(const Grid<T, dim> & o):
GridBase<T>(o),
n(o.n),
strides(o.strides)
{}
template <typename T, UInt dim>
Grid<T, dim>::Grid(Grid<T, dim>&& o):
GridBase<T>(o),
n(std::move(o.n)),
strides(std::move(o.strides))
{}
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
void Grid<T, dim>::resize(const std::array<UInt, dim> & n) {
this->n = n;
UInt size = this->computeSize();
this->data.resize(size);
this->data.assign(size, T(0.));
this->computeStrides();
}
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
void Grid<T, dim>::computeStrides() {
std::copy(n.begin()+1, n.end(), strides.rbegin()+2);
strides[dim] = 1;
strides[dim-1] = this->nb_components;
std::partial_sum(strides.rbegin(), strides.rend(),
strides.rbegin(), std::multiplies<UInt>());
}
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
void Grid<T, dim>::printself(std::ostream & str) const {
str << "Grid(" << dim << ", " << this->nb_components << ") {";
const auto size = this->dataSize();
for (UInt i = 0 ; i < size ; i++) {
str << (*this)(i) << ", ";
}
str << "\b\b}";
}
/* -------------------------------------------------------------------------- */
#define GRID_SCALAR_OPERATOR_IMPL(op) \
template <typename T, UInt dim> \
inline Grid<T, dim> & Grid<T, dim>::operator op(const T & e) { \
const auto size = this->dataSize(); \
_Pragma("omp parallel for") \
for (UInt i = 0 ; i < size ; i++) { \
(*this)(i) op e; \
} \
return *this; \
} \
GRID_SCALAR_OPERATOR_IMPL(+=);
GRID_SCALAR_OPERATOR_IMPL(*=);
GRID_SCALAR_OPERATOR_IMPL(-=);
GRID_SCALAR_OPERATOR_IMPL(/=);
GRID_SCALAR_OPERATOR_IMPL(=);
#undef GRID_SCALAR_OPERATOR_IMPL
/* -------------------------------------------------------------------------- */
/// Class instanciation
#define GRID_INSTANCIATE_TYPE(type) template class Grid<type, 1>; \
template class Grid<type, 2>; \
template class Grid<type, 3>
GRID_INSTANCIATE_TYPE(Real);
GRID_INSTANCIATE_TYPE(UInt);
GRID_INSTANCIATE_TYPE(Complex);
GRID_INSTANCIATE_TYPE(int);
GRID_INSTANCIATE_TYPE(bool);
GRID_INSTANCIATE_TYPE(unsigned long);
#undef GRID_INSTANCIATE_TYPE
__END_TAMAAS__

Event Timeline