IFS_mock.py 6 KB
Newer Older
rmarti1's avatar
rmarti1 committed
#!/usr/bin/env python3
# Copyright 2021 - Barcelona Supercomputing Center
# Author: Rodrigo Martín Posada
# MIT License

import f90nml
rmarti1's avatar
rmarti1 committed
import pyoasis
from pyoasis import OASIS  # pylint: disable=no-name-in-module
import numpy as np
rmarti1's avatar
rmarti1 committed
import logging
NAMELIST_FILE_NAME = 'namelist.amip'
rmarti1's avatar
rmarti1 committed
L080_NX = 35718  # number of LAND grid cells at T159 resolution
L128_NX = 88838  # number of LAND grid cells at T255 resolution
rmarti1's avatar
rmarti1 committed
L256_NX = 348528  # number of LAND grid cells at T511 resolution
CO2_MODE = False

def def_local_partition(nx, npes, mype):
    il_size = nx // npes
    il_offset = mype * il_size
    if (npes-mype) <= nx % npes:
        il_size += 1
        il_offset += (nx % npes) - (npes-mype)
    return il_size, il_offset
rmarti1's avatar
rmarti1 committed
if __name__ == '__main__':
    # Constants from namelist
    nml = f90nml.read(NAMELIST_FILE_NAME)
    COUPLING_INTERVAL = nml['NAMAMIP']['TimeStepSec']
    RUN_LENGTH_SEC = nml['NAMAMIP']['RunLengthSec']

    # Init OASIS
    comp = pyoasis.Component("IFS_MOCK")
rmarti1's avatar
rmarti1 committed
    local_comm = comp.localcomm
    # Get rank in local communicator
    mype = comp.localcomm.rank
    npes = comp.localcomm.size

    # Unit for output messages
rmarti1's avatar
rmarti1 committed
    out_ifs = 'IFS_mock.out_'+str(100+mype)
rmarti1's avatar
rmarti1 committed
    logging.basicConfig(filename=out_ifs, format='%(message)s', level=logging.DEBUG)
    logging.debug('-----------------------------------------------------------')
    logging.debug('I am IFS-mock process with rank : {}'.format(mype))
    logging.debug('in my local communicator gathering {} processes'.format(npes))
    logging.debug('----------------------------------------------------------')
rmarti1's avatar
rmarti1 committed

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    #  PARTITION DEFINITION
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    with open('namcouple', 'r') as f:
        contents = f.read()
rmarti1's avatar
rmarti1 committed
            CO2_MODE = True
        if 'L080' in contents:
            nx = L080_NX
        elif 'L128' in contents:
            nx = L128_NX
        elif 'L256' in contents:
            nx = L256_NX
        else:
            raise Exception('Invalid IFS grid in namcouple')
rmarti1's avatar
rmarti1 committed
    # Definition of the local partition
    il_size, il_offset = def_local_partition(nx, npes, mype)
rmarti1's avatar
rmarti1 committed
    logging.debug('Local partition definition')
    logging.debug('il_size = {} il_offset = {} mype = {}'.format(il_size, il_offset, mype))
rmarti1's avatar
rmarti1 committed
    partition = pyoasis.ApplePartition(il_offset, il_size)
        if mype == 0:
            logging.debug('Local serial partition')
            logging.debug('nx_co2 = {} mype = {}'.format(nx_co2, mype))
            partition2 = pyoasis.SerialPartition(nx_co2)
rmarti1's avatar
rmarti1 committed

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    #  DECLARATION OF THE COUPLING FIELDS
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    A_SST = pyoasis.Var("A_SST", partition, OASIS.IN)
    A_Ice_frac = pyoasis.Var("A_Ice_frac", partition, OASIS.IN)
