Skip to content

Commit

Permalink
Enable MOM6 standalone exe and ACCESS3MOM6 library
Browse files Browse the repository at this point in the history
  • Loading branch information
minghangli-uni committed Jan 20, 2025
1 parent bea9146 commit d2112f8
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 76 deletions.
181 changes: 105 additions & 76 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ cmake_minimum_required(VERSION 3.18)
#[==============================================================================[
# Basic project definition #
#]==============================================================================]

project(MOM6 VERSION "0.1.0"
DESCRIPTION "MOM6"
HOMEPAGE_URL https://github.com/ACCESS-NRI/MOM6
Expand All @@ -12,51 +11,41 @@ project(MOM6 VERSION "0.1.0"
#[==============================================================================[
# Project configuration #
#]==============================================================================]
# Configurations to build
list(APPEND KnownConfigurations MOM6 ACCESS-MOM6)
option(ENABLE_MOM6 "Building MOM6 executable standalone" OFF)
option(ENABLE_ACCESS_MOM6 "Building MOM6 library with Access3share" ON)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR})
include(GNUInstallDirs)
include(FortranLib)
message(STATUS "Configuration")
message(STATUS " Building MOM6 executable standalone " ${ENABLE_MOM6})
message(STATUS " Building MOM6 library with Access3share " ${ENABLE_ACCESS_MOM6})

# Common compiler flags and definitions
if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fbacktrace -fconvert=big-endian -ffree-line-length-none -ffixed-line-length-none")
if(${CMAKE_Fortran_COMPILER_VERSION} VERSION_GREATER_EQUAL 10)
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fallow-argument-mismatch")
endif()
set(CMAKE_Fortran_FLAGS "-fdefault-real-8 -fdefault-double-8")
set(CMAKE_Fortran_FLAGS_RELEASE "-O")
set(CMAKE_Fortran_FLAGS_DEBUG "-g -Wall -Og -ffpe-trap=zero,overflow -fcheck=bounds")
elseif(CMAKE_Fortran_COMPILER_ID MATCHES "Intel")
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -qno-opt-dynamic-align -convert big_endian -assume byterecl -ftz -traceback -assume realloc_lhs -fp-model source")
set(CMAKE_Fortran_FLAGS_RELEASE "-O2 -debug minimal")
set(CMAKE_Fortran_FLAGS_DEBUG "-O0 -g -check uninit -check bounds -check pointers -fpe0 -check noarg_temp_created")
set(CMAKE_Fortran_FLAGS "-r8")
else()
message(WARNING "Fortran compiler with ID ${CMAKE_Fortran_COMPILER_ID} will be used with CMake default options")
endif()
# build options
option(ENABLE_MOM_SYMMETRIC "Use MOM symmetric memory" ON )
option(ENABLE_CESMCOUPLED "Use CESMCOUPLED" OFF)

if(CMAKE_C_COMPILER_ID MATCHES "GNU")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
set(CMAKE_C_FLAGS_RELEASE "-O")
set(CMAKE_C_FLAGS_DEBUG "-g -Wall -Og -fbacktrace -ffpe-trap=invalid,zero,overflow -fcheck=bounds")
elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -traceback -qno-opt-dynamic-align -fp-model precise -std=gnu99")
set(CMAKE_C_FLAGS_RELEASE "-O2 -debug minimal")
set(CMAKE_C_FLAGS_DEBUG "-O0 -g")
else()
message(WARNING "C compiler with ID ${CMAKE_C_COMPILER_ID} will be used with CMake default options")
if (ENABLE_CESMCOUPLED AND NOT ENABLE_ACCESS_MOM6)
message(FATAL_ERROR "ENABLE_CESMCOUPLED can only be set if ENABLE_ACCESS_MOM6 is ON.")
endif()

add_compile_definitions(
_USE_GENERIC_TRACER
_USE_MOM6_DIAG
)
message(STATUS "Build options")
message(STATUS "MOM_SYMMETRIC ${ENABLE_MOM_SYMMETRIC}")
message(STATUS "CESMCOUPLED ${ENABLE_CESMCOUPLED}" )

# Conditionally define CESMCOUPLED
if (CESMCOUPLED)
add_compile_definitions(
CESMCOUPLED
)
#[==============================================================================[
# Project configuration #
#]==============================================================================]
# include libraries
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR})
include(GNUInstallDirs)
include(FortranLib)
include(CompilerFlags)

# Precessor (macro) definitions
add_compile_definitions(_USE_GENERIC_TRACER _USE_MOM6_DIAG)

if (ENABLE_CESMCOUPLED)
add_compile_definitions(CESMCOUPLED)
endif()

## Fortran modules path; currently this is simply set to be the include dir
Expand All @@ -68,37 +57,47 @@ set(CMAKE_INSTALL_MODULEDIR ${CMAKE_INSTALL_INCLUDEDIR}
#[==============================================================================[
# External packages #
#]==============================================================================]

find_package(NetCDF REQUIRED Fortran)
find_package(fms COMPONENTS R8 REQUIRED)
find_package(ESMF 8.3.0 MODULE REQUIRED)
#find_package(PIO 2.5.3 REQUIRED COMPONENTS C Fortran)
#find_package(FoX 4.1.2 REQUIRED)
find_package(PIO 2.6.2 REQUIRED COMPONENTS C Fortran)

if (ENABLE_ACCESS_MOM6)
find_package(ESMF 8.3.0 MODULE REQUIRED)
find_package(Access3Share REQUIRED cdeps timing share nuopc_cap_share)
endif()

### Targets

## MOM6 library

set(SRC "${CMAKE_CURRENT_SOURCE_DIR}/../src")
set(CONFIG_SRC "${CMAKE_CURRENT_SOURCE_DIR}/../config_src")
# set paths for the source code and config source
set(SRC "${CMAKE_SOURCE_DIR}/../src")
set(CONFIG_SRC "${CMAKE_SOURCE_DIR}/../config_src")

add_fortran_library(mom6lib mod STATIC)

add_fortran_library(OM3_mom6 mod STATIC)
target_include_directories(mom6lib PRIVATE $<BUILD_INTERFACE:${SRC}/framework>) # include version_variable.h

target_include_directories(OM3_mom6 PRIVATE $<BUILD_INTERFACE:${SRC}/framework>)
if(OM3_MOM_SYMMETRIC)
target_include_directories(OM3_mom6 PRIVATE $<BUILD_INTERFACE:${CONFIG_SRC}/memory/dynamic_symmetric>)
if(ENABLE_MOM_SYMMETRIC)
set(MOM_MEMORY_DIR $<BUILD_INTERFACE:${CONFIG_SRC}/memory/dynamic_symmetric>) # include MOM_memory.h
else()
target_include_directories(OM3_mom6 PRIVATE $<BUILD_INTERFACE:${CONFIG_SRC}/memory/dynamic_nonsymmetric>)
set(MOM_MEMORY_DIR $<BUILD_INTERFACE:${CONFIG_SRC}/memory/dynamic_nonsymmetric>) # include MOM_memory.h
endif()
target_compile_options(OM3_mom6 PRIVATE "$<$<COMPILE_LANGUAGE:Fortran>:${fortran_compile_flags}>")
target_link_libraries(OM3_mom6
target_include_directories(mom6lib PRIVATE ${MOM_MEMORY_DIR})

target_compile_options(mom6lib PRIVATE "$<$<COMPILE_LANGUAGE:Fortran>:${fortran_compile_flags}>")

# link libraries
target_link_libraries(mom6lib PRIVATE FMS::fms_r8)

if(ENABLE_ACCESS_MOM6)
target_link_libraries(mom6lib
PUBLIC esmf
PRIVATE FMS::fms_r8
#PRIVATE AccessOM3::nuopc_cap_share AccessOM3::share FMS::fms_r8
PRIVATE Access3::nuopc_cap_share Access3::share Access3::timing Access3::cdeps-common
)
endif()


target_sources(OM3_mom6 PRIVATE
target_sources(mom6lib PRIVATE
${SRC}/ALE/coord_adapt.F90
${SRC}/ALE/coord_hycom.F90
${SRC}/ALE/coord_rho.F90
Expand Down Expand Up @@ -404,42 +403,72 @@ target_sources(OM3_mom6 PRIVATE
${CONFIG_SRC}/infra/FMS2/MOM_time_manager.F90

${CONFIG_SRC}/infra/FMS2/MOM_couplertype_infra.F90 #add_patched_sources
)

if (ENABLE_ACCESS_MOM6)
target_sources(mom6lib PRIVATE
${CONFIG_SRC}/drivers/nuopc_cap/mom_cap_time.F90
${CONFIG_SRC}/drivers/nuopc_cap/mom_surface_forcing_nuopc.F90
${CONFIG_SRC}/drivers/nuopc_cap/ocn_comp_NUOPC.F90
${CONFIG_SRC}/drivers/nuopc_cap/time_utils.F90

${CONFIG_SRC}/drivers/nuopc_cap/mom_cap.F90 #add_patched_sources
${CONFIG_SRC}/drivers/nuopc_cap/mom_cap_methods.F90 #add_patched_sources
${CONFIG_SRC}/drivers/nuopc_cap/mom_ocean_model_nuopc.F90 #add_patched_sources
)
endif()

)

if (ENABLE_MOM6)
target_sources(mom6lib PRIVATE
${CONFIG_SRC}/drivers/solo_driver/MOM_surface_forcing.F90
${CONFIG_SRC}/drivers/solo_driver/MESO_surface_forcing.F90
${CONFIG_SRC}/drivers/solo_driver/user_surface_forcing.F90
)
endif()
### Install and Export

## Library
if(OM3_LIB_INSTALL)
set_target_properties(OM3_mom6 PROPERTIES
OUTPUT_NAME access-mom6
EXPORT_NAME mom6
if(ENABLE_ACCESS_MOM6)
set_target_properties(mom6lib PROPERTIES
OUTPUT_NAME access-mom6lib
EXPORT_NAME mom6lib
)
install(TARGETS OM3_mom6
EXPORT AccessOM3mom6_Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessOM3_RunTime NAMELINK_COMPONENT AccessOM3_Development
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessOM3_Development
install(TARGETS mom6lib
EXPORT MOM6libTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessMOM6Cmeps_runtime NAMELINK_COMPONENT AccessMOM6Cmeps_Development
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT AccessMOM6Cmeps_Development
)
# Fortran module files are a special case, as currently there is no standard
# way of handling them in CMake
target_include_directories(OM3_mom6 PUBLIC "$<INSTALL_INTERFACE:${CMAKE_INSTALL_MODULEDIR}/access-mom6>")
get_target_property(mom_moddir OM3_mom6 Fortran_MODULE_DIRECTORY)
target_include_directories(mom6lib PUBLIC "$<INSTALL_INTERFACE:${CMAKE_INSTALL_MODULEDIR}/mom6lib>")
get_target_property(mom_moddir mom6lib Fortran_MODULE_DIRECTORY)
install(FILES ${mom_moddir}/ocn_comp_nuopc.mod ${mom_moddir}/mom_cap_mod.mod
DESTINATION ${CMAKE_INSTALL_MODULEDIR}/access-mom6
COMPONENT AccessOM3_Development
DESTINATION ${CMAKE_INSTALL_MODULEDIR}/mom6lib
COMPONENT AccessMOM6Cmeps_Development
)
install(EXPORT MOM6libTargets
FILE MOM6libTargets.cmake
NAMESPACE Access3::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Mom6lib
)
endif()

# executable
if (ENABLE_MOM6)
add_executable(MOM6 ${CONFIG_SRC}/drivers/solo_driver/MOM_driver.F90)
target_link_libraries(MOM6 PRIVATE mom6lib)

target_include_directories(MOM6 PRIVATE
${SRC}/framework
${MOM_MEMORY_DIR}
${CONFIG_SRC}/drivers/solo_driver
)

set_target_properties(MOM6 PROPERTIES
LINKER_LANGUAGE Fortran
OUTPUT_NAME mom6
)
install(EXPORT AccessOM3mom6_Targets
FILE AccessOM3mom6Targets.cmake
NAMESPACE AccessOM3::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/AccessOM3

install(TARGETS MOM6
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
endif()
32 changes: 32 additions & 0 deletions cmake/CompilerFlags.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Common compiler flags and definitions

# Fortran compiler flags
if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fbacktrace -fconvert=big-endian -ffree-line-length-none -ffixed-line-length-none")
if(${CMAKE_Fortran_COMPILER_VERSION} VERSION_GREATER_EQUAL 10)
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fallow-argument-mismatch")
endif()
set(CMAKE_Fortran_FLAGS "-fdefault-real-8 -fdefault-double-8")
set(CMAKE_Fortran_FLAGS_RELEASE "-O")
set(CMAKE_Fortran_FLAGS_DEBUG "-g -Wall -Og -ffpe-trap=zero,overflow -fcheck=bounds")
elseif(CMAKE_Fortran_COMPILER_ID MATCHES "Intel")
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -qno-opt-dynamic-align -convert big_endian -assume byterecl -ftz -traceback -assume realloc_lhs -fp-model source")
set(CMAKE_Fortran_FLAGS_RELEASE "-O2 -debug minimal")
set(CMAKE_Fortran_FLAGS_DEBUG "-O0 -g -check uninit -check bounds -check pointers -fpe0 -check noarg_temp_created")
set(CMAKE_Fortran_FLAGS "-r8")
else()
message(WARNING "Fortran compiler with ID ${CMAKE_Fortran_COMPILER_ID} will be used with CMake default options")
endif()

# C compiler flags
if(CMAKE_C_COMPILER_ID MATCHES "GNU")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
set(CMAKE_C_FLAGS_RELEASE "-O")
set(CMAKE_C_FLAGS_DEBUG "-g -Wall -Og -fbacktrace -ffpe-trap=invalid,zero,overflow -fcheck=bounds")
elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -traceback -qno-opt-dynamic-align -fp-model precise -std=gnu99")
set(CMAKE_C_FLAGS_RELEASE "-O2 -debug minimal")
set(CMAKE_C_FLAGS_DEBUG "-O0 -g")
else()
message(WARNING "C compiler with ID ${CMAKE_C_COMPILER_ID} will be used with CMake default options")
endif()

0 comments on commit d2112f8

Please sign in to comment.