Quandary
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
PetscTS Class Reference

Petsc's adaptive timestepper. More...

#include <timestepper.hpp>

Inheritance diagram for PetscTS:
Inheritance graph
[legend]
Collaboration diagram for PetscTS:
Collaboration graph
[legend]

Public Member Functions

 PetscTS (const Config &config, MasterEq *mastereq_, Output *output_, int ninit_local)
 Constructor for PetscTS.
 
 ~PetscTS ()
 
Vec solveODE (int initid, int iinit_local, Vec rho_t0) override
 Overwrites the default time-stepping by calling PETSc's TSSolve.
 
void solveAdjointODE (int iinit_local, Vec rho_t0_bar, double Jbar_leakage, double Jbar_weightedcost, double Jbar_dpdm, double Jbar_energy) override
 Overwrites the default adjoint time-stepping by calling PETSc's TSSolve on the adjoint system.
 
void evolveFWD (const double, const double, Vec) override
 Evolves state forward by one time-step from tstart to tstop.
 
void evolveBWD (const double, const double, const Vec, Vec, Vec, bool) override
 Evolves adjoint state backward by one time-step and updates reduced gradient.
 
double getMinTimestepSize () const override
 Get the smallest timestep size chosen during adaptive timestepping.
 
- Public Member Functions inherited from TimeStepper
 TimeStepper ()
 
 TimeStepper (const Config &config, MasterEq *mastereq_, Output *output_, int ninit_local)
 Constructor for time stepper.
 
virtual ~TimeStepper ()
 
double getLeakageIntegral ()
 
double getWeightedCostIntegral ()
 
double getEnergyIntegral ()
 
double getDPDMIntegral ()
 
Vec getReducedGradient ()
 
void setWriteTrajectoryDataFiles (bool write)
 
void setOptimTarget (OptimTarget *optim_target_)
 
Vec getFinalState (size_t iinit_local)
 Retrieves the final state for a specific local initial condition.
 
double evalLeakage (const Vec x)
 Evaluates leakage into guard levels.
 
void evalLeakage_diff (const Vec x, Vec xbar, double Jbar)
 Computes derivative of leakage term.
 
double evalWeightedCost (double time, const Vec x)
 Evaluates the weighted cost function term.
 
void evalWeightedCost_diff (double time, const Vec x, Vec xbar, double Jbar)
 Computes derivative of weighted cost function term.
 
double evalDpDm (Vec x, Vec xm1, Vec xm2)
 Evaluates second-order derivative variation for the state.
 
void evalDpDm_diff (int n, Vec xbar, double Jbar)
 Computes derivative of second-order derivative variation term.
 
double evalEnergy (double time)
 Evaluates energy integral term.
 
void evalEnergy_diff (double time, double Jbar, Vec redgrad)
 Computes derivative of energy integral.
 

Static Public Member Functions

static PetscErrorCode RHSMatrixUpdate (TS ts, PetscReal t, Vec x, Mat A, Mat B, void *ptr)
 PETSc callback to update the RHS matrix for the forward system at time t.
 
static PetscErrorCode dRHSdpMatrixUpdate (TS ts, PetscReal t, Vec x, Mat A, void *ptr)
 PETSc callback to update the matrix for the derivative of the RHS with respect to control parameters at time t.
 
static PetscErrorCode computedRHSdp (Mat A, Vec x, Vec y)
 PETSc callback to compute the action of the derivative of the RHS with respect to parameters on a vector, used during adjoint solves.
 
static PetscErrorCode IntegralCosts (TS ts, PetscReal t, Vec x, Vec F, void *ctx)
 PETSc callback to evaluate integral cost functions at time t for the current state x.
 
static PetscErrorCode dIntegralCostdYUpdate (TS ts, PetscReal t, Vec x, Mat A, Mat B, void *ptr)
 PETSc callback to update the matrix for the derivative of integral cost functions with respect to the state at time t.
 
static PetscErrorCode dIntegralCostdPUpdate (TS ts, PetscReal t, Vec x, Mat A, void *ptr)
 PETSc callback to update the matrix for the derivative of integral cost functions with respect to the parameters at time t.
 
