NSSC/Exercise_01/main.cpp

91 lines
2.8 KiB
C++

/**
* g++ main.cpp -std=c++17 -Wall -Wpedantic -pedantic -o main; ./main
*/
#include <stddef.h>
#include <iostream>
#include <functional>
#define _USE_MATH_DEFINES /* enables math constants from cmath */
#include <cmath>
#ifdef USE_MPI
#include <mpi.h>
#endif
#include "Matrix.h"
#include "Solver.h"
int main(int argn, char* argv[]) {
/******************************* MPI Setup ********************************/
#ifdef USE_MPI
// Initialize MPI
MPI_Init(nullptr, nullptr);
// Get MPI config
int mpi_size; /*< MPI pool size (a.k.a. total number of processes) */
int mpi_rank; /*< MPI rank (a.k.a. process ID in the context of MPI) */
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
#endif
/**************************** Parse Arguments *****************************/
if (argn < 3) {
std::cerr << "usage: " << argv[0] << " <resolution> <iterations>" << std::endl;
return -1;
} else if (argn > 3) {
std::cerr << "warning: " << "ignoring all but the first two params" << std::endl;
}
// TODO: make this proper!!!
size_t resolution = atol(argv[1]);
size_t iterations = atol(argv[2]);
if (resolution < 1 || resolution > 65536
|| iterations < 1 || iterations > 65536) {
std::cerr << "error: parsing arguments failed" << std::endl;
}
/************************* Initialize PDE Solver **************************/
size_t nx = resolution;
size_t ny = resolution;
const double k = M_PI;
const double h = 1.0 / static_cast<double>(resolution - 1);
// Declare right hand side function f(x, y) = k^2 sin(2 pi x) sinh(2 pi y)
std::function<double(double, double)> fun = [k](double x, double y) {
return k * k * sin(M_2_PI * x) * sinh(M_2_PI * y);
};
// Boundary conditions
/** North boundary condition g(x) = k^2 sin(2 pi x) sinh(2 pi) */
std::function<double(double)> gN = [k](double x) {
return k * k * sin(M_2_PI * x) * sinh(M_2_PI);
};
/** East, South and West boundary conditions are simply = 0 */
std::function<double(double)> g0 = [k](double) { return 0.0; };
/******************************* Solve PDE ********************************/
Matrix<double> solution;
{
// Instanciate solver (local instance)
Solver solver(nx, ny, 0., 1., 0., 1., h, k, fun, gN, g0, g0, g0);
// Run solver iterations
for (size_t iter = 0; iter < iterations; ++iter) {
solver.iterate();
}
// extract solution
solution = std::move(solver.solution());
}
/****************************** Tests/Report ******************************/
std::cout << solution << std::endl;
// MPI shutdown/cleanup
#ifdef USE_MPI
MPI_Finalize();
#endif
return 0;
}