ETikhonov

new version

Jul 1st, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3. #include <cmath>
  4. #include <fstream>
  5. #include <string>
  6. #include <vector>
  7.  
  8. #define Nstep 3200
  9. #define A -1.
  10. #define B 1.
  11. #define MAXIT 100000000
  12. #define TOL 1e-9
  13. #define M_PI        3.14159265358979323846
  14.  
  15. std::vector<double> Set1DGrid(double a,double b, std::function<double(double)> f);
  16. void SetBC(double lower, double upper, std::vector<double> & solution);
  17. void IterativeSolver(std::vector<double> & u,std::vector<double> & rhs, double tol);
  18. void WriteFile(std::string filename, std::vector<double> f, std::vector<double> grid);
  19. double dx = (B-A)/(Nstep-1);
  20.  
  21. std::function<double(double)> Grid = [](double x) { return x; };
  22. std::function<double(double)> RHS = [](double x) { return -M_PI*exp(x)*(M_PI*exp(x)*cos(M_PI*exp(x))+sin(M_PI*exp(x))); };
  23. std::function<double(double)> FirstGuess = [](double x) { return 0; };
  24. std::function<double(double)> ExactResult = [](double x) { return cos(M_PI*exp(x)); };
  25.  
  26. int main (int argc, char *argv[]){
  27.     std::vector<double> MyGrid = Set1DGrid(A,B, Grid);
  28.     std::vector<double> rhs = Set1DGrid(A,B, RHS);
  29.     std::vector<double> u = Set1DGrid(A,B, FirstGuess);
  30.     std::vector<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.     return 0;
  36. }
  37. std::vector<double> Set1DGrid(double a,double b, std::function<double(double)> f){
  38.     std::vector<double> grid(Nstep);
  39.     for (int i=0;i<Nstep;++i)
  40.         grid[i]=f(a+i*(b-a)/(Nstep-1));
  41.     return grid;
  42. }
  43. void SetBC(double lower, double upper,std::vector<double> & solution){
  44.     solution[0]=lower;
  45.     solution[Nstep-1]=upper;
  46. }
  47. void IterativeSolver(std::vector<double> & u,std::vector<double> & rhs, double tol){
  48.     int iter=0;
  49.     std::vector<double> prevU = u;
  50.     double max;
  51.     while (iter<=MAXIT){
  52.         max=0;
  53.         for (int i=1;i<Nstep-1;++i){
  54.             u[i]=(prevU[i+1]+prevU[i-1]-dx*dx*rhs[i])*0.5;
  55.             max = fabs(u[i]-prevU[i]) > max? fabs(u[i]-prevU[i]):max;
  56.         }
  57.         if (max < tol){
  58.             std::cout<<"#Solution converged in "<<iter<<" iterations with error "<<
  59.             TOL<<std::endl;
  60.             break;
  61.         }
  62.         prevU=u;
  63.         ++iter;
  64.     }
  65. }
  66. void WriteFile(std::string filename,std::vector<double> f,std::vector<double> grid){
  67.     std::ofstream outfile;
  68.     outfile.open(filename.c_str());
  69.     if (!outfile.is_open())
  70.     {
  71.         std::cout<<"Cannot open file "<<filename<<"."<<std::endl;
  72.         return;
  73.     }
  74.     for (int i=0;i<Nstep;++i)
  75.         outfile<<grid[i]<<"  "<<f[i]<<std::endl;
  76.     outfile.close();
  77. }
Advertisement
Add Comment
Please, Sign In to add comment