Skip to content

History matching of simulation models ​

We demonstrate history matching of a Direct Column Breakthrough (DCB) simulation model in Mocca. We leverage the powerful and flexible optimization functionality of Jutul to set up and perform the history matching. For more details about the DCB modelling, see the breakthrough curve example.

Import necessary modules

julia
import Jutul
import Mocca
using Mocca.Units

The breakthrough ​

CO₂/N₂ fed at a constant interstitial velocity into an adiabatic column of zeolite 13X full of nitrogen, as in the breakthrough example but on a coarser grid.

julia
gas = Mocca.haghpanah_2013_gas()
feed = Mocca.Feed(gas; composition = (CO2 = 0.15, N2 = 0.85), temperature = 298.15, pressure = 1bar)
fs = Mocca.Flowsheet(gas, feed)
Mocca.add_bed!(fs, :Bed, Mocca.load_adsorbent("zeolite_13x_haghpanah_2013"), Mocca.haghpanah_2013_column();
    ncells = 50, thermal = Mocca.Adiabatic())
Mocca.add_device!(fs, :V_feed, :feed; at = (:Bed, :bottom))
Mocca.add_device!(fs, :V_product, :product; at = (:Bed, :top))

v_ref = 1.0 # interstitial velocity [m/s]
duration = 1000.0
process = Mocca.Process(fs, Mocca.Cycle([Mocca.Adsorb(duration; velocity = v_ref)]))
Process: CO2/N2 feed at 1.0 bar, 298.15 K, capturing CO2
  Bed: fixed bed of zeolite 13X, 50 cells
    bottom V_feed (feed), from boundary at 1.0 bar
    top    V_product (product), to boundary at 1.0 bar
  cycle of 1 stages, 1000.0 s:
    adsorption                          1000.0 s   V_feed, V_product

We create a function for setting up new simulation cases from the value of the parameter we wish to tune, the feed velocity. The feed is the flow controller on V_feed, so a case for velocity v keeps the model, initial state and parameters of the process, and gives the controller the flow rate of v. The rate is the only thing that depends on v, and it accepts the automatic differentiation numbers the optimizer passes in.

julia
bed = process.flowsheet[:Bed]
stage = only(Mocca.stages(process))
state0 = Mocca.initial_state(process)
function setup_case(prm, step_info = missing)
    settings = copy(stage.settings)
    rate = Mocca.interstitial_flow_rate(bed, prm["velocity"])
    settings[:V_feed] = merge(settings[:V_feed], (law = Mocca.VolumetricFlow(rate),))
    forces, dt = Mocca.setup_schedule(process.flowsheet, process.model, [Mocca.Stage(stage.name, stage.duration, settings)];
        max_dt = 20.0)
    return Mocca.MoccaCase(process.model, dt, forces; state0 = state0, parameters = process.parameters)
end;

Create synthetic reference data ​

julia
case_ref = setup_case(Dict("velocity" => v_ref));

Configure simulator which will be used in the history matching. The time step selector limits the change of the bed's variables per step.

julia
sim, cfg = Mocca.setup_process_simulator(case_ref.model, case_ref.state0, case_ref.parameters;
    timestep_selector_cfg = (Bed = (y = 0.01, Temperature = 10.0, Pressure = 10.0),),
    initial_dt = 1.0,
    output_substates = true,
    info_level = -1
);

Run reference simulation to generate "ground truth" data from the result

julia
states, timesteps_out = Mocca.simulate_process(case_ref;
    simulator = sim,
    config = cfg
);

times_ref = cumsum(timesteps_out)
total_time = times_ref[end]
last_cell_idx = Jutul.number_of_cells(bed.domain)
qCO2_ref = map(s -> s[:Bed][:AdsorbedConcentration][1, last_cell_idx], states)
qCO2_ref_by_time = Jutul.get_1d_interpolator(times_ref, qCO2_ref);

Setting up and solving the optimization problem ​

We define a suitable objective function to quantify the match between our simulations and the reference solution. Here we choose deviation of adsorbed CO2 in the last grid cell.

julia
function objective_function(model, state, dt, step_info, forces)
    current_time = step_info[:time]
    q_co2 = state[:Bed][:AdsorbedConcentration][1, last_cell_idx]
    q_co2_ref = qCO2_ref_by_time(current_time)
    v = dt/total_time*(q_co2 - q_co2_ref)^2
    return v
end;

Perturb the known velocity to form our initial guess for the optimization

julia
prm_guess = Dict("velocity" => 1.5 * v_ref);

Activate the velocity as a free parameter

julia
dprm = Jutul.DictOptimization.DictParameters(prm_guess)
Jutul.DictOptimization.free_optimization_parameter!(dprm, "velocity"; rel_min = 0.001, rel_max = 100.0)
DictParameters with 1 parameters (1 active), and 0 multipliers:
Active optimization parameters
┌──────────┬───────────────┬───────┬────────┬───────┐
│     Name │ Initial value │ Count │    Min │   Max │
├──────────┼───────────────┼───────┼────────┼───────┤
│ velocity │ 1.5           │     1 │ 0.0015 │ 150.0 │
└──────────┴───────────────┴───────┴────────┴───────┘
No inactive optimization parameters.
No multipliers set.

