/* Solves Poisson's equation with Dirichlet BC on a cubical grid of arbitrary size Processors: n */ #define MAIN_CPP #include #include #include #include #include "petscsolver.h" /* constants that would normally be imported from input card */ const int g_Ni = 100; //number of nodes per dimension const double h = 1E-4; //distance between grid points const double reltol = 1E-5; //rtol in KSPSetTolerances int main(int argc, char **argv) { MPI_Init(&argc,&argv); int size,rank; MPI_Comm_size(MPI_COMM_WORLD,&size); MPI_Comm_rank(MPI_COMM_WORLD,&rank); if (rank==0) { for (int i=0; i primes; std::priority_queue factors; int product = size; for (int i = 2; i <= product; i++){ while (product%i==0) { primes.push(i); product /= i; } } while (primes.size()<3) { primes.push(1); } for (int i=0; i<3; i++) { factors.push(primes.top()); primes.pop(); } while (!primes.empty()) { int i = factors.top(); factors.pop(); i *= primes.top(); primes.pop(); factors.push(i); } int dims[3]; //process partition in each direction, as passed to DMDACreate3d for (int i = 0; i < 3; i++) { dims[i] = factors.top(); factors.pop(); } if (rank==0) std::cout<<'\n'<=R*R || k==0 || k==g_Ni-1) { phi[idx] = 100.; charge[idx] = 0.; bound[idx] = 1; } else { phi[idx] = 0.; charge[idx] = 0.; bound[idx] = 0; } } } } /* Create and initialize solver */ PETScSolver *petscsolver; petscsolver = new PETScSolver(argc,argv,g_Ni,g_Ni,g_Ni,dims,l_Ni,l_Nj,l_Nk,Nall,h); petscsolver->init(bound); //initializes matrix /* Solve */ petscsolver->initsolve(phi,charge,reltol); /* Solve without setting options. Note that this is normally repeatedly called with altered charge array and phi array of previous timestep as initial guess */ petscsolver->solve(0,phi,charge,reltol); petscsolver->solve(1,phi,charge,reltol); delete petscsolver; delete[] bound; delete[] phi; delete[] charge; MPI_Barrier(MPI_COMM_WORLD); if (rank == 0) std::cout<<".. done"<