Skip to content

Commit

Permalink
Adding fpm support (#16)
Browse files Browse the repository at this point in the history
* Changes to make compatible with fpm

* Adding fpm support

* Adding clarification that you don't need to pre-install BMI binding if using fpm

* Updating example dir in GitHub workflow
  • Loading branch information
samharrison7 authored Oct 10, 2023
1 parent 30b8449 commit 8703d95
Show file tree
Hide file tree
Showing 67 changed files with 161 additions and 1,152 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/conda-and-cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:

- name: Memcheck
if: matrix.os == 'ubuntu-latest'
working-directory: ${{ github.workspace }}/examples
working-directory: ${{ github.workspace }}/example
run: |
sudo apt-get install -y valgrind
valgrind \
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.lo
*.o
*.obj
*.mod

# Precompiled Headers
*.gch
Expand Down Expand Up @@ -32,5 +33,8 @@

# Build and install directories for testing
_build
build
_install

# IDEs
.vscode/
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ message("-- bmif_inc - ${bmif_inc}")

add_subdirectory(heat)
add_subdirectory(bmi_heat)
add_subdirectory(tests)
add_subdirectory(examples)
add_subdirectory(test)
add_subdirectory(example)

include(CTest)
62 changes: 55 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ This repository is organized with the following directories:
<dt>bmi_heat</dt>
<dd>Holds the BMI for the model and a main program to run the
model through its BMI</dd>
<dt>tests</dt>
<dt>test</dt>
<dd>Unit tests for the BMI-ed model</dd>
<dt>examples</dt>
<dd>Examples of controlling the model through its BMI</dd>
Expand All @@ -41,14 +41,15 @@ This example can be built on Linux, macOS, and Windows.

**Prerequisites:**
* A Fortran compiler
* CMake
* The Fortran BMI bindings. Follow the build and install directions
given in the
* CMake or [Fortran Package Manager](https://fpm.fortran-lang.org/)
* If using CMake, the Fortran BMI bindings. Follow the build and
install directions given in the
[README](https://github.com/csdms/bmi-fortran/blob/master/README.md)
in that repository. You can choose to build them from source or
install them through a conda binary.
install them through a conda binary. If using fpm, the binding
will be automatically downloaded and built for you.

### Linux and macOS
### CMake - Linux and macOS

To build this example from source with cmake,
using the current Fortran BMI version, run
Expand Down Expand Up @@ -98,7 +99,7 @@ run unit tests and examples of using the sample implementation with

ctest

### Windows
### CMake - Windows

An additional prerequisite is needed for Windows:

Expand Down Expand Up @@ -132,6 +133,48 @@ run unit tests and examples of using the sample implementation with
ctest


### Fortran Package Manager (fpm)

If you don't already have fpm installed, you can do so via Conda:

conda install fpm -c conda-forge

Then, to build and install:

fpm build --profile release
fpm install --prefix <path-to-installation>

where `<path-to-installation>` is the base directory in which to
install the model. Installation is optional.

To run the tests:

fpm test -- test/sample.cfg

Here, `test/sample.cfg` is passed as a command line parameter to the
run executables, and tells the tests where to find the test config
file.

To run all of the examples:

fpm run --example --all -- example

Similarly, `example` tells the example executables to look in the
`example` directory for config files. To run individual tests:

fpm run --example <example-name> -- example

Where `<example-name>` is the name of the example to run. To see
a list of available examples, run `fpm run --example`. Note that the
non-BMI heat model executable is not built by default when using fpm.
If you want to build and install this too, you can do so from the
heat directory:

cd heat
fpm build --profile release
fpm install --prefix <path-to-installation>


## Use

Run the heat model through its BMI with the `run_bmiheatf` program,
Expand All @@ -144,3 +187,8 @@ If `run_bmiheatf` is in your path, run it with

Output from the model is written to the file **bmiheatf.out**
in the current directory.

If you built the model using fpm, you can alternatively run the
program using

fpm run -- test.cfg
14 changes: 7 additions & 7 deletions examples/CMakeLists.txt → example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ include(CTest)
include_directories(${CMAKE_Fortran_MODULE_DIRECTORY})

function(make_example example_name)
add_test(NAME ${example_name} COMMAND ${example_name})
add_test(NAME ${example_name} COMMAND ${example_name} ${CMAKE_CURRENT_SOURCE_DIR})
add_executable(${example_name} ${example_name}.f90 testing_helpers.f90)
target_link_libraries(${example_name} ${bmi_name})
endfunction(make_example)
Expand All @@ -16,9 +16,9 @@ make_example(set_value_ex)
make_example(conflicting_instances_ex)
make_example(change_diffusivity_ex)

file(
COPY ${CMAKE_CURRENT_SOURCE_DIR}/test1.cfg
DESTINATION ${CMAKE_BINARY_DIR}/examples)
file(
COPY ${CMAKE_CURRENT_SOURCE_DIR}/test2.cfg
DESTINATION ${CMAKE_BINARY_DIR}/examples)
# file(
# COPY ${CMAKE_CURRENT_SOURCE_DIR}/test1.cfg
# DESTINATION ${CMAKE_BINARY_DIR}/example)
# file(
# COPY ${CMAKE_CURRENT_SOURCE_DIR}/test2.cfg
# DESTINATION ${CMAKE_BINARY_DIR}/example)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ program change_diffusivity
use testing_helpers, only: print_array
implicit none

character (len=*), parameter :: config_file = "test1.cfg"
character (len=256) :: config_file
character (len=*), parameter :: &
dname = "plate_surface__thermal_diffusivity"
character (len=*), parameter :: &
Expand All @@ -17,6 +17,10 @@ program change_diffusivity
integer, dimension(2) :: tdims
real :: diffusivity(1), temperature(50)
integer :: status

! Get the config file directory and create the path based on this
call get_command_argument(1, config_file)
config_file = trim(config_file) // "/test1.cfg"

! Run model to the end with alpha=1.0 (from cfg file).
status = m%initialize(config_file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ program conflicting_instances_ex

type (bmi_heat) :: m1
type (bmi_heat) :: m2
character (len=256) :: cfg_dir
character (len=BMI_MAX_VAR_NAME) :: &
cfg_file1 = "test1.cfg", cfg_file2 = "test2.cfg"
integer :: s
Expand All @@ -17,6 +18,11 @@ program conflicting_instances_ex
real, allocatable :: z1(:), z2(:)
character(len=30) :: rowfmt1, rowfmt2

! Prepend the config file paths with the correct directory
call get_command_argument(1, cfg_dir)
cfg_file1 = trim(cfg_dir) // "/" // trim(cfg_file1)
cfg_file2 = trim(cfg_dir) // "/" // trim(cfg_file2)

write(*, "(a, a10, a10)") "Configuration files: ", cfg_file1, cfg_file2

write (*,"(a)",advance="no") "Initializing..."
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 8703d95

Please sign in to comment.