Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <cmath>
- #include <fstream>
- #include <string>
- #define Nstep 3200
- #define A -1.
- #define B 1.
- #define MAXIT 100000000
- #define TOL 1e-9
- # define M_PI 3.14159265358979323846
- double * Set1DGrid(double a,double b, double (*pf) (double));
- double MyFunc(double x);
- double RHS(double x);
- double FirstGuess(double x);
- double ExactResult(double x);
- void Copy(double *dest, double *source);
- void SetBC(double lower, double upper, double *solution);
- void PrintGrid(double *grid);
- void PrintOnGrid(double *f,double *grid);
- void IterativeSolver(double *u, double *rhs, double tol);
- void WriteFile(std::string filename, double *f, double *grid);
- double dx = (B-A)/(Nstep-1);
- int main (int argc, char *argv[]){
- double * MyGrid = Set1DGrid(A,B, MyFunc);
- double * rhs = Set1DGrid(A,B, RHS);
- double * u = Set1DGrid(A,B, FirstGuess);
- double * exact = Set1DGrid(A,B, ExactResult);
- SetBC(cos(M_PI*exp(-1)),cos(M_PI*exp(1)),u);
- IterativeSolver(u,rhs,TOL);
- WriteFile("solution.txt",u,MyGrid);
- WriteFile("exact.txt",exact,MyGrid);
- delete MyGrid;
- delete rhs;
- delete u;
- delete exact;
- return 0;
- }
- double * Set1DGrid(double a,double b, double (*pf)(double)){
- double * grid = new double[Nstep];
- for (int i=0;i<Nstep;++i)
- grid[i]=pf(a+i*(b-a)/(Nstep-1));
- return grid;
- }
- double MyFunc(double x)
- {
- return x;
- }
- double RHS(double x){
- return -M_PI*exp(x)*(M_PI*exp(x)*cos(M_PI*exp(x))+sin(M_PI*exp(x)));
- }
- double FirstGuess(double x){
- return 0;
- }
- void SetBC(double lower, double upper, double *solution){
- solution[0]=lower;
- solution[Nstep-1]=upper;
- }
- void IterativeSolver(double *u, double *rhs, double tol){
- int iter=0;
- double * prevU = new double[Nstep];
- Copy(prevU,u);
- double max;
- while (iter<=MAXIT){
- max=0;
- for (int i=1;i<Nstep-1;++i){
- u[i]=(prevU[i+1]+prevU[i-1]-dx*dx*rhs[i])*0.5;
- max = fabs(u[i]-prevU[i]) > max? fabs(u[i]-prevU[i]):max;
- }
- if (max < tol){
- std::cout<<"#Solution converged in "<<iter<<" iterations with error "<<
- TOL<<std::endl;
- break;
- }
- Copy(prevU,u);
- ++iter;
- }
- delete prevU;
- }
- void PrintGrid(double * grid){
- for (int i=0;i<Nstep;++i)
- std::cout<<grid[i]<<" ";
- std::cout<<std::endl;
- }
- void PrintOnGrid(double *f,double *grid){
- for (int i=0;i<Nstep;++i)
- std::cout<<grid[i]<<" "<<f[i]<<std::endl;
- }
- double ExactResult(double x){
- return cos(M_PI*exp(x));
- }
- void Copy(double *dest, double *source){
- for (int i=0;i<Nstep;++i)
- dest[i]=source[i];
- }
- void WriteFile(std::string filename, double *f, double *grid){
- std::ofstream outfile;
- outfile.open(filename.c_str());
- if (!outfile.is_open())
- {
- std::cout<<"Cannot open file "<<filename<<"."<<std::endl;
- return;
- }
- for (int i=0;i<Nstep;++i)
- outfile<<grid[i]<<" "<<f[i]<<std::endl;
- outfile.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment