Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MUSICA Tutorial Chapter 1 #132

Merged
merged 45 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
0b5f817
Save.
dwfncar May 2, 2024
f775a51
Added installation subsection to getting started section.
dwfncar May 2, 2024
56e609a
Created getting_started subdir.
dwfncar May 3, 2024
14925da
Save.
dwfncar May 3, 2024
35e4235
Added config options list.
dwfncar May 7, 2024
57ce38e
Added MUSICA overview subsection.
dwfncar May 8, 2024
f96236b
Added empty user_guide/introduction.
dwfncar May 8, 2024
4a3dcdc
Renamed section to model_interface.
dwfncar May 8, 2024
c90a4b5
Merge branch 'main' into develop-host-model
dwfncar May 8, 2024
99193be
Merge branch 'develop-docs' into develop-host-model
dwfncar May 9, 2024
6a5ff87
Merge branch 'main' into develop-host-model
dwfncar May 13, 2024
c4aa116
Merge branch 'main' into develop-host-model
dwfncar May 27, 2024
c55cb02
Updated MICM dependency github tag.
dwfncar May 27, 2024
186f97d
Removed config options for installation.rst.
dwfncar May 27, 2024
fa08421
Start on C Fortran interface example.
dwfncar May 27, 2024
8e852ba
Added example fortran subroutine with C binding.
dwfncar May 27, 2024
3c6f203
Added example Makefile, output for demo Fortran C interface example.
dwfncar May 27, 2024
d9561ed
Added get_micm_version.
dwfncar May 28, 2024
be494ad
Added git_micm_version_c.
dwfncar May 28, 2024
0568527
Added get_micm_version_c.
dwfncar May 28, 2024
bd02b47
Added set(MAKE_Fortran_FLAGS -fPIE) to CMakeLists.txt.
dwfncar May 28, 2024
607d944
Don't need _c for get_micm_version in micm.F90.
dwfncar May 28, 2024
a95ccf3
Added public :: get_micm_version.
dwfncar May 28, 2024
868ebc8
In micm.cpp, have get_micm_version return String.
dwfncar May 28, 2024
078edc4
Comment get_micm_version for now.
dwfncar May 28, 2024
61702ce
Save.
dwfncar May 28, 2024
200ce7c
Added get_micm_version to micm.hpp header.
dwfncar May 28, 2024
787e45b
Call get_micm_version in test_micm_api.F90.
dwfncar May 29, 2024
6d03be2
Print MICM version in get_micm_version for testing.
dwfncar May 29, 2024
cac5253
Added get_char_array in util.F90.
dwfncar May 29, 2024
395c54a
Removed some example code blocks.
dwfncar May 29, 2024
00233f0
Updated installation.rst.
dwfncar May 29, 2024
82d1fef
Added Tutorial section.
dwfncar May 29, 2024
9a4b68e
Added Tutorial Chapter 1.
dwfncar May 29, 2024
0f7b871
Update docs/source/getting_started/overview.rst
dwfncar May 30, 2024
b7cfca6
Removed -fPIE gfortran option.
dwfncar May 30, 2024
409ec75
Created test/tutorial subdir, added test_get_micm_version.f90 tutoria…
dwfncar Jun 4, 2024
1bff161
Added subdir test.
dwfncar Jun 4, 2024
c4bc9a3
Added CMakeLists.txt at MUSICA/test level.
dwfncar Jun 4, 2024
e6df2c2
Added CMakeLists.txt.
dwfncar Jun 4, 2024
4eee37c
Moved test_get_micm_version.f90 to fortran/test_fetch_content_integra…
dwfncar Jun 4, 2024
a07af28
Added test_get_micm_version for tutorial.
dwfncar Jun 4, 2024
7216f22
Merge main to develop-host-model with some conflict resolutions.
dwfncar Jun 4, 2024
823cc55
Use literal include for chapter 1 tutorial with test_get_micm_version…
dwfncar Jun 11, 2024
4a2d7f0
Merged main with conflict resolutions.
dwfncar Jun 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@ endif()
# Musica python
if(MUSICA_ENABLE_PYTHON_LIBRARY)
add_subdirectory(python)
endif()
endif()
2 changes: 1 addition & 1 deletion cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ endif()

if(MUSICA_BUILD_DOCS)
find_package(Sphinx REQUIRED)
endif()
endif()
6 changes: 0 additions & 6 deletions docs/source/getting_started.rst

This file was deleted.

10 changes: 10 additions & 0 deletions docs/source/getting_started/getting_started.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
###############
Getting Started
###############

.. toctree::
:maxdepth: 1
:caption: Contents:

overview
installation
16 changes: 16 additions & 0 deletions docs/source/getting_started/installation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Installation
============

MUSICA source code may be cloned from its public GitHub repository
and configured and built with the cmake utility.
In brief:

.. code-block:: console

$ git clone https://github.com/NCAR/musica.git
$ cd musica
$ mkdir build
$ cd build
$ ccmake ..
$ make
$ make install
19 changes: 19 additions & 0 deletions docs/source/getting_started/overview.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Overview
========

The specific goal of MUSICA is to produce a new model independent infrastructure,
which will enable chemistry and aerosols to be simulated
at a large number of different resolutions in a single, coherent fashion.
At first, MUSICA will be configured within the NCAR Community Earth system Model (CESM)
and through this enable full feedbacks between the atmosphere, ocean and land.
The infrastructure will unify the different chemical transport models
including CAM-Chem, WACCM and WRF-Chem, and NCAR LES with chemistry
and a box model in a single modular framework.
The model infrastructure will be open source,
flexible and computationally efficient in order
to facilitate community co-development and use for scientific and operational purposes.

At the heart of MUSICA is the standalone Model Independent Chemistry Model (MICM), which is a gas-phase kinetic solver. MICM is made available by the MUSICA wrapper which satisfies the requirements of the Common Community Physics Package (CCPP)
and that can be connected to any CCPP compliant atmosphere model.
MUSICA and MICM will have a flexible design to handle a variety of gas phase and aerosol schemes
and associated chemical modules such as deposition or photolysis.
13 changes: 7 additions & 6 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ Welcome to MUSICA's documentation!

.. grid-item-card:: Getting started
:img-top: _static/index_getting_started.svg
:link: getting_started
:link: getting_started/getting_started
:link-type: doc

Check out the getting started guide to build and install musica.

.. grid-item-card:: User guide
:img-top: _static/index_user_guide.svg
:link: user_guide/index
:link: user_guide/user_guide
:link-type: doc

.. grid-item-card:: API reference
Expand All @@ -41,13 +41,14 @@ Welcome to MUSICA's documentation!
:maxdepth: 2
:caption: Contents:

getting_started
user_guide/index
getting_started/getting_started
user_guide/user_guide
tutorial/tutorial
api/index
contributing/index

.. getting_started
.. user_guide/index
.. getting_started/getting_started
.. user_guide/user_guid
.. api/index
.. contributing/index
.. citing_and_bibliography/index
Expand Down
43 changes: 43 additions & 0 deletions docs/source/tutorial/chapter1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Chapter 1
=========

First Fortran MUSICA Program
----------------------------
The MUSICA library can be used within a fortran program.
To get started, let us create a simple program that links
to MUSICA and prints the version of MICM.

Here are the contents of the program `demo.f90`:

.. literalinclude:: ../../../fortran/test/fetch_content_integration/test_get_micm_version.F90
:language: f90

From the ``musica_micm`` module, we only need the function ``get_micm_version``,
which returns a derived string type from the ``musica_util`` module, ``string_t``.
The ``string_t`` type will be discussed in more detail in later chapters.
To print the version string we just want the fortran character array,
accessed by ``get_char_array``.

Now, to build this simple program,
invoke the `gfortran` compiler and link to ``libmusica-fortran``, ``libmusica``,
and the standard C++ library ``libstdc++``.
The full command is

.. code-block:: bash

gfortran -o demo demo.f90 -I<MUSICA_DIR>/include -L<MUSICA_DIR>/lib -lmusica-fortran -lmusica -lstdc++

``<MUSICA_DIR>`` is the full path of the MUSICA installation directory,
specified by the option ``CMAKE_INSTALL_PREFIX``
during the `cmake` configuration process.
Note the include path allows the linker to find the ``musica_micm.mod`` and ``musica_util.mod``
module definition files.

When the `demo` program is run it should display the MICM version:

.. code-block:: bash

$ ./demo
MICM version 3.5.0
$

9 changes: 9 additions & 0 deletions docs/source/tutorial/tutorial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
########
Tutorial
########

.. toctree::
:maxdepth: 1
:caption: Contents:

chapter1.rst
78 changes: 78 additions & 0 deletions docs/source/user_guide/fortran_c.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
##############
Model Inteface
##############

Fortran C Interface Example
---------------------------
mattldawson marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: cpp

#include <stdio.h>

void test_proc_c(int n, double A[3][2]) {

printf("test_proc_c\n");
printf("n = %d\n", n);

printf("matrix A\n");

for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%6.2f ", A[j][i]);
}
printf("\n");
}
}


.. code-block:: f90

program demo_fort
use iso_c_binding, only: c_int, c_double
implicit none
integer :: i, j
integer(c_int) :: n_fort = 7
real(c_double), dimension(2, 3) :: A_fort

interface
subroutine test_proc_c(n_c, A_c) bind(C, name='test_proc_c')
use iso_c_binding, only: c_int, c_double
integer(c_int), intent(in), value :: n_c
real(c_double), dimension(2, 3), intent(in) :: A_c
end subroutine test_proc_c
end interface

do j = 1, 3
do i = 1, 2
A_fort(i, j) = real(i + j, c_double)
end do
end do

call test_proc_c(n_fort, A_fort)
end program demo_fort

.. code-block:: bash

all: test_proc_c.o demo_fort.o demo_fort

test_proc_c.o : test_proc_c.c
gcc -c test_proc_c.c

demo_fort.o : demo_fort.f90
gfortran -c demo_fort.f90

demo_fort : test_proc_c.o demo_fort.o
gfortran -o demo_fort demo_fort.o test_proc_c.o -lc

clean:
rm -f test_proc_c.o demo_fort.o demo_fort

.. code-block:: console

$ ./demo_fort
test_proc_c
n = 7
matrix A
2.00 3.00 4.00
3.00 4.00 5.00

3 changes: 0 additions & 3 deletions docs/source/user_guide/index.rst

This file was deleted.

4 changes: 4 additions & 0 deletions docs/source/user_guide/model_interface.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
##############
Model Inteface
##############

9 changes: 9 additions & 0 deletions docs/source/user_guide/user_guide.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
##########
User Guide
##########

.. toctree::
:maxdepth: 1
:caption: Contents:

model_interface
17 changes: 15 additions & 2 deletions fortran/micm.F90
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ module musica_micm
use musica_util, only: assert, mapping_t
implicit none

public :: micm_t
public :: get_micm_version, micm_t
private


interface
function get_micm_version_c() bind(C, name="get_micm_version")
use musica_util, only: string_t_c
type(string_t_c) :: get_micm_version_c
end function get_micm_version_c

function create_micm_c(config_path, error) bind(C, name="CreateMicm")
use musica_util, only: error_t_c
import c_ptr, c_int, c_char
Expand Down Expand Up @@ -122,6 +127,14 @@ end subroutine delete_mappings_c

contains

function get_micm_version() result(value)
use musica_util, only: string_t, string_t_c
type(string_t) :: value
type(string_t_c) :: string_c
string_c = get_micm_version_c()
value = string_t(string_c)
end function get_micm_version

function constructor(config_path, error) result( this )
use musica_util, only: error_t_c, error_t, copy_mappings
type(micm_t), pointer :: this
Expand Down Expand Up @@ -257,4 +270,4 @@ subroutine finalize(this)
ASSERT(error%is_success())
end subroutine finalize

end module musica_micm
end module musica_micm
19 changes: 18 additions & 1 deletion fortran/test/fetch_content_integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ enable_testing()
# API Test
if (MUSICA_ENABLE_MICM)
add_executable(test_micm_fortran_api test_micm_api.F90)
add_executable(test_get_micm_version test_get_micm_version.F90)

target_link_libraries(test_micm_fortran_api
PRIVATE
Expand All @@ -48,6 +49,22 @@ if (MUSICA_ENABLE_MICM)
COMMAND $<TARGET_FILE:test_micm_fortran_api>
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)

target_link_libraries(test_get_micm_version
PRIVATE
musica::musica-fortran
)

set_target_properties(test_get_micm_version
PROPERTIES
LINKER_LANGUAGE Fortran
)

add_test(
NAME test_get_micm_version
COMMAND $<TARGET_FILE:test_get_micm_version>
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)
endif()

# API Test
Expand All @@ -69,4 +86,4 @@ if (MUSICA_ENABLE_TUVX)
COMMAND $<TARGET_FILE:test_tuvx_fortran_api>
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)
endif()
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
program demo
use musica_util, only: string_t
use musica_micm, only: get_micm_version
implicit none
type(string_t) :: micm_version
micm_version = get_micm_version()
print *, "MICM version ", micm_version%get_char_array()
end program demo
7 changes: 5 additions & 2 deletions fortran/test/fetch_content_integration/test_micm_api.F90
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ program test_micm_api

use, intrinsic :: iso_c_binding
use, intrinsic :: ieee_arithmetic
use musica_micm, only: micm_t
use musica_util, only: assert, error_t, mapping_t
use musica_micm, only: micm_t, get_micm_version
use musica_util, only: assert, error_t, mapping_t, string_t

#include "micm/util/error.hpp"

Expand All @@ -19,6 +19,7 @@ program test_micm_api

subroutine test_api()

type(string_t) :: micm_version
type(micm_t), pointer :: micm
real(c_double) :: time_step
real(c_double) :: temperature
Expand All @@ -43,6 +44,8 @@ subroutine test_api()
num_user_defined_reaction_rates = 3
user_defined_reaction_rates = (/ 0.1, 0.2, 0.3 /)

micm_version = get_micm_version()
print *, "[test micm fort api] MICM version ", micm_version%get_char_array()

write(*,*) "[test micm fort api] Creating MICM solver..."
micm => micm_t(config_path, error)
Expand Down
3 changes: 2 additions & 1 deletion fortran/test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ create_standard_test_fortran(NAME fortran_util SOURCES util.F90)

if (MUSICA_ENABLE_MICM)
create_standard_test_fortran(NAME micm_fortran_api SOURCES ../fetch_content_integration/test_micm_api.F90)
create_standard_test_fortran(NAME get_micm_version SOURCES ../fetch_content_integration/test_get_micm_version.F90)
endif()

if (MUSICA_ENABLE_TUVX)
Expand All @@ -16,4 +17,4 @@ if (MUSICA_ENABLE_TUVX)
if (MUSICA_ENABLE_MPI)
create_standard_test_fortran(NAME connect_to_tuvx_mpi SOURCES tuvx_mpi.F90)
endif()
endif()
endif()
Loading
Loading