Skip to content

Commit e75088e

Browse files
committed
Getting cmake working.
1 parent f6ced35 commit e75088e

22 files changed

+902
-759
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ doc/graph/
1111
doxygen/
1212
bin/
1313
build/
14+
release/
15+
debug/
1416
obj/
1517
log/
1618
cache/
@@ -58,6 +60,10 @@ scratch/
5860
*.out
5961
*.app
6062

63+
# Debugging
64+
massif*
65+
gmon.out
66+
6167
# IDEs
6268
*.cbp
6369
*.depend
@@ -68,6 +74,7 @@ scratch/
6874
.vscode/
6975
.project
7076
Debug/
77+
Release/
7178
*.sln
7279
*.u2d
7380
*.vfproj*

.gitmodules

-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
path = vendor/mo_netcdf
88
url = https://github.com/schaefed/mo_netcdf
99

10-
[submodule "json2netcdf"]
11-
path = vendor/json2netcdf
12-
url = https://github.com/samharrison7/json2netcdf
13-
1410
[submodule "vendor/datetime-fortran"]
1511
path = vendor/datetime-fortran
1612
url = https://github.com/samharrison7/datetime-fortran

CHANGELOG.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
# Changelog
22

3-
All notable changes to the model will be documented in this file.
3+
All notable changes to the model will be documented in this file. Breaking changes (i.e. those that cause changes to the model's interface) are denoted by ⚠️
44

55
## [Unreleased]
66

7+
### Added
8+
9+
- ⚠️ Added option to write to NetCDF file. Specify `&output > write_netcdf = .true.` to use. This requires the compilation of a new Fortran source file, `src/Data/NetCDFOutputModule.f90`, and your compilation process will need ammending. The [example Makefile](./Makefile.example) has been updated accordingly. NetCDF output variables are stored in memory for the entire model run (as opposed to CSV files, which are written iteratively on each time step), which means choosing to write to NetCDF may use significantly more memory.
10+
- Along with this, added the option for NetCDF output to be chunked when the model is run in batch mode. If `&output > chunk_netcdf = .true.`, then a NetCDF file is written at the end of every chunk in the batch run. If `.false.`, the NetCDF is written at the end of the entire batch. As output variables are stored in memory, for particularly long batch runs, memory usage may be very high for non-chunked NetCDF output. The `nco` command line tools can be used to easily merge chunked NetCDF files after the run, e.g. using [`ncrcat`](http://nco.sourceforge.net/nco.html#ncrcat).
11+
12+
### Changed
13+
14+
- ⚠️ `src/CheckpointModule1.f90` renamed to `src/CheckpointModule.f90`. Your compilation process may need ammending. The [example Makefile](./Makefile.example) has been updated.
15+
- Submodule `vendor/mo_netcdf` updated to latest version to allow the creation of scalar variables.
16+
- Submodule `vendor/json2netcdf` removed.
17+
718
## [0.0.1] - 2021-03-20
819

920
### Added

CMakeLists.txt

+19-14
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,35 @@ cmake_minimum_required(VERSION 3.13)
33
project(nanofase)
44
enable_language(Fortran)
55

6-
if (CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
7-
set(debug_flags "-Wall -O0 -fcheck=all -fbackslash -g -ffpe-trap=zero,invalid,overflow,underflow -pg")
6+
# GFortran compiler
7+
if (CMAKE_Fortran_COMPILER_ID MATCHES GNU)
8+
set(debug_flags "-Wall -O1 -fcheck=all -fbackslash -g -ffpe-trap=zero,invalid,overflow,underflow -pg")
89
set(release_flags "-O3 -fbackslash")
910
endif()
1011

11-
if (CMAKE_Fortran_COMPILER_ID MATCHES "Intel")
12+
# Intel Fortran compiler
13+
if (CMAKE_Fortran_COMPILER_ID MATCHES Intel)
1214
set(debug_flags "/assume:bscc /fpe:0 /check:bounds /traceback /check:stack /debug:full /Od")
1315
set(release_flags "/O3 /assume:bscc")
1416
endif()
1517

16-
# Get the NetCDF flags by running nf-config command
17-
execute_process(COMMAND nf-config --fflags --flibs OUTPUT_VARIABLE netcdf_flags)
18-
# For some reason nf-config returns string with newline characters, so we need to remove these
19-
string(REPLACE "\n" " " netcdf_flags ${netcdf_flags})
20-
21-
set(CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} ${debug_flags} ${netcdf_flags}")
22-
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${release_flags} ${netcdf_flags}")
23-
# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${netcdf_flags}")
18+
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" CACHE INTERNAL "Location of CMake modules.")
19+
set(NETCDF_F90 "YES")
20+
find_package(NetCDF REQUIRED)
2421

22+
set(CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} ${debug_flags}")
23+
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${release_flags}")
2524

25+
# Sources to compile
2626
file(GLOB_RECURSE sources src/*.f90 vendor/*.f90)
2727
list(FILTER sources EXCLUDE REGEX ".*vendor/feh/example/.*f90$")
28-
list(FILTER sources EXCLUDE REGEX ".*vendor/datetime-fortran/src/tests/datetime_tests.f90$")
28+
list(FILTER sources EXCLUDE REGEX ".*vendor/feh/tests/.*f90$")
29+
list(FILTER sources EXCLUDE REGEX ".*vendor/datetime-fortran/src/tests/.*f90$")
2930
list(FILTER sources EXCLUDE REGEX ".*vendor/mo_netcdf/examples/.*f90$")
3031
list(FILTER sources EXCLUDE REGEX ".*vendor/mo_netcdf/tests/.*f90$")
31-
add_executable(prog ${sources})
32-
target_link_options(prog INTERFACE "${netcdf_flags}")
32+
list(FILTER sources EXCLUDE REGEX "src/Data/DataOutputModule.f90")
33+
34+
# Compile to executable and link to NetCDF
35+
add_executable(nanofase ${sources})
36+
target_include_directories(nanofase PUBLIC "${NETCDF_INCLUDES}")
37+
target_link_libraries(nanofase PRIVATE "${NETCDF_LIBRARIES_F90}")

Makefile.example

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ OBJ = vendor/feh/src/ErrorInstance.o \
8080
src/GridCell/classGridCell2.o \
8181
src/Environment/spcEnvironment.o \
8282
src/Environment/classEnvironment1.o \
83+
src/Data/NetCDFOutputModule.o \
8384
src/Data/DataOutputModule1.o \
84-
src/CheckpointModule1.o \
85+
src/CheckpointModule.o \
8586
src/main.o
8687

8788
OBJECTS = $(notdir $(OBJ))

cmake/FindNetCDF.cmake

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# - Find NetCDF
2+
# Find the native NetCDF includes and library
3+
#
4+
# NETCDF_INCLUDES - where to find netcdf.h, etc
5+
# NETCDF_LIBRARIES - Link these libraries when using NetCDF
6+
# NETCDF_FOUND - True if NetCDF found including required interfaces (see below)
7+
#
8+
# Your package can require certain interfaces to be FOUND by setting these
9+
#
10+
# NETCDF_CXX - require the C++ interface and link the C++ library
11+
# NETCDF_F77 - require the F77 interface and link the fortran library
12+
# NETCDF_F90 - require the F90 interface and link the fortran library
13+
#
14+
# The following are not for general use and are included in
15+
# NETCDF_LIBRARIES if the corresponding option above is set.
16+
#
17+
# NETCDF_LIBRARIES_C - Just the C interface
18+
# NETCDF_LIBRARIES_CXX - C++ interface, if available
19+
# NETCDF_LIBRARIES_F77 - Fortran 77 interface, if available
20+
# NETCDF_LIBRARIES_F90 - Fortran 90 interface, if available
21+
#
22+
# Normal usage would be:
23+
# set (NETCDF_F90 "YES")
24+
# find_package (NetCDF REQUIRED)
25+
# target_link_libraries (uses_f90_interface ${NETCDF_LIBRARIES})
26+
# target_link_libraries (only_uses_c_interface ${NETCDF_LIBRARIES_C})
27+
28+
if (NETCDF_INCLUDES AND NETCDF_LIBRARIES)
29+
# Already in cache, be silent
30+
set (NETCDF_FIND_QUIETLY TRUE)
31+
endif (NETCDF_INCLUDES AND NETCDF_LIBRARIES)
32+
33+
find_path (NETCDF_INCLUDES netcdf.h
34+
HINTS NETCDF_DIR ENV NETCDF_DIR)
35+
36+
find_library (NETCDF_LIBRARIES_C NAMES netcdf)
37+
mark_as_advanced(NETCDF_LIBRARIES_C)
38+
39+
set (NetCDF_has_interfaces "YES") # will be set to NO if we're missing any interfaces
40+
set (NetCDF_libs "${NETCDF_LIBRARIES_C}")
41+
42+
get_filename_component (NetCDF_lib_dirs "${NETCDF_LIBRARIES_C}" PATH)
43+
44+
macro (NetCDF_check_interface lang header libs)
45+
if (NETCDF_${lang})
46+
find_path (NETCDF_INCLUDES_${lang} NAMES ${header}
47+
HINTS "${NETCDF_INCLUDES}" NO_DEFAULT_PATH)
48+
find_library (NETCDF_LIBRARIES_${lang} NAMES ${libs}
49+
HINTS "${NetCDF_lib_dirs}" NO_DEFAULT_PATH)
50+
mark_as_advanced (NETCDF_INCLUDES_${lang} NETCDF_LIBRARIES_${lang})
51+
if (NETCDF_INCLUDES_${lang} AND NETCDF_LIBRARIES_${lang})
52+
list (INSERT NetCDF_libs 0 ${NETCDF_LIBRARIES_${lang}}) # prepend so that -lnetcdf is last
53+
else (NETCDF_INCLUDES_${lang} AND NETCDF_LIBRARIES_${lang})
54+
set (NetCDF_has_interfaces "NO")
55+
message (STATUS "Failed to find NetCDF interface for ${lang}")
56+
endif (NETCDF_INCLUDES_${lang} AND NETCDF_LIBRARIES_${lang})
57+
endif (NETCDF_${lang})
58+
endmacro (NetCDF_check_interface)
59+
60+
NetCDF_check_interface (CXX netcdfcpp.h netcdf_c++)
61+
NetCDF_check_interface (F77 netcdf.inc netcdff)
62+
NetCDF_check_interface (F90 netcdf.mod netcdff)
63+
64+
set (NETCDF_LIBRARIES "${NetCDF_libs}" CACHE STRING "All NetCDF libraries required for interface level")
65+
66+
# handle the QUIETLY and REQUIRED arguments and set NETCDF_FOUND to TRUE if
67+
# all listed variables are TRUE
68+
include (FindPackageHandleStandardArgs)
69+
find_package_handle_standard_args (NetCDF DEFAULT_MSG NETCDF_LIBRARIES NETCDF_INCLUDES NetCDF_has_interfaces)
70+
71+
mark_as_advanced (NETCDF_LIBRARIES NETCDF_INCLUDES)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
&batch_config
2+
n_chunks = 3
3+
/
4+
5+
&chunks
6+
input_files = "data.example/test-scenario.nc", "data.example/test-scenario-t11.nc", "data.example/test-scenario.nc"
7+
constants_files = "data.example/constants_test-scenario.nml", "data.example/constants_test-scenario.nml", "data.example/constants_test-scenario.nml"
8+
start_dates = "2015-01-01", "2015-01-11", "2015-01-23"
9+
n_timesteps_per_chunk = 10, 11, 10
10+
/

config.example/config.example.nml

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ output_path = "data/output/" ! Path to store the outp
3232
! the more data that is output and the larger the output data file sizes
3333
&output
3434
write_csv = .true. ! Should we write output data to CSV files (n.b. turning this off currently suppresses all output, except for runs to steady state)
35+
write_netcdf = .true. ! Should we write output data to a NetCDF file?
36+
netcdf_write_mode = 'itr' ! When should we write to the NetCDF file? Every time step ('itr') or at the end of the run ('end'). Every time step is slower, end uses much more memory
3537
write_metadata_as_comment = .true. ! Should output data metadata (e.g. column descriptions) be included as comments at top of CSV files?
3638
write_compartment_stats = .true. ! Should a file with summary stats for each compartment (soil, water, sediment) be written?
3739
include_waterbody_breakdown = .true. ! For surface water output, include breakdown over waterbodies or aggregate at grid cell level?

config.example/test-scenario.example.nml

+6-27
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ n_sediment_layers = 4 ! Number of sediment lay
88
n_nm_size_classes = 5 ! Number of NM size classes
99
n_spm_size_classes = 5 ! Number of SPM size classes
1010
n_fractional_compositions = 4 ! Number of fractional compositions for sediment
11-
n_other_sites = 1 ! Number of other sampling sites
1211
/
1312

1413
&nanomaterial
@@ -17,15 +16,6 @@ n_nm_forms = 4 ! Number of NM forms (fr
1716
n_nm_extra_states = 2 ! Number of extra NM states, other than heteroaggregated to SPM
1817
/
1918

20-
! Run the model in calibration mode? Documentation here: [doc/calibration.md](../doc/calibration.md)
21-
&calibrate
22-
calibration_run = .false. ! Run in calibration mode or not? Following variables only required if this is true
23-
site_data = "" ! Path to CSV of sampling sites
24-
start_site = "" ! Code of start site (most upstream) in sampling sites CSV
25-
end_site = "" ! Code of end site (most downstream) in sampling sites CSV
26-
other_sites = "" ! Which other sites should be used from sampling sites CSV? Array of length &allocatable_array_sizes > n_other_sites
27-
/
28-
2919
! Paths to data. For info on compiling data for the NanoFASE model, see the nanofase-data repo: https://github.com/NERC-CEH/nanofase-data
3020
&data
3121
input_file = "data.example/test-scenario.nc" ! Path to NetCDF input data, which includes most of the spatial and temporally resolved data
@@ -34,14 +24,16 @@ output_path = "data/output/" ! Path to store the outp
3424
/
3525

3626
&output
37-
write_csv = .true. ! Should we write output data to CSV files (n.b. turning this off currently suppresses all output, except for runs to steady state)
38-
write_metadata_as_comment = .true. ! Should output data metadata (e.g. column descriptions) be included as comment at top of CSV file?
27+
write_csv = .true. ! Should we write output data to CSV files?
28+
write_netcdf = .true. ! Should we write output data to a NetCDF file?
29+
netcdf_write_mode = 'end'
30+
write_metadata_as_comment = .false. ! Should output data metadata (e.g. column descriptions) be included as comment at top of CSV file?
31+
include_waterbody_breakdown = .true.
3932
include_sediment_layer_breakdown = .true. ! Include breakdown of data over sediment layers?
4033
include_soil_layer_breakdown = .false. ! Include breaedown of data over soil layers?
4134
soil_pec_units = 'kg/kg' ! What units to use for soil PEC - kg/kg or kg/m3?
4235
sediment_pec_units = 'kg/kg' ! What units to use for sediment PEC - kg/kg or kg/m3?
4336
include_soil_state_breakdown = .false. ! Include breakdown of NM state - free vs attached to soil matrix
44-
! output_rate_constant_summary = .true.
4537
/
4638

4739
&run
@@ -56,19 +48,6 @@ log_file_path = "log/" ! Where to place model l
5648
warm_up_period = 0 ! Warm up period before main simulation begin. *Not yet implemented.*
5749
/
5850

59-
&checkpoint
60-
checkpoint_file = "./checkpoint.dat" ! Location of checkpoint file to read from and/or save to
61-
save_checkpoint = .false. ! Save a checkpoint file when the run is finished? Defaults to false
62-
reinstate_checkpoint = .false. ! Reinstate a checkpoint from checkpoint_file? Defaults to false
63-
preserve_timestep = .false. ! Should the timestep from the checkpoint be used as a starting timestep in a reinstated run?
64-
/
65-
66-
&steady_state
67-
run_to_steady_state = .false.
68-
mode = 'sediment_size_distribution'
69-
delta = 1e-4 ! Delta value to test whether at steady state
70-
/
71-
7251
&soil
7352
soil_layer_depth = 0.05, 0.15, 0.2 ! Depth of each soil layer. Array of length &allocatable_array_sizes > n_soil_layers
7453
include_bioturbation = .true. ! Should bioturbation be modelled?
@@ -77,7 +56,7 @@ include_attachment = .true. ! Should attachment be m
7756

7857
&sediment
7958
sediment_layer_depth = 0.01, 0.01, 0.01, 0.01 ! Depth of each sediment layer. Array of length &allocatable_array_sizes > n_sediment_layers
80-
spm_size_classes = 1e-6, 3e-6, 10e-6, 30e-6, 100e-6 ! Diameter of SPM in each binned size class [m]
59+
spm_size_classes = 0.002e-3, 0.06e-3, 0.2e-3, 0.6e-3, 2.0e-3 ! Diameter of SPM in each binned size class [m]
8160
include_bed_sediment = .true. ! Should bed sediment be modelled?
8261
sediment_particle_densities = 1500, 2600, 2600, 2600 ! Density of sediment particles in each fractional composition class [kg/m3]
8362
/

data.example/test-scenario-t11.nc

74.9 KB
Binary file not shown.

data.example/test-scenario.nc

7.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)