Page MenuHomec4science

grid.cpp
No OneTemporary

File Metadata

Created
Tue, Jun 4, 05:14

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():
data() {
std::fill(this->n, this->n+dim, 0);
std::fill(this->L, this->L+dim, 0);
this->nb_components = 1;
}
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
Grid<T, dim>::Grid(const UInt n[dim], const Real L[dim], UInt nb_components):
data() {
std::copy(n, n+dim, this->n);
std::copy(L, L+dim, this->L);
this->nb_components = nb_components;
this->resize(this->n);
}
template <typename T, UInt dim>
Grid<T, dim>::Grid(std::initializer_list<UInt> n,
std::initializer_list<Real> L,
UInt nb_components) :
data() {
TAMAAS_ASSERT(n.size() == dim && L.size() == dim,
"Initializer lists have wrong size");
std::copy(n.begin(), n.end(), this->n);
std::copy(L.begin(), L.end(), this->L);
this->nb_components = nb_components;
this->resize(this->n);
}
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
Grid<T, dim>::~Grid() {}
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
void Grid<T, dim>::resize(const UInt *n) {
if (n != this->n)
std::copy(n, n+dim, this->n);
UInt size = this->computeSize();
data.resize(size);
data.assign(size, T(0.));
}
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
void Grid<T, dim>::resize(std::initializer_list<UInt> n) {
TAMAAS_ASSERT(n.size() == dim, "Initializer list has wrong size");
UInt n_[dim] = {0};
std::copy(n.begin(), n.end(), n_);
resize(n_);
}
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
void Grid<T, dim>::uniformSetComponents(const Grid<T, 1> &vec) {
TAMAAS_ASSERT(vec.dataSize() == this->nb_components,
"Cannot set grid field with values of vector");
#pragma omp parallel for
for (UInt i = 0 ; i < dataSize() ; i++) {
data[i] = vec(i % nb_components);
}
}
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
void Grid<T, dim>::printself(std::ostream & str) const {
str << "Grid(" << dim << ", " << nb_components << ") {";
for (UInt i = 0 ; i < data.size() - 1 ; i++) {
str << data[i] << ", ";
}
str << data[data.size()-1] << "}";
}
/* -------------------------------------------------------------------------- */
#define GRID_SCALAR_OPERATOR_IMPL(op) \
template <typename T, UInt dim> \
inline void Grid<T, dim>::operator op(const T & e) { \
_Pragma("omp parallel for") \
for (UInt i = 0 ; i < this->data.size() ; i++) { \
data[i] op e; \
} \
} \
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