Commit a4779030 authored by jonas.kusch's avatar jonas.kusch
Browse files

Fokker-Planck 2D and product quadrature added

parent 2df6fb69
Pipeline #117962 failed with stages
in 2 minutes and 50 seconds
......@@ -55,12 +55,13 @@ enum BOUNDARY_TYPE { DIRICHLET, NEUMANN, NONE, INVALID };
/*! @brief Enum for all currently available quadratures in rtsn.
* Option enums are written in capital letters with underscores as spaces (e.g option "time integration" has option enum "TIME_INTEGRATION")
*/
enum QUAD_NAME { QUAD_MonteCarlo, QUAD_GaussLegendreTensorized, QUAD_GaussLegendre1D, QUAD_LevelSymmetric, QUAD_Lebedev, QUAD_LDFESA };
enum QUAD_NAME { QUAD_MonteCarlo, QUAD_GaussLegendreTensorized, QUAD_GaussLegendre1D, QUAD_LevelSymmetric, QUAD_Lebedev, QUAD_LDFESA, QUAD_Product };
/*! @brief Conversion Map String to enum
*/
inline std::map<std::string, QUAD_NAME> Quadrature_Map{ { "MONTE_CARLO", QUAD_MonteCarlo },
{ "GAUSS_LEGENDRE_TENSORIZED", QUAD_GaussLegendreTensorized },
{ "PRODUCT", QUAD_Product },
{ "GAUSS_LEGENDRE_1D", QUAD_GaussLegendre1D },
{ "LEVEL_SYMMETRIC", QUAD_LevelSymmetric },
{ "LEBEDEV", QUAD_Lebedev },
......@@ -85,7 +86,7 @@ inline std::map<std::string, PROBLEM_NAME> Problem_Map{ { "LINESOURCE", PROBLEM_
{ "WATERPHANTOM", PROBLEM_WaterPhantom },
{ "AIRCAVITY", PROBLEM_AirCavity },
{ "MUSCLEBONELUNG", PROBLEM_MuscleBoneLung },
{ "PHANTOM2D", PROBLEM_Phantom2D},
{ "PHANTOM2D", PROBLEM_Phantom2D },
{ "LINESOURCE_PSEUDO_1D", PROBLEM_LineSource_Pseudo_1D },
{ "LINESOURCE_PSEUDO_1D_PHYSICS", PROBLEM_LineSource_Pseudo_1D_Physics } };
......@@ -95,10 +96,25 @@ enum KERNEL_NAME { KERNEL_Isotropic, KERNEL_Isotropic1D };
inline std::map<std::string, KERNEL_NAME> Kernel_Map{ { "ISOTROPIC", KERNEL_Isotropic }, { "ISOTROPIC_1D", KERNEL_Isotropic1D } };
// Solver name
enum SOLVER_NAME { SN_SOLVER, CSD_SN_SOLVER, CSD_SN_NOTRAFO_SOLVER, CSD_SN_FOKKERPLANCK_SOLVER, CSD_SN_FOKKERPLANCK_TRAFO_SOLVER, PN_SOLVER, MN_SOLVER };
enum SOLVER_NAME {
SN_SOLVER,
CSD_SN_SOLVER,
CSD_SN_NOTRAFO_SOLVER,
CSD_SN_FOKKERPLANCK_SOLVER,
CSD_SN_FOKKERPLANCK_TRAFO_SOLVER,
CSD_SN_FOKKERPLANCK_TRAFO_SOLVER_2D,
PN_SOLVER,
MN_SOLVER
};
inline std::map<std::string, SOLVER_NAME> Solver_Map{
{ "SN_SOLVER", SN_SOLVER }, { "CSD_SN_SOLVER", CSD_SN_SOLVER },{ "CSD_SN_NOTRAFO_SOLVER", CSD_SN_NOTRAFO_SOLVER }, { "CSD_SN_FOKKERPLANCK_SOLVER", CSD_SN_FOKKERPLANCK_SOLVER }, { "CSD_SN_FOKKERPLANCK_TRAFO_SOLVER", CSD_SN_FOKKERPLANCK_TRAFO_SOLVER }, { "PN_SOLVER", PN_SOLVER }, { "MN_SOLVER", MN_SOLVER } };
inline std::map<std::string, SOLVER_NAME> Solver_Map{ { "SN_SOLVER", SN_SOLVER },
{ "CSD_SN_SOLVER", CSD_SN_SOLVER },
{ "CSD_SN_NOTRAFO_SOLVER", CSD_SN_NOTRAFO_SOLVER },
{ "CSD_SN_FOKKERPLANCK_SOLVER", CSD_SN_FOKKERPLANCK_SOLVER },
{ "CSD_SN_FOKKERPLANCK_TRAFO_SOLVER", CSD_SN_FOKKERPLANCK_TRAFO_SOLVER },
{ "CSD_SN_FOKKERPLANCK_TRAFO_SOLVER_2D", CSD_SN_FOKKERPLANCK_TRAFO_SOLVER_2D },
{ "PN_SOLVER", PN_SOLVER },
{ "MN_SOLVER", MN_SOLVER } };
// Entropy functional
enum ENTROPY_NAME { QUADRATIC, MAXWELL_BOLZMANN, BOSE_EINSTEIN, FERMI_DIRAC };
......
#include "quadratures/productquadrature.h"
#include "toolboxes/errormessages.h"
ProductQuadrature::ProductQuadrature( unsigned order ) : QuadratureBase( order ) {
SetName();
SetNq();
SetPointsAndWeights();
SetConnectivity();
}
void ProductQuadrature::SetPointsAndWeights() {
Vector nodes1D( 2 * _order ), weights1D( 2 * _order );
// construct companion matrix
Matrix CM( 2 * _order, 2 * _order, 0.0 );
for( unsigned i = 0; i < 2 * _order - 1; ++i ) {
CM( i + 1, i ) = std::sqrt( 1 / ( 4 - 1 / std::pow( static_cast<double>( i + 1 ), 2 ) ) );
CM( i, i + 1 ) = std::sqrt( 1 / ( 4 - 1 / std::pow( static_cast<double>( i + 1 ), 2 ) ) );
}
// compute eigenvalues and -vectors of the companion matrix
auto evSys = ComputeEigenValTriDiagMatrix( CM );
for( unsigned i = 0; i < 2 * _order; ++i ) {
if( std::fabs( evSys.first[i] ) < 1e-15 ) // avoid rounding errors
nodes1D[i] = 0;
else
nodes1D[i] = evSys.first[i];
weights1D[i] = 2 * std::pow( evSys.second( 0, i ), 2 );
}
// sort nodes increasingly and also reorder weigths for consistency
std::vector<unsigned> sortOrder( nodes1D.size() );
std::iota( sortOrder.begin(), sortOrder.end(), 0 );
std::sort( sortOrder.begin(), sortOrder.end(), [&]( unsigned i, unsigned j ) { return nodes1D[i] < nodes1D[j]; } );
Vector sorted_nodes( static_cast<unsigned>( sortOrder.size() ) ), sorted_weights( static_cast<unsigned>( sortOrder.size() ) );
std::transform( sortOrder.begin(), sortOrder.end(), sorted_nodes.begin(), [&]( unsigned i ) { return nodes1D[i]; } );
std::transform( sortOrder.begin(), sortOrder.end(), sorted_weights.begin(), [&]( unsigned i ) { return weights1D[i]; } );
nodes1D = sorted_nodes;
weights1D = sorted_weights;
// setup equidistant angle phi around z axis
Vector phi( 2 * _order );
for( unsigned i = 0; i < 2 * _order; ++i ) {
phi[i] = ( i + 0.5 ) * M_PI / _order;
}
// unsigned range = std::floor( _order / 2.0 ); // comment (steffen): why do we only need half of the points:
//=> In 2D we would count everything twice. (not wrong with scaling
// resize points and weights
_points.resize( _nq );
_pointsSphere.resize( _nq );
for( auto& p : _points ) {
p.resize( 3 );
}
for( auto& p : _pointsSphere ) {
p.resize( 2 );
}
_weights.resize( _nq );
// transform tensorized (x,y,z)-grid to spherical grid points
for( unsigned j = 0; j < 2 * _order; ++j ) {
for( unsigned i = 0; i < 2 * _order; ++i ) {
_points[j * ( 2 * _order ) + i][0] = sqrt( 1 - nodes1D[j] * nodes1D[j] ) * std::cos( phi[i] );
_points[j * ( 2 * _order ) + i][1] = sqrt( 1 - nodes1D[j] * nodes1D[j] ) * std::sin( phi[i] );
_points[j * ( 2 * _order ) + i][2] = nodes1D[j];
_pointsSphere[j * ( 2 * _order ) + i][0] = nodes1D[j]; // my
_pointsSphere[j * ( 2 * _order ) + i][1] = phi[i]; // phi
_weights[j * ( 2 * _order ) + i] = M_PI / _order * weights1D[j];
}
}
}
void ProductQuadrature::SetConnectivity() { // TODO
// Not initialized for this quadrature.
VectorVectorU connectivity;
_connectivity = connectivity;
}
std::pair<Vector, Matrix> ProductQuadrature::ComputeEigenValTriDiagMatrix( const Matrix& mat ) {
// copied from 'Numerical Recipes' and updated + modified to work with blaze
unsigned n = mat.rows();
Vector d( n, 0.0 ), e( n, 0.0 );
Matrix z( n, n, 0.0 );
for( unsigned i = 0; i < n; ++i ) {
d[i] = mat( i, i );
z( i, i ) = 1.0;
i == 0 ? e[i] = 0.0 : e[i] = mat( i, i - 1 );
}
int m, l, iter, i, k;
m = l = iter = i = k = 0;
double s, r, p, g, f, dd, c, b;
s = r = p = g = f = dd = c = b = 0.0;
const double eps = std::numeric_limits<double>::epsilon();
for( i = 1; i < static_cast<int>( n ); i++ ) e[i - 1] = e[i];
e[n - 1] = 0.0;
for( l = 0; l < static_cast<int>( n ); l++ ) {
iter = 0;
do {
for( m = l; m < static_cast<int>( n ) - 1; m++ ) {
dd = std::fabs( d[m] ) + std::fabs( d[m + 1] );
if( std::fabs( e[m] ) <= eps * dd ) break;
}
if( m != l ) {
if( iter++ == 30 ) ErrorMessages::Error( "Solving the tridiagonal matrix took too many iterations!", CURRENT_FUNCTION );
g = ( d[l + 1] - d[l] ) / ( 2.0 * e[l] );
r = Pythag( g, 1.0 );
g = d[m] - d[l] + e[l] / ( g + std::copysign( r, g ) );
s = c = 1.0;
p = 0.0;
for( i = m - 1; i >= l; i-- ) {
f = s * e[i];
b = c * e[i];
e[i + 1] = ( r = Pythag( f, g ) );
if( r == 0.0 ) {
d[i + 1] -= p;
e[m] = 0.0;
break;
}
s = f / r;
c = g / r;
g = d[i + 1] - p;
r = ( d[i] - g ) * s + 2.0 * c * b;
d[i + 1] = g + ( p = s * r );
g = c * r - b;
for( k = 0; k < static_cast<int>( n ); k++ ) {
f = z( static_cast<unsigned>( k ), static_cast<unsigned>( i ) + 1 );
z( static_cast<unsigned>( k ), static_cast<unsigned>( i ) + 1 ) =
s * z( static_cast<unsigned>( k ), static_cast<unsigned>( i ) ) + c * f;
z( static_cast<unsigned>( k ), static_cast<unsigned>( i ) ) =
c * z( static_cast<unsigned>( k ), static_cast<unsigned>( i ) ) - s * f;
}
}
if( r == 0.0 && i >= l ) continue;
d[l] -= p;
e[l] = g;
e[m] = 0.0;
}
} while( m != l );
}
return std::make_pair( d, z );
}
double ProductQuadrature::Pythag( const double a, const double b ) {
// copied from 'Numerical Recipes'
double absa = std::fabs( a ), absb = std::fabs( b );
return ( absa > absb ? absa * std::sqrt( 1.0 + ( absb / absa ) * ( absb / absa ) )
: ( absb == 0.0 ? 0.0 : absb * std::sqrt( 1.0 + ( absa / absb ) * ( absa / absb ) ) ) );
}
bool ProductQuadrature::CheckOrder() {
if( _order % 2 == 1 ) { // order needs to be even
ErrorMessages::Error( "ERROR! Order " + std::to_string( _order ) + " for " + GetName() + " not available. \n Order must be an even number. ",
CURRENT_FUNCTION );
}
return true;
}
#include "quadratures/quadraturebase.h"
#include "quadratures/productquadrature.h"
#include "quadratures/qgausslegendre1D.h"
#include "quadratures/qgausslegendretensorized.h"
#include "quadratures/qldfesa.h"
......@@ -14,6 +15,7 @@ QuadratureBase* QuadratureBase::CreateQuadrature( QUAD_NAME name, unsigned order
switch( name ) {
case QUAD_MonteCarlo: return new QMonteCarlo( order );
case QUAD_GaussLegendreTensorized: return new QGaussLegendreTensorized( order );
case QUAD_Product: return new ProductQuadrature( order );
case QUAD_GaussLegendre1D: return new QGaussLegendre1D( order );
case QUAD_LevelSymmetric: return new QLevelSymmetric( order );
case QUAD_LDFESA: return new QLDFESA( order );
......
......@@ -62,10 +62,10 @@ CSDSolverTrafoFP2D::CSDSolverTrafoFP2D( Config* settings ) : SNSolver( settings
double dMinus = 0.0;
DPlus = DMinus - 2 * _mu[0] * w[0];
for( unsigned j = 1; j < 2 * order; ++j ) {
for( unsigned j = 0; j < 2 * order - 1; ++j ) {
DMinus = DPlus;
DPlus = DMinus - 2 * _mu[j] * w[j];
dPlus = ( sqrt( 1 - _mu[j] * _mu[j] ) - sqrt( 1 - _mu[j - 1] * _mu[j - 1] ) ) / ( _mu[1] - _mu[0] );
dPlus = ( sqrt( 1 - _mu[j + 1] * _mu[j + 1] ) - sqrt( 1 - _mu[j] * _mu[j] ) ) / ( _mu[j + 1] - _mu[j] );
c = ( DPlus * dPlus - DMinus * dMinus ) / _wp[j];
K = 2 * ( 1 - _mu[j] * _mu[j] ) + c * sqrt( 1 - _mu[j] * _mu[j] );
gamma[j] = M_PI * M_PI * K / ( 2 * order * ( 1 - std::cos( M_PI / order ) ) );
......
......@@ -10,6 +10,7 @@
#include "solvers/csdsnsolverfp.h"
#include "solvers/csdsnsolvernotrafo.h"
#include "solvers/csdsolvertrafofp.h"
#include "solvers/csdsolvertrafofp2d.h"
#include "solvers/mnsolver.h"
#include "solvers/pnsolver.h"
#include "solvers/snsolver.h"
......@@ -82,6 +83,7 @@ Solver* Solver::Create( Config* settings ) {
case CSD_SN_NOTRAFO_SOLVER: return new CSDSNSolverNoTrafo( settings );
case CSD_SN_FOKKERPLANCK_SOLVER: return new CSDSNSolverFP( settings );
case CSD_SN_FOKKERPLANCK_TRAFO_SOLVER: return new CSDSolverTrafoFP( settings );
case CSD_SN_FOKKERPLANCK_TRAFO_SOLVER_2D: return new CSDSolverTrafoFP2D( settings );
case PN_SOLVER: return new PNSolver( settings );
case MN_SOLVER: return new MNSolver( settings );
default: return new SNSolver( settings );
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment