Skip to content

Commit 2652767

Browse files
committed
update musica build and dependencies
1 parent 66c49e6 commit 2652767

File tree

6 files changed

+193
-83
lines changed

6 files changed

+193
-83
lines changed

cime_config/atm_musica_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
MUSICA_CCPP_SCHEME_NAME = "musica_ccpp"
77
MUSICA_CONFIG_DIR_NAME = "musica_configurations"
88
MUSICA_REPO_URL = "https://github.com/NCAR/musica.git"
9-
MUSICA_TAG = "72c3b398fa4713effc5648b29b8070cb432eebf2"
9+
MUSICA_TAG = "25fff7ae42d146bf3f83ad5ac18b3caac8701ddd"
1010
CHEMISTRY_DATA_REPO_URL = "https://github.com/NCAR/cam-sima-chemistry-data.git"
1111
CHEMISTRY_DATA_TAG = "71ed143c54b0d5d6e3e70f3d05d413fddcf8d59e"

cime_config/buildlib

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ from CIME.buildlib import parse_input
3232
from CIME.build import get_standard_makefile_args
3333
from CIME.Tools.standard_script_setup import check_minimum_python_version
3434
from CIME.locked_files import lock_file, unlock_file
35+
from CIME.XML.env_build import EnvBuild
3536
#pylint: enable=wrong-import-position
3637

3738
check_minimum_python_version(3, 7) #CAM requires version 3.7 or greater
@@ -180,7 +181,7 @@ def _build_cam():
180181
musica_install_path = _build_musica(caseroot)
181182

182183
cam_linked_libs = case.get_value("CAM_LINKED_LIBS")
183-
musica_libs = "-lmusica-fortran -lmusica -lmechanism_configuration -lyaml-cpp -lstdc++"
184+
musica_libs = _get_musica_libs(caseroot)
184185
if not musica_libs in cam_linked_libs:
185186
_set_musica_lib_path(musica_install_path, caseroot)
186187

@@ -249,6 +250,64 @@ def _copy2_as_needed(src: str, dst: str) -> None:
249250
# Example scenario: User added some new source code files.
250251
shutil.copy2(src, dst)
251252

253+
###############################################################################
254+
def _cmake_default_args(caseroot):
255+
###############################################################################
256+
# Returns a dictionary of CMake variables based on the Macros.cmake file for
257+
# the build.
258+
259+
unlock_file("env_build.xml", caseroot)
260+
build = EnvBuild(case_root=caseroot)
261+
with Case(caseroot) as case:
262+
macro_path = os.path.abspath(os.path.join(caseroot, "cmake_macros", ""))
263+
args = "-DCONVERT_TO_MAKE=ON "
264+
args += "-DCASEROOT={} ".format(caseroot)
265+
args += "-DCOMPILER={} ".format(build.get_value("COMPILER"))
266+
args += "-DOS={} ".format(build.get_value("OS"))
267+
args += "-DMACH={} ".format(case.get_value("MACH"))
268+
args += "-DCMAKE_C_COMPILER_WORKS=1 "
269+
args += "-DCMAKE_Fortran_COMPILER_WORKS=1 "
270+
args += "-DCMAKE_CXX_COMPILER_WORKS=1 "
271+
args += "-DDEBUG={} ".format(build.get_value("DEBUG"))
272+
cmd = "cmake {} .".format(args)
273+
rc, out, err = run_cmd(cmd, combine_output=True, from_dir=macro_path)
274+
expect(rc == 0, "Command {} failed with rc={} out={} err={}".format(cmd, rc, out, err))
275+
276+
arg_dict = {}
277+
for line in out.splitlines():
278+
if ":=" in line:
279+
key, val = line.split(":=")
280+
arg_dict[key.replace('CIME_SET_MAKEFILE_VAR','').strip()] = val.strip()
281+
282+
unlock_file("env_build.xml", caseroot)
283+
return arg_dict
284+
285+
###############################################################################
286+
def _get_musica_libs(caseroot: str) -> str:
287+
###############################################################################
288+
"""
289+
Returns the MUSICA libraries to be linked to CAM.
290+
291+
Args:
292+
caseroot: CASEROOT where the xmlchange command is located
293+
294+
Raises:
295+
Exception: If the subprocess for the xmlquery command fails,
296+
an exception is raised with the error message.
297+
298+
Returns:
299+
musica_libs: MUSICA libraries to be linked to CAM
300+
"""
301+
unlock_file("env_build.xml", caseroot)
302+
build = EnvBuild(case_root=caseroot)
303+
if build.get_value("DEBUG"):
304+
musica_libs = "-lmusica-fortran -lmusica -lmechanism_configuration -lyaml-cppd -lstdc++"
305+
else:
306+
musica_libs = "-lmusica-fortran -lmusica -lmechanism_configuration -lyaml-cpp -lstdc++"
307+
lock_file("env_build.xml", caseroot)
308+
309+
return musica_libs
310+
252311
###############################################################################
253312
def _build_musica(clone_dest: str) -> str:
254313
###############################################################################
@@ -266,20 +325,29 @@ def _build_musica(clone_dest: str) -> str:
266325
musica_install_path: path to the MUSICA installation directory
267326
"""
268327
_clone_and_checkout(MUSICA_REPO_URL, MUSICA_TAG, clone_dest)
328+
build = EnvBuild(case_root=clone_dest)
329+
arg_dict = _cmake_default_args(clone_dest)
269330

270331
bld_path = os.path.join(clone_dest, "musica", "build")
271332
if os.path.exists(bld_path):
272333
shutil.rmtree(bld_path)
273334
os.makedirs(bld_path)
274-
275335
install_dir = "install"
336+
337+
with Case(clone_dest) as case:
338+
if build.get_value("DEBUG"):
339+
cmake_build_type = "Debug"
340+
else:
341+
cmake_build_type = "Release"
342+
276343
command = [
277344
"cmake",
278345
f"-D CMAKE_INSTALL_PREFIX={install_dir}",
279-
"-D CMAKE_BUILD_TYPE=Release",
346+
f"-D CMAKE_BUILD_TYPE={cmake_build_type}",
347+
f"-D CMAKE_Fotran_FLAGS={arg_dict['FFLAGS']}",
280348
"-D MUSICA_ENABLE_TESTS=OFF",
281349
"-D MUSICA_BUILD_FORTRAN_INTERFACE=ON",
282-
"-D MICM_DEFAULT_VECTOR_SIZE=128",
350+
"-D MUSICA_SET_MICM_DEFAULT_VECTOR_SIZE=128",
283351
".."
284352
]
285353
try:
@@ -366,14 +434,14 @@ def _set_musica_lib_path(musica_install_path: str, caseroot: str) -> None:
366434
an exception is raised with the error message.
367435
"""
368436

369-
unlock_file("env_build.xml", caseroot)
437+
musica_libs = _get_musica_libs(caseroot)
370438

371439
command = [
372440
"./xmlchange",
373441
"--append",
374442
# The libraries must be on the same line because CIME flags an
375443
# error for multi-character arguments preceded by a single dash
376-
f"CAM_LINKED_LIBS=-L{os.path.join(musica_install_path, 'lib64')} -lmusica-fortran -lmusica -lmechanism_configuration -lyaml-cpp -lstdc++"
444+
f"CAM_LINKED_LIBS=-L{os.path.join(musica_install_path, 'lib64')} {musica_libs}"
377445
]
378446
try:
379447
subprocess.run(command, cwd=caseroot, stdout=subprocess.PIPE,
@@ -386,8 +454,6 @@ def _set_musica_lib_path(musica_install_path: str, caseroot: str) -> None:
386454
except OSError as e:
387455
raise OSError("An error occurred while executing the 'xmlchange' command.") from e
388456

389-
lock_file("env_build.xml", caseroot)
390-
391457
###############################################################################
392458
def _clone_and_checkout(repo_url: str, tag_name: str, clone_dest: str) -> None:
393459
###############################################################################

src/control/cam_comp.F90

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ subroutine cam_init(caseid, ctitle, model_doi_url, &
100100
use air_composition, only: air_composition_init
101101
use cam_ccpp_cap, only: cam_ccpp_initialize_constituents
102102
use cam_ccpp_cap, only: cam_model_const_properties
103-
use cam_ccpp_cap, only: cam_constituents_array
104103
use physics_grid, only: columns_on_task
105104
use vert_coord, only: pver
106105
use phys_vars_init_check, only: mark_as_initialized
@@ -156,7 +155,6 @@ subroutine cam_init(caseid, ctitle, model_doi_url, &
156155
character(len=cx) :: errmsg
157156

158157
type(ccpp_constituent_prop_ptr_t), pointer :: constituent_properties(:)
159-
real(kind_phys), pointer :: constituents_array(:,:,:)
160158
!-----------------------------------------------------------------------
161159

162160
call init_pio_subsystem()
@@ -255,17 +253,14 @@ subroutine cam_init(caseid, ctitle, model_doi_url, &
255253
! Read tropopause climatology
256254
call tropopause_climo_read_file()
257255

258-
! Temporary: Prescribe realistic but inaccurate physical quantities
256+
! TEMPORARY: Prescribe realistic but inaccurate physical quantities
259257
! necessary for MUSICA that are currently unavailable in CAM-SIMA.
260-
! It also initializes the MUSICA constituent values until the file
261-
! I/O object is implemented.
262258
!
263259
! Remove this when MUSICA input data are available from CAM-SIMA or
264260
! other physics schemes.
265261
constituent_properties => cam_model_const_properties()
266-
constituents_array => cam_constituents_array()
267262
call musica_ccpp_dependencies_init(columns_on_task, pver, &
268-
constituent_properties, constituents_array, phys_suite_name)
263+
constituent_properties, phys_suite_name)
269264

270265
! Initialize orbital data
271266
call orbital_data_init(columns_on_task)
@@ -295,10 +290,15 @@ subroutine cam_timestep_init()
295290
!
296291
!-----------------------------------------------------------------------
297292

298-
use phys_comp, only: phys_timestep_init
299-
use physics_grid, only: lat_rad, lon_rad
300-
use orbital_data, only: orbital_data_advance
301-
use stepon, only: stepon_timestep_init
293+
use phys_comp, only: phys_timestep_init
294+
use physics_grid, only: lat_rad, lon_rad
295+
use orbital_data, only: orbital_data_advance
296+
use stepon, only: stepon_timestep_init
297+
use cam_ccpp_cap, only: cam_constituents_array
298+
use ccpp_kinds, only: kind_phys
299+
use musica_ccpp_dependencies, only: set_initial_musica_concentrations
300+
301+
real(kind_phys), pointer :: constituents_array(:,:,:)
302302

303303
! Update current fractional calendar day. Needs to be updated at every timestep.
304304
calday = get_curr_calday()
@@ -319,6 +319,21 @@ subroutine cam_timestep_init()
319319
call stepon_timestep_init(dtime_phys, cam_runtime_opts, phys_state, phys_tend, &
320320
dyn_in, dyn_out)
321321
call t_stopf('stepon_timestep_init')
322+
323+
!----------------------------------------------------------
324+
! TEMPORARY: Set initial MUSICA constituent values
325+
!
326+
! This is a temporary workaround to initialize
327+
! MUSICA constituent values until the file I/O
328+
! capability is implemented.
329+
! Remove this when MUSICA species are initialized
330+
! by CAM-SIMA.
331+
!----------------------------------------------------------
332+
if (is_first_timestep) then
333+
constituents_array => cam_constituents_array()
334+
call set_initial_musica_concentrations(constituents_array)
335+
end if
336+
322337
!
323338
!----------------------------------------------------------
324339
! PHYS_TIMESTEP_INIT Call the Physics package

src/data/air_composition.F90

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,6 @@ subroutine air_composition_init()
179179

180180
liq_num = 0
181181
ice_num = 0
182-
has_liq = .false.
183-
has_ice = .false.
184182
ix = -1
185183
! standard dry air (constant composition)
186184
o2_mwi = 1._kind_phys / 32._kind_phys
@@ -239,9 +237,10 @@ subroutine air_composition_init()
239237
icnst = 1
240238
water_species_num = 0
241239
dry_species_num = 0
242-
has_ice = .false.
243240
do idx = 1, num_advected
244241
cnst_stdname = const_name(idx)
242+
has_liq = .false.
243+
has_ice = .false.
245244
select case (TRIM((cnst_stdname)))
246245
!
247246
! O
@@ -437,6 +436,8 @@ subroutine air_composition_init()
437436
!
438437
! If support for more major species is to be included add code here
439438
!
439+
case default
440+
cycle ! skip thermodynamically inactive species
440441
end select
441442

442443
if (masterproc) then
@@ -460,8 +461,6 @@ subroutine air_composition_init()
460461
end if
461462
write(iulog, *) " "
462463
end if
463-
has_liq = .false.
464-
has_ice = .false.
465464
end do
466465

467466
!Set dry air thermodynamic properities if no dry air species provided:
@@ -565,12 +564,14 @@ subroutine water_composition_update(mmr, ncol, energy_formula, cp_or_cv_dycore,
565564
! SE
566565
! Note: species index subset to 1: because SIMA currently uses index 0. See GitHub issue #334 in ESCOMP/CAM-SIMA.
567566
call get_cp(mmr(:ncol,:,:), .false., cp_or_cv_dycore(:ncol,:), &
568-
factor=to_dry_factor, active_species_idx_dycore=thermodynamic_active_species_idx(1:), &
567+
factor=to_dry_factor, &
568+
active_species_idx_dycore=thermodynamic_active_species_idx(1:thermodynamic_active_species_num), &
569569
cpdry=cpairv(:ncol,:))
570570
else if (energy_formula == ENERGY_FORMULA_DYCORE_MPAS) then
571571
! MPAS
572572
! Note: species index subset to 1: because SIMA currently uses index 0. See GitHub issue #334 in ESCOMP/CAM-SIMA.
573-
call get_R(mmr(:ncol,:,:), thermodynamic_active_species_idx(1:), &
573+
call get_R(mmr(:ncol,:,:), &
574+
thermodynamic_active_species_idx(1:thermodynamic_active_species_num), &
574575
cp_or_cv_dycore(:ncol,:), fact=to_dry_factor, Rdry=rairv(:ncol,:))
575576

576577
! internal energy coefficient for MPAS

0 commit comments

Comments
 (0)