Page MenuHomec4science

grid.cpp
No OneTemporary

File Metadata

Created
Wed, Nov 13, 21:38

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 (int m, int n): m_x(m), m_y(n), m_grid(m * n) {}
/* -------------------------------------------------------------------------- */
void Grid::clear() { std::fill(m_grid.begin(), m_grid.end(), 0.); }
/* -------------------------------------------------------------------------- */
int Grid::x() const { return m_x; }
int Grid::y() 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.x() || m_y != b.y()){
throw "The Grid has the wrong size";
}
for(int x(0); x<m_x; x++){
for(int 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.x() || m_y != b.y()){
throw "The Grid has the wrong size";
}
for(int x(0); x<m_x; x++){
for(int 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(int x(0); x<m_x; x++){
for(int 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(int x(0); x<m_x; x++){
for(int y(0); y<m_y; y++) {
ret(x,y) = 1/get(x,y);
}
}
return ret;
}
void Grid::applyBoundaryConditions () {
for(int x(0); x<m_x; x++){
get(x,0) = get(x,1);
get(x,m_y-1) = get(x,m_y-2);
}
for(int 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(int x(0); x<m_x; x++){
for(int 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(int x(0);x<A.x();x++) {
os << "[ ";
for (int y (0); y < A.y(); y++) {
os << A (x, y) << " ";
}
os<< "]" << std::endl;
}
return os;
}
void Grid::resize (int nx, int ny) {
m_x = nx;
std::cout << "NX GRID : " << nx << " m_x : " << m_x << std::endl;
m_x = ny;
m_grid.resize(nx*ny);
}
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.x(),a.y());
for(int x(0); x<a.x(); x++){
for(int y(0); y<a.y(); y++) {
ret(x,y) = sqrt(a(x,y));
}
}
return ret;
}
double max (Grid const &a) {
double ret(a(0,0));
for(int x(0); x<a.x(); x++){
for(int y(0); y<a.y(); y++) {
if(ret < a(x,y)){
ret = a(x,y);
}
}
}
return ret;
}
Grid pow (Grid const &a, double p) {
Grid ret(a.x(),a.y());
for(int x(0); x<a.x(); x++){
for(int y(0); y<a.y(); y++) {
ret(x,y) = pow(a(x,y),p);
}
}
return ret;
}
Grid maxAbs (Grid const &a, Grid const &b) {
if(a.x() != b.x() || a.y() != b.y()){
throw "The Grid has the wrong size";
}
Grid ret(a.x(),a.y());
for(int x(0); x<a.x(); x++){
for(int y(0); y<a.y(); y++) {
ret(x,y) = std::max(abs(a(x,y)),abs(b(x,y)));
}
}
return ret;
}
/* -------------------------------------------------------------------------- */

Event Timeline