ETikhonov

Old code

Jul 1st, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <fstream>
  5. #include <string>
  6.  
  7. #define Nstep 3200
  8. #define A -1.
  9. #define B 1.
  10. #define MAXIT 100000000
  11. #define TOL 1e-9
  12. # define M_PI       3.14159265358979323846
  13.  
  14. double * Set1DGrid(double a,double b, double (*pf) (double));
  15. double MyFunc(double x);
  16. double RHS(double x);
  17. double FirstGuess(double x);
  18. double ExactResult(double x);
  19. void Copy(double *dest, double *source);
  20. void SetBC(double lower, double upper, double *solution);
  21. void PrintGrid(double *grid);
  22. void PrintOnGrid(double *f,double *grid);
  23. void IterativeSolver(double *u, double *rhs, double tol);
  24. void WriteFile(std::string filename, double *f, double *grid);
  25. double dx = (B-A)/(Nstep-1);
  26. int main (int argc, char *argv[]){
  27.     double * MyGrid = Set1DGrid(A,B, MyFunc);
  28.     double * rhs = Set1DGrid(A,B, RHS);
  29.     double * u = Set1DGrid(A,B, FirstGuess);
  30.     double * exact = Set1DGrid(A,B, ExactResult);
  31.     SetBC(cos(M_PI*exp(-1)),cos(M_PI*exp(1)),u);
  32.     IterativeSolver(u,rhs,TOL);
  33.     WriteFile("solution.txt",u,MyGrid);
  34.     WriteFile("exact.txt",exact,MyGrid);
  35.     delete MyGrid;
  36.     delete rhs;
  37.     delete u;
  38.     delete exact;
  39.     return 0;
  40. }
  41. double * Set1DGrid(double a,double b, double (*pf)(double)){
  42.     double * grid = new double[Nstep];
  43.     for (int i=0;i<Nstep;++i)
  44.         grid[i]=pf(a+i*(b-a)/(Nstep-1));
  45.     return grid;
  46. }
  47. double MyFunc(double x)
  48. {
  49.     return x;
  50. }
  51. double RHS(double x){
  52.     return -M_PI*exp(x)*(M_PI*exp(x)*cos(M_PI*exp(x))+sin(M_PI*exp(x)));
  53. }
  54. double FirstGuess(double x){
  55.     return 0;
  56. }
  57. void SetBC(double lower, double upper, double *solution){
  58.     solution[0]=lower;
  59.     solution[Nstep-1]=upper;
  60. }
  61. void IterativeSolver(double *u, double *rhs, double tol){
  62.     int iter=0;
  63.     double * prevU = new double[Nstep];
  64.     Copy(prevU,u);
  65.     double max;
  66.     while (iter<=MAXIT){
  67.         max=0;
  68.         for (int i=1;i<Nstep-1;++i){
  69.             u[i]=(prevU[i+1]+prevU[i-1]-dx*dx*rhs[i])*0.5;
  70.             max = fabs(u[i]-prevU[i]) > max? fabs(u[i]-prevU[i]):max;
  71.         }
  72.         if (max < tol){
  73.             std::cout<<"#Solution converged in "<<iter<<" iterations with error "<<
  74.             TOL<<std::endl;
  75.             break;
  76.         }
  77.         Copy(prevU,u);
  78.         ++iter;
  79.     }
  80.     delete prevU;
  81. }
  82. void PrintGrid(double * grid){
  83.     for (int i=0;i<Nstep;++i)
  84.     std::cout<<grid[i]<<" ";
  85.     std::cout<<std::endl;
  86. }
  87. void PrintOnGrid(double *f,double *grid){
  88.     for (int i=0;i<Nstep;++i)
  89.     std::cout<<grid[i]<<" "<<f[i]<<std::endl;
  90. }
  91. double ExactResult(double x){
  92.     return cos(M_PI*exp(x));
  93. }
  94. void Copy(double *dest, double *source){
  95.     for (int i=0;i<Nstep;++i)
  96.         dest[i]=source[i];
  97. }
  98. void WriteFile(std::string filename, double *f, double *grid){
  99.     std::ofstream outfile;
  100.     outfile.open(filename.c_str());
  101.     if (!outfile.is_open())
  102.     {
  103.         std::cout<<"Cannot open file "<<filename<<"."<<std::endl;
  104.         return;
  105.     }
  106.     for (int i=0;i<Nstep;++i)
  107.         outfile<<grid[i]<<"  "<<f[i]<<std::endl;
  108.     outfile.close();
  109. }
Advertisement
Add Comment
Please, Sign In to add comment