static PetscErrorCode monitorTrajectory (TS ts, PetscInt step, PetscReal time, Vec state, void *ctx)
 PETSc callback to monitor the trajectory during time-stepping and write data to output files.
 

Protected Attributes

TS ts
 Backward-compatible alias to ts_pool[0].
 
std::vector< TS > ts_pool
 One PETSc TS per initial condition.
 
std::vector< Vec > q_pool
 One quadrature state vector per TS/initial condition.
 
Vec redgrad_ts
 TS-internal gradient vector with PETSc communicator-compatible layout.
 
Mat dRHSdp
 MatShell for applying derivative of the RHS to control parameters.
 
Mat dIntegralCostdY
 Dense matrix for derivative of integral costs wrt state.
 
Mat dIntegralCostdP
 Dense matrix for derivative of integral costs wrt parameters.
 
PetscReal dRHSdp_time
 Cached time used by the RHSJacobianP MatShell callbacks.
 
double adj_scale_leakage
 Per-solve scaling for leakage integral adjoint contribution.
 
double adj_scale_weightedcost
 Per-solve scaling for weighted-cost integral adjoint contribution.
 
double adj_scale_energy
 Per-solve scaling for energy integral adjoint contribution.
 
double min_timestep_size
 Smallest timestep size chosen during adaptive timestepping.
 
- Protected Attributes inherited from TimeStepper
Vec x
 Auxiliary vector for forward time stepping.
 
Vec xadj
 Auxiliary vector needed for adjoint (backward) time stepping.
 
Vec xprimal
 Auxiliary vector for backward time stepping.
 
std::vector< std::vector< Vec > > trajectory_states
 Storage for primal states during forward evolution, one trajectory for each local initial condition..
 
std::vector< Vec > final_states
 Storage for final states for each local initial condition. Always filled after solveODE.
 
std::vector< Vec > dpdm_states
 Storage for states needed for second-order derivative penalty.
 
int mpirank_world
 MPI rank in global communicator.
 
int mpisize_petsc
 MPI size in Petsc communicator.
 
int mpirank_petsc
 MPI rank in Petsc communicator.
 
PetscInt localsize_u
 Size of local sub vector u or v in state x=[u,v].
 
PetscInt ilow
 First index of the local sub vector u,v.
 
PetscInt iupp
 Last index (+1) of the local sub vector u,v.
 
bool eval_leakage
 Flag to compute leakage integral term.
 
bool eval_energy
 Flag to compute energy integral term.
 
bool eval_dpdm
 Flag to compute second-order derivative integral term.
 
bool eval_weightedcost
 Flag to compute weighted cost integral term.
 
double leakage_integral
 Sums the integral over leakage.
 
double energy_integral
 Sums the energy term.
 
double dpdm_integral
 Sums second-order derivative variation value.
 
double weightedcost_integral
 Sums the integral over weighted cost function.
 
double weightedcost_width
 Width parameter for weighted cost function.
 
int ntime
 Number of time steps.
 
double total_time
 Final evolution time.
 
double dt
 Time step size.
 
bool writeTrajectoryDataFiles
 Flag to determine whether or not trajectory data will be written to files during forward simulation *‍/.
 
Vec redgrad
 Reduced gradient vector for optimization.
 
OptimTargetoptim_target
 Pointer to optimization target specification.
 
Outputoutput
 Pointer to output handler.
 
MasterEqmastereq
 Pointer to master equation solver.
 

Detailed Description

Petsc's adaptive timestepper.

Constructor & Destructor Documentation

◆ PetscTS()

PetscTS::PetscTS ( const Config config,
MasterEq mastereq_,
Output output_,
int  ninit_local 
)

Constructor for PetscTS.

Parameters
configConfiguration parameters from input file
mastereq_Pointer to master equation solver
output_Pointer to output handler
ninit_localNumber of initial conditions local to this processor

◆ ~PetscTS()

PetscTS::~PetscTS ( )

Member Function Documentation

◆ computedRHSdp()

PetscErrorCode PetscTS::computedRHSdp ( Mat  A,
Vec  x,
Vec  y 
)
static

