Page MenuHomec4science

grid.cpp
No OneTemporary

File Metadata

Created
Sat, Jun 29, 00:31

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>
/* -------------------------------------------------------------------------- */
__BEGIN_TAMAAS__
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
Grid<T, dim>::Grid():
data() {
memset(this->n, 0, dim * sizeof(UInt));
memset(this->L, 0, dim * sizeof(Real));
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() {
memcpy(this->n, n, dim * sizeof(UInt));
memcpy(this->L, L, dim * sizeof(Real));
this->nb_components = nb_components;
data.resize(this->computeSize());
}
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
Grid<T, dim>::~Grid() {}
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
void Grid<T, dim>::resize(const UInt *n) {
memcpy(this->n, n, dim * sizeof(UInt));
UInt size = this->computeSize();
data.resize(size);
data.assign(size, T(0.));
}
/* -------------------------------------------------------------------------- */
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
/* -------------------------------------------------------------------------- */
template <UInt dim>
Grid<complex, dim> * fromHermitian(const Grid<complex, dim> &herm) {
Grid<complex, dim> * s = new Grid<complex, dim>();
memcpy(s->L, herm.L, dim * sizeof(Real));
memcpy(s->n, herm.n, (dim-1) * sizeof(Real));
s[dim-1] = 2*(herm.L[dim-1]-1);
s->nb_components = herm.nb_components;
s->resize(s->n);
UInt tuple[dim+1] = {0};
UInt reverse_tuple[dim+1] = {0};
// Loop over elements of full surface
for (complex * tracker = s->getInternalData() ;
tracker < s->end() ;
tracker++) {
// if we are in part not stored in hermitian surface
if (tuple[dim-1] > herm.n[dim-1])
(*s)(tuple) = std::conj(herm(reverse_tuple));
else
(*s)(tuple) = herm(reverse_tuple);
// update tuple
UInt mod = s->nb_components;
for (Int i = dim ; i >= 0 ; i--) {
tuple[i] = (tuple[i]+1) % mod;
if (tuple[i]) break;
else mod = s->n[i-1]; // out of bounds should be impossible here
}
// update reverse tuple (should not go out of hermitian surface)
if (tuple[dim-1] > herm.n[dim-1])
for (UInt i = 0 ; i < dim ; i++)
reverse_tuple[i] = (tuple[i])?s->n[i]-tuple[i]:0;
else
for (UInt i = 0 ; i < dim ; i++)
reverse_tuple[i] = tuple[i];
reverse_tuple[dim] = tuple[dim];
}
return s;
}
/* -------------------------------------------------------------------------- */
/// 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