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

5
6
//#include <chrono>

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

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

steffen.schotthoefer's avatar
steffen.schotthoefer committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    // 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.

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

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

33
34
    // Initialize lagrange Multiplier
    _alpha = VectorVector( _nCells, Vector( _nTotalEntries, 0.0 ) );
35
36

    // Initialize and Pre-Compute Moments at quadrature points
37
    _moments = VectorVector( _nq, Vector( _nTotalEntries, 0.0 ) );
38
    ComputeMoments();
steffen.schotthoefer's avatar
steffen.schotthoefer committed
39
40
}

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

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

53
void MNSolver::ComputeMoments() {
steffen.schotthoefer's avatar
steffen.schotthoefer committed
54
    double my, phi;
55
56

    for( unsigned idx_quad = 0; idx_quad < _nq; idx_quad++ ) {
57
58
        my  = _quadrature->GetPointsSphere()[idx_quad][0];
        phi = _quadrature->GetPointsSphere()[idx_quad][1];
59
60

        _moments[idx_quad] = _basis.ComputeSphericalBasis( my, phi );
61
    }
62
63
64
65
66
67
}

Vector MNSolver::ConstructFlux( unsigned idx_cell ) {

    // ---- Integration of Moment of flux ----
    double w, entropyL, entropyR, entropyFlux;
68

69
    Vector flux( _nTotalEntries, 0.0 );
70
71
    Vector omega( 3, 0.0 );

72
    for( unsigned idx_quad = 0; idx_quad < _nq; idx_quad++ ) {
73
74
75
76
77

        w = _quadrature->GetWeights()[idx_quad];

        entropyFlux = 0.0;    // Reset temorary flux

78
79
80
81
        entropyL = _entropy->EntropyPrimeDual( blaze::dot( _alpha[idx_cell], _moments[idx_quad] ) );

        omega = _quadrature->GetPoints()[idx_quad];    // Use Pointer!

82
83
84
85
86
        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 {
87
                entropyR = _entropy->EntropyPrimeDual( blaze::dot( _alpha[_neighbors[idx_cell][idx_neigh]], _moments[idx_quad] ) );
88
            }
89
            entropyFlux += _g->Flux( omega, entropyL, entropyR, _normals[idx_cell][idx_neigh] );
90
        }
91
        flux += _moments[idx_quad] * ( w * entropyFlux );
92
    }
93
    return flux;
94
95
}

steffen.schotthoefer's avatar
steffen.schotthoefer committed
96
97
98
99
100
101
102
103
104
105
106
107
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
108
109

    double mass1 = 0;
steffen.schotthoefer's avatar
steffen.schotthoefer committed
110
111
    for( unsigned i = 0; i < _nCells; ++i ) {
        _solverOutput[i] = _psi[i][0];
steffen.schotthoefer's avatar
steffen.schotthoefer committed
112
        mass1 += _psi[i][0] * _areas[i];
steffen.schotthoefer's avatar
steffen.schotthoefer committed
113
114
    }

115
116
117
118
119
    dFlux   = blaze::l2Norm( fluxNew - fluxOld );
    fluxOld = fluxNew;

    Save( -1 );

steffen.schotthoefer's avatar
steffen.schotthoefer committed
120
121
122
    if( rank == 0 ) log->info( "{:10}   {:10}", "t", "dFlux" );
    if( rank == 0 ) log->info( "{:03.8f}   {:01.5e} {:01.5e}", -1.0, dFlux, mass1 );

123
124
125
126
    // Time measurement
    // auto start = chrono::steady_clock::now();
    // auto end   = chrono::steady_clock::now();

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

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

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

134
135
            // ------- Reconstruction Step -------

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

138
            // ------- Flux Computation Step ---------
139

140
141
            // Dirichlet Boundaries are finished now
            if( _boundaryCells[idx_cell] == BOUNDARY_TYPE::DIRICHLET ) continue;
steffen.schotthoefer's avatar
steffen.schotthoefer committed
142

143
            psiNew[idx_cell] = ConstructFlux( idx_cell );
144

145
            // ------ FVM Step ------
146

147
148
149
            // NEED TO VECTORIZE
            for( unsigned idx_system = 0; idx_system < _nTotalEntries; idx_system++ ) {

150
151
152
153
154
                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 */
155
            }
steffen.schotthoefer's avatar
steffen.schotthoefer committed
156
        }
157
158
159
        _psi = psiNew;

        // pseudo time iteration output
160
161
162
163
164
        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
165
        }
166

steffen.schotthoefer's avatar
steffen.schotthoefer committed
167
168
        dFlux   = blaze::l2Norm( fluxNew - fluxOld );
        fluxOld = fluxNew;
169
        if( rank == 0 ) log->info( "{:03.8f}   {:01.5e} {:01.5e}", _energies[idx_energy], dFlux, mass );
170
        Save( idx_energy );
steffen.schotthoefer's avatar
steffen.schotthoefer committed
171
172
    }
}
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

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 );
}