PETSc callback to compute the action of the derivative of the RHS with respect to parameters on a vector, used during adjoint solves.

Computes y = (dRHS/dp)^T x via MasterEq::compute_dRHS_dParams.

Parameters
AMatrix representing the derivative of the RHS with respect to parameters
xInput vector
yOutput vector
Returns
PetscErrorCode PETSc error code

◆ dIntegralCostdPUpdate()

PetscErrorCode PetscTS::dIntegralCostdPUpdate ( TS  ts,
PetscReal  t,
Vec  x,
Mat  A,
void *  ptr 
)
static

PETSc callback to update the matrix for the derivative of integral cost functions with respect to the parameters at time t.

Parameters
tsPETSc TS context
tCurrent time
xCurrent state vector
AMatrix to store the derivative of integral cost functions with respect to the parameters
ptrPointer to PetscTS object (self)
Returns
PetscErrorCode PETSc error code

◆ dIntegralCostdYUpdate()

PetscErrorCode PetscTS::dIntegralCostdYUpdate ( TS  ts,
PetscReal  t,
Vec  x,
Mat  A,
Mat  B,
void *  ptr 
)
static

PETSc callback to update the matrix for the derivative of integral cost functions with respect to the state at time t.

Parameters
tsPETSc TS context
tCurrent time
xCurrent state vector
AMatrix to store the derivative of integral cost functions with respect to the state
BMatrix to store additional information (not used here, but required by PETSc's interface)
ptrPointer to PetscTS object (self)
Returns
PetscErrorCode PETSc error code

◆ dRHSdpMatrixUpdate()

PetscErrorCode PetscTS::dRHSdpMatrixUpdate ( TS  ts,
PetscReal  t,
Vec  x,
Mat  A,
void *  ptr 
)
static

PETSc callback to update the matrix for the derivative of the RHS with respect to control parameters at time t.

Parameters
tsPETSc TS context
tCurrent time
xCurrent state vector (not used here, but required by PETSc's interface)
AMatrix to store the derivative of the RHS with respect to parameters
ptrPointer to PetscTS object (self)
Returns
PetscErrorCode PETSc error code

◆ evolveBWD()

void PetscTS::evolveBWD ( const double  tstart,
const double  tstop,
const Vec  x_stop,
Vec  x_adj,
Vec  grad,
bool  compute_gradient 
)
inlineoverridevirtual

Evolves adjoint state backward by one time-step and updates reduced gradient.

Abstract base-class implementation is empty. Derived classes that need backward time-stepping should implement this function.

Parameters
tstartStart time (backward evolution)
tstopStop time (backward evolution)
x_stopState at stop time
x_adjAdjoint state vector
gradGradient vector to update
compute_gradientFlag to compute gradient

Reimplemented from TimeStepper.

◆ evolveFWD()

void PetscTS::evolveFWD ( const double  tstart,
const double  tstop,
Vec  x 
)
inlineoverridevirtual

Evolves state forward by one time-step from tstart to tstop.

Pure virtual function to be implemented by the derived time-stepping classes.

Parameters
tstartStart time
tstopStop time
xState vector to evolve

Implements TimeStepper.

◆ getMinTimestepSize()

double PetscTS::getMinTimestepSize ( ) const
inlineoverridevirtual

Get the smallest timestep size chosen during adaptive timestepping.

Returns
double Smallest timestep size from the last forward solve

Reimplemented from TimeStepper.

◆ IntegralCosts()

PetscErrorCode PetscTS::IntegralCosts ( TS  ts,
PetscReal  t,
Vec  x,
Vec  F,
void *  ctx 
)
static

PETSc callback to evaluate integral cost functions at time t for the current state x.

Parameters
tsPETSc TS context
tCurrent time
xCurrent state vector
FVector to store the evaluated integral cost function values
ctxPointer to PetscTS object (self)
Returns
PetscErrorCode PETSc error code

◆ monitorTrajectory()

PetscErrorCode PetscTS::monitorTrajectory ( TS  ts,
PetscInt  step,
PetscReal  time,
Vec  state,
void *  ctx 
)
static

PETSc callback to monitor the trajectory during time-stepping and write data to output files.

This is called by PETSc's TS at each accepted time step. We use it to write trajectory data to output files via the Output handler, and also to track the minimum timestep size chosen by the adaptive time-stepping.

Parameters
tsPETSc TS context
stepCurrent time step index
timeCurrent time
stateCurrent state vector
ctxPointer to PetscTS object (self)
Returns
PetscErrorCode PETSc error code

◆ RHSMatrixUpdate()

PetscErrorCode PetscTS::RHSMatrixUpdate ( TS  ts,
PetscReal  t,
Vec  x,
Mat  A,
Mat  B,
void *  ptr 
)
static

PETSc callback to update the RHS matrix for the forward system at time t.

This is called by PETSc's TS at each time step to get the current system matrix. We use it to update the MasterEq's RHS MatShell with the current time and assemble it, which is needed for time-dependent Hamiltonian terms.

Parameters
tsPETSc TS context
tCurrent time
xCurrent state vector (not used here, but required by PETSc's interface)
AMatrix to store the RHS (Jacobian) for the forward system
BMatrix to store the mass matrix (not used here, but required by PETSc's interface)
ptrPointer to PetscTS object (self)
Returns
PetscErrorCode PETSc error code

◆ solveAdjointODE()

void PetscTS::solveAdjointODE ( int  iinit_local,
Vec  rho_t0_bar,
double  Jbar_leakage,
double  Jbar_weightedcost,
double  Jbar_dpdm,
double  Jbar_energy 
)
overridevirtual

Overwrites the default adjoint time-stepping by calling PETSc's TSSolve on the adjoint system.

Parameters
iinit_localLocal index of initial condition for this processor
rho_t0_barTerminal condition for adjoint state
Jbar_leakageAdjoint seed of leakage integral term
Jbar_weightedcostAdjoint seed of weighted cost integral term
Jbar_dpdmAdjoint seed of second-order derivative variation term
Jbar_energyAdjoint seed of energy integral term

Reimplemented from TimeStepper.

◆ solveODE()

Vec PetscTS::solveODE ( int  initid,
int  iinit_local,
Vec  rho_t0 
)
overridevirtual

Overwrites the default time-stepping by calling PETSc's TSSolve.

Parameters
initidInitial condition identifier
iinit_localLocal index of initial condition for this processor
rho_t0Initial state vector
Returns
Vec Final state vector at time T

Reimplemented from TimeStepper.

Member Data Documentation

◆ adj_scale_energy

double PetscTS::adj_scale_energy
protected

Per-solve scaling for energy integral adjoint contribution.

◆ adj_scale_leakage

double PetscTS::adj_scale_leakage
protected

Per-solve scaling for leakage integral adjoint contribution.

◆ adj_scale_weightedcost

double PetscTS::adj_scale_weightedcost
protected

Per-solve scaling for weighted-cost integral adjoint contribution.

◆ dIntegralCostdP

Mat PetscTS::dIntegralCostdP
protected

Dense matrix for derivative of integral costs wrt parameters.

◆ dIntegralCostdY

Mat PetscTS::dIntegralCostdY
protected

Dense matrix for derivative of integral costs wrt state.

◆ dRHSdp

Mat PetscTS::dRHSdp
protected

MatShell for applying derivative of the RHS to control parameters.

◆ dRHSdp_time

PetscReal PetscTS::dRHSdp_time
protected

Cached time used by the RHSJacobianP MatShell callbacks.

◆ min_timestep_size

double PetscTS::min_timestep_size
protected

Smallest timestep size chosen during adaptive timestepping.

◆ q_pool

std::vector<Vec> PetscTS::q_pool
protected

One quadrature state vector per TS/initial condition.

◆ redgrad_ts

Vec PetscTS::redgrad_ts
protected

TS-internal gradient vector with PETSc communicator-compatible layout.

◆ ts

TS PetscTS::ts
protected

Backward-compatible alias to ts_pool[0].

◆ ts_pool

std::vector<TS> PetscTS::ts_pool
protected

One PETSc TS per initial condition.


The documentation for this class was generated from the following files: