MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
McGrawCorrection.h
Go to the documentation of this file.
1 
12 double cos_quad_alpha(double **bk, double **differenceTable, int k, int nNodes);
13 void McGrawCorrection(double *moments, int nNodes);
14 
15 
16 double cos_quad_alpha(double **bk, double **differenceTable, int k, int nNodes)
17 {
18  int n = 2*nNodes;
19  std::vector<double> bk_kthRow(n);
20  std::vector<double> dt_3rdColumn(n);
21  for (int i = 0; i < n; i++)
22  {
23  bk_kthRow[i] = bk[k][i];
24  dt_3rdColumn[i] = differenceTable[i][3];
25  }
26  double nominator = inner_product(bk_kthRow.begin(), bk_kthRow.end(), dt_3rdColumn.begin(), 0.0);
27  double denominator = vectorNorm(bk_kthRow)*vectorNorm(dt_3rdColumn);
28  return ( pow((nominator/denominator),2) );
29 }
30 
31 void McGrawCorrection(double *moments, int nNodes)
32 {
33  int n = 2*nNodes;
34  int maxIteration = 100;
35 
36  // dynamic allocation of bkk, bk and difference teble
37  double** bkk = new double*[n];
38  double** bk = new double*[n];
39  double** differenceTable = new double*[n];
40 
41  for(int i = 0; i < n; ++i)
42  {
43  bkk[i] = new double[n];
44  bk[i] = new double[n];
45  differenceTable[i] = new double[n];
46  }
47 
48  // build bkk and bk
49  for (int r = 0; r < n; r++)
50  {
51  for (int c = 0; c < n; c++)
52  {
53  bkk[r][c] = 0.0;
54  }
55  }
56  for (int k = 0; k < n; k++)
57  {
58  for (int i = 0; i < n; i++)
59  {
60  if (i == k)
61  {
62  bkk[i][0] = 1.0 ;
63  }
64  else
65  {
66  bkk[i][0] = 0.0;
67  }
68  }
69 
70  for (int j=1; j <= 3; j++)
71  {
72  for (int i = 0; i < (n-j); i++)
73  {
74  bkk[i][j]=bkk[i+1][j-1]-bkk[i][j-1];
75  }
76  }
77  // build bk
78  for (int i = 0; i < n; i+=1)
79  {
80  bk[k][i] = bkk[i][3];
81  }
82  }
83 
84  int iteration = 0;
85  int realizable = 1;
86 
87  while (realizable == 1 && iteration < maxIteration)
88  {
89  realizable = 0;
90  // initializeDifferenceTable(differenceTable, nNodes);
91  buildDifferenceTable(differenceTable, moments, nNodes);
92  realizable = isRealizable(differenceTable, nNodes);
93 
94  if ( realizable == 1 )
95  {
96  iteration++;
97  // cout << "iteration: " << iteration << endl;
98  int k_star = 1;
99 
100  for (int k = 0; k < n; k++)
101  {
102  double cqa = cos_quad_alpha(bk, differenceTable, k, nNodes);
103  double cqa_star = cos_quad_alpha(bk, differenceTable, k_star, nNodes);
104  if(cqa >= cqa_star)
105  {
106  k_star = k;
107  }
108  }
109  std::vector<double> bk_starRow(n);
110  std::vector<double> dt_3rdColumn(n);
111  for (int i = 0; i < n; i++)
112  {
113  bk_starRow[i] = bk[k_star][i];
114  dt_3rdColumn[i] = differenceTable[i][3];
115  }
116  double lnck = -( inner_product(bk_starRow.begin(), bk_starRow.end(), dt_3rdColumn.begin(), 0.0) )/( vectorNorm(bk_starRow) );
117  moments[k_star] = exp(lnck)*moments[k_star];
118  }
119  }
120 }
void buildDifferenceTable(double **differenceTable, double *mom, int nNodes)
constructs the difference table
void McGrawCorrection(double *moments, int nNodes)
McGraw correction algorithm.
double vectorNorm(vector< double > const &v)
normalized vector used in McGraw correction algorithm
int isRealizable(double **differenceTable, int nNodes)
checks if the moments are realizable