EigenUnitTest.cpp File Reference
#include <Eigen/Sparse>
#include <vector>
#include <iostream>
#include "Helpers/Helpers.h"

Typedefs

typedef Eigen::SparseMatrix< doublemat
 
typedef Eigen::Triplet< doubleT
 

Functions

int main (int argc, char **argv)
 

Variables

std::vector< TtripletList
 

Typedef Documentation

◆ mat

◆ T

Function Documentation

◆ main()

int main ( int argc  ,
char **  argv 
)
15  {
16  double L = 1;
17  int varnum = 5;
18  double dx = L/(varnum-1);
19  double dt = pow(dx,2);//does not need to be this, but keeps it anyway
20  double A = dt/2.0/pow(dx,2);
21  double B = -(1+A*2);
22 
23 
24  //construct coefficient sparse matrix
25  //diagonal elements
26  for(int i=0; i<varnum; i++) {
27  tripletList.push_back(T(i, i, B));
28  }
29 
30  //right-diagonal elements
31  for(int j=1; j<varnum; j++){
32  tripletList.push_back(T(j-1,j,A));
33  }
34  //left-diagonal elements
35  for(int k=0; k<varnum-1; k++){
36  tripletList.push_back(T(k+1, k, A));
37  }
38 
39  //distribute the elements to the matrix
40  mat C(varnum,varnum);
41  C.setFromTriplets(tripletList.begin(), tripletList.end());
42 
43  //construct RHS vector
44  Eigen::VectorXd T(varnum), K(varnum);
45  for(int i=0; i<varnum; i++)
46  T(i) = 0;
47 
48  for(int j=1; j<varnum-1; j++){
49  K(j) = -T(j)-A*(T(j+1)-2*T(j)+T(j-1));
50  }
51  K(0) = -T(0)-A*(T(1)-2*T(0));
52  K(varnum-1) = -T(varnum-1)-A*(1-2*T(varnum-1)+T(varnum-2))-A;
53 
54 
55  //iterate with time
57  for(double t=0; t<dt*100; t+=dt){
58  solver.compute(C);
59  T = solver.solve(K);
60 
61  for(int j=1; j<varnum-1; j++){
62  K(j) = -T(j)-A*(T(j+1)-2*T(j)+T(j-1));
63  }
64  K(0) = -T(0)-A*(T(1)-2*T(0));
65  K(varnum-1) = -T(varnum-1)-A*(1-2*T(varnum-1)+T(varnum-2))-A;
66 
67  //output the values at the grid points to the console
68  std::cout << "t=" << t << "s" << "\t" << T(0) << "\t" << T((varnum-1)*1/4) << "\t" << T((varnum-1)/2) << "\t" << T((varnum-1)*3/4) << "\t" << T(varnum-1)<< std::endl;
69  }
70  //check last timestep
71  helpers::check(T(0),0.166667,1e-5, "Gridpoint 0");
72  helpers::check(T((varnum-1)*1/4),0.333333,1e-5, "Gridpoint 1/4");
73  helpers::check(T((varnum-1)*2/4),0.5,1e-5, "Gridpoint 2/4");
74  helpers::check(T((varnum-1)*3/4),0.666667,1e-5, "Gridpoint 3/4");
75  helpers::check(T(varnum-1),0.833333,1e-5, "Gridpoint 1");
76 }
BiCGSTAB< SparseMatrix< double > > solver
Definition: BiCGSTAB_simple.cpp:5
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Array< double, 1, 3 > e(1./3., 0.5, 2.)
std::vector< T > tripletList
Definition: EigenUnitTest.cpp:12
Eigen::Triplet< double > T
Definition: EigenUnitTest.cpp:11
MatrixXd L
Definition: LLT_example.cpp:6
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
Matrix< Scalar, Dynamic, Dynamic > C
Definition: bench_gemm.cpp:49
EIGEN_DEVICE_FUNC const GlobalUnaryPowReturnType< Derived, ScalarExponent > pow(const Eigen::ArrayBase< Derived > &x, const ScalarExponent &exponent)
Definition: GlobalFunctions.h:137
A conjugate gradient solver for sparse (or dense) self-adjoint problems.
Definition: ConjugateGradient.h:152
Definition: matrices.h:74
char char char int int * k
Definition: level2_impl.h:374
double K
Wave number.
Definition: sphere_scattering.cc:115
void check(double real, double ideal, double error, std::string errorMessage)
Definition: TestHelpers.cc:16
t
Definition: plotPSD.py:36
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References helpers::check(), e(), i, j, k, PlanarWave::K, L, Eigen::ArrayBase< Derived >::pow(), solver, plotPSD::t, and tripletList.

Variable Documentation

◆ tripletList

std::vector<T> tripletList

Referenced by main().