rmarti1's avatar
rmarti1 committed
    logging.debug('var_id A_SST, var_id A_Ice_frac {} {}'.format(A_SST._id, A_Ice_frac._id))
    if CO2_MODE:
        A_CO2_emis = pyoasis.Var("A_CO2_emis", partition, OASIS.IN)
        logging.debug('var_id A_CO2_emis {}'.format(A_CO2_emis._id))
        if mype == 0:
            A_CO2_cconc = pyoasis.Var("A_CO2_cconc", partition2, OASIS.IN)
            logging.debug('var_id A_CO2_cconc {}'.format(A_CO2_cconc._id))
            A_CO2_econc = pyoasis.Var("A_CO2_econc", partition2, OASIS.IN)
            logging.debug('var_id A_CO2_econc {}'.format(A_CO2_cconc._id))
rmarti1's avatar
rmarti1 committed

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    #         TERMINATION OF DEFINITION PHASE
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
rmarti1's avatar
rmarti1 committed
    logging.debug('End of initialisation phase')
rmarti1's avatar
rmarti1 committed

    comp.enddef()

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    #  TIME STEP LOOP
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
rmarti1's avatar
rmarti1 committed
    logging.debug('Timestep, field min, mean and max value')
rmarti1's avatar
rmarti1 committed
    field_recv_A_SST = pyoasis.asarray(np.full(il_size, -1.0))
    field_recv_A_Ice_frac = pyoasis.asarray(np.full(il_size, -1.0))
    field_recv_A_CO2_emis = pyoasis.asarray(np.full(il_size, -1.0))
    if CO2_MODE and mype == 0:
        field_recv_A_CO2_cconc = pyoasis.asarray(np.full(nx_co2, -1.0))
        field_recv_A_CO2_econc = pyoasis.asarray(np.full(nx_co2, -1.0))
    for itap_sec in range(0, RUN_LENGTH_SEC, COUPLING_INTERVAL):
rmarti1's avatar
rmarti1 committed
        A_SST.get(itap_sec, field_recv_A_SST)
        A_Ice_frac.get(itap_sec, field_recv_A_Ice_frac)

rmarti1's avatar
rmarti1 committed
        logging.debug('ITAP_SEC: {}'.format(itap_sec))
        logging.debug('A_SST -> {:.3e} {:.3e} {:.3e}'.format(
            np.min(field_recv_A_SST),
            np.mean(field_recv_A_SST),
            np.max(field_recv_A_SST)))
        logging.debug('A_Ice_frac -> {:.3e} {:.3e} {:.3e}'.format(
            np.min(field_recv_A_Ice_frac),
            np.mean(field_recv_A_Ice_frac),
            np.max(field_recv_A_Ice_frac)))
        if CO2_MODE:
            A_CO2_emis.get(itap_sec, field_recv_A_CO2_emis)
            logging.debug('A_CO2_emis -> {:.3e} {:.3e} {:.3e}'.format( np.min(field_recv_A_CO2_emis), np.mean(field_recv_A_CO2_emis), np.max(field_recv_A_CO2_emis)))
            if mype == 0:
                A_CO2_cconc.get(itap_sec, field_recv_A_CO2_cconc)
                logging.debug('A_CO2_cconc -> {:.3e} {:.3e} {:.3e}'.format( np.min(field_recv_A_CO2_cconc),  np.mean(field_recv_A_CO2_cconc), np.max(field_recv_A_CO2_cconc)))
                A_CO2_econc.get(itap_sec, field_recv_A_CO2_econc)
                logging.debug('A_CO2_econc -> {:.3e} {:.3e} {:.3e}'.format( np.min(field_recv_A_CO2_econc),  np.mean(field_recv_A_CO2_econc), np.max(field_recv_A_CO2_econc)))
rmarti1's avatar
rmarti1 committed
        logging.debug('--------------------------------------\n')
rmarti1's avatar
rmarti1 committed

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    #         TERMINATION
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
rmarti1's avatar
rmarti1 committed
    logging.debug('End of the program')
rmarti1's avatar
rmarti1 committed

    del comp