Advertisement
Guest User

Untitled

a guest
May 28th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.88 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "interfaces.h"
  5.  
  6. int main(int argc, char** argv) {
  7.  int i = 0, j = 0, k;
  8.   // Storage:
  9.   // All arrays are allocated as two-dimensional arrays. Although they are dynamically
  10.   // allocated, they can be accessed as if they would be allocated statically:
  11.   //    int mien_coarse[ne_coarse][NEN];
  12.   int (*mien_coarse)[NEN] = (int(*)[NEN]) malloc(sizeof(int)*nen*ne_coarse);
  13.   double (*mxyz_coarse)[NSD] = (double(*)[NSD]) malloc(sizeof(double)*nsd*nn_coarse);
  14.   double (*data_coarse)[NDF] = (double(*)[NDF]) malloc(sizeof(double)*ndf*nn_coarse);
  15.   double (*mxyz_fine)[NSD] = (double(*)[NSD]) malloc(sizeof(double)*nsd*nn_fine);
  16.   double (*data_fine)[NDF] = (double(*)[NDF]) malloc(sizeof(double)*ndf*nn_fine);
  17.  
  18.   // Use the following variables for timing
  19.   double start_time; // Write start time to this variable
  20.   double end_time;   // Write end time to this variable
  21.  
  22.   // Read coarse mesh
  23.   read_nodes(&mxyz_coarse[0][0], nn_coarse, nsd, "Data/mxyz.coarse");
  24.   read_connectivity(&mien_coarse[0][0], ne_coarse, nen, "Data/mien.coarse");
  25.   read_data(&data_coarse[0][0], nn_coarse, ndf, "Data/pres.coarse");
  26.  
  27.   // Read fine mesh
  28.   read_nodes(&mxyz_fine[0][0], nn_fine, nsd, "Data/mxyz.fine");
  29.   check_input(mxyz_coarse, mien_coarse, data_coarse, mxyz_fine, nn_fine, nn_coarse, ne_coarse);
  30.  
  31.   // TODO: Implement the projection of the coarse grid to the fine nodes. Use the following routine:
  32.   // int check_with_tolerance(xi_eta_zeta, xe, ye, ze, xx, yy, zz, tol);
  33.   // This function should return 1 if the fine node was found in the element and 0 if it was not found.
  34.   // Use the following variables as input and output:
  35.   double xi_eta_zeta[NSD];  // The parametric coordinats (can be computed in the function)
  36.   double xe[NEN];           // The coarse element level x-coordinates
  37.   double ye[NEN];           // The coarse element level y-coordinates
  38.   double ze[NEN];           // The coarse element level z-coordinates
  39.   double xx;                // The fine node x-coordinate
  40.   double yy;                // The fine node y-coordinate
  41.   double zz;                // The fine node z-coordinate
  42.   double tol;               // A tolerance which can be used in the function
  43.  
  44.   // YOUR CODE STARTS HERE
  45.   double xxbar;
  46.   double yybar;
  47.   double zzbar;
  48.   double xbar[4];
  49.   double ybar[4];
  50.   double zbar[4];
  51.   int n1, n2, n3, n4;
  52.   int count;
  53.   tol = 0.001;
  54.   for(i = 0; i < nn_fine; i++){
  55.     xx = mxyz_fine[i][0];
  56.     yy = mxyz_fine[i][1];
  57.     zz = mxyz_fine[i][2];
  58.     for(j = 0; j < ne_coarse; j++){
  59.       //printf("\t[%d]\n", j);
  60.       n1 = mien_coarse[j][0]-1;
  61.       n2 = mien_coarse[j][1]-1;
  62.       n3 = mien_coarse[j][2]-1;
  63.       n4 = mien_coarse[j][3]-1;
  64.       xe[0] = mxyz_coarse[n1][0];
  65.       xe[1] = mxyz_coarse[n2][0];
  66.       xe[2] = mxyz_coarse[n3][0];
  67.       xe[3] = mxyz_coarse[n4][0];
  68.       ye[0] = mxyz_coarse[n1][1];
  69.       ye[1] = mxyz_coarse[n2][1];
  70.       ye[2] = mxyz_coarse[n3][1];
  71.       ye[3] = mxyz_coarse[n4][1];
  72.       ze[0] = mxyz_coarse[n1][2];
  73.       ze[1] = mxyz_coarse[n2][2];
  74.       ze[2] = mxyz_coarse[n3][2];
  75.       ze[3] = mxyz_coarse[n4][2];
  76.  
  77.       //printf("%lf\n%lf\n%lf", xe[0] ,xe[1], xe[2]);
  78.  
  79.       if(check_with_tolerance(xi_eta_zeta, xe, ye, ze, xx, yy, zz, tol) == 1){
  80.     for(k = 0; k < ndf; ++k){ // Loop over degrees of freedom and interpolate
  81.                   // TODO: Interpolate the data_fine[i][k]. This is the k-th degree of freedom of the i-th node.
  82.                   // In this project the number of degrees of freedom is 1. Use for interpolation the function
  83.       double coarse_data[NEN]; // Store the coarse element-level data values in this array
  84.  
  85.       // YOUR CODE STARTS HERE
  86.       coarse_data[0] = data_coarse[n1][0];
  87.       coarse_data[1] = data_coarse[n2][0];
  88.       coarse_data[2] = data_coarse[n3][0];
  89.       coarse_data[3] = data_coarse[n4][0];
  90.                  
  91.       data_fine[i][k] = interpolate_data(xi_eta_zeta, coarse_data);
  92.       count++;
  93.     }
  94.     break;
  95.     printf("node:%d\tcount:%d\n",i,count);
  96.     // YOUR CODE STARTS HERE
  97.       }
  98.       else{
  99.     //printf("\t\t\t\tno![%d]\n", j);      
  100.     continue;
  101.       }
  102.     }  
  103.   }
  104.   for(i = 0; i < 11; i++){
  105.     printf("\t\t%lf\n", data_fine[i][k]);
  106.   }
  107.   // DO NOT CHANGE THE FOLLOWING LINES
  108.  
  109.   // Output
  110.   write_data(&data_fine[0][0], nn_fine, ndf, "Data/pres.fine");
  111.   check_output(data_fine, nn_fine, tol);
  112.   //check_data(&data_fine[0][0], nn_fine, ndf, "Data/pres.fine");
  113.  
  114.   /*for(i = 0; i < nn_fine; i++){
  115.     printf("%lf\n", data_fine[i][0]);
  116.     }*/
  117.  
  118.   // Deallocate memory
  119.   free(mien_coarse);
  120.   free(mxyz_coarse);
  121.   free(mxyz_fine);
  122.   free(data_coarse);
  123.   free(data_fine);
  124.  
  125.   // Timing output
  126.   double dt = end_time - start_time;
  127.   unsigned nn = nn_fine;
  128.   printf("TIMING OUTPUT:\n");
  129.   printf("--------------\n");
  130.   printf("  Needed %e seconds to project %u nodes.\n", dt, nn);
  131.   printf("The tolerance for interpolating all nodes was: %e\n", tol);
  132.  
  133.   return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement