use strict;
use warnings;

use ChemPlugin;

# don't buffer output
$| = 1;

print "Model a flow-through reactor\n\n";

# Configure and initialize the inlet fluid
my $cp_inlet = new ChemPlugin('stdout');
my $cmds = 'Ca++ = 1 mmol/kg; HCO3- = 1 mmol/kg;""pH = 1; balance on Cl-';
$cp_inlet->Config($cmds);
$cp_inlet->Initialize();

# Configure and initialize the stirred reactor
my $cp = new ChemPlugin('stdout');
$cmds = 'swap Calcite for Ca++; Calcite = 0.03 free m3; Cl- = 1 mmol/kg;'.
'swap CO2(g) for H+; fugacity CO2(g) = 1; balance on HCO3-;'.
'volume = 1 m3; fix f CO2(g); delxi = 0.01; pluses = banner';

$cp->Config($cmds);
$cp->Initialize(1.0, 'day');

# Link reactor to inlet and free outlet, set rate of flow
my $link1 = $cp->Link($cp_inlet);
my $link2 = $cp->Link(); # creates outlet link

$link1->FlowRate(10.0, 'm3/day');
$link2->FlowRate(-10.0, 'm3/day');

$cp->PlotHeader('FlowThrough.gtp','char');
$cp->PlotBlock();

while(1){
   my $deltat = $cp->ReportTimeStep();
   if($cp->AdvanceTimeStep($deltat)){
      last;
   }
   if($cp->AdvanceTransport()){
      last;
   }
   if($cp->AdvanceChemical()){
      last;
   }
   $cp->PlotBlock();
}
