model
Single column forward model core.
This module contains the main class SingleColumnModel which is the core of Tunax for
implementing computing the evolution of a water column of the ocean. It also contains functions
that are used in this computation. The model was traduced from Frotran to JAX with the work of
Florian Lemarié and Manolis Perrot [1], the translation was done in part using the work of Anthony
Zhou, Linnia Hawkins and Pierre Gentine [2]. this class and these functions can be obtained by the
prefix tunax.model. or directly by tunax..
References
- StatesTime
Type that represent the values that are transformed in an integration step of the model.
alias of
Tuple[State,ClosureStateAbstract,float]
- class SingleColumnModel[source]
Single column forward model core.
This forward model of tunax is the combination of 4 things : the physical case
case_tracable, an initial state of the water columninit_state, the time information withnt,dtandp_outand the abstraction of the chosen closure for eddy-diffusivityclosure. Adding a set of parameters for the closure one can run the model with the methodrun(). The builder of this class takes the case as arguments all the attributes, except for the case : the parameter is an instance ofCasewhich is transformed in aCaseTracableinstance for JAX purpose and for the closure : the parameter is only the name of the closure.- Parameters:
init_state (State) – cf.
init_state.case (Case) – Physical case and forcings of the experiment.
closure_name (str) – Name of the chosen closure, must be a key of
CLOSURES_REGISTRY, see its documentation for the available closures.start_time (float, default=0.) – cf.
start_time.
- case_tracable
Physical case and forcings of the experiment, made tracable for JAX purposes.
- Type:
- checkpoint
Use the
checkpoint()on the partial run method. Used for economize the memory when computing the gradient, especially on GPUs.- Type:
bool, default=False
Note
The closure parameters are given only at the end when we compute the model so that the calibrations of the parameters are easier.
- tra_promote(promotions)[source]
Increase the dimension type of the tracers.
It is usefull to use
vmap()on several instances ofSingleColumnModelto do batch computing (run the model in parallel).- Parameters:
promotions (Dict[str, str]) – The keys of the dictionnary are the name of the tracer variables to modify the dimensions of the forcing, one of {
't','s','b','pt’}, the values are the new dimensions of the forcings, possible values {'constant','variable'}.- Returns:
model – The
selfobject with the several promoted forcings.- Return type:
- step(state, closure_state, time, closure_parameters)[source]
Runs one time-step of the model.
This functions first call the closure to compute the eddy-diffusivity and viscosity, and then integrate the equations of tracers and momentum. It modifies the
statewith these new values and then returns the newstateandclosure_state.- Parameters:
state (State) – State of the water column at the current
time.closure_state (ClosureStateAbstract) – State of the water column for the closure variables at the current
time.time (float) – Time of the current iteration (the mehtod integrates from this time to the next one).
closure_parameters (ClosureParametersAbstract) – A set of parameters of the used closure.
- Return type:
- Returns:
state (State) – State of the water column after the integration.
closure_state (ClosureStateAbstract) – State of the water column for the closure variables after the integration.
time (float) – Value of the time after the integration.
- step_check(state, closure_state, time, closure_parameters)[source]
Checkpointed version of
run()to save memory during the gradient computation.- Parameters:
state (State) – State of the water column at the current
time.closure_state (ClosureStateAbstract) – State of the water column for the closure variables at the current
time.time (float) – Time of the current iteration (the mehtod integrates from this time to the next one).
closure_parameters (ClosureParametersAbstract) – A set of parameters of the used closure.
- Return type:
- Returns:
state (State) – State of the water column after the integration.
closure_state (ClosureStateAbstract) – State of the water column for the closure variables after the integration.
time (float) – Value of the time after the integration.
- run_partial(state0, closure_state0, time0, n_steps, closure_parameters)[source]
Runs a certain number of time steps.
Computes a loop of integration for a number of time steps of
n_steps, and return the last states of the loop.- Parameters:
state0 (State) – State of the water column at the beginning of the integration loop.
closure_state (ClosureStateAbstract) – State of the water column for the closure variables at the beginning of the integration loop.
time0 (float) – Begining time of the integration loop.
n_steps (int) – Number of integration steps.
closure_parameters (ClosureParametersAbstract) – A set of parameters of the used closure.
closure_state0 (ClosureStateAbstract)
- Return type:
- Returns:
state (State) – State of the water column after a number of
n_stepsintegration steps.closure_state (ClosureStateAbstract) – State of the water column for the closure variables after a number of
n_stepsintegration steps.time (float) – Value of the time after a number of
n_stepsintegration steps.
- run(closure_parameters)[source]
Main run the model.
Computes the
ntintegration steps of lenghtdtfrom the initial stateinit_statewith and doing an output of the state everyp_outsteps with the physical case and forcings corresponding tocase_tracable. The closure for eddy- diffusivity used isclosurewith the values of the parameters described by the parameterclosure_parameters.- Parameters:
closure_parameters (ClosureParametersAbstract) – The set of parameters to use for the computation of the closure of eddy-diffusivity.
- Returns:
trajectory – The trajectory with all the output steps of the integration loop.
- Return type:
- jit_run(closure_parameters)[source]
Jitted version of
run().This method does exacly like
run()butjit()is applied on it, which means that the first call of this method will be the compilation of the function, and the next ones will be the compiled execution of the function which are supposed to be faster. There will be a compilation each time that this method will be call for a new “shape” of theSingleColumnModelinstance (which means that all the leafs of the pytree have the same shape asArray), but even if this method is call on different instances, if they have the same shape, the compilation will be done only one time. Moreover, this method should use only for direct methods, if one wants to apply agrad()over it’s better to put thejit()outside.- Parameters:
closure_parameters (ClosureParametersAbstract) – cf.
run().- Returns:
trajectory – cf.
run().- Return type:
- lmd_swfrac(hz)[source]
Compute solar forcing.
Compute fraction of solar shortwave flux penetrating to specified depth due to exponential decay in Jerlov water type. This function is called once before running the model.
- tracer_flux(tracer, case_tracable, grid, i_time)[source]
Computes flux of the tracer forcing.
This function get the flux of the forcing at a certain time depending on the type of the forcing, the flux being the derivative of the forcing along the depth.
- Parameters:
tracer (str) – Name of the tracer variable of the concerned forcing. One of {
't','s','b','pt’}.case_tracable (CaseTracable) – Physical case which contains the forcings type and values.
grid (Grid) – Vertical grid of the water column.
i_time (int) – Index of the time iteration corresponding to the index in the forcing.
- Returns:
df – Flux of the forcing of the tracer. At each cell it represents the difference between the input and the ouput flux.
- Return type:
float
Arrayof shape (nz)- Raises:
ValueError – If the forcing type is not one of {‘borders’, ‘constant’, ‘variable’}.
- advance_tra_ed(state, akt, dt, case_tracable, i_time)[source]
Integrate vertical diffusion term for tracers.
First the flux divergences are computed taking in account the forcings. Then the diffusion equation of the tracers system is solved, and the tracers at next time-step are returned. The solved equation is for \(C\) a tracer :
\(\partial _z ( K_m \partial _z C) + \partial _t C + F = 0\)
where \(F\) is the representation of the forcings. This equation is solved for every tracer indicated in
eos_tracersand the passive tracer ifdo_ptis set.- Parameters:
state (State) – State of the water column at the current iteration.
akt (float
Arrayof shape (nz)+1) – Eddy-diffusivity on the interfaces of the cells \(\left[\text m ^2 \cdot \text s ^{-1}\right]\).dt (float) – Time-step of the integration step.
case_tracable (CaseTracable) – Physical case which contains the forcings type and values.
i_time (int) – Index of the time iteration corresponding to the index in the forcing.
- Returns:
state – The state of the water column with the values of the tracers after the integration and the diffusion by the eddy-diffusivity.
- Return type:
- advance_dyn_cor_ed(state, akv, dt, case_tracable)[source]
Integrate vertical diffusion and Coriolis terms for momentum.
First the Coriolis term is computed, then the momentum forcings are applied and finally, the diffusion equation is solved. The momentum at next time-step is returned. The equation which is solved is :
\(\partial_z (K_v \partial_z U) + F_{\text{cor}}(U) + F = 0\)
where \(F_{\text{cor}}\) represent the Coriolis effect, and \(F\) represent the effect of the forcings on the momentum.
- Parameters:
state (State) – State of the water column at the current iteration.
akv (float
Arrayof shape (nz+1)) – Eddy-viscosity on the interfaces of the cells \(\left[\text m ^2 \cdot \text s ^{-1}\right]\).dt (float) – Time-step of the integration step.
case_tracable (CaseTracable) – Physical case which contains the forcings type and values.
- Returns:
state – The state of the water column with the values of the momentum after the integration and the diffusion by the eddy-viscosity.
- Return type:
- diffusion_solver(ak, hz, f, dt)[source]
Solve a diffusion problem with finite volumes.
The diffusion problems can be written
\(\partial _z (K \partial _z X) + \dfrac f {\Delta t \Delta x} = 0\)
where we are searching for \(X\) and where \(f\) represents the temporal derivative and forcings. This function transforms this problem in a tridiagonal system and then solve it.
- Parameters:
ak (float
Arrayof shape (nz+1)) – Diffusion at the cell interfaces \(K\) in \(\left[\text m ^2 \cdot \text s ^{-1}\right]\).hz (float
Arrayof shape (nz)) – Thickness of cells from deepest to shallowest \(\left[\text m\right]\).f (float
Arrayof shape (nz)) – Right-hand flux of the equation \(f\) in \([[X] \cdot \text m ]\).dt (float) – Time-step of discretisation \([\text s]\).
- Returns:
x – Solution of the diffusion problem \(X\) in \(\left[[X]\right]\).
- Return type:
float
Arrayof shape (nz)