Toast++  1.0.2 (r.539)
Forward and inverse modelling in optical tomography
fwdsolver.h
1 // -*-C++-*-
2 // ==========================================================================
3 // Forward model: finite element method
4 // ==========================================================================
5 
6 #ifndef __FWDSOLVER_H
7 #define __FWDSOLVER_H
8 
9 //#include "supermatrix.h"
10 #include "toasttype.h"
11 #include "mathlib.h"
12 
13 class Solution;
14 class MWsolution;
15 
17 
18 enum LSOLVER{
22 };
24 
26 
27 enum DataScale {
31 };
33 
34 // =========================================================================
35 // Nonmember declarations
36 
37 template<class T> class TFwdSolver;
38 
39 template<class T>
40 STOASTLIB TVector<T> ProjectSingle (const QMMesh *mesh, int q,
41  const TCompRowMatrix<T> &mvec, const TVector<T> &phi,
43 
44 template<class T>
45 STOASTLIB TVector<T> ProjectAll (const QMMesh *mesh,
46  const TCompRowMatrix<T> &mvec, const TVector<T> *phi,
48 
49 // =========================================================================
50 
77 template<class T> class TFwdSolver {
78 public:
85  TFwdSolver (const QMMesh *mesh, LSOLVER linsolver, double tol = 1e-10);
86 
95  TFwdSolver (const QMMesh *mesh, const char *solver, double tol = 1e-10);
96 
104  TFwdSolver (const QMMesh *mesh, ParamParser &pp);
105 
109  ~TFwdSolver ();
110 
121  void SetDataScaling (DataScale scl);
122 
129  DataScale GetDataScaling () const;
130 
140  void SetPrecon (PreconType type);
141 
142  void SetPhaseUnwrap (bool unwrap)
143  { unwrap_phase = unwrap; }
144 
162  void ReadParams (ParamParser &pp);
163 
171  void WriteParams (ParamParser &pp);
172 
187  void SetLinSolver (const char *solver, double tol = 1e-10);
188 
193  inline LSOLVER LinSolver() const { return solvertp; }
194 
200  inline double GetLinSolverTol() const { return iterative_tol; }
201 
208  inline void SetLinSolverMaxit (int maxit) { iterative_maxit = maxit; }
209 
215  inline const QMMesh *MeshPtr() const { return meshptr; }
216 
229  void Allocate ();
230 
241  void AssembleSystemMatrix (const Solution &sol, double omega = 0,
242  bool elbasis = false);
243 
244  void AssembleSystemMatrixComponent (const RVector &prm, int type);
245 
254  void AssembleMassMatrix (const Mesh *mesh = 0);
255 
274  void Reset (const Solution &sol, double omega = 0, bool elbasis = false);
275 
285  void CalcField (const TVector<T> &qvec, TVector<T> &phi,
286  IterativeSolverResult *res = 0) const;
287 
299  void CalcFields (const TCompRowMatrix<T> &qvec, TVector<T> *phi,
300  IterativeSolverResult *res = 0) const;
301 
315  void CalcFields (int q0, int q1, const TCompRowMatrix<T> &qvec,
316  TVector<T> *phi, IterativeSolverResult *res = 0) const;
317 
326  TVector<T> ProjectSingle (int q, const TCompRowMatrix<T> &mvec,
327  const TVector<T> &phi, DataScale scl = DATA_DEFAULT) const;
328 
339  const TVector<T> *dphi, DataScale scl = DATA_DEFAULT);
340 
351  const TCompRowMatrix<T> &mvec, const Solution &sol, double omega,
352  DataScale scl = DATA_DEFAULT);
353 
366  const TVector<T> *dphi, DataScale scl = DATA_DEFAULT);
367  FVector ProjectAll_singlereal (const TCompRowMatrix<T> &mvec,
368  const TVector<T> *dphi, DataScale scl = DATA_DEFAULT);
369 
387  const TCompRowMatrix<T> &mvec, const Solution &sol, double omega,
388  DataScale scl = DATA_DEFAULT);
389  FVector ProjectAll_singlereal (const TCompRowMatrix<T> &qvec,
390  const TCompRowMatrix<T> &mvec, const Solution &sol, double omega,
391  DataScale scl = DATA_DEFAULT);
392 
393  const QMMesh *meshptr;
395  IterativeMethod method;
396 #ifdef MPI_FWDSOLVER
398 #else
400 #endif
405  //mutable SuperLU_data<T> lu_data; ///< parameters for LU solver
406  void *SuperLU;
407  double iterative_tol;
410 
411 protected:
412  void Setup ();
413  void SetupType ();
414  void DeleteType ();
415 
425  RVector UnfoldComplex (const TVector<T> &vec) const;
426  FVector UnfoldSComplex (const TVector<T> &vec) const;
427 
428 #ifdef UNDEF
429  void UnfoldComplex (const TVector<T> &vec, RVector &res) const;
430  void UnfoldComplex (const TVector<T> &vec, FVector &res) const;
431 #endif
432 
436 
437  // ===============================================================
438  // MPI-specific functions and data members
439 
440 #ifdef MPI_FWDSOLVER
441 
442 public:
443 
449  void Setup_MPI();
450 
454  void Cleanup_MPI();
455 
463  void DistributeSources_MPI (int nq) const;
464 
477  void CalcFields_proc (const TCompRowMatrix<T> &qvec,
478  TVector<T> *phi) const;
479 
492  void ProjectAll_proc (const TCompRowMatrix<T> &mvec,
493  const TVector<T> *phi, DataScale scl, TVector<T> &proj);
494 
495 protected:
496 
497  MPI_Datatype mpitp;
498  int rnk;
499  int sze;
500  mutable int nQ;
501  mutable int *Q0, *Q1;
502 
503 #endif // MPI_FWDSOLVER
504 
505 #if THREAD_LEVEL==2
506  int nthread;
507 #endif
508 };
509 
510 
511 // OBSOLETE
512 STOASTLIB void Project_cplx (const QMMesh &mesh, int q, const CVector &phi,
513  CVector &proj);
514 
515 // ==========================================================================
516 // Some macros
517 
518 #ifdef MPI_FWDSOLVER
519 #define SETUP_MPI() Setup_MPI()
520 #define CLEANUP_MPI() Cleanup_MPI()
521 #else
522 #define SETUP_MPI()
523 #define CLEANUP_MPI()
524 #endif
525 
526 // ==========================================================================
527 // template typedefs
528 
533 
534 // ==========================================================================
535 // extern declarations of FwdSolver (only required for VS)
536 
537 #ifndef __FWDSOLVER_CC
538 extern template class STOASTLIB TFwdSolver<float>;
539 extern template class STOASTLIB TFwdSolver<double>;
540 extern template class STOASTLIB TFwdSolver<std::complex<float> >;
541 extern template class STOASTLIB TFwdSolver<std::complex<double> >;
542 #endif // !__FWDSOLVER_CC
543 
544 #endif // !__FWDSOLVER_H
void AssembleMassMatrix(const Mesh *mesh=0)
Construct the FEM mass matrix for time-dependent problems.
void ReadParams(ParamParser &pp)
Read solver parameters from a parameter file.
void WriteParams(ParamParser &pp)
Write the current solver settings to a parameter file.
LSOLVER solvertp
linear solver type
Definition: fwdsolver.h:394
TVector< T > ProjectAll(const TCompRowMatrix< T > &mvec, const TVector< T > *dphi, DataScale scl=DATA_DEFAULT)
Return boundary data for all sources and all detectors, given photon density fields for all sources...
LSOLVER
Definition: fwdsolver.h:18
Definition: pparse.h:11
PreconType
Definition: precon.h:23
void Reset(const Solution &sol, double omega=0, bool elbasis=false)
Reset the forward solver by re-building the system matrices from a new set of parameters.
double iterative_tol
iterative solver tolerance
Definition: fwdsolver.h:407
void SetLinSolver(const char *solver, double tol=1e-10)
Set the solver type and tolerance.
TVector< T > * pphi
work buffer for field calculation
Definition: fwdsolver.h:434
direct solver (LU)
Definition: fwdsolver.h:20
DataScale GetDataScaling() const
Returns the current default setting for data scaling.
TVector< T > ProjectSingle(int q, const TCompRowMatrix< T > &mvec, const TVector< T > &phi, DataScale scl=DATA_DEFAULT) const
Return boundary data for a single source, given the corresponding photon density field.
void SetPrecon(PreconType type)
Set the preconditioning method.
void SetLinSolverMaxit(int maxit)
Set max iteration count for iterative solver.
Definition: fwdsolver.h:208
Finite-element mesh management.
Definition: mesh.h:145
DataScale
Definition: fwdsolver.h:27
void * SuperLU
SuperLU solver engine.
Definition: fwdsolver.h:406
TFwdSolver(const QMMesh *mesh, LSOLVER linsolver, double tol=1e-10)
Constructor. Creates a forward solver instance.
Templated forward solver class.
Definition: fwdsolver.h:37
const QMMesh * meshptr
pointer to the associated FEM mesh
Definition: fwdsolver.h:393
TCompRowMatrix< T > * FL
lower triangle of system matrix decomposition
Definition: fwdsolver.h:401
void Allocate()
Evaluates fill structure of system matrices and allocates dynamic memory for them.
const QMMesh * MeshPtr() const
Returns a pointer to the FEM mesh associated with the forward solver instance.
Definition: fwdsolver.h:215
Definition: qmmesh.h:22
RVector UnfoldComplex(const TVector< T > &vec) const
Unfold real and imaginary parts of a complex vector.
TCompRowMatrix< T > * F
FEM system matrix.
Definition: fwdsolver.h:399
void CalcField(const TVector< T > &qvec, TVector< T > &phi, IterativeSolverResult *res=0) const
Calculate photon density field for a given source vector.
Distributed compressed-row sparse matrix class.
Definition: crmatrix_mpi.h:33
int iterative_maxit
iterative solver max iterations (0 for auto)
Definition: fwdsolver.h:408
RVector ProjectAll_real(const TCompRowMatrix< T > &mvec, const TVector< T > *dphi, DataScale scl=DATA_DEFAULT)
Return boundary data for the complex case in a real vector.
bool unwrap_phase
use phase unwrapping?
Definition: fwdsolver.h:435
void SetDataScaling(DataScale scl)
Set the default scaling for data returned by the projection methods.
TCompRowMatrix< T > * B
mass matrix; only used for time-domain problems
Definition: fwdsolver.h:404
linear scaling
Definition: fwdsolver.h:29
double GetLinSolverTol() const
Returns the solver tolerance.
Definition: fwdsolver.h:200
Definition: mwsolution.h:8
iterative solver
Definition: fwdsolver.h:21
TVector< T > * Fd
diagonal of Cholesky factorisation
Definition: fwdsolver.h:402
undefined
Definition: fwdsolver.h:19
LSOLVER LinSolver() const
Returns the current solver type.
Definition: fwdsolver.h:193
void AssembleSystemMatrix(const Solution &sol, double omega=0, bool elbasis=false)
Construct the FEM system matrix from a set of parameter distributions for a given modulation frequenc...
TPreconditioner< T > * precon
preconditioner instance
Definition: fwdsolver.h:403
PreconType precontp
preconditioner
Definition: fwdsolver.h:409
void CalcFields(const TCompRowMatrix< T > &qvec, TVector< T > *phi, IterativeSolverResult *res=0) const
Calculate photon density fields for all sources.
Definition: matrix.h:34
DataScale dscale
default data scaling: DATA_LIN or DATA_LOG
Definition: fwdsolver.h:433
Definition: solution.h:39
~TFwdSolver()
Destructor. Destroys the forward solver instance.
default method
Definition: fwdsolver.h:28
logarithmic scaling
Definition: fwdsolver.h:30
IterativeMethod method
iterative solver method, if applicable
Definition: fwdsolver.h:395