mnsolver.cpp 11.9 KB
Newer Older
1
#include "solvers/mnsolver.h"
2
3
#include "common/config.h"
#include "common/io.h"
4
#include "common/mesh.h"
5
6
7
#include "entropies/entropybase.h"
#include "fluxes/numericalflux.h"
#include "optimizers/optimizerbase.h"
8
#include "problems/problembase.h"
9
10
#include "quadratures/quadraturebase.h"
#include "solvers/sphericalharmonics.h"
11
#include "toolboxes/errormessages.h"
12
#include "toolboxes/textprocessingtoolbox.h"
13

14
// externals
15
#include "spdlog/spdlog.h"
16
#include <mpi.h>
steffen.schotthoefer's avatar
steffen.schotthoefer committed
17

18
#include <fstream>
19
20
//#include <chrono>

21
MNSolver::MNSolver( Config* settings ) : Solver( settings ) {
steffen.schotthoefer's avatar
steffen.schotthoefer committed
22

23
    // Is this good (fast) code using a constructor list?
24
25
    _LMaxDegree    = settings->GetMaxMomentDegree();
    _nTotalEntries = GlobalIndex( _LMaxDegree, int( _LMaxDegree ) ) + 1;
26
27
28
29
30
31
32

    // build quadrature object and store quadrature points and weights
    _quadPoints       = _quadrature->GetPoints();
    _weights          = _quadrature->GetWeights();
    _nq               = _quadrature->GetNq();
    _quadPointsSphere = _quadrature->GetPointsSphere();
    _settings->SetNQuadPoints( _nq );
33

34
35
36
    // Initialize Scatter Matrix --
    _scatterMatDiag = Vector( _nTotalEntries, 0.0 );
    ComputeScatterMatrix();
steffen.schotthoefer's avatar
steffen.schotthoefer committed
37

38
39
40
41
42
    // Initialize Entropy
    _entropy = EntropyBase::Create( _settings );

    // Initialize Optimizer
    _optimizer = OptimizerBase::Create( _settings );
steffen.schotthoefer's avatar
steffen.schotthoefer committed
43

44
45
    // Initialize lagrange Multiplier
    _alpha = VectorVector( _nCells, Vector( _nTotalEntries, 0.0 ) );
46
47

    // Initialize and Pre-Compute Moments at quadrature points
48
    _basis = new SphericalHarmonics( _LMaxDegree );
49

50
    _moments = VectorVector( _nq, Vector( _nTotalEntries, 0.0 ) );
51
    ComputeMoments();
52
53

    // Solver output
54
    PrepareVolumeOutput();
steffen.schotthoefer's avatar
steffen.schotthoefer committed
55
56
}

57
58
59
MNSolver::~MNSolver() {
    delete _entropy;
    delete _optimizer;
60
    delete _basis;
61
}
62

63
64
65
66
67
68
69
70
71
void MNSolver::ComputeScatterMatrix() {

    // --- Isotropic ---
    _scatterMatDiag[0] = -1.0;
    for( unsigned idx_diag = 1; idx_diag < _nTotalEntries; idx_diag++ ) {
        _scatterMatDiag[idx_diag] = 0.0;
    }
}

steffen.schotthoefer's avatar
steffen.schotthoefer committed
72
73
74
75
76
77
int MNSolver::GlobalIndex( int l, int k ) const {
    int numIndicesPrevLevel  = l * l;    // number of previous indices untill level l-1
    int prevIndicesThisLevel = k + l;    // number of previous indices in current level
    return numIndicesPrevLevel + prevIndicesThisLevel;
}

78
void MNSolver::ComputeMoments() {
steffen.schotthoefer's avatar
steffen.schotthoefer committed
79
    double my, phi;
80
81

    for( unsigned idx_quad = 0; idx_quad < _nq; idx_quad++ ) {
82
83
        my  = _quadPointsSphere[idx_quad][0];
        phi = _quadPointsSphere[idx_quad][1];
84

85
        _moments[idx_quad] = _basis->ComputeSphericalBasis( my, phi );
86
    }
87
88
89
90
91
}

Vector MNSolver::ConstructFlux( unsigned idx_cell ) {

    // ---- Integration of Moment of flux ----
92
    double entropyL, entropyR, entropyFlux;
93

94
    Vector flux( _nTotalEntries, 0.0 );
95

96
    for( unsigned idx_quad = 0; idx_quad < _nq; idx_quad++ ) {
97
98
99

        entropyFlux = 0.0;    // Reset temorary flux

100
101
        entropyL = _entropy->EntropyPrimeDual( blaze::dot( _alpha[idx_cell], _moments[idx_quad] ) );

102
103
104
105
106
        for( unsigned idx_neigh = 0; idx_neigh < _neighbors[idx_cell].size(); idx_neigh++ ) {
            // Store fluxes in psiNew, to save memory
            if( _boundaryCells[idx_cell] == BOUNDARY_TYPE::NEUMANN && _neighbors[idx_cell][idx_neigh] == _nCells )
                entropyR = entropyL;
            else {
107
                entropyR = _entropy->EntropyPrimeDual( blaze::dot( _alpha[_neighbors[idx_cell][idx_neigh]], _moments[idx_quad] ) );
108
            }
109
            entropyFlux += _g->Flux( _quadPoints[idx_quad], entropyL, entropyR, _normals[idx_cell][idx_neigh] );
110
        }
111
        flux += _moments[idx_quad] * ( _weights[idx_quad] * entropyFlux );
112
113

        // ------- Relizablity Reconstruction Step ----
114
    }
115
    return flux;
116
117
}

118
119
void MNSolver::ComputeRealizableSolution( unsigned idx_cell ) {
    double entropyReconstruction = 0.0;
120
    _sol[idx_cell]               = 0;
121
    for( unsigned idx_quad = 0; idx_quad < _nq; idx_quad++ ) {
122
        // Make entropyReconstruction a member vector, s.t. it does not have to be re-evaluated in ConstructFlux
123
        entropyReconstruction = _entropy->EntropyPrimeDual( blaze::dot( _alpha[idx_cell], _moments[idx_quad] ) );
124
        _sol[idx_cell] += _moments[idx_quad] * ( _weights[idx_quad] * entropyReconstruction );
125
126
127
    }
}

128
void MNSolver::IterPreprocessing() {
steffen.schotthoefer's avatar
steffen.schotthoefer committed
129

130
    // ------- Reconstruction Step ----------------
131

132
    _optimizer->SolveMultiCell( _alpha, _sol, _moments );
133

134
135
136
137
138
    // ------- Relizablity Reconstruction Step ----
    for( unsigned idx_cell = 0; idx_cell < _nCells; idx_cell++ ) {
        ComputeRealizableSolution( idx_cell );
    }
}
139

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
void MNSolver::IterPostprocessing() {
    // --- Update Solution ---
    _sol = _solNew;

    // --- Compute Flux for solution and Screen Output ---
    ComputeRadFlux();
}

void MNSolver::ComputeRadFlux() {
    double firstMomentScaleFactor = sqrt( 4 * M_PI );
    for( unsigned idx_cell = 0; idx_cell < _nCells; ++idx_cell ) {
        _fluxNew[idx_cell] = _sol[idx_cell][0] * firstMomentScaleFactor;
    }
}

155
void MNSolver::FluxUpdate() {
156
157
158
159
    // Loop over the grid cells
    for( unsigned idx_cell = 0; idx_cell < _nCells; idx_cell++ ) {
        // Dirichlet Boundaries stay
        if( _boundaryCells[idx_cell] == BOUNDARY_TYPE::DIRICHLET ) continue;
160
        _solNew[idx_cell] = ConstructFlux( idx_cell );
161
162
    }
}
163

164
void MNSolver::FVMUpdate( unsigned idx_energy ) {
165
166
167
168
    // Loop over the grid cells
    for( unsigned idx_cell = 0; idx_cell < _nCells; idx_cell++ ) {
        // Dirichlet Boundaries stay
        if( _boundaryCells[idx_cell] == BOUNDARY_TYPE::DIRICHLET ) continue;
169

170
        for( unsigned idx_system = 0; idx_system < _nTotalEntries; idx_system++ ) {
171

172
173
174
175
176
            _solNew[idx_cell][idx_system] = _sol[idx_cell][idx_system] -
                                            ( _dE / _areas[idx_cell] ) * _solNew[idx_cell][idx_system] /* cell averaged flux */
                                            - _dE * _sol[idx_cell][idx_system] *
                                                  ( _sigmaT[idx_energy][idx_cell]                                    /* absorbtion influence */
                                                    + _sigmaS[idx_energy][idx_cell] * _scatterMatDiag[idx_system] ); /* scattering influence */
177
        }
178

179
        _solNew[idx_cell][0] += _dE * _Q[0][idx_cell][0];
steffen.schotthoefer's avatar
steffen.schotthoefer committed
180
181
    }
}
182

183
void MNSolver::PrepareVolumeOutput() {
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    unsigned nGroups = (unsigned)_settings->GetNVolumeOutput();

    _outputFieldNames.resize( nGroups );
    _outputFields.resize( nGroups );

    // Prepare all OutputGroups ==> Specified in option VOLUME_OUTPUT
    for( unsigned idx_group = 0; idx_group < nGroups; idx_group++ ) {
        // Prepare all Output Fields per group

        // Different procedure, depending on the Group...
        switch( _settings->GetVolumeOutput()[idx_group] ) {
            case MINIMAL:
                // Currently only one entry ==> rad flux
                _outputFields[idx_group].resize( 1 );
                _outputFieldNames[idx_group].resize( 1 );

200
201
                _outputFields[idx_group][0].resize( _nCells );
                _outputFieldNames[idx_group][0] = "radiation flux density";
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
                break;

            case MOMENTS:
                // As many entries as there are moments in the system
                _outputFields[idx_group].resize( _nTotalEntries );
                _outputFieldNames[idx_group].resize( _nTotalEntries );

                for( int idx_l = 0; idx_l <= (int)_LMaxDegree; idx_l++ ) {
                    for( int idx_k = -idx_l; idx_k <= idx_l; idx_k++ ) {
                        _outputFields[idx_group][GlobalIndex( idx_l, idx_k )].resize( _nCells );

                        _outputFieldNames[idx_group][GlobalIndex( idx_l, idx_k )] =
                            std::string( "u_" + std::to_string( idx_l ) + "^" + std::to_string( idx_k ) );
                    }
                }
                break;
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241

            case DUAL_MOMENTS:
                // As many entries as there are moments in the system
                _outputFields[idx_group].resize( _nTotalEntries );
                _outputFieldNames[idx_group].resize( _nTotalEntries );

                for( int idx_l = 0; idx_l <= (int)_LMaxDegree; idx_l++ ) {
                    for( int idx_k = -idx_l; idx_k <= idx_l; idx_k++ ) {
                        _outputFields[idx_group][GlobalIndex( idx_l, idx_k )].resize( _nCells );

                        _outputFieldNames[idx_group][GlobalIndex( idx_l, idx_k )] =
                            std::string( "alpha_" + std::to_string( idx_l ) + "^" + std::to_string( idx_k ) );
                    }
                }
                break;

            case ANALYTIC:
                // one entry per cell
                _outputFields[idx_group].resize( 1 );
                _outputFieldNames[idx_group].resize( 1 );
                _outputFields[idx_group][0].resize( _nCells );
                _outputFieldNames[idx_group][0] = std::string( "analytic radiation flux density" );
                break;

242
243
244
245
246
            default: ErrorMessages::Error( "Volume Output Group not defined for MN Solver!", CURRENT_FUNCTION ); break;
        }
    }
}

247
void MNSolver::WriteVolumeOutput( unsigned idx_pseudoTime ) {
248
    unsigned nGroups = (unsigned)_settings->GetNVolumeOutput();
249

250
    // Check if volume output fields are written to file this iteration
251
    if( ( _settings->GetVolumeOutputFrequency() != 0 && idx_pseudoTime % (unsigned)_settings->GetVolumeOutputFrequency() == 0 ) ||
252
        ( idx_pseudoTime == _nEnergies - 1 ) /* need sol at last iteration */ ) {
253

254
255
256
        for( unsigned idx_group = 0; idx_group < nGroups; idx_group++ ) {
            switch( _settings->GetVolumeOutput()[idx_group] ) {
                case MINIMAL:
257
                    for( unsigned idx_cell = 0; idx_cell < _nCells; ++idx_cell ) {
258
                        _outputFields[idx_group][0][idx_cell] = _fluxNew[idx_cell];
259
                    }
260
261
262
263
264
265
                    break;
                case MOMENTS:
                    for( unsigned idx_sys = 0; idx_sys < _nTotalEntries; idx_sys++ ) {
                        for( unsigned idx_cell = 0; idx_cell < _nCells; ++idx_cell ) {
                            _outputFields[idx_group][idx_sys][idx_cell] = _sol[idx_cell][idx_sys];
                        }
266
                    }
267
268
269
270
271
272
273
274
275
276
277
                    break;
                case DUAL_MOMENTS:
                    for( unsigned idx_sys = 0; idx_sys < _nTotalEntries; idx_sys++ ) {
                        for( unsigned idx_cell = 0; idx_cell < _nCells; ++idx_cell ) {
                            _outputFields[idx_group][idx_sys][idx_cell] = _alpha[idx_cell][idx_sys];
                        }
                    }
                    break;
                case ANALYTIC:
                    // Compute total "mass" of the system ==> to check conservation properties
                    for( unsigned idx_cell = 0; idx_cell < _nCells; ++idx_cell ) {
278

279
                        double time = idx_pseudoTime * _dE;
280

281
282
283
284
                        _outputFields[idx_group][0][idx_cell] = _problem->GetAnalyticalSolution(
                            _mesh->GetCellMidPoints()[idx_cell][0], _mesh->GetCellMidPoints()[idx_cell][1], time, _sigmaS[idx_pseudoTime][idx_cell] );
                    }
                    break;
285

286
287
                default: ErrorMessages::Error( "Volume Output Group not defined for MN Solver!", CURRENT_FUNCTION ); break;
            }
288
289
        }
    }
290
}
291

292
293
294
295
296
297
void MNSolver::WriteNNTrainingData( unsigned idx_pseudoTime ) {
    std::string filename = "trainNN.csv";
    std::ofstream myfile;
    myfile.open( filename, std::ofstream::app );

    for( unsigned idx_cell = 0; idx_cell < _nCells; idx_cell++ ) {
298
        myfile << 0 << ", " << _nTotalEntries << "," << idx_pseudoTime;
299
        for( unsigned idx_sys = 0; idx_sys < _nTotalEntries; idx_sys++ ) {
300
            myfile << "," << _sol[idx_cell][idx_sys];
301
        }
302
        myfile << " \n" << 1 << ", " << _nTotalEntries << "," << idx_pseudoTime;
303
        for( unsigned idx_sys = 0; idx_sys < _nTotalEntries; idx_sys++ ) {
304
            myfile << "," << _alpha[idx_cell][idx_sys];
305
        }
306
        myfile << "\n";
307
308
309
    }
    myfile.close();
}