Advertisement
Guest User

Untitled

a guest
May 27th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define PARAMETER1 100
  6. #define nx1 5
  7. #define nx2 10
  8. #define nx3 10
  9. #define ny1 4
  10. #define ny2 5
  11. #define ny3 10
  12. #define Nx nx1+nx2+nx3+1
  13. #define Ny ny1+ny2+ny3+1
  14. #define X1_LENGTH 5.0    
  15. #define X2_LENGTH 10.0
  16. #define X3_LENGTH 50.0
  17. #define Y1_LENGTH 0.04
  18. #define Y2_LENGTH 5.0
  19. #define Y3_LENGTH 20.0
  20.  
  21. double f(int j,int i);
  22. double g(int j,int i);
  23. double dx(int i);
  24. double calc_value_pos(int i);
  25. double calc_value_neg(int i);
  26. double **matrix;
  27. double f1(int j,int i);
  28. double calc_value2_pos(int i);
  29. double calc_value2_neg(int i);
  30. double f2(int j,int i);
  31.  
  32. int main(void){
  33.  
  34.     matrix=(double **)malloc((size_t) ((Ny+1)*(Nx+1)+1)*sizeof(double * ));
  35.     for (int j=1; j<=(Ny+1)*(Nx+1); j++) {
  36.         matrix[j] = (double *)malloc((size_t) ((Ny+1)*(Nx+1)+1)*        sizeof(double ));
  37.     }
  38.  
  39.     for (int j=1; j<=Ny+1; j++){
  40.         for (int i=1; i<=Nx+1; i++){
  41.             matrix[j][i]=0.0;
  42.         }
  43.     }
  44.  
  45.     for (int i=0; i<=Nx; i++){
  46.         for (int j=0; j<=Ny; j++){
  47.             int k=i+(j)*(Nx+1)+1;
  48.             matrix[k][k] = f(j,i);      /*including this line causes the problem*/
  49.             //matrix[k][k] = f1(j,i);     /* including this line does not cause the problem*/
  50.         }
  51.     }
  52.     return 1;
  53. }
  54.  
  55. double f(int j, int i){
  56.     double output=f1(j,i)+f2(j,i);
  57.     return output;
  58. }
  59. double f1(int j, int i){
  60.     double output;
  61.     if (i==0 || j==0 || i==Nx || j==Ny) output=0.0;
  62.     if (i!=0 && j!= 0 && i!= Nx && j!=Ny) {
  63.         output = (g(j,i)*g(j,i+1))/(g(j,i)*calc_value_pos(i)+g(j,i+1)*calc_value_neg(i));
  64.         double output2;
  65.         output2=1.0/( (calc_value_neg(i)/g(j,i)) + (calc_value_pos(i)/g(j,i+1)));
  66.         /*if (output2!=output2) printf("output2: %lf\n",output2);*/
  67.         if (output != output2 && output==output) printf("(j%d,i%d) output:%lf \toutput2:%lf\n",j,i,output,output2);
  68.     }
  69.     if (output!=output) output=0.0;
  70.     return output;
  71. }
  72.  
  73. double f2(int j,int i){
  74.     /* calculates f2 for (j,i) */
  75.     double output;
  76.     if (j==0 || i==0) output=0.0;
  77.     else output = g(j,i)*g(j,i-1)/(g(j,i)*calc_value2_neg(i)+g(j,i-1)*calc_value2_pos(i));
  78.     if (output!=output) output=0.0;
  79.     return output;
  80. }
  81.  
  82. double calc_value2_pos(int i){
  83.     /* calculates calc_value2_pos for X=i */
  84.     double output= dx(i)/2.0;
  85.     return output;
  86. }
  87.  
  88. double calc_value2_neg(int i){
  89.     /* calculates calc_value2_neg for X=i */
  90.     if (i==0) printf("Warning off grid\n");
  91.     double output= dx(i-1)/2.0;
  92.     return output;
  93. }
  94.  
  95. double g(int j,int i){
  96.     double output=PARAMETER1;
  97.     if (j==0 || i==0) output=0.0;
  98.     if (j>=ny1+1 && j<=ny1+ny2 && i>=1 && i<=nx1) output=0.0;
  99.     if (output!=output) printf("calc_D(%d,%d) error: output==nan\n",j,i);
  100.     return output;
  101. }
  102.  
  103. double calc_value_pos(int i){
  104.     if (i==Nx) printf("Warning east pos\n");
  105.     double output;
  106.     output = dx(i+1)/2.0;
  107.     if (output!=output) printf("calc_delta_e_pos(%d) error: output==nan\n",i);
  108.     return output;
  109. }
  110.  
  111. double calc_value_neg(int i){
  112.     double output=dx(i)/2.0;
  113.     if (output!=output) printf("calc_delta_e_neg(%d) error: output==nan\n",i);
  114.     return output;
  115. }
  116.  
  117. double dx(int i){
  118.     double output;
  119.     if (i<=nx1) output=1.0*X1_LENGTH/nx1;
  120.     if (i<=nx1+nx2 && i>=nx1+1) output=1.0*X2_LENGTH/nx2;
  121.     if (i<=nx1+nx2+nx3 && i>=nx1+nx2+1) output=1.0*X3_LENGTH/nx3;
  122.     if (output!=output) printf("delta_x(%d) error: output==nan\n",i);
  123.     return output;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement