mnsolver.cpp 7.9 KB
Newer Older
1
#include "solvers/mnsolver.h"
2
#include "toolboxes/textprocessingtoolbox.h"
3
//#include <chrono>
4
#include <mpi.h>
steffen.schotthoefer's avatar
steffen.schotthoefer committed
5

6
MNSolver::MNSolver( Config* settings ) : Solver( settings ), _nMaxMomentsOrder( settings->GetMaxMomentDegree() ), _basis( _nMaxMomentsOrder ) {
steffen.schotthoefer's avatar
steffen.schotthoefer committed
7
8
    // Is this good (fast) code using a constructor list?

9
    _nTotalEntries = GlobalIndex( _nMaxMomentsOrder, int( _nMaxMomentsOrder ) ) + 1;
10
    _quadrature    = QuadratureBase::CreateQuadrature( _settings->GetQuadName(), settings->GetQuadOrder() );
11

steffen.schotthoefer's avatar
steffen.schotthoefer committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    // transform sigmaT and sigmaS in sigmaA.
    _sigmaA = VectorVector( _nEnergies, Vector( _nCells, 0 ) );    // Get rid of this extra vektor!

    for( unsigned n = 0; n < _nEnergies; n++ ) {
        for( unsigned j = 0; j < _nCells; j++ ) {
            _sigmaA[n][j] = 0;    //_sigmaT[n][j] - _sigmaS[n][j];
            _sigmaS[n][j] = 1;
        }
    }

    // Initialize Scatter Matrix
    _scatterMatDiag    = Vector( _nTotalEntries, 1.0 );
    _scatterMatDiag[0] = 0.0;    // First entry is zero by construction.

26
27
28
29
30
    // Initialize Entropy
    _entropy = EntropyBase::Create( _settings );

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

32
33
34
35
36
    // Initialize lagrange Multipliers
    _alpha = VectorVector( _nCells );
    for( unsigned idx_cells = 0; idx_cells < _nTotalEntries; idx_cells++ ) {
        _alpha[idx_cells] = Vector( _nTotalEntries, 0.0 );
    }
37
38
39
40

    // Initialize and Pre-Compute Moments at quadrature points
    _moments = VectorVector( _nq );
    ComputeMoments();
steffen.schotthoefer's avatar
steffen.schotthoefer committed
41
42
}

43
44
45
46
47
MNSolver::~MNSolver() {
    delete _quadrature;
    delete _entropy;
    delete _optimizer;
}
48

steffen.schotthoefer's avatar
steffen.schotthoefer committed
49
50
51
52
53
54
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;
}

55
56
57
Vector MNSolver::ConstructFlux( unsigned idx_cell, unsigned idx_neigh ) {

    // ---- Integration of Moment of flux ----
58
59
60
    double x, y, w, entropy, velocity;
    // double z;

61
62
    Vector flux( _nTotalEntries, 0.0 );
    for( unsigned idx_quad = 0; idx_quad < _nq; idx_quad++ ) {
63
64
65
66
        x = _quadrature->GetPoints()[idx_quad][0];
        y = _quadrature->GetPoints()[idx_quad][1];
        // z = _quadrature->GetPoints()[idx_quad][2];
        w = _quadrature->GetWeights()[idx_quad];
67
68

        // --- get upwinding direction (2D)
69
        // DOUBLE CHECK IF WE NEED DIMENSION SPLITTING!!!
70
71
72
73

        velocity = _normals[idx_cell][idx_neigh][0] * x + _normals[idx_cell][idx_neigh][1] * y;

        // Perform upwinding
74
75
        ( velocity > 0 ) ? ( entropy = _entropy->EntropyPrimeDual( blaze::dot( _alpha[idx_cell], _moments[idx_quad] ) ) )
                         : ( entropy = _entropy->EntropyPrimeDual( blaze::dot( _alpha[idx_neigh], _moments[idx_quad] ) ) );
76
77

        // integrate
78
        flux += _moments[idx_quad] * ( w * velocity * entropy );
79
    }
steffen.schotthoefer's avatar
steffen.schotthoefer committed
80

81
82
83
    return flux;
}

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
void MNSolver::ComputeMoments() {
    double x, y, z, w;

    for( unsigned idx_quad = 0; idx_quad < _nq; idx_quad++ ) {
        x                  = _quadrature->GetPoints()[idx_quad][0];
        y                  = _quadrature->GetPoints()[idx_quad][1];
        z                  = _quadrature->GetPoints()[idx_quad][2];
        _moments[idx_quad] = _basis.ComputeSphericalBasis( x, y, z );
    }

    Vector results( _moments[0].size(), 0.0 );

    // Normalize Moments

    for( unsigned idx_quad = 0; idx_quad < _nq; idx_quad++ ) {
        x = _quadrature->GetPoints()[idx_quad][0];
        y = _quadrature->GetPoints()[idx_quad][1];
        z = _quadrature->GetPoints()[idx_quad][2];
        w = _quadrature->GetWeights()[idx_quad];

        for( unsigned idx_sys = 0; idx_sys < 9; idx_sys++ ) {
            results[idx_sys] += w * _moments[idx_quad][idx_sys] * _moments[idx_quad][idx_sys];
        }
    }

    for( unsigned idx_quad = 0; idx_quad < _nq; idx_quad++ ) {
        for( unsigned idx_sys = 0; idx_sys < 9; idx_sys++ ) {
111
            _moments[idx_quad][idx_sys] /= sqrt( results[idx_sys] );
112
113
114
115
        }
    }
}

steffen.schotthoefer's avatar
steffen.schotthoefer committed
116
117
118
119
120
121
122
123
124
125
126
127
void MNSolver::Solve() {

    int rank;
    MPI_Comm_rank( MPI_COMM_WORLD, &rank );

    auto log = spdlog::get( "event" );

    // angular flux at next time step (maybe store angular flux at all time steps, since time becomes energy?)
    VectorVector psiNew = _psi;
    double dFlux        = 1e10;
    Vector fluxNew( _nCells, 0.0 );
    Vector fluxOld( _nCells, 0.0 );
steffen.schotthoefer's avatar
steffen.schotthoefer committed
128
129

    double mass1 = 0;
steffen.schotthoefer's avatar
steffen.schotthoefer committed
130
131
    for( unsigned i = 0; i < _nCells; ++i ) {
        _solverOutput[i] = _psi[i][0];
steffen.schotthoefer's avatar
steffen.schotthoefer committed
132
        mass1 += _psi[i][0] * _areas[i];
steffen.schotthoefer's avatar
steffen.schotthoefer committed
133
134
135
136
137
    }

    if( rank == 0 ) log->info( "{:10}   {:10}", "t", "dFlux" );
    if( rank == 0 ) log->info( "{:03.8f}   {:01.5e} {:01.5e}", -1.0, dFlux, mass1 );

138
139
140
141
    // Time measurement
    // auto start = chrono::steady_clock::now();
    // auto end   = chrono::steady_clock::now();

steffen.schotthoefer's avatar
steffen.schotthoefer committed
142
143
    // Loop over energies (pseudo-time of continuous slowing down approach)

144
    for( unsigned idx_energy = 0; idx_energy < _nEnergies; idx_energy++ ) {
steffen.schotthoefer's avatar
steffen.schotthoefer committed
145

146
147
        // Loop over the grid cells
        for( unsigned idx_cell = 0; idx_cell < _nCells; idx_cell++ ) {
steffen.schotthoefer's avatar
steffen.schotthoefer committed
148

149
150
151
152
153
            // Reset temporary variable
            for( unsigned idx_sys = 0; idx_sys < _nTotalEntries; idx_sys++ ) {
                psiNew[idx_cell][idx_sys] = 0.0;
            }

154
155
156
157
            // ------- Reconstruction Step -------

            _alpha[idx_cell] = _optimizer->Solve( _psi[idx_cell] );

158
            // ------- Flux Computation Step ---------
159
160
161
162
163

            // Loop over neighbors
            for( unsigned idx_neigh = 0; idx_neigh < _neighbors[idx_cell].size(); idx_neigh++ ) {
                // Store fluxes in psiNew, to save memory
                psiNew[idx_cell] += ConstructFlux( idx_cell, idx_neigh );
164
                //   std::cout << " psiNew[" << idx_cell << "] " << psiNew[idx_cell] << "\n";
steffen.schotthoefer's avatar
steffen.schotthoefer committed
165
166
            }

167
168
            // std::cout << " TOTAL psiNew[" << idx_cell << "] " << psiNew[idx_cell] << "\n";

169
            // ------ FVM Step ------
170

171
172
173
            // NEED TO VECTORIZE
            for( unsigned idx_system = 0; idx_system < _nTotalEntries; idx_system++ ) {

174
175
176
177
178
179
                psiNew[idx_cell][idx_system] =
                    _psi[idx_cell][idx_system] - ( _dE / _areas[idx_cell] ) * psiNew[idx_cell][idx_system]; /* cell averaged flux */

                //- _dE * _psi[idx_cell][idx_system] *
                //      ( _sigmaA[idx_energy][idx_cell]                                    /* absorbtion influence */
                //        + _sigmaS[idx_energy][idx_cell] * _scatterMatDiag[idx_system] ); /* scattering influence */
180
            }
steffen.schotthoefer's avatar
steffen.schotthoefer committed
181
        }
182
183
184
        _psi = psiNew;

        // pseudo time iteration output
185
186
187
188
189
        double mass = 0.0;
        for( unsigned idx_cell = 0; idx_cell < _nCells; ++idx_cell ) {
            fluxNew[idx_cell]       = _psi[idx_cell][0];    // zeroth moment is raditation densitiy we are interested in
            _solverOutput[idx_cell] = _psi[idx_cell][0];
            mass += _psi[idx_cell][0] * _areas[idx_cell];
steffen.schotthoefer's avatar
steffen.schotthoefer committed
190
        }
191
192

        Save( idx_energy );
steffen.schotthoefer's avatar
steffen.schotthoefer committed
193
194
        dFlux   = blaze::l2Norm( fluxNew - fluxOld );
        fluxOld = fluxNew;
195
        if( rank == 0 ) log->info( "{:03.8f}   {:01.5e} {:01.5e}", _energies[idx_energy], dFlux, mass );
steffen.schotthoefer's avatar
steffen.schotthoefer committed
196
197
    }
}
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217

void MNSolver::Save() const {
    std::vector<std::string> fieldNames{ "flux" };
    std::vector<double> flux;
    flux.resize( _nCells );

    for( unsigned i = 0; i < _nCells; ++i ) {
        flux[i] = _psi[i][0];
    }
    std::vector<std::vector<double>> scalarField( 1, flux );
    std::vector<std::vector<std::vector<double>>> results{ scalarField };
    ExportVTK( _settings->GetOutputFile(), results, fieldNames, _mesh );
}

void MNSolver::Save( int currEnergy ) const {
    std::vector<std::string> fieldNames{ "flux" };
    std::vector<std::vector<double>> scalarField( 1, _solverOutput );
    std::vector<std::vector<std::vector<double>>> results{ scalarField };
    ExportVTK( _settings->GetOutputFile() + "_" + std::to_string( currEnergy ), results, fieldNames, _mesh );
}