Page MenuHomec4science

grid.cpp
No OneTemporary

File Metadata

Created
Fri, Aug 30, 18:10

grid.cpp

//
// Created by Arnaud Pannatier on 06.05.18.
// Based on the code of :
// - Nicolas Richart <nicolas.richart@epfl.ch>
// - Vincent Keller <vincent.keller@epfl.ch>
// - Vittoria Rezzonico <vittoria.rezzonico@epfl.ch>
// See the files AUTHORS and COPYRIGHT for the concerning information
//
#include <math.h>
#include "grid.h"
Grid::Grid (std::size_t m, std::size_t n): m_x(m), m_y(n), m_grid(m * n) {}
/* -------------------------------------------------------------------------- */
void Grid::clear() { std::fill(m_grid.begin(), m_grid.end(), 0.); }
/* -------------------------------------------------------------------------- */
std::size_t Grid::m() const { return m_x; }
std::size_t Grid::n() const { return m_y; }
/* -------------------------------------------------------------------------- */
Grid& Grid::operator*= (const double a) {
for(auto& x:m_grid){
x = x*a;
}
return *this;
}
Grid& Grid::operator*= (Grid const &b) {
if(m_x != b.m() || m_y != b.n()){
throw "The Grid has the wrong size";
}
for(std::size_t x(0); x<m_x; x++){
for(std::size_t y(0); y<m_y; y++) {
get(x,y) = get(x,y) * b(x,y);
}
}
return *this;
}
Grid &Grid::operator+= (const double a) {
for(auto& x:m_grid){
x = x+a;
}
return *this;
}
Grid &Grid::operator+= (Grid const &b) {
if(m_x != b.m() || m_y != b.n()){
throw "The Grid has the wrong size";
}
for(std::size_t x(0); x<m_x; x++){
for(std::size_t y(0); y<m_y; y++) {
get(x,y) = get(x,y) + b(x,y);
}
}
return *this;
}
Grid Grid::operator- () const {
Grid ret(m_x,m_y);
for(std::size_t x(0); x<m_x; x++){
for(std::size_t y(0); y<m_y; y++) {
ret(x,y) = -get(x,y);
}
}
return ret;
}
Grid Grid::inv () const {
Grid ret(m_x,m_y);
for(std::size_t x(0); x<m_x; x++){
for(std::size_t y(0); y<m_y; y++) {
ret(x,y) = 1/get(x,y);
}
}
return ret;
}
void Grid::applyBoundaryConditions () {
for(std::size_t x(0); x<m_x; x++){
get(x,0) = get(x,1);
get(x,m_y-1) = get(x,m_y-2);
}
for(std::size_t y(0); y< m_y; y++){
get(0,y) = get(1,y);
get(m_x-1,y) = get(m_x-2,y);
}
}
void Grid::imposeTolerances (double tol, double value, const Grid& H ) {
for(std::size_t x(0); x<m_x; x++){
for(std::size_t y(0); y<m_y; y++) {
if(H(x,y)<=tol){
get(x,y) = value;
}
}
}
}
std::ostream &operator<< (std::ostream &os, const Grid & A) {
for(std::size_t x(0);x<A.m();x++) {
os << "[ ";
for (std::size_t y (0); y < A.n(); y++) {
os << A (x, y) << " ";
}
os<< "]" << std::endl;
}
return os;
}
Grid operator* (double a, Grid const &g) {
Grid ret(g);
return ret*=a;
}
Grid operator* (Grid const &a, Grid const &b) {
Grid ret(a);
return ret*=b;
}
Grid operator+ (double a, Grid const &g) {
Grid ret(g);
return ret+=a;
}
Grid operator+ (Grid const &a, Grid const &b) {
Grid ret(a);
return ret+=b;
}
Grid sqrt (Grid const &a) {
Grid ret(a.m(),a.n());
for(std::size_t x(0); x<a.m(); x++){
for(std::size_t y(0); y<a.n(); y++) {
ret(x,y) = sqrt(a(x,y));
}
}
return ret;
}
double max (Grid const &a) {
double ret(a(0,0));
for(std::size_t x(0); x<a.m(); x++){
for(std::size_t y(0); y<a.n(); y++) {
if(ret < a(x,y)){
ret = a(x,y);
}
}
}
return ret;
}
Grid pow (Grid const &a, double p) {
Grid ret(a.m(),a.n());
for(std::size_t x(0); x<a.m(); x++){
for(std::size_t y(0); y<a.n(); y++) {
ret(x,y) = pow(a(x,y),p);
}
}
return ret;
}
Grid maxAbs (Grid const &a, Grid const &b) {
if(a.m() != b.m() || a.n() != b.n()){
throw "The Grid has the wrong size";
}
Grid ret(a.m(),a.n());
for(std::size_t x(0); x<a.m(); x++){
for(std::size_t y(0); y<a.n(); y++) {
ret(x,y) = std::max(abs(a(x,y)),abs(b(x,y)));
}
}
return ret;
}
/* -------------------------------------------------------------------------- */

Event Timeline