Newer
Older
Etienne Tourigny
committed
# Copyright 2022 - Barcelona Supercomputing Center
# Authors: Etienne Tourigny, Rodrigo Martín Posada
# MIT License
import logging
import numpy as np
Etienne Tourigny
committed
import math
Etienne Tourigny
committed
import pyoasis
from pyoasis import OASIS
Etienne Tourigny
committed
from DataCouplerUtils import scale_factor_from_str
Etienne Tourigny
committed
class OasisOutputVar:
Etienne Tourigny
committed
def __init__(self, conf, partition, grid_shape, fileIn_vars ):
Etienne Tourigny
committed
self._conf = conf
self._id = conf['id']
Etienne Tourigny
committed
self._reset = conf['reset']
self._scale_factor = scale_factor_from_str(conf['scale_factor']) if type(conf['scale_factor']) == str else conf['scale_factor']
Etienne Tourigny
committed
self._offset = np.float64(conf['offset'])
self._grid_shape = grid_shape
Etienne Tourigny
committed
self._oasis_var = pyoasis.Var(self._id, partition, OASIS.OUT)
Etienne Tourigny
committed
self._send_field = pyoasis.asarray(np.zeros(grid_shape, dtype=np.float64))
self._fileIn_vars = fileIn_vars
logging.debug('OasisOutputVar() conf={} reset={}'.format(str(conf),str(self._reset)))
Etienne Tourigny
committed
def add_file_input_var(self, varname, var):
self._fileIn_vars[varname] = var
def file_input_vars(self):
return self._fileIn_vars
def update(self,time):
logging.debug('OasisOutputVar::update() var {} at time {}'.format(self._id,time))
Etienne Tourigny
committed
logging.debug('OasisOutputVar() conf={} reset={}'.format(str(self._conf),str(self._reset)))
# reset if needed
if self._reset:
Etienne Tourigny
committed
logging.debug('reset field for var {} at time {}'.format(self._id,time))
Etienne Tourigny
committed
# accumulate values from each fileIn_var linked to this oasisOut_var
Etienne Tourigny
committed
for (varname,fileIn_var) in self._fileIn_vars.items():
Etienne Tourigny
committed
if time == 0 or fileIn_var.accum:
self._send_field += fileIn_var.send_field()
logging.debug('accumulating {} from fileIn var {} for oasisOut var {} total= {}'.format(fileIn_var.send_field(), varname, self._id, self._send_field))
Etienne Tourigny
committed
def put(self, time):
# send the data
Etienne Tourigny
committed
logging.debug('OasisOutputVar::put() var {} at time {} field {}'.format(self._id,time,self._send_field))
Etienne Tourigny
committed
if math.isclose(self._scale_factor,1) and math.isclose(self._offset,0):
self._oasis_var.put(time, self._send_field)
else:
logging.debug('OasisOutputVar::put() var {} with offset {} and scale_factor {}'.format(self._id,self._offset,self._scale_factor))
self._oasis_var.put(time, self._send_field*self._scale_factor+self._offset)