summaryrefslogtreecommitdiff
path: root/src/utilities/anisoDiscr.h
blob: a509541b5a45b44324d1ae3847e24faaa47e6418 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*  This file is part of the OpenLB library
 *
 *  Copyright (C) 2018 Julius Woerner, Albert Mink
 *  E-mail contact: info@openlb.net
 *  The most recent release of OpenLB can be downloaded at
 *  <http://www.openlb.net/>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this program; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA  02110-1301, USA.
*/


/** \file
 * discretization of anisotrphic phasefunction
 * DOI 10.1016/j.ijheatmasstransfer.2011.11.009
 *
 */


#include <iostream>
#include <vector>
#include <cmath>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include <string>
#include <math.h>
#ifdef FEATURE_OPENBLAS
#include <openblas/lapacke.h>
#endif

#include "utilities/vectorHelpers.h"

namespace olb {

/** Function to compute Henyey Greenstein phase funtion
 * \param cosTheta cos(theta) of scattering event with net direction theta
 * \param g anisotropy factor
 */
double henyeyGreenstein(double cosTheta, double g)
{
  return (1-g*g) / std::pow(1+g*g-2*g*cosTheta ,1.5);
}

// check for energy conservation
template< int q, typename DESCRIPTOR>
std::array<double,q> testEnergyConservationColumn( const std::array<std::array<double,q>,q>&  phi )
{
  std::array<double,q> discIntegral;
  for( int iVec = 0; iVec < q; iVec++ ) {
    discIntegral[iVec] = 0;
    for( int iSum = 0; iSum < q; iSum++ ) {
      discIntegral[iVec] += descriptors::t<double,DESCRIPTOR>(iSum) * phi[iSum][iVec];
    }
  }
  return discIntegral;
}

// check for energy conservation
template<int q, typename DESCRIPTOR>
std::array<double,q> testEnergyConservationRow( const std::array<std::array<double,q>,q>& phi )
{
  std::array<double,q> discIntegral;
  for( int iVec = 0; iVec < q; iVec++ ) {
    discIntegral[iVec] = 0;
    for( int iSum = 0; iSum < q; iSum++ ) {
      discIntegral[iVec] += descriptors::t<double,DESCRIPTOR>(iSum) * phi[iVec][iSum];
    }
  }
  return discIntegral;
}

// check for anisotropy conservation
template< int q>
std::array<double,q> testAnisotropyConservationColumn( const std::array<std::array<double,q>,q>&  phi,
  const double weights[q], std::array<std::array<double,q>,q>& cosTheta)
{
  std::array<double,q> discIntegral;
  for( int iVec = 0; iVec < q; iVec++ ) {
    discIntegral[iVec] = 0;
    for( int iSum = 0; iSum < q; iSum++ ) {
      discIntegral[iVec] += weights[iSum] * cosTheta[iVec][iSum]* phi[iVec][iSum];
    }
  }
  return discIntegral;
}


/** Computes vector [a, a+stepsize, a+2*stepsize, ..., b]
 * \param stepsize
 * \param a
 * \param b
 */
std::vector<double> linespace( double const stepsize, double const a, double const b )
{
  std::vector<double> linspace{}; // initalize to empty
  if( util::nearZero( a-b ) ) {
    return linspace;
  }
  int const h = (int) ( (b-a) / stepsize);
  for (int n = 0; n <= h; n++) {
    linspace.push_back( a +double(n)/h * b );
  }
  return linspace;
}

/**
 * \tparam DESCRIPTOR a lattice stencil
 * \tparam q number of lattice stencils MINUS one, 0th direction not needed
 * \param stepsize choose precision
 * \param anisotropyFactor also known as g
 * \param solution for debugging
 * \param phi is anisotropy matrix(sym), element [i][j] probability of scattering from i into j (direction)
 * \param breakAfter to limit endless iteration for testing
 */
template<typename DESCRIPTOR>
void computeAnisotropyMatrix( double const stepsize, double const anisotropyFactor,
  double solution[(DESCRIPTOR::q-1)*((DESCRIPTOR::q-1)+1)/2],
  std::array<std::array<double,DESCRIPTOR::q-1>, DESCRIPTOR::q-1>& phi, int const breakAfter = -1)
{
  using namespace descriptors;

  OstreamManager clout( std::cout, "AnisotropyMatrix" );
  clout << "Call AnistorpiyMatrix()" << std::endl;
#ifdef FEATURE_OPENBLAS
  clout << "Compute anisotropy matrix ..." << std::endl;
  typedef DESCRIPTOR L;
  int const q = L::q-1;
  int const mm = 2*q;
  int const nn = (q+1)*q/2;

  // qxq matrix 0th row and 0th column are empty
  std::vector<std::vector<double>> angleProb;
  angleProb.resize(q);
  for ( int n = 0; n < q; n++ ) {
    angleProb[n].resize(q);
  }
  double dotProduct;
  double normI;
  double normJ;
  for (int iPop=0; iPop<q; iPop++) {
    for (int jPop=0; jPop<q; jPop++) {
      // shift by 1 due to notation in DESCRIPTOR/DESCRIPTOR
      // exclude 0th direction
      dotProduct = c<L>(iPop+1)*c<L>(jPop+1);
      normI = std::sqrt( c<L>(iPop+1)*c<L>(iPop+1) );
      normJ = std::sqrt( c<L>(jPop+1)*c<L>(jPop+1) );
      angleProb[iPop][jPop] = dotProduct / (normI*normJ);
    }
  }

  for (int i=0; i<q; i++) {
    for (int j=0; j<q; j++) {
      phi[i][j]=1.0;
    }
  }

  for( int i = 0; i < nn; i++ ) {
    solution[i] = 0;
  };

  int iter;
  double U[mm][nn];
  double U_array[nn*mm];
  std::vector<double> anisoIterVector = linespace( stepsize, 0, anisotropyFactor );

  // additional condition only for unit testing
  size_t index = 0;
  for ( ; index < anisoIterVector.size() && index != (std::size_t)(breakAfter); index++)
  {
    // wipe matrices and vectors
    for (int m = 0; m < mm; m++) {
      for (int n = 0; n < nn; n++) {
        U[m][n] = 0;
      }
    }

    // compute matrix U, iter handels symmetry
    iter = 0;
    for (int m=0; m<q; m++) {
      for (int n=m; n<q; n++) {
        U[m][iter]   = t<double,L>(m+1)*phi[n][m];
        U[n][iter]   = t<double,L>(n+1)*phi[m][n];
        U[m+q][iter] = t<double,L>(m+1)*phi[n][m]*angleProb[n][m];
        U[n+q][iter] = t<double,L>(n+1)*phi[m][n]*angleProb[m][n];
        iter++;
      }
    }

    double sum1;
    double sum2;
    // compute vector b
    for (int n=0; n<q; n++) {
      // get sum
      sum1 = 0;
      sum2 = 0;
      for (int k=0; k<q; k++) {
        sum1 += t<double,L>(k+1)*phi[k][n];
        sum2 += t<double,L>(