mirror of
https://codeberg.org/vcbferreira/NuFI_deal.ii
synced 2026-08-12 14:33:18 +02:00
reorganized project for cleaner filesystem
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
||||
#ifndef NUFI_BLAS_H
|
||||
#define NUFI_BLAS_H
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
/*!
|
||||
* \brief Convenience wrappers for BLAS, with overloads for single and double
|
||||
* precision.
|
||||
*/
|
||||
namespace blas
|
||||
{
|
||||
|
||||
double dot( const size_t n, const double *x, size_t incx,
|
||||
const double *y, size_t incy );
|
||||
|
||||
float dot( const size_t n, const float *x, size_t incx,
|
||||
const float *y, size_t incy );
|
||||
|
||||
void axpy( size_t n, double alpha, const double *x, size_t incx,
|
||||
double *y, size_t incy );
|
||||
|
||||
void axpy( size_t n, float alpha, const float *x, size_t incx,
|
||||
float *y, size_t incy );
|
||||
|
||||
|
||||
void scal( size_t n, double alpha, double *x, size_t incx );
|
||||
void scal( size_t n, float alpha, float *x, size_t incx );
|
||||
|
||||
void copy( size_t n, const double *x, size_t incx, double *y, size_t incy );
|
||||
void copy( size_t n, const float *x, size_t incx, float *y, size_t incy );
|
||||
|
||||
void ger( const size_t M, const size_t N, const double alpha,
|
||||
const double *X, const size_t incX, const double *Y, const size_t incY,
|
||||
double *A, const size_t lda);
|
||||
|
||||
void ger( const size_t M, const size_t N, const float alpha,
|
||||
const float *X, const size_t incX, const float *Y, const size_t incY,
|
||||
float *A, const size_t lda);
|
||||
|
||||
|
||||
void gemv( const char trans, size_t m, size_t n,
|
||||
double alpha, const double *a, size_t lda,
|
||||
const double *x, size_t incx, double beta,
|
||||
double *y, size_t incy );
|
||||
|
||||
void gemv( const char trans, size_t m, size_t n,
|
||||
float alpha, const float *a, size_t lda,
|
||||
const float *x, size_t incx, float beta,
|
||||
float *y, size_t incy );
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
#ifndef FIELDS_H
|
||||
#define FIELDS_H
|
||||
|
||||
#include <cmath>
|
||||
#include <deal.II/base/function.h>
|
||||
#include "nufi/parameters.h"
|
||||
#include "nufi/splines.h"
|
||||
#include "nufi/lsmr.h"
|
||||
|
||||
using namespace dealii;
|
||||
|
||||
inline double f0(const double x,
|
||||
const double v,
|
||||
const double eps = Parameters::EPS,
|
||||
const double k = Parameters::WAVE_NR)
|
||||
{
|
||||
const double prefactor = Parameters::F0_FACTOR * (1.0 + eps * std::cos(k*x));
|
||||
const double gaussian = v*v * std::exp(-0.5 * v*v);
|
||||
|
||||
return prefactor * gaussian;
|
||||
}
|
||||
|
||||
|
||||
inline double compute_rho(const double x,
|
||||
const unsigned int Nv = Parameters::NV)
|
||||
{
|
||||
const double dv = (Parameters::V_DOMAIN_RIGHT - Parameters::V_DOMAIN_LEFT) / Nv;
|
||||
|
||||
double integral = 0.0;
|
||||
|
||||
for (unsigned int i = 0; i < Nv; ++i)
|
||||
{
|
||||
const double v = Parameters::V_DOMAIN_LEFT + (i + 0.5) * dv;
|
||||
integral += f0(x, v) * dv;
|
||||
}
|
||||
|
||||
return 1.0 - integral;
|
||||
}
|
||||
|
||||
|
||||
template <size_t dx = 0>
|
||||
double eval(double x, const double *coeffs) noexcept
|
||||
{
|
||||
using std::floor;
|
||||
|
||||
// Shift to a box that starts at 0.
|
||||
x -= Parameters::X_DOMAIN_LEFT;
|
||||
|
||||
// Get "periodic position" in box at origin.
|
||||
x = x - Parameters::LX * floor( x/Parameters::LX );
|
||||
|
||||
// Knot number
|
||||
double x_knot = floor( x/Parameters::SPLINE_DX);
|
||||
|
||||
size_t ii = static_cast<size_t>(x_knot);
|
||||
|
||||
// Convert x to reference coordinates.
|
||||
x = x/Parameters::SPLINE_DX - x_knot;
|
||||
|
||||
// Scale according to derivative.
|
||||
double factor = 1;
|
||||
for ( size_t i = 0; i < dx; ++i ) factor *= 1/Parameters::SPLINE_DX;
|
||||
|
||||
return factor*splines1d::eval<double,Parameters::SPLINE_ORDER,dx>(x, coeffs + ii);
|
||||
}
|
||||
|
||||
template <typename real, size_t order>
|
||||
void interpolate( real *coeffs, const real *values) // Least Squares needs to be made
|
||||
{
|
||||
std::unique_ptr<real[]> tmp { new real[ Parameters::SPLINE_NX ] };
|
||||
|
||||
for ( size_t i = 0; i < Parameters::SPLINE_NX; ++i )
|
||||
tmp[ i ] = coeffs[ i ];
|
||||
|
||||
struct mat_t // STRUCT AND CONFIG NEEDS TO BE REVIEWED
|
||||
{
|
||||
real N[ order ];
|
||||
|
||||
mat_t()
|
||||
{
|
||||
splines1d::N<real,order>(0,N);
|
||||
}
|
||||
|
||||
void operator()( const real *in, real *out ) const
|
||||
{
|
||||
for ( size_t i = 0; i < Parameters::SPLINE_NX; ++i )
|
||||
{
|
||||
real result = 0;
|
||||
if ( i + order <= Parameters::SPLINE_NX )
|
||||
{
|
||||
for ( size_t ii = 0; ii < order; ++ii )
|
||||
result += N[ii] * in[ i + ii ];
|
||||
}
|
||||
else
|
||||
{
|
||||
for ( size_t ii = 0; ii < order; ++ii )
|
||||
result += N[ii]*in[ (i+ii) % Parameters::SPLINE_NX];
|
||||
}
|
||||
out[ i ] = result;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct transposed_mat_t // STRUCT AND CONFIG NEEDS TO BE REVIEWED
|
||||
{
|
||||
real N[ order ];
|
||||
|
||||
transposed_mat_t()
|
||||
{
|
||||
splines1d::N<real,order>(0,N);
|
||||
}
|
||||
|
||||
void operator()( const real *in, real *out ) const
|
||||
{
|
||||
for ( size_t i = 0; i < Parameters::SPLINE_NX; ++i )
|
||||
out[ i ] = 0;
|
||||
|
||||
for ( size_t i = 0; i < Parameters::SPLINE_NX; ++i )
|
||||
{
|
||||
if ( i + order <= Parameters::SPLINE_NX )
|
||||
{
|
||||
for ( size_t ii = 0; ii < order; ++ii )
|
||||
out[ i + ii ] += N[ii] * in[ i ];
|
||||
}
|
||||
else
|
||||
{
|
||||
for ( size_t ii = 0; ii < order; ++ii )
|
||||
out[ (i+ii) % Parameters::SPLINE_NX ] += N[ii]*in[ i ];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
mat_t M; transposed_mat_t Mt;
|
||||
lsmr_options<real> opt; opt.silent = true;
|
||||
lsmr( Parameters::SPLINE_NX, Parameters::SPLINE_NX , M, Mt, values, tmp.get(), opt );
|
||||
|
||||
if ( opt.iter == opt.max_iter )
|
||||
std::cerr << "Warning. LSMR did not converge.\n";
|
||||
|
||||
for ( size_t i = 0; i < Parameters::SPLINE_NX + order - 1; ++i )
|
||||
coeffs[ i ] = tmp[ i % Parameters::SPLINE_NX ];
|
||||
}
|
||||
|
||||
|
||||
template <int dim>
|
||||
class ChargeDensity : public Function<dim> // only uses f0
|
||||
{
|
||||
public:
|
||||
ChargeDensity(double eps,
|
||||
double k,
|
||||
unsigned int Nv)
|
||||
: Function<dim>(1), eps(eps), k(k), Nv(Nv) {}
|
||||
|
||||
virtual double value(const Point<dim> &p,
|
||||
[[maybe_unused]] const unsigned int component = 0) const override
|
||||
{
|
||||
return compute_rho(p[0], Nv);
|
||||
}
|
||||
|
||||
private:
|
||||
const double eps;
|
||||
const double k;
|
||||
const unsigned int Nv;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
+252
@@ -0,0 +1,252 @@
|
||||
#ifndef LSMR_H
|
||||
#define LSMR_H
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include "nufi/blas.h"
|
||||
|
||||
template <typename real>
|
||||
struct lsmr_options
|
||||
{
|
||||
///////////
|
||||
// INPUT //
|
||||
///////////
|
||||
|
||||
// Whether to print messages to std::cout.
|
||||
bool silent = true;
|
||||
|
||||
// Residual of normal equations AᵀAx = Aᵀb
|
||||
bool relative_residual = true;
|
||||
real target_residual = std::numeric_limits<real>::epsilon();
|
||||
size_t max_iter = 1000;
|
||||
|
||||
// How many Lánczos vectors to keep for local reorthogonalisation.
|
||||
// Choose zero for no reorthogonalisation, pure LSMR.
|
||||
// Choose a large value for complete reorthognalisation.
|
||||
//
|
||||
// In an ideal world without roundoff errors, this would have no effect
|
||||
// at all, as the Lánczos vectors would be perfectly orthogonal. In practice
|
||||
// this property is lost rather quickly. One may choose to store some of
|
||||
// the most recent Lánczos vectors to enforce this property manually. This
|
||||
// increase convergence speed at the cost of additional memory requirements.
|
||||
size_t reorthogonalise_u = 50;
|
||||
size_t reorthogonalise_v = 50;
|
||||
|
||||
////////////
|
||||
// OUTPUT //
|
||||
////////////
|
||||
|
||||
// Iteration count and reached residual.
|
||||
// Estimates of ‖A‖ and cond(A)
|
||||
size_t iter; real residual;
|
||||
real norm_A_estimate, cond_estimate;
|
||||
};
|
||||
|
||||
template <typename real, typename mat, typename transposed_mat>
|
||||
void lsmr( size_t m, size_t n, const mat& A, const transposed_mat& At,
|
||||
const real *b, real *x, lsmr_options<real> &S );
|
||||
|
||||
namespace lsmr_impl
|
||||
{
|
||||
|
||||
template <typename real>
|
||||
real norm( size_t n, const real *x )
|
||||
{
|
||||
using std::hypot;
|
||||
|
||||
real result = 0;
|
||||
for ( size_t i = 0; i < n; ++i )
|
||||
result = hypot(result,x[i]);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Reorthognalise u with respect to the previous vectors in buffer,
|
||||
// using the modified Gram–Schmidt process. Overwrite the oldest vector
|
||||
// in buffer when full.
|
||||
template <typename real>
|
||||
void reorthogonalise( real *buf, size_t n, size_t buffer_max,
|
||||
real *u, size_t iter )
|
||||
{
|
||||
using std::min;
|
||||
using blas::dot;
|
||||
using blas::axpy;
|
||||
using blas::scal;
|
||||
using blas::copy;
|
||||
|
||||
size_t n_buffered = min( iter+1, buffer_max );
|
||||
for ( size_t i = 0; i < n_buffered; ++i )
|
||||
{
|
||||
real fac = -dot( n, u, 1, buf + i*n, 1 );
|
||||
axpy( n, fac, buf + i*n, 1, u, 1 );
|
||||
}
|
||||
|
||||
scal( n, 1/norm(n,u), u, 1 );
|
||||
copy( n, u, 1, buf + ((iter+1)%buffer_max)*n, 1 );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template <typename real, typename mat, typename transposed_mat>
|
||||
void lsmr( size_t m, size_t n, const mat& A, const transposed_mat& At,
|
||||
const real *b, real *x, lsmr_options<real> &S )
|
||||
{
|
||||
using std::min;
|
||||
using std::max;
|
||||
using std::abs;
|
||||
using std::swap;
|
||||
using std::hypot;
|
||||
using blas::axpy;
|
||||
using blas::scal;
|
||||
using blas::copy;
|
||||
using lsmr_impl::norm;
|
||||
using lsmr_impl::reorthogonalise;
|
||||
|
||||
|
||||
// Allocation of buffers.
|
||||
size_t max_buf = min(n,m)-1;
|
||||
S.reorthogonalise_u = min(S.reorthogonalise_u,max_buf);
|
||||
S.reorthogonalise_v = min(S.reorthogonalise_v,max_buf);
|
||||
size_t u_buffer_size = max( S.reorthogonalise_u, size_t(1) );
|
||||
size_t v_buffer_size = max( S.reorthogonalise_v, size_t(1) );
|
||||
|
||||
std::unique_ptr<real[]> data { new real[ n*( 4 + v_buffer_size ) +
|
||||
m*( 2 + u_buffer_size ) ] {} };
|
||||
|
||||
real *u = data.get();
|
||||
real *utmp = u + m;
|
||||
real *ubuf = utmp + m;
|
||||
real *v = ubuf + m*u_buffer_size;
|
||||
real *vtmp = v + n;
|
||||
real *h = vtmp + n;
|
||||
real *h_bar = h + n;
|
||||
real *vbuf = h_bar + n;
|
||||
|
||||
At(b,v);
|
||||
const real norm_ATb = norm(n,v);
|
||||
|
||||
|
||||
A(x,u); axpy(m,real(-1),b,1,u,1);
|
||||
scal(m, real(-1), u, 1 ); // u = b - Ax;
|
||||
|
||||
real alpha = 0;
|
||||
real beta = norm(m,u);
|
||||
|
||||
if ( beta > real(0) )
|
||||
{
|
||||
scal(m, real(1)/beta, u, 1 ); // u = b - Ax / norm(b-Ax)
|
||||
At(u,v); // v = At*u
|
||||
alpha = norm(n,v);
|
||||
}
|
||||
|
||||
if ( alpha > real(0) )
|
||||
scal(n, real(1)/alpha, v, 1 ); // v = At*u/norm(At*u)
|
||||
|
||||
copy(n,u,1,ubuf,1); // u_buf.col(0) = u_buf
|
||||
copy(n,v,1,vbuf,1); // v_buf.col(0) = v
|
||||
copy(n,v,1,h,1); // h = v
|
||||
|
||||
if ( alpha * beta == real(0) ) return;
|
||||
|
||||
|
||||
real alpha_bar = alpha, zeta_bar = alpha*beta;
|
||||
real rho = 1, rho_bar = 1, c_bar = 1, s_bar = 0;
|
||||
real c, s, theta, zeta, theta_bar, rho_prev, rho_bar_prev;
|
||||
|
||||
// For estimating the condition number.
|
||||
real sigma_max = 0, sigma_min = std::numeric_limits<real>::max();
|
||||
real rho_bar_max = 0, rho_bar_min = std::numeric_limits<real>::max();
|
||||
|
||||
S.norm_A_estimate = 0;
|
||||
for ( S.iter = 0; S.iter < S.max_iter; ++S.iter )
|
||||
{
|
||||
// Continue the bidiagonalisation.
|
||||
A(v,utmp); axpy(m,-alpha,u,1,utmp,1); swap(u,utmp); // u = A*v - alpha*u
|
||||
beta = norm(m,u);
|
||||
|
||||
if ( beta > 0 )
|
||||
{
|
||||
scal(m, real(1)/beta, u, 1 );
|
||||
if ( S.reorthogonalise_u )
|
||||
reorthogonalise( ubuf, m, u_buffer_size, u, S.iter );
|
||||
|
||||
S.norm_A_estimate = hypot( alpha, S.norm_A_estimate );
|
||||
S.norm_A_estimate = hypot( beta , S.norm_A_estimate );
|
||||
|
||||
At(u,vtmp); axpy(n,-beta,v,1,vtmp,1); swap(v,vtmp); // v = At*u - beta*v
|
||||
alpha = norm(n,v);
|
||||
|
||||
if ( alpha > 0 )
|
||||
{
|
||||
scal(n,real(1)/alpha, v, 1 );
|
||||
if ( S.reorthogonalise_v )
|
||||
reorthogonalise( vbuf, n, v_buffer_size, v, S.iter );
|
||||
}
|
||||
}
|
||||
|
||||
// Construct and apply rotation P_k
|
||||
rho_prev = rho;
|
||||
rho = hypot(alpha_bar,beta);
|
||||
c = alpha_bar/rho;
|
||||
s = beta/rho;
|
||||
theta = s*alpha;
|
||||
alpha_bar = c*alpha;
|
||||
|
||||
// Construct and apply rotation \bar{P}_k
|
||||
rho_bar_prev = rho_bar;
|
||||
if ( S.iter )
|
||||
{
|
||||
rho_bar_max = max( rho_bar, rho_bar_max );
|
||||
rho_bar_min = min( rho_bar, rho_bar_min );
|
||||
}
|
||||
theta_bar = s_bar*rho;
|
||||
rho_bar = hypot( c_bar*rho, theta );
|
||||
if ( S.iter )
|
||||
{
|
||||
sigma_max = max( rho_bar_max, c_bar*rho );
|
||||
sigma_min = min( rho_bar_min, c_bar*rho );
|
||||
}
|
||||
c_bar = c_bar * rho/rho_bar;
|
||||
s_bar = theta/rho_bar;
|
||||
zeta = c_bar * zeta_bar;
|
||||
zeta_bar = -s_bar*zeta_bar;
|
||||
|
||||
|
||||
// Update h, h_bar, x
|
||||
scal(n, -(theta_bar*rho)/(rho_prev*rho_bar_prev), h_bar, 1 ) ;
|
||||
axpy(n, real(1), h, 1, h_bar, 1 ); // h_bar = h - factor*h_bar
|
||||
|
||||
axpy( n, zeta/(rho*rho_bar), h_bar, 1, x, 1 ); // x += factor * h_bar
|
||||
|
||||
scal(n, -theta/rho, h, 1 );
|
||||
axpy(n, real(1), v, 1, h, 1 ); // h = v - factor*h;
|
||||
|
||||
// Estimate quantities.
|
||||
if ( S.relative_residual ) S.residual = abs(zeta_bar)/norm_ATb;
|
||||
else S.residual = abs(zeta_bar);
|
||||
S.cond_estimate = sigma_max / sigma_min;
|
||||
|
||||
if ( S.residual <= S.target_residual )
|
||||
{
|
||||
if ( S.silent == false )
|
||||
{
|
||||
std::cout << "LSMR: Iteration: " << std::setw(4) << S.iter << ", "
|
||||
<< "Residual: " << std::setw(12) << std::scientific << S.residual << ", "
|
||||
<< "cond estimate: " << std::setw(12) << std::scientific << S.cond_estimate << ".\n";
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if ( S.silent == false && (S.iter%10) == 0 )
|
||||
{
|
||||
std::cout << "LSMR: Iteration: " << std::setw(4) << S.iter << ", "
|
||||
<< "Residual: " << std::setw(12) << std::scientific << S.residual << ", "
|
||||
<< "cond estimate: " << std::setw(12) << std::scientific << S.cond_estimate << ".\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
#ifndef NUFI_SOLVER_H
|
||||
#define NUFI_SOLVER_H
|
||||
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <deal.II/base/point.h>
|
||||
#include <deal.II/base/tensor.h>
|
||||
#include <deal.II/numerics/fe_field_function.h>
|
||||
|
||||
#include "nufi/parameters.h"
|
||||
#include "nufi/poisson_problem.h"
|
||||
#include "nufi/fields.h"
|
||||
|
||||
using namespace dealii;
|
||||
|
||||
class NuFISolver
|
||||
{
|
||||
public:
|
||||
NuFISolver();
|
||||
|
||||
void run();
|
||||
double eval_rho(unsigned int n, double x, const double *E_coeffs, unsigned int Nv = Parameters::NV) const;
|
||||
double eval_ftilda(unsigned int n, double x, double u, const double *E_coeffs) const;
|
||||
|
||||
private:
|
||||
|
||||
|
||||
unsigned int Nt = std::floor(Parameters::TMAX/Parameters::DT);
|
||||
unsigned int Nx = Parameters::SPLINE_NX;
|
||||
|
||||
double Lx = Parameters::LX;
|
||||
|
||||
std::vector<double> rho;
|
||||
|
||||
unsigned int order;
|
||||
|
||||
PoissonProblem<1> poisson;
|
||||
|
||||
};
|
||||
|
||||
template<unsigned int dim>
|
||||
class ChargeDensity_NuFI : public Function<dim>
|
||||
{
|
||||
public:
|
||||
ChargeDensity_NuFI(const double *rho_values, unsigned int Nx)
|
||||
: Function<dim>(), rho(rho_values), Nx(Nx) {}
|
||||
|
||||
virtual double value(const Point<dim> &p,
|
||||
[[maybe_unused]] const unsigned int component = 0) const override
|
||||
{
|
||||
const double x = p[0];
|
||||
|
||||
// Map x -> grid index
|
||||
const double L = Parameters::LX;
|
||||
const double dx = L / (Nx-1);
|
||||
|
||||
int i = static_cast<int>(std::floor((x - Parameters::X_DOMAIN_LEFT) / dx));
|
||||
|
||||
// periodic wrap
|
||||
i = (i % Nx + Nx) % Nx;
|
||||
|
||||
return rho[i];
|
||||
}
|
||||
|
||||
private:
|
||||
const double *rho;
|
||||
const unsigned int Nx;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,41 @@
|
||||
#ifndef PARAMETERS_H
|
||||
#define PARAMETERS_H
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace Parameters
|
||||
{
|
||||
constexpr unsigned int DIMENSION = 1;
|
||||
|
||||
constexpr double X_DOMAIN_LEFT = 0.0;
|
||||
constexpr double X_DOMAIN_RIGHT = 4*M_PI;
|
||||
constexpr double LX = std::abs(X_DOMAIN_RIGHT- X_DOMAIN_LEFT);
|
||||
|
||||
constexpr double V_DOMAIN_LEFT = -10.0;
|
||||
constexpr double V_DOMAIN_RIGHT = 10.0;
|
||||
|
||||
constexpr unsigned int NV = 512;
|
||||
|
||||
constexpr unsigned int GLOBAL_REFINEMENT = 8;
|
||||
constexpr unsigned int FE_DEGREE = 4;
|
||||
|
||||
constexpr double EPS = 0.01;
|
||||
constexpr double WAVE_NR = 0.5;
|
||||
constexpr double F0_FACTOR = 0.39894228040143267793994;
|
||||
|
||||
// NUFI options
|
||||
constexpr double DT=1./16.;
|
||||
constexpr unsigned int TMAX = 10;
|
||||
|
||||
|
||||
//spline options
|
||||
constexpr int SPLINE_NX = 512;
|
||||
constexpr double SPLINE_DX = LX/SPLINE_NX;
|
||||
constexpr size_t SPLINE_ORDER = 4;
|
||||
|
||||
//Plotting options
|
||||
constexpr int PLOT_FREQUENCY = 2;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,257 @@
|
||||
#ifndef POISSON_NON_PERIODIC_H
|
||||
#define POISSON_NON_PERIODIC_H
|
||||
|
||||
#include "nufi/parameters.h"
|
||||
#include <deal.II/base/point.h>
|
||||
#include <deal.II/grid/tria.h>
|
||||
#include <deal.II/dofs/dof_handler.h>
|
||||
#include <deal.II/grid/grid_generator.h>
|
||||
|
||||
#include <deal.II/fe/fe_q.h>
|
||||
|
||||
#include <deal.II/dofs/dof_tools.h>
|
||||
|
||||
#include <deal.II/fe/fe_values.h>
|
||||
#include <deal.II/base/quadrature_lib.h>
|
||||
|
||||
#include <deal.II/base/function.h>
|
||||
#include <deal.II/numerics/vector_tools.h>
|
||||
#include <deal.II/numerics/matrix_tools.h>
|
||||
|
||||
#include <deal.II/lac/vector.h>
|
||||
#include <deal.II/lac/full_matrix.h>
|
||||
#include <deal.II/lac/sparse_matrix.h>
|
||||
#include <deal.II/lac/dynamic_sparsity_pattern.h>
|
||||
#include <deal.II/lac/solver_cg.h>
|
||||
#include <deal.II/lac/precondition.h>
|
||||
|
||||
#include <deal.II/numerics/data_out.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
using namespace dealii;
|
||||
|
||||
|
||||
template<int dim>
|
||||
class Poisson_non_periodic
|
||||
{
|
||||
public:
|
||||
Poisson_non_periodic ();
|
||||
|
||||
void run();
|
||||
|
||||
void initialize();
|
||||
void solve_step();
|
||||
|
||||
void set_rhs_function(std::unique_ptr<Function<dim>> rhs_function);
|
||||
|
||||
const Vector<double> &get_solution() const { return solution; }
|
||||
const DoFHandler<dim> &get_dof_handler() const { return dof_handler; }
|
||||
|
||||
std::vector<double> sample_electric_field(const Poisson_non_periodic<dim> &problem, // sampling to save as spline
|
||||
unsigned int Nx,
|
||||
double x_min,
|
||||
double x_max);
|
||||
void output_results(unsigned int n);
|
||||
private:
|
||||
|
||||
void make_grid();
|
||||
void setup_system();
|
||||
void assemble_system();
|
||||
void solve();
|
||||
void output_results() const;
|
||||
|
||||
Triangulation<1> triangulation;
|
||||
const FE_Q<1> fe;
|
||||
DoFHandler<1> dof_handler;
|
||||
|
||||
SparsityPattern sparsity_pattern;
|
||||
SparseMatrix<double> system_matrix;
|
||||
|
||||
Vector<double> solution;
|
||||
Vector<double> system_rhs;
|
||||
|
||||
std::unique_ptr<const Function<dim>> rhs_function;
|
||||
|
||||
};
|
||||
|
||||
template<int dim>
|
||||
Poisson_non_periodic<dim>::Poisson_non_periodic()
|
||||
: fe(/* polynomial degree = */ 1)
|
||||
, dof_handler(triangulation)
|
||||
{}
|
||||
|
||||
template <int dim>
|
||||
void Poisson_non_periodic<dim>::set_rhs_function(std::unique_ptr<Function<dim>> rhs)
|
||||
{
|
||||
rhs_function = std::move(rhs);
|
||||
}
|
||||
|
||||
|
||||
template<int dim>
|
||||
void Poisson_non_periodic<dim>::make_grid()
|
||||
{
|
||||
Point<dim, double> x0 = Parameters::X_DOMAIN_RIGHT;
|
||||
Point<dim, double> x1 = Parameters::X_DOMAIN_RIGHT;
|
||||
GridGenerator::hyper_rectangle(triangulation, x0, x1);
|
||||
triangulation.refine_global(Parameters::GLOBAL_REFINEMENT);
|
||||
|
||||
std::cout << "Number of active cells: " << triangulation.n_active_cells()
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<int dim>
|
||||
void Poisson_non_periodic<dim>::setup_system()
|
||||
{
|
||||
dof_handler.distribute_dofs(fe);
|
||||
std::cout << "Number of degrees of freedom: " << dof_handler.n_dofs()
|
||||
<< std::endl;
|
||||
|
||||
DynamicSparsityPattern dsp(dof_handler.n_dofs());
|
||||
DoFTools::make_sparsity_pattern(dof_handler, dsp);
|
||||
sparsity_pattern.copy_from(dsp);
|
||||
|
||||
system_matrix.reinit(sparsity_pattern);
|
||||
|
||||
solution.reinit(dof_handler.n_dofs());
|
||||
system_rhs.reinit(dof_handler.n_dofs());
|
||||
}
|
||||
|
||||
|
||||
template<int dim>
|
||||
void Poisson_non_periodic<dim>::assemble_system()
|
||||
{
|
||||
const QGauss<1> quadrature_formula(fe.degree + 1);
|
||||
FEValues<1> fe_values(fe,
|
||||
quadrature_formula,
|
||||
update_values | update_gradients | update_JxW_values);
|
||||
|
||||
const unsigned int dofs_per_cell = fe.n_dofs_per_cell();
|
||||
|
||||
FullMatrix<double> cell_matrix(dofs_per_cell, dofs_per_cell);
|
||||
Vector<double> cell_rhs(dofs_per_cell);
|
||||
|
||||
std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
|
||||
|
||||
for (const auto &cell : dof_handler.active_cell_iterators())
|
||||
{
|
||||
fe_values.reinit(cell);
|
||||
|
||||
cell_matrix = 0;
|
||||
cell_rhs = 0;
|
||||
|
||||
for (const unsigned int q_index : fe_values.quadrature_point_indices())
|
||||
{
|
||||
|
||||
const double rho = rhs_function->value(fe_values.quadrature_point(q_index));
|
||||
|
||||
for (const unsigned int i : fe_values.dof_indices())
|
||||
for (const unsigned int j : fe_values.dof_indices())
|
||||
cell_matrix(i, j) +=
|
||||
(fe_values.shape_grad(i, q_index) * // grad phi_i(x_q)
|
||||
fe_values.shape_grad(j, q_index) * // grad phi_j(x_q)
|
||||
fe_values.JxW(q_index)); // dx
|
||||
|
||||
for (const unsigned int i : fe_values.dof_indices())
|
||||
cell_rhs(i) += (fe_values.shape_value(i, q_index) * // phi_i(x_q)
|
||||
rho * // f(x_q)
|
||||
fe_values.JxW(q_index)); // dx
|
||||
}
|
||||
cell->get_dof_indices(local_dof_indices);
|
||||
|
||||
for (const unsigned int i : fe_values.dof_indices())
|
||||
for (const unsigned int j : fe_values.dof_indices())
|
||||
system_matrix.add(local_dof_indices[i],
|
||||
local_dof_indices[j],
|
||||
cell_matrix(i, j));
|
||||
|
||||
for (const unsigned int i : fe_values.dof_indices())
|
||||
system_rhs(local_dof_indices[i]) += cell_rhs(i);
|
||||
}
|
||||
|
||||
|
||||
std::map<types::global_dof_index, double> boundary_values;
|
||||
VectorTools::interpolate_boundary_values(dof_handler,
|
||||
types::boundary_id(0),
|
||||
Functions::ZeroFunction<1>(),
|
||||
boundary_values);
|
||||
MatrixTools::apply_boundary_values(boundary_values,
|
||||
system_matrix,
|
||||
solution,
|
||||
system_rhs);
|
||||
}
|
||||
|
||||
|
||||
template<int dim>
|
||||
void Poisson_non_periodic<dim>::solve()
|
||||
{
|
||||
SolverControl solver_control(1000, 1e-6 * system_rhs.l2_norm());
|
||||
SolverCG<Vector<double>> solver(solver_control);
|
||||
solver.solve(system_matrix, solution, system_rhs, PreconditionIdentity());
|
||||
|
||||
std::cout << solver_control.last_step()
|
||||
<< " CG iterations needed to obtain convergence." << std::endl;
|
||||
}
|
||||
|
||||
template <int dim>
|
||||
void Poisson_non_periodic<dim>::output_results(unsigned int n)
|
||||
{
|
||||
|
||||
// --- extract DoF coordinates ---
|
||||
std::vector<Point<dim>> support_points(dof_handler.n_dofs());
|
||||
Vector<double> x_coordinate(dof_handler.n_dofs());
|
||||
|
||||
for (unsigned int i = 0; i < support_points.size(); ++i)
|
||||
x_coordinate[i] = support_points[i][0]; // x-component in 1D
|
||||
|
||||
//---- Output density ----
|
||||
ChargeDensity<dim> rho(Parameters::EPS, Parameters::WAVE_NR, Parameters::NV);
|
||||
|
||||
DataOut<dim> data_out_rho;
|
||||
data_out_rho.attach_dof_handler(dof_handler);
|
||||
|
||||
Vector<double> density(solution.size());
|
||||
VectorTools::interpolate(dof_handler, rho, density);
|
||||
|
||||
data_out_rho.add_data_vector(density, "density");
|
||||
data_out_rho.add_data_vector(x_coordinate, "x_coordinate");
|
||||
|
||||
data_out_rho.build_patches();
|
||||
|
||||
std::ofstream out1("results/density_" + std::to_string(n) + ".vtk");
|
||||
data_out_rho.write_vtk(out1);
|
||||
|
||||
//---- Output electric field & potential ----
|
||||
DataOut<dim> data_out_E;
|
||||
data_out_E.attach_dof_handler(dof_handler);
|
||||
|
||||
ElectricFieldPostprocessor<dim> electric_field;
|
||||
Vector<double> dummy(solution.size() * dim);
|
||||
|
||||
data_out_E.add_data_vector(solution, "potential");
|
||||
data_out_E.add_data_vector(solution, electric_field);
|
||||
data_out_E.add_data_vector(x_coordinate, "x_coordinate");
|
||||
|
||||
data_out_E.build_patches();
|
||||
|
||||
std::ofstream out2("results/electric_field_"+ std::to_string(n)+".vtk");
|
||||
data_out_E.write_vtk(out2);
|
||||
}
|
||||
|
||||
template <int dim>
|
||||
void Poisson_non_periodic<dim>::initialize()
|
||||
{
|
||||
make_mesh(); // build grid
|
||||
setup_system(); // distribute DoFs and matrices
|
||||
}
|
||||
|
||||
template <int dim>
|
||||
void Poisson_non_periodic<dim>::solve_step()
|
||||
{
|
||||
assemble_system();
|
||||
solve();
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,308 @@
|
||||
#ifndef POISSON_PROBLEM_H
|
||||
#define POISSON_PROBLEM_H
|
||||
|
||||
#include <deal.II/base/function.h>
|
||||
|
||||
#include <deal.II/base/quadrature_lib.h>
|
||||
#include <deal.II/base/logstream.h>
|
||||
#include <deal.II/base/tensor.h>
|
||||
#include <deal.II/base/utilities.h>
|
||||
#include <deal.II/base/index_set.h>
|
||||
|
||||
#include <deal.II/lac/vector.h>
|
||||
#include <deal.II/lac/full_matrix.h>
|
||||
#include <deal.II/lac/sparse_matrix.h>
|
||||
#include <deal.II/lac/dynamic_sparsity_pattern.h>
|
||||
#include <deal.II/lac/solver_cg.h>
|
||||
#include <deal.II/lac/precondition.h>
|
||||
#include <deal.II/lac/affine_constraints.h>
|
||||
|
||||
#include <deal.II/grid/tria.h>
|
||||
#include <deal.II/grid/grid_generator.h>
|
||||
#include <deal.II/grid/grid_tools.h>
|
||||
|
||||
#include <deal.II/dofs/dof_handler.h>
|
||||
#include <deal.II/dofs/dof_tools.h>
|
||||
#include <deal.II/dofs/dof_renumbering.h>
|
||||
|
||||
#include <deal.II/fe/fe_q.h>
|
||||
#include <deal.II/fe/fe_values.h>
|
||||
|
||||
#include <deal.II/numerics/data_out.h>
|
||||
#include <deal.II/numerics/vector_tools.h>
|
||||
#include <deal.II/numerics/fe_field_function.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "nufi/parameters.h"
|
||||
|
||||
using namespace dealii;
|
||||
|
||||
// =-=-=-=-= Poisson Solver =-=-=-=-=
|
||||
|
||||
template <int dim>
|
||||
class PoissonProblem
|
||||
{
|
||||
public:
|
||||
PoissonProblem(unsigned int degree);
|
||||
|
||||
void initialize();
|
||||
void solve_step();
|
||||
void run();
|
||||
|
||||
void set_rhs_function(std::unique_ptr<Function<dim>> rhs_function);
|
||||
|
||||
const Vector<double> &get_solution() const { return solution; }
|
||||
const DoFHandler<dim> &get_dof_handler() const { return dof_handler; }
|
||||
|
||||
std::vector<double> sample_electric_field(const PoissonProblem<dim> &problem, // sampling to save as spline
|
||||
unsigned int Nx,
|
||||
double x_min,
|
||||
double x_max);
|
||||
|
||||
private:
|
||||
void create_mesh();
|
||||
void setup_system();
|
||||
void assemble_system();
|
||||
void solve();
|
||||
|
||||
Triangulation<dim> triangulation;
|
||||
FE_Q<dim> fe;
|
||||
DoFHandler<dim> dof_handler;
|
||||
|
||||
AffineConstraints<double> constraints;
|
||||
|
||||
SparsityPattern sparsity_pattern;
|
||||
SparseMatrix<double> system_matrix;
|
||||
|
||||
Vector<double> solution; // phi
|
||||
Vector<double> system_rhs;
|
||||
|
||||
std::unique_ptr<const Function<dim>> rhs_function;
|
||||
|
||||
MappingQ<dim> mapping;
|
||||
};
|
||||
|
||||
// Utilities
|
||||
|
||||
template <int dim>
|
||||
void PoissonProblem<dim>::set_rhs_function(std::unique_ptr<Function<dim>> rhs)
|
||||
{
|
||||
rhs_function = std::move(rhs);
|
||||
}
|
||||
|
||||
template <int dim>
|
||||
PoissonProblem<dim>::PoissonProblem(unsigned int degree)
|
||||
: fe(degree)
|
||||
, dof_handler(triangulation)
|
||||
, mapping(degree)
|
||||
{}
|
||||
|
||||
template <int dim>
|
||||
std::vector<double> PoissonProblem<dim>::sample_electric_field(
|
||||
const PoissonProblem<dim> &problem,
|
||||
unsigned int Nx,
|
||||
double x_min,
|
||||
double x_max)
|
||||
{
|
||||
|
||||
const auto &dof_handler = problem.get_dof_handler();
|
||||
const auto &solution = problem.get_solution();
|
||||
|
||||
Functions::FEFieldFunction<dim, Vector<double>>
|
||||
field_function(dof_handler, solution, mapping);
|
||||
|
||||
std::vector<double> values(Nx);
|
||||
|
||||
double Lx = x_max - x_min;
|
||||
double dx = Lx / Nx;
|
||||
|
||||
for (unsigned int i = 0; i < Nx; ++i)
|
||||
{
|
||||
double x = x_min + i * dx;
|
||||
|
||||
Point<dim> p;
|
||||
p[0] = x;
|
||||
|
||||
Tensor<1, dim> grad = field_function.gradient(p);
|
||||
|
||||
values[i] = -grad[0]; // E = -dφ/dx
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
// dealii Poisson
|
||||
|
||||
template<int dim>
|
||||
void PoissonProblem<dim>::create_mesh()
|
||||
{
|
||||
|
||||
GridGenerator::hyper_cube(triangulation,
|
||||
Parameters::X_DOMAIN_LEFT,
|
||||
Parameters::X_DOMAIN_RIGHT);
|
||||
|
||||
// Make x-dim boundaries periodic
|
||||
Tensor<1, dim> offset;
|
||||
std::vector<GridTools::PeriodicFacePair<
|
||||
typename Triangulation<dim>::cell_iterator>> periodicity_vector;
|
||||
|
||||
GridTools::collect_periodic_faces(triangulation,
|
||||
0,
|
||||
1,
|
||||
0,
|
||||
periodicity_vector,
|
||||
offset);
|
||||
|
||||
triangulation.add_periodicity(periodicity_vector);
|
||||
|
||||
triangulation.refine_global(Parameters::GLOBAL_REFINEMENT);
|
||||
}
|
||||
|
||||
template <int dim>
|
||||
void PoissonProblem<dim>::setup_system()
|
||||
{
|
||||
|
||||
dof_handler.distribute_dofs(fe);
|
||||
|
||||
constraints.clear();
|
||||
DoFTools::make_hanging_node_constraints(dof_handler, constraints);
|
||||
|
||||
// 'boundary' condition phi(x_0) = 0
|
||||
constraints.add_line(0);
|
||||
constraints.set_inhomogeneity(0, 0.0);
|
||||
|
||||
constraints.close();
|
||||
|
||||
DynamicSparsityPattern dsp(dof_handler.n_dofs());
|
||||
DoFTools::make_sparsity_pattern(dof_handler, dsp, constraints);
|
||||
sparsity_pattern.copy_from(dsp);
|
||||
|
||||
system_matrix.reinit(sparsity_pattern);
|
||||
solution.reinit(dof_handler.n_dofs());
|
||||
system_rhs.reinit(dof_handler.n_dofs());
|
||||
}
|
||||
|
||||
// =-=-=-=-= E_field = -dPhi/dx =-=-=-=-=
|
||||
|
||||
template <int dim>
|
||||
class ElectricFieldPostprocessor : public DataPostprocessorVector<dim>
|
||||
{
|
||||
public:
|
||||
ElectricFieldPostprocessor()
|
||||
: DataPostprocessorVector<dim>("electric_field", update_gradients)
|
||||
{}
|
||||
|
||||
virtual void evaluate_scalar_field(
|
||||
const DataPostprocessorInputs::Scalar<dim> &input_data,
|
||||
std::vector<Vector<double>> &computed_quantities) const override
|
||||
{
|
||||
AssertDimension(input_data.solution_gradients.size(),
|
||||
computed_quantities.size());
|
||||
|
||||
for (unsigned int p = 0; p < input_data.solution_gradients.size(); ++p)
|
||||
{
|
||||
AssertDimension(computed_quantities[p].size(), dim);
|
||||
for (unsigned int d = 0; d < dim; ++d)
|
||||
computed_quantities[p][d] = -input_data.solution_gradients[p][d];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <int dim>
|
||||
void PoissonProblem<dim>::assemble_system()
|
||||
{
|
||||
QGauss<dim> quadrature_formula(fe.degree + 1);
|
||||
FEValues<dim> fe_values(fe, quadrature_formula,
|
||||
update_values |
|
||||
update_gradients |
|
||||
update_quadrature_points |
|
||||
update_JxW_values);
|
||||
|
||||
const unsigned int dofs_per_cell = fe.n_dofs_per_cell();
|
||||
const unsigned int n_q_points = quadrature_formula.size();
|
||||
|
||||
FullMatrix<double> cell_matrix(dofs_per_cell, dofs_per_cell);
|
||||
Vector<double> cell_rhs(dofs_per_cell);
|
||||
std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
|
||||
|
||||
Assert(rhs_function != nullptr, ExcMessage("RHS function not set"));
|
||||
|
||||
for (const auto &cell : dof_handler.active_cell_iterators())
|
||||
{
|
||||
fe_values.reinit(cell);
|
||||
cell_matrix = 0;
|
||||
cell_rhs = 0;
|
||||
|
||||
for (unsigned int q = 0; q < n_q_points; ++q)
|
||||
{
|
||||
const double rho = rhs_function->value(fe_values.quadrature_point(q));
|
||||
|
||||
for (unsigned int i = 0; i < dofs_per_cell; ++i)
|
||||
{
|
||||
for (unsigned int j = 0; j < dofs_per_cell; ++j)
|
||||
cell_matrix(i, j) +=
|
||||
fe_values.shape_grad(i, q) *
|
||||
fe_values.shape_grad(j, q) *
|
||||
fe_values.JxW(q);
|
||||
|
||||
cell_rhs(i) +=
|
||||
fe_values.shape_value(i, q) *
|
||||
rho *
|
||||
fe_values.JxW(q);
|
||||
}
|
||||
}
|
||||
|
||||
cell->get_dof_indices(local_dof_indices);
|
||||
constraints.distribute_local_to_global(cell_matrix,
|
||||
cell_rhs,
|
||||
local_dof_indices,
|
||||
system_matrix,
|
||||
system_rhs);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <int dim>
|
||||
void PoissonProblem<dim>::solve()
|
||||
{
|
||||
|
||||
SolverControl solver_control(1000, 1e-12);
|
||||
SolverCG<Vector<double>> solver(solver_control);
|
||||
|
||||
PreconditionSSOR<SparseMatrix<double>> preconditioner;
|
||||
preconditioner.initialize(system_matrix, 1.2);
|
||||
|
||||
solver.solve(system_matrix, solution, system_rhs, preconditioner);
|
||||
constraints.distribute(solution);
|
||||
}
|
||||
|
||||
template <int dim>
|
||||
void PoissonProblem<dim>::initialize()
|
||||
{
|
||||
create_mesh(); // build grid
|
||||
setup_system(); // distribute DoFs and matrices
|
||||
}
|
||||
|
||||
template <int dim>
|
||||
void PoissonProblem<dim>::solve_step()
|
||||
{
|
||||
assemble_system();
|
||||
solve();
|
||||
}
|
||||
|
||||
|
||||
// NuFI doesnt use this, kept only for testing PoissonProblem
|
||||
template <int dim>
|
||||
void PoissonProblem<dim>::run()
|
||||
{
|
||||
create_mesh();
|
||||
setup_system();
|
||||
assemble_system();
|
||||
solve();
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef SAVE_RESULTS_H
|
||||
#define SAVE_RESULTS_H
|
||||
|
||||
#include <string>
|
||||
#include "nufi/nufi_solver.h"
|
||||
|
||||
|
||||
void save_ftilda( const NuFISolver &solver,
|
||||
unsigned int n,
|
||||
const double *E_coeffs,
|
||||
unsigned int Nx_out,
|
||||
unsigned int Nv_out,
|
||||
const std::string &filename);
|
||||
|
||||
void save_rho(const NuFISolver &solver,
|
||||
unsigned int n,
|
||||
const double *E_coeffs,
|
||||
unsigned int Nx_out,
|
||||
const std::string &filename);
|
||||
|
||||
void save_Efield(unsigned int n,
|
||||
const double *E_coeffs,
|
||||
unsigned int Nx_out,
|
||||
const std::string &filename);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,90 @@
|
||||
#ifndef SPLINES_H
|
||||
#define SPLINES_H
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace splines1d
|
||||
{
|
||||
|
||||
template <typename real>
|
||||
constexpr real faculty( size_t n ) noexcept
|
||||
{
|
||||
return (n > 1) ? real(n)*faculty<real>(n-1) : real(1);
|
||||
}
|
||||
|
||||
template <typename real, size_t order, size_t derivative = 0>
|
||||
void N( real x, real *result, size_t stride = 1 ) noexcept
|
||||
{
|
||||
static_assert( order > 0, "Splines must have order greater than zero." );
|
||||
constexpr int n { order };
|
||||
constexpr int d { derivative };
|
||||
|
||||
if ( derivative >= order )
|
||||
for ( size_t i = 0; i < order; ++i )
|
||||
result[ i*stride ] = 0;
|
||||
|
||||
if ( n == 1 )
|
||||
{
|
||||
*result = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
real v[n]; v[n-1] = 1;
|
||||
for ( int k = 1; k < n - d; ++k )
|
||||
{
|
||||
v[n-k-1] = (1-x)*v[n-k];
|
||||
|
||||
for ( int i = 1-k; i < 0; ++i )
|
||||
v[n-1+i] = (x-i)*v[n-1+i] + (k+1+i-x)*v[n+i];
|
||||
|
||||
v[n-1] *= x;
|
||||
}
|
||||
|
||||
// Differentiate if necessary.
|
||||
for ( size_t j = derivative; j-- > 0; )
|
||||
{
|
||||
v[j] = -v[j+1];
|
||||
for ( size_t i = j + 1; i < order - 1; ++i )
|
||||
v[i] = v[i] - v[i+1];
|
||||
}
|
||||
|
||||
constexpr real factor = real(1) / faculty<real>(order-derivative-1);
|
||||
for ( size_t i = 0; i < order; ++i )
|
||||
result[i*stride] = v[i]*factor;
|
||||
}
|
||||
|
||||
template <typename real, size_t order, size_t derivative = 0>
|
||||
real eval( real x, const real *coefficients, size_t stride = 1 ) noexcept
|
||||
{
|
||||
static_assert( order > 0, "Splines must have order greater than zero." );
|
||||
static_assert( order > derivative, "Too high derivative requested." );
|
||||
constexpr size_t n { order };
|
||||
constexpr size_t d { derivative };
|
||||
|
||||
if ( d >= n ) return 0;
|
||||
if ( n == 1 ) return *coefficients;
|
||||
|
||||
// Gather coefficients.
|
||||
real c[ order ];
|
||||
for ( size_t j = 0; j < order; ++j )
|
||||
c[j] = coefficients[ stride * j ];
|
||||
|
||||
// Differentiate if necessary.
|
||||
for ( size_t j = 1; j <= d; ++j )
|
||||
for ( size_t i = n; i-- > j; )
|
||||
c[i] = c[i] - c[i-1];
|
||||
|
||||
// Evaluate using de Boor’s algorithm.
|
||||
for ( size_t j = 1; j < n-d; ++j )
|
||||
for ( size_t i = n-d; i-- > j; )
|
||||
c[d+i] = (x+n-d-1-i)*c[d+i] + (i-j+1-x)*c[d+i-1];
|
||||
|
||||
constexpr real factor = real(1) / faculty<real>(order-derivative-1);
|
||||
return factor*c[n-1];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user