IFS_mock.py 4.23 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 pyoasis
from pyoasis import OASIS  # pylint: disable=no-name-in-module
import numpy as np

rmarti1's avatar
rmarti1 committed
# Constants
NX_ATMO = 88838  # number of LAND grid cells at T255 resolution
COUPLING_INTERVAL = 86400
RUN_LENGTH_SEC = 31536000
NUMBER_TIME_STEPS = int(RUN_LENGTH_SEC / COUPLING_INTERVAL)


rmarti1's avatar
rmarti1 committed
def def_local_partition(nlon, nlat, npes, mype):
    il_extentx = nlon
    il_extenty = int(nlat/npes)
    if (mype == npes-1):
rmarti1's avatar
rmarti1 committed
        il_extenty = nlat - int(nlat/npes) * mype
rmarti1's avatar
rmarti1 committed
    il_size = il_extentx * il_extenty
    il_offsetx = 0
rmarti1's avatar
rmarti1 committed
    il_offsety = int(nlat/npes) * mype
rmarti1's avatar
rmarti1 committed
    il_offset = nlon * il_offsety
    return il_extentx, il_extenty, il_size, il_offsetx, il_offsety, il_offset

rmarti1's avatar
rmarti1 committed
if __name__ == '__main__':
rmarti1's avatar
rmarti1 committed
    nlon = int(NX_ATMO / 86)
    nlat = int(NX_ATMO / nlon)
rmarti1's avatar
rmarti1 committed
    comp = pyoasis.Component("ifs-mock")
    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
    w_unit = open(out_ifs, 'w')
    print('-----------------------------------------------------------', file=w_unit)
    print(f'I am IFS-mock process with rank : {mype}', file=w_unit)
    print(f'in my local communicator gathering {npes} processes', file=w_unit)
    print('----------------------------------------------------------', file=w_unit)
    w_unit.flush()

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

    # Definition of the local partition
rmarti1's avatar
rmarti1 committed
    il_extentx, il_extenty, il_size, il_offsetx, il_offsety, il_offset = def_local_partition(nlon, nlat, npes, mype)
rmarti1's avatar
rmarti1 committed
    print('Local partition definition', file=w_unit)
rmarti1's avatar
rmarti1 committed
    print('il_extentx, il_extenty, il_size, il_offsetx, il_offsety, il_offset = ',
          il_extentx, il_extenty, il_size, il_offsetx, il_offsety, il_offset, file=w_unit)
rmarti1's avatar
rmarti1 committed
    w_unit.flush()
    partition = pyoasis.ApplePartition(il_offset, il_size)

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    #  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)
    print(f'var_id A_SST, var_id A_Ice_frac {A_SST._id} {A_Ice_frac._id}', file=w_unit)
    w_unit.flush()

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    #         TERMINATION OF DEFINITION PHASE
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    print('End of initialisation phase', file=w_unit)
    w_unit.flush()

    comp.enddef()

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    #  TIME STEP LOOP
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
rmarti1's avatar
rmarti1 committed
    print('Timestep, field min, mean and max value', file=w_unit)
rmarti1's avatar
rmarti1 committed
    w_unit.flush()

rmarti1's avatar
rmarti1 committed
    for ib in range(NUMBER_TIME_STEPS):
        itap_sec = COUPLING_INTERVAL * ib  # time in seconds
rmarti1's avatar
rmarti1 committed

        field_recv_A_SST = pyoasis.asarray(np.full((il_extenty, il_extentx), -1.0))
        field_recv_A_Ice_frac = pyoasis.asarray(np.full((il_extenty, il_extentx), -1.0))

        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
        print(f'ITAP_SEC: {itap_sec}', file=w_unit)
rmarti1's avatar
rmarti1 committed
        print('A_SST ->', itap_sec,
              '%.5f' % np.min(field_recv_A_SST),
              '%.5f' % np.mean(field_recv_A_SST),
              '%.5f' % np.max(field_recv_A_SST),
              file=w_unit)
        print('A_Ice_frac ->', itap_sec,
              '%.5f' % np.min(field_recv_A_Ice_frac),
              '%.5f' % np.mean(field_recv_A_Ice_frac),
              '%.5f' % np.max(field_recv_A_Ice_frac),
              file=w_unit)
rmarti1's avatar
rmarti1 committed
        print(f'--------------------------------------\n', file=w_unit)
rmarti1's avatar
rmarti1 committed
        w_unit.flush()

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    #         TERMINATION
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    print('End of the program', file=w_unit)
    w_unit.flush()

    del comp