CAMP 1.0.0
Chemistry Across Multiple Phases
Boot CAMP: Part 3 - Updating CAMP Parameters

So far, we've built a simple box model and set up a simple chemical mechanism to run in that box model. Now, we're going to make some final adjustments to make this a fully operational box model. In many cases, the host model needs to provide some information to CAMP that changes over the course of a model run. We've seen how to update environmental conditions like temperature and pressure, but other information requires some knowledge of the system being modeled. In the case of our simple mechanism, this is \(\ce{NO2}\) photolysis. CAMP photolysis reactions need to know the photolysis rate at a given moment in the model run. Other reactions that need some external help include emissions, first order loss, and wet deposition.

Let's update our box model to set the \(\ce{NO2}\) photolysis rate. Before the call to solver_initialize(), we'll add the following code:

The mechanism_data_t structure and associated subroutines.
The rxn_data_t structure and associated subroutines.
Definition: rxn_data.F90:60
The abstract rxn_factory_t structure and associated subroutines.
The rxn_photolysis_t type and associated functions.
integer(kind=i_kind) :: i_rxn
character(len=:), allocatable :: photo_label
type(mechanism_data_t), pointer :: mechanism
type(rxn_factory_t) :: rxn_factory
class(rxn_data_t), pointer :: photo_rxn
type(rxn_update_data_photolysis_t) :: NO2_photolysis
if( .not.camp_core%get_mechanism( "my simple mechanism", mechanism ) ) then
write(*,*) "Missing mechanism!"
stop 3
end if
do i_rxn = 1, mechanism%size( )
photo_rxn => mechanism%get_rxn( i_rxn )
select type( photo_rxn )
class is( rxn_photolysis_t )
if( photo_rxn%property_set%get_string( "my photo label", photo_label ) ) then
if( photo_label .eq. "NO2 photolysis" ) then
call camp_core%initialize_update_object( photo_rxn, no2_photolysis )
end if
end if
end select
end do
Generic test reaction data type.

We first find the chemical mechanism, which we named my simple mechanism in the last part of the tutorial. Next, we cycle through the reactions in that mechanism looking for rxn_photolysis_t reactions. Then, we check the properties of the photolysis reactions looking for a key-value pair name my photo label and make sure its value is NO2 photolysis, as we specified in the last section. The property_set of reactions gives you direct access to the input data for each reaction. This includes the information CAMP uses, like reactants and A, as well as those it doesn't use, like our my photo label. There are functions to get string, integers, real numbers, and subsets of properties from property_set. To see the available functions, see camp_property. The last step is to fix our rxn_update_data_photolysis_t object to the \(\ce{NO2}\) photolysis reaction we located, then at the end just make sure we found the reaction we were looking for. This and similar objects for other reactions that accept external updates allow you to change reaction parameters during a model run.

Finally, we'll add the following code before we start to loop over time and solve the chemistry:

call no2_photolysis%set_rate( 12.2d0 ) ! rate in s-1
call camp_core%update_data( no2_photolysis )

Here, we simply set the reaction property of interest in our update data object, in this case the photolysis rate, and then pass this data to CAMP. This can be done before any call to solve(). This rate will remain the same until we change it again.

Now, our box model code and our input files are complete. To compile the code, try something like:

gfortran -o run_box_model box_model.F90 -lcamp -I/usr/local/include/camp

Where the include path points to where the CAMP library .mod files are installed. If you have trouble compiling or running because of missing libraries, make sure your LD_LIBRARY_PATH and PATH include the directories where the CAMP, json-fortran, SUNDIALS, and SuiteSparse libraries are installed.

Then, to run to mechanism:

./run_box_model > output.txt

If you have gnuplot installed, you can copy /doc/camp_tutorial/boot_camp/part_3_code/plot.conf to the directory with your results and check out them out:

gnuplot plot.conf

Our results look like this:

In the next installment of Boot CAMP, we'll start passing messages!

The files described in this part of the tutorial and needed to run the box model and plot the results can be found in /doc/camp_tutorial/boot_camp/part_3_code.


Docker Instructions

Inside the container:

dnf install -y gnuplot
mkdir boot-camp
cd boot-camp
cp ../camp/doc/camp_tutorial/boot_camp/part_3_code/* .
gfortran -o run_box_model box_model.F90 -lcamp -I/usr/local/include/camp
./run_box_model > output.txt
gnuplot plot.conf
exit

Back outside the container:

docker cp camp:/boot-camp/results.png .
docker container rm camp
open results.png

< Previous: Boot CAMP: Part 2 - Mechanism Index Next: Boot CAMP: Part 4 - Message Passing >