Run the optimization

julia
prm_opt = Jutul.DictOptimization.optimize(dprm, objective_function, setup_case;
    config = cfg,
    max_it = 10,
    obj_change_tol = 1e-6,
    solution_history = true
);
Optimization: Starting calibration of 1 parameters.
Optimization: Setting up adjoint storage.
Optimization: Finished setup in 23.4704337 seconds.
Optimization: Adjoint solve took 24.658161463 seconds.

Optimization: Objective #1: 3.43043e+05, gradient 2-norm: 5.64226e+05
It.  | Objective  | Proj. grad | Linesearch-its
-----------------------------------------------
   0 | 6.0799e-01 | 1.5000e+02 | -
Optimization: Adjoint solve took 0.331095288 seconds.

Optimization: Objective #2: 1.16451e+06 (f/f0=3.395e+00), gradient 2-norm: 1.57192e-06
Optimization: Adjoint solve took 1.616387357 seconds.

Optimization: Objective #3: 2.34981e+05 (f/f0=6.850e-01), gradient 2-norm: 7.14801e+05
Optimization: Adjoint solve took 0.303969706 seconds.

Optimization: Objective #4: 1.16451e+06 (f/f0=3.395e+00), gradient 2-norm: 1.57192e-06
Optimization: Adjoint solve took 1.5664046 seconds.

Optimization: Objective #5: 1.20237e+05 (f/f0=3.505e-01), gradient 2-norm: 8.92722e+05
Optimization: Adjoint solve took 0.275809605 seconds.

Optimization: Objective #6: 1.16451e+06 (f/f0=3.395e+00), gradient 2-norm: 1.57192e-06
LBFGS: Line search unable to succeed in 5 iterations ...
Optimization: Adjoint solve took 1.55701707 seconds.

Optimization: Objective #7: 1.20237e+05 (f/f0=3.505e-01), gradient 2-norm: 8.92722e+05
   1 | 2.1310e-01 | 1.5000e+02 | 5
Optimization: Adjoint solve took 0.321174684 seconds.

Optimization: Objective #8: 1.16451e+06 (f/f0=3.395e+00), gradient 2-norm: 1.57192e-06
Optimization: Adjoint solve took 1.558203884 seconds.

Optimization: Objective #9: 1.65765e+04 (f/f0=4.832e-02), gradient 2-norm: 6.34164e+05
   2 | 2.9379e-02 | 2.3733e+02 | 2
Optimization: Adjoint solve took 1.63253928 seconds.

Optimization: Objective #10: 4.10472e+05 (f/f0=1.197e+00), gradient 2-norm: 2.39584e+06
Optimization: Adjoint solve took 1.532278 seconds.

Optimization: Objective #11: 3.07245e+02 (f/f0=8.956e-04), gradient 2-norm: 9.99881e+04
   3 | 5.4454e-04 | 1.6859e+02 | 2
Optimization: Adjoint solve took 1.561891964 seconds.

Optimization: Objective #12: 3.01782e+01 (f/f0=8.797e-05), gradient 2-norm: 3.18117e+04
   4 | 5.3486e-05 | 2.6582e+01 | 1
Optimization: Adjoint solve took 1.546222632 seconds.

Optimization: Objective #13: 3.05490e-03 (f/f0=8.905e-09), gradient 2-norm: 3.19166e+02
   5 | 5.4143e-09 | 8.4571e+00 | 1
Optimization: Adjoint solve took 1.534892179 seconds.

Optimization: Objective #14: 9.73254e-08 (f/f0=2.837e-13), gradient 2-norm: 1.80154e+00
   6 | 1.7249e-13 | 8.4850e-02 | 1
Optimization: Finished in 81.124346105 seconds.

We can see a clear reduction of the objective function value throughout the optimization iterations, indicating a close match between the reference solution and our simulation.

julia
f = Mocca.plot_optimization_history(dprm)

We can look at the optimization result:

julia
dprm
DictParameters with 1 parameters (1 active), and 0 multipliers:
Active optimization parameters
┌──────────┬───────────────┬───────┬────────┬───────┬─────────────────┬────────┐
│     Name │ Initial value │ Count │    Min │   Max │ Optimized value │ Change │
├──────────┼───────────────┼───────┼────────┼───────┼─────────────────┼────────┤
│ velocity │ 1.5           │     1 │ 0.0015 │ 150.0 │ 1.0             │ -33.0% │
└──────────┴───────────────┴───────┴────────┴───────┴─────────────────┴────────┘
No inactive optimization parameters.
No multipliers set.

and see that the value matches the reference velocity

julia
v_ref
1.0

Check size of objective mismatch

julia
dprm.history.objectives
14-element Vector{Float64}:
 343042.87865582283
      1.1645146915217924e6
 234980.97798580813
      1.1645146915217924e6
 120236.86741399331
      1.1645146915217924e6
 120236.86741399331
      1.1645146915217924e6
  16576.495565678913
 410471.75007362664
    307.2448855087528
     30.17822458017353
      0.003054897079642174
      9.732535917709084e-8

Example on GitHub ​

If you would like to run this example yourself, it can be downloaded from the Mocca.jl GitHub repository.


This page was generated using Literate.jl.