#include <allium/main/init.hpp>
#include <allium/mesh/petsc_mesh_spec.hpp>
#include <allium/mesh/petsc_mesh.hpp>
#include <allium/ipc/comm.hpp>
#include <allium/la/linear_operator.hpp>
#include <allium/la/cg.hpp>
#include <allium/util/memory.hpp>
#include <iomanip>
using namespace allium;
using Mesh = PetscMesh<double, 2>;
using LocalMesh = PetscLocalMesh<double, 2>;
void zero_boundary(::LocalMesh& mesh)
{
auto global_range = mesh.mesh_spec()->range();
auto range = mesh.mesh_spec()->local_ghost_range();
auto val = local_mesh(mesh);
for (auto p : range) {
if (p[0] == -1
|| p[1] == -1
|| p[0] == global_range.end_pos()[0]
|| p[1] == global_range.end_pos()[1]) {
val(p[0], p[1]) = 0.0;
}
}
}
double solution(double x, double y) {
return x*x*x - 3 * x*y*y;
}
void initialize_exact_solution(Mesh& mesh, double h)
{
auto range = mesh.mesh_spec()->local_range();
auto val = local_mesh(mesh);
for (auto p : range) {
double x = p[0] * h;
double y = p[1] * h;
val(p[0], p[1]) = solution(x, y);
}
}
void initialize_boundary(::LocalMesh& mesh, double h)
{
auto global_range = mesh.mesh_spec()->range();
auto range = mesh.mesh_spec()->local_ghost_range();
auto val = local_mesh(mesh);
std::cout << global_range << std::endl;
for (auto p : range) {
if (p[0] == -1
|| p[1] == -1
|| p[0] == global_range.end_pos()[0]
|| p[1] == global_range.end_pos()[1])
{
double x = p[0] * h;
double y = p[1] * h;
val(p[0], p[1]) = solution(x, y);
} else {
val(p[0], p[1]) = 0.0;
}
}
}
void apply_laplace(Mesh& f, double h, const ::LocalMesh& u)
{
auto range = u.mesh_spec()->local_range();
auto u_val = local_mesh(u);
auto f_val = local_mesh(f);
for (auto p : range) {
f_val(p[0], p[1]) = 1.0 / (h*h) * ( 4 * u_val(p[0], p[1])
-u_val(p[0]-1, p[1])
-u_val(p[0], p[1]-1)
-u_val(p[0], p[1]+1)
-u_val(p[0]+1, p[1]));
}
}
template <typename M>
void print_mesh(const M& mesh) {
auto comm = mesh.mesh_spec()->comm();
for (int i=0; i < comm.size(); ++i) {
if (i == comm.rank()) {
std::cout << "rank " << comm.rank() << std::endl;
auto val = local_mesh(mesh);
auto range = mesh.local_range();
for (int i = range.begin_pos()[0]; i < range.end_pos()[0]; i++) {
for (int j = range.begin_pos()[1]; j < range.end_pos()[1]; ++j) {
std::cout << std::setw(10) << val(i, j) << " ";
}
std::cout << std::endl;
}
}
comm.barrier();
}
}
void initialize_rhs(double h, Mesh& rhs)
{
::LocalMesh domain(rhs.mesh_spec());
initialize_boundary(domain, h);
apply_laplace(rhs, h, domain);
rhs *= (-1.0);
}
int main(int argc, char** argv)
{
Init init(argc, argv);
const int N = 10;
double h = 1.0 / (N-1);
auto comm = Comm::world();
if (comm.rank() == 0)
std::cout << "Poisson 2D solver" << std::endl;
auto spec = std::shared_ptr<PetscMeshSpec<2>>(
new PetscMeshSpec<2>(
comm,
{DM_BOUNDARY_GHOSTED, DM_BOUNDARY_GHOSTED},
DMDA_STENCIL_STAR,
{N, N},
{PETSC_DECIDE, PETSC_DECIDE},
1,
1));
Mesh rhs(spec);
Mesh solution(spec);
CgSolver<Mesh> solver(1e-10);
auto op
= shared_copy(make_linear_operator<Mesh>(
[h](Mesh& result, const Mesh& arg) {
::LocalMesh local_arg(arg.mesh_spec());
local_arg.assign(arg);
zero_boundary(local_arg);
apply_laplace(result, h, local_arg);
}));
solver.setup(op);
initialize_rhs(h, rhs);
set_zero(solution);
solver.solve(solution, rhs);
Mesh residual(spec);
Mesh tmp1(spec);
op->apply(tmp1, solution);
residual.assign(rhs);
residual.add_scaled(-1.0, tmp1);
std::cout << "residual, l2 norm: " << residual.l2_norm() << std::endl;
Mesh error(spec);
initialize_exact_solution(error, h);
error.add_scaled(-1.0, solution);
std::cout << "l2 error: " << error.l2_norm() << std::endl;
return 0;
}