C++QEDCore  2.100.2 (v2 Milestone 10 Development branch)
a framework for simulating open quantum dynamics – core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MatrixOfHamiltonian.h
1 // Copyright András Vukics 2006–2014. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.txt)
2 // -*- C++ -*-
3 #ifndef CPPQEDCORE_STRUCTURE_MATRIXOFHAMILTONIAN_H_INCLUDED
4 #define CPPQEDCORE_STRUCTURE_MATRIXOFHAMILTONIAN_H_INCLUDED
5 
6 #include "StateVector.tcc"
7 
8 // NEEDS_WORK the input of hamiltonian should be sharedPointerized here, in which case RANK could be extracted through a traits class
9 
10 template<typename T> // T should model both Hamiltonian and DimensionsBookkeeper
11 const linalg::CMatrix
12 calculateMatrix(const T& hamiltonian, double t=0, double t0=0)
13 {
14  using namespace linalg;
15 
16  static const int RANK=T::DIMESIONS_BOOKKEEPER_RANK;
18 
19  const int dim=hamiltonian.getTotalDimension();
20 
21  StateVector
22  psi (hamiltonian.getDimensions()),
23  dpsidt(psi);
24  // both are now 0
25 
26  CMatrix res(dim,dim);
27 
28  CVector psi1D(psi.vectorView());
29 
30  const CVector dpsidt1D(dpsidt.vectorView());
31 
32  // Since the hamiltonian is in general non-Hermitian, all matrix elements have to be separately calculated.
33  for (int row=0; row<dim; ++row) {
34  psi1D(row)=1;
35  for (int col=0; col<dim; ++col) {
36  if (const auto ha=dynamic_cast<const structure::Hamiltonian<RANK>*>(&hamiltonian))
37  ha->addContribution(t,psi.getArray(),dpsidt.getArray(),t0);
38  res(col,row)=DCOMP_I*dpsidt1D(col);
39  // note that addContribution is meant to calculate Hamiltonian over DCOMP_I, which we correct here
40  dpsidt=0;
41  }
42  psi1D(row)=0;
43  }
44 
45  return res;
46 
47 }
48 
49 
50 
51 
52 #endif // CPPQEDCORE_STRUCTURE_MATRIXOFHAMILTONIAN_H_INCLUDED
CArray< 1 > CVector
Complex vector.
Definition: CVector.h:13
quantumdata::StateVector< 1 > StateVector
unary StateVector
Definition: Free.h:39
State vector of arbitrary arity.
Definition: StateVector.h:58
const dcomp DCOMP_I(0, 1)
Imaginary unit.
The interface every system having (possibly non-Hermitian) Hamiltonian time-evolution must present to...
Definition: Hamiltonian.h:27
Contains utilities for linear algebra.
Definition: CMatrix.h:11
CArray< 2 > CMatrix
Complex matrix.
Definition: CMatrix.h:14