Quandary
Loading...
Searching...
No Matches
timestepper.hpp
Go to the documentation of this file.
1#include <petsc/private/tsimpl.h> /*I "petscts.h" I*/
2#include <petscts.h>
3#include <petscksp.h>
4#include "mastereq.hpp"
5#include <assert.h>
6#include <iostream>
7#include "defs.hpp"
8#include "output.hpp"
9#include "optimtarget.hpp"
10#include <deque>
11#pragma once
12
34 protected:
35 Vec x;
36 Vec xadj;
37 Vec xprimal;
38 std::vector<Vec> store_states;
39 std::vector<Vec> dpdm_states;
43 PetscInt localsize_u;
44 PetscInt ilow;
45 PetscInt iupp;
46
49 bool eval_dpdm;
51
57
58 public:
60 int ntime;
61 double total_time;
62 double dt;
64
65 Vec redgrad;
66
69
70 public:
71 bool storeFWD;
72
73 TimeStepper();
74
84 TimeStepper(MasterEq* mastereq_, int ntime_, double total_time_, Output* output_, bool storeFWD_);
85
86 virtual ~TimeStepper();
87
94 Vec getState(size_t tindex);
95
96 void setEvalLeakage(bool flag){ eval_leakage = flag; };
97 void setEvalWeightedCost(bool flag, double width){ eval_weightedcost = flag; weightedcost_width = width; };
98 void setEvalDPDM(bool flag){ eval_dpdm = flag; };
99 void setEvalEnergy(bool flag){ eval_energy = flag; };
100
104 double getDPDMIntegral(){ return dpdm_integral; };
105
115 Vec solveODE(int initid, Vec rho_t0);
116
130 void solveAdjointODE(Vec rho_t0_bar, Vec finalstate, double Jbar_leakage, double Jbar_weightedcost, double Jbar_dpdm, double Jbar_energy);
131
138 double evalLeakage(const Vec x);
139
147 void evalLeakage_diff(const Vec x, Vec xbar, double Jbar);
148
156 double evalWeightedCost(double time, const Vec x);
157
166 void evalWeightedCost_diff(double time, const Vec x, Vec xbar, double Jbar);
167
176 double evalDpDm(Vec x, Vec xm1, Vec xm2);
177
185 void evalDpDm_diff(int n, Vec xbar, double Jbar);
186
193 double evalEnergy(double time);
194
202 void evalEnergy_diff(double time, double Jbar, Vec redgrad);
203
213 virtual void evolveFWD(const double tstart, const double tstop, Vec x) = 0;
214
227 virtual void evolveBWD(const double tstart, const double tstop, const Vec x_stop, Vec x_adj, Vec grad, bool compute_gradient);
228};
229
236class ExplEuler : public TimeStepper {
237 protected:
238 Vec stage;
239 public:
249 ExplEuler(MasterEq* mastereq_, int ntime_, double total_time_, Output* output_, bool storeFWD_);
250
251 ~ExplEuler();
252
260 void evolveFWD(const double tstart, const double tstop, Vec x);
261
272 void evolveBWD(const double tstart, const double tstop, const Vec x_stop, Vec x_adj, Vec grad, bool compute_gradient);
273};
274
288class ImplMidpoint : public TimeStepper {
289 protected:
292 KSP ksp;
301 Vec tmp, err;
302
303 public:
315 ImplMidpoint(MasterEq* mastereq_, int ntime_, double total_time_, LinearSolverType linsolve_type_, int linsolve_maxiter_, Output* output_, bool storeFWD_);
316
318
326 virtual void evolveFWD(const double tstart, const double tstop, Vec x);
327
338 virtual void evolveBWD(const double tstart, const double tstop, const Vec x_stop, Vec x_adj, Vec grad, bool compute_gradient);
339
350 int NeumannSolve(Mat A, Vec b, Vec x, double alpha, bool transpose);
351};
352
361 protected:
362
363 std::vector<double> gamma;
364 std::vector<Vec> x_stage;
365 Vec aux;
366 int order;
367
368 public:
381 CompositionalImplMidpoint(int order_, MasterEq* mastereq_, int ntime_, double total_time_, LinearSolverType linsolve_type_, int linsolve_maxiter_, Output* output_, bool storeFWD_);
382
384
392 void evolveFWD(const double tstart, const double tstop, Vec x);
393
404 void evolveBWD(const double tstart, const double tstop, const Vec x_stop, Vec x_adj, Vec grad, bool compute_gradient);
405};
Compositional implicit midpoint rule for higher-order accuracy.
Definition timestepper.hpp:360
int order
Order of the compositional method.
Definition timestepper.hpp:366
void evolveFWD(const double tstart, const double tstop, Vec x)
Evolves state forward using compositional implicit midpoint rule.
Definition timestepper.cpp:792
~CompositionalImplMidpoint()
Definition timestepper.cpp:784
void evolveBWD(const double tstart, const double tstop, const Vec x_stop, Vec x_adj, Vec grad, bool compute_gradient)
Evolves adjoint backward using compositional implicit midpoint rule and accumulates gradient.
Definition timestepper.cpp:812
std::vector< Vec > x_stage
Storage for primal states at intermediate stages.
Definition timestepper.hpp:364
std::vector< double > gamma
Coefficients for compositional step sizes.
Definition timestepper.hpp:363
Vec aux
Auxiliary vector.
Definition timestepper.hpp:365
Explicit Euler time integration scheme.
Definition timestepper.hpp:236
void evolveBWD(const double tstart, const double tstop, const Vec x_stop, Vec x_adj, Vec grad, bool compute_gradient)
Evolves adjoint backward using explicit Euler method.
Definition timestepper.cpp:514
~ExplEuler()
Definition timestepper.cpp:497
void evolveFWD(const double tstart, const double tstop, Vec x)
Evolves state forward using explicit Euler method.
Definition timestepper.cpp:501
Vec stage
Intermediate vector.
Definition timestepper.hpp:238
Implicit midpoint rule time integration scheme.
Definition timestepper.hpp:288
~ImplMidpoint()
Definition timestepper.cpp:567
LinearSolverType linsolve_type
Linear solver type (GMRES or NEUMANN)
Definition timestepper.hpp:294
virtual void evolveFWD(const double tstart, const double tstop, Vec x)
Evolves state forward using implicit midpoint rule.
Definition timestepper.cpp:592
int linsolve_iterstaken_avg
Average number of linear solver iterations.
Definition timestepper.hpp:298
Vec rhs
Definition timestepper.hpp:291
PC preconditioner
Preconditioner for linear solver.
Definition timestepper.hpp:293
double linsolve_error_avg
Average error of linear solver.
Definition timestepper.hpp:299
int NeumannSolve(Mat A, Vec b, Vec x, double alpha, bool transpose)
Solves (I - alpha*A) * x = b using Neumann iterations.
Definition timestepper.cpp:705
double linsolve_reltol
Relative tolerance for linear solver.
Definition timestepper.hpp:297
Vec tmp
Definition timestepper.hpp:301
int linsolve_maxiter
Maximum number of linear solver iterations.
Definition timestepper.hpp:295
Vec rhs_adj
Right-hand side vectors for forward and adjoint.
Definition timestepper.hpp:291
Vec stage
Definition timestepper.hpp:290
Vec err
Auxiliary vectors for Neumann iterations.
Definition timestepper.hpp:301
virtual void evolveBWD(const double tstart, const double tstop, const Vec x_stop, Vec x_adj, Vec grad, bool compute_gradient)
Evolves adjoint backward using implicit midpoint rule and adds to reduced gradient.
Definition timestepper.cpp:639
int linsolve_counter
Counter for linear solve calls.
Definition timestepper.hpp:300
double linsolve_abstol
Absolute tolerance for linear solver.
Definition timestepper.hpp:296
Vec stage_adj
Intermediate stage vectors for forward and adjoint.
Definition timestepper.hpp:290
KSP ksp
PETSc's linear solver context for GMRES.
Definition timestepper.hpp:292
Implementation of the real-valued right-hand-side (RHS) system matrix of the quantum dynamical equati...
Definition mastereq.hpp:92
Optimization target specification for quantum control.
Definition optimtarget.hpp:22
Output management for quantum control simulations and optimization.
Definition output.hpp:15
Base class for time integration schemes to evolve the quantum dynamics.
Definition timestepper.hpp:33
double evalWeightedCost(double time, const Vec x)
Evaluates the weighted cost function term.
Definition timestepper.cpp:317
double weightedcost_integral
Sums the integral over weighted cost function.
Definition timestepper.hpp:55
Vec redgrad
Reduced gradient vector for optimization.
Definition timestepper.hpp:65
int mpirank_petsc
MPI rank in Petsc communicator.
Definition timestepper.hpp:42
MasterEq * mastereq
Pointer to master equation solver.
Definition timestepper.hpp:59
int ntime
Number of time steps.
Definition timestepper.hpp:60
PetscInt iupp
Last index (+1) of the local sub vector u,v.
Definition timestepper.hpp:45
bool writeTrajectoryDataFiles
Flag to determine whether or not trajectory data will be written to files during forward simulation *...
Definition timestepper.hpp:63
virtual ~TimeStepper()
Definition timestepper.cpp:73
Vec xprimal
Auxiliary vector for backward time stepping.
Definition timestepper.hpp:37
std::vector< Vec > dpdm_states
Storage for states needed for second-order derivative penalty.
Definition timestepper.hpp:39
double getDPDMIntegral()
Definition timestepper.hpp:104
int mpirank_world
MPI rank in global communicator.
Definition timestepper.hpp:40
void setEvalWeightedCost(bool flag, double width)
Definition timestepper.hpp:97
double evalLeakage(const Vec x)
Evaluates leakage into guard levels.
Definition timestepper.cpp:263
void solveAdjointODE(Vec rho_t0_bar, Vec finalstate, double Jbar_leakage, double Jbar_weightedcost, double Jbar_dpdm, double Jbar_energy)
Solves the adjoint ODE backward in time.
Definition timestepper.cpp:189
double getEnergyIntegral()
Definition timestepper.hpp:103
void evalEnergy_diff(double time, double Jbar, Vec redgrad)
Computes derivative of energy integral.
Definition timestepper.cpp:467
double total_time
Final evolution time.
Definition timestepper.hpp:61
OptimTarget * optim_target
Pointer to optimization target specification.
Definition timestepper.hpp:67
Output * output
Pointer to output handler.
Definition timestepper.hpp:68
double dpdm_integral
Sums second-order derivative variation value.
Definition timestepper.hpp:54
double evalEnergy(double time)
Evaluates energy integral term.
Definition timestepper.cpp:453
bool eval_weightedcost
Flag to compute weighted cost integral term.
Definition timestepper.hpp:50
virtual void evolveBWD(const double tstart, const double tstop, const Vec x_stop, Vec x_adj, Vec grad, bool compute_gradient)
Evolves adjoint state backward by one time-step and updates reduced gradient.
Definition timestepper.cpp:490
Vec x
Auxiliary vector for forward time stepping.
Definition timestepper.hpp:35
PetscInt ilow
First index of the local sub vector u,v.
Definition timestepper.hpp:44
TimeStepper()
Definition timestepper.cpp:4
bool eval_dpdm
Flag to compute second-order derivative integral term.
Definition timestepper.hpp:49
void setEvalDPDM(bool flag)
Definition timestepper.hpp:98
virtual void evolveFWD(const double tstart, const double tstop, Vec x)=0
Evolves state forward by one time-step from tstart to tstop.
bool eval_leakage
Flag to compute leakage integral term.
Definition timestepper.hpp:47
double evalDpDm(Vec x, Vec xm1, Vec xm2)
Evaluates second-order derivative variation for the state.
Definition timestepper.cpp:351
void setEvalLeakage(bool flag)
Definition timestepper.hpp:96
double energy_integral
Sums the energy term.
Definition timestepper.hpp:53
double weightedcost_width
Width parameter for weighted cost function.
Definition timestepper.hpp:56
void setEvalEnergy(bool flag)
Definition timestepper.hpp:99
void evalWeightedCost_diff(double time, const Vec x, Vec xbar, double Jbar)
Computes derivative of weighted cost function term.
Definition timestepper.cpp:332
Vec xadj
Auxiliary vector needed for adjoint (backward) time stepping.
Definition timestepper.hpp:36
PetscInt localsize_u
Size of local sub vector u or v in state x=[u,v].
Definition timestepper.hpp:43
double getLeakageIntegral()
Definition timestepper.hpp:101
void evalLeakage_diff(const Vec x, Vec xbar, double Jbar)
Computes derivative of leakage term.
Definition timestepper.cpp:292
double dt
Time step size.
Definition timestepper.hpp:62
int mpisize_petsc
MPI size in Petsc communicator.
Definition timestepper.hpp:41
bool eval_energy
Flag to compute energy integral term.
Definition timestepper.hpp:48
void evalDpDm_diff(int n, Vec xbar, double Jbar)
Computes derivative of second-order derivative variation term.
Definition timestepper.cpp:381
Vec solveODE(int initid, Vec rho_t0)
Solves the ODE forward in time.
Definition timestepper.cpp:95
bool storeFWD
Flag to store primal states during forward evaluation.
Definition timestepper.hpp:71
std::vector< Vec > store_states
Storage for primal states during forward evolution.
Definition timestepper.hpp:38
double getWeightedCostIntegral()
Definition timestepper.hpp:102
double leakage_integral
Sums the integral over leakage.
Definition timestepper.hpp:52
Vec getState(size_t tindex)
Retrieves stored state at a specific time index.
Definition timestepper.cpp:85
Core type definitions and enumerations for Quandary quantum optimal control.
LinearSolverType
Available types for solving linear systems at each time step.
Definition defs.hpp:113