-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from NCAR/70-tutorial-ch2
MUSICA Tutorial Chapter 2
- Loading branch information
Showing
6 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
Chapter 2 | ||
========= | ||
|
||
An MICM Box Model Fortran Example | ||
--------------------------------- | ||
|
||
In this next MUSICA Fortran example, | ||
we will setup a MICM solver, starting with a set of MICM configuration files, | ||
and run the solver for a single integration time step. | ||
|
||
The MICM configuration is specified in a top-level ``config.json`` file, | ||
which simply lists the chemical species configuration file followed by | ||
the reactions configuration file. | ||
|
||
.. literalinclude:: ../../../configs/analytical/config.json | ||
:language: json | ||
|
||
For this example, we will have a system of three chemical species | ||
`A`, `B`, and `C`, defined in the JSON file ``species.json`` as follows: | ||
|
||
.. literalinclude:: ../../../configs/analytical/species.json | ||
:language: json | ||
|
||
The ``reactions.json`` specifies a mechanism, or a set of reactions for the system. | ||
Here, we will introduce two Arrhenius type reactions, the first | ||
with `B` evolving to `C`, and specifying all five reaction parameters, | ||
and the second reaction with `A` evolving to `B` and using only two reaction parameters. | ||
The mechanism configuration might then be set up as: | ||
|
||
.. literalinclude:: ../../../configs/analytical/reactions.json | ||
:language: json | ||
|
||
More information on MICM configurations and reactions can be found in the MICM documentation | ||
at `https://ncar.github.io/micm/user_guide/`_ | ||
|
||
The Fortran example code is shown below in full: | ||
|
||
.. literalinclude:: ../../../fortran/test/fetch_content_integration/test_micm_box_model.F90 | ||
:language: f90 | ||
|
||
From the ``musica_util`` module we need the Fortran types | ||
``error_t``, ``string_t``, and ``mapping_t``. | ||
A pointer to a ``musica_micm::micm_t`` will serve as the interface to the MICM solver | ||
(in the example the pointer name is ``micm``). | ||
Note that the ``config_path`` in the code sample has been set to ``configs/analytical``, | ||
so that subdir should be created relative to the main program and contain | ||
the MICM JSON configuration files, | ||
or otherwise the ``config_path`` should be modified appropriately. | ||
The initial species concentrations are initialized in the ``concentrations`` array, | ||
which is an argument to the MICM solver. | ||
|
||
Finally, a single time step solution is obtained through a call to ``micm%solve``, | ||
after which the updated concentrations may be displayed. | ||
|
||
.. code-block:: bash | ||
$ ./test_micm_box_model | ||
Creating MICM solver... | ||
Species Name:A, Index: 1 | ||
Species Name:B, Index: 2 | ||
Species Name:C, Index: 3 | ||
Solving starts... | ||
After solving, concentrations 0.38 1.61E-009 2.62 | ||
$ | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ Tutorial | |
:caption: Contents: | ||
|
||
chapter1.rst | ||
chapter2.rst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
fortran/test/fetch_content_integration/test_micm_box_model.F90
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
program test_micm_box_model | ||
|
||
use, intrinsic :: iso_c_binding | ||
use, intrinsic :: ieee_arithmetic | ||
|
||
use musica_util, only: error_t, string_t, mapping_t | ||
use musica_micm, only: micm_t, solver_stats_t | ||
|
||
implicit none | ||
|
||
call box_model() | ||
|
||
contains | ||
|
||
subroutine box_model() | ||
|
||
character(len=256) :: config_path | ||
|
||
real(c_double), parameter :: GAS_CONSTANT = 8.31446261815324_c_double ! J mol-1 K-1 | ||
|
||
real(c_double) :: time_step | ||
real(c_double) :: temperature | ||
real(c_double) :: pressure | ||
real(c_double) :: air_density | ||
|
||
integer(c_int) :: num_concentrations = 3 | ||
real(c_double), dimension(3) :: concentrations | ||
|
||
integer(c_int) :: num_user_defined_reaction_rates = 0 | ||
real(c_double), dimension(:), allocatable :: user_defined_reaction_rates | ||
|
||
type(string_t) :: solver_state | ||
type(solver_stats_t) :: solver_stats | ||
type(error_t) :: error | ||
|
||
type(micm_t), pointer :: micm | ||
|
||
integer :: i | ||
|
||
config_path = "configs/analytical" | ||
|
||
time_step = 200 | ||
temperature = 273.0 | ||
pressure = 1.0e5 | ||
air_density = pressure / (GAS_CONSTANT * temperature) | ||
|
||
concentrations = (/ 1.0, 1.0, 1.0 /) | ||
|
||
write(*,*) "Creating MICM solver..." | ||
micm => micm_t(config_path, error) | ||
|
||
do i = 1, size( micm%species_ordering ) | ||
associate(the_mapping => micm%species_ordering(i)) | ||
print *, "Species Name:", the_mapping%name(), ", Index:", the_mapping%index() | ||
end associate | ||
end do | ||
|
||
write(*,*) "Solving starts..." | ||
! call micm%solve(time_step, temperature, pressure, num_concentrations, concentrations, & | ||
! num_user_defined_reaction_rates, user_defined_reaction_rates, error) | ||
call micm%solve(time_step, temperature, pressure, air_density, num_concentrations, concentrations, & | ||
num_user_defined_reaction_rates, user_defined_reaction_rates, solver_state, solver_stats, error) | ||
write(*,*) "After solving, concentrations", concentrations | ||
|
||
deallocate( micm ) | ||
|
||
end subroutine box_model | ||
|
||
end program test_micm_box_model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters