[5725] | 1 | # pyOASIS - A Python wrapper for OASIS |
---|
| 2 | # Authors: Philippe Gambron, Rupert Ford |
---|
| 3 | # Copyright (C) 2019 UKRI - STFC |
---|
| 4 | |
---|
| 5 | # This program is free software: you can redistribute it and/or modify |
---|
| 6 | # it under the terms of the GNU Lesser General Public License as |
---|
| 7 | # published by the Free Software Foundation, either version 3 of the |
---|
| 8 | # License, or any later version. |
---|
| 9 | |
---|
| 10 | # This program is distributed in the hope that it will be useful, |
---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | # GNU Lesser General Public License for more details. |
---|
| 14 | |
---|
| 15 | # A copy of the GNU Lesser General Public License, version 3, is supplied |
---|
| 16 | # with this program, in the file lgpl-3.0.txt. It is also available at |
---|
| 17 | # <https://www.gnu.org/licenses/lgpl-3.0.html>. |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | import numpy |
---|
| 21 | import pyoasis.mod_oasis_auxiliary_routines |
---|
| 22 | import pyoasis.mod_oasis_getput_interface |
---|
| 23 | import pyoasis.mod_oasis_method |
---|
| 24 | import pyoasis.mod_oasis_part |
---|
| 25 | import pyoasis.mod_oasis_sys |
---|
| 26 | import pyoasis.mod_oasis_var |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | class Var: |
---|
| 30 | """ |
---|
| 31 | Variable data |
---|
| 32 | |
---|
| 33 | :param string name: name of the variable data |
---|
| 34 | :type partition: partition identifier |
---|
| 35 | :param inout: flag indicating whether the data is outgoing \ |
---|
| 36 | or ingoing |
---|
| 37 | :type inout: pyoasis.OasisParameter |
---|
| 38 | :param int bundle_size: size of a bundle of fields |
---|
| 39 | :raises OasisException: if OASIS is unable to initialise \ |
---|
| 40 | the variable data |
---|
| 41 | :raises PyOasisException: if an incorrect parameter is supplied |
---|
| 42 | """ |
---|
| 43 | |
---|
| 44 | def __init__(self, name, partition, inout, bundle_size=1): |
---|
| 45 | """Constructor""" |
---|
| 46 | |
---|
| 47 | pyoasis.check_types([str, pyoasis.Partition, |
---|
| 48 | pyoasis.OasisParameters, int], |
---|
| 49 | [name, partition, inout, bundle_size]) |
---|
| 50 | if len(name) == 0: |
---|
| 51 | raise pyoasis.PyOasisException("Name empty") |
---|
| 52 | _id_partition = partition._id |
---|
| 53 | self._partition_local_size = partition.local_size |
---|
| 54 | if _id_partition < 0: |
---|
| 55 | raise pyoasis.PyOasisException("Partition identifier <0.") |
---|
| 56 | if not (inout == pyoasis.OasisParameters.OASIS_IN |
---|
| 57 | or inout == pyoasis.OasisParameters.OASIS_OUT): |
---|
| 58 | raise pyoasis.PyOasisException("inout parameter neither OASIS_IN or OASIS_OUT.") |
---|
| 59 | if bundle_size < 1: |
---|
| 60 | raise pyoasis.PyOasisException("Bundle size <1.") |
---|
| 61 | self._name = name |
---|
| 62 | self.bundle_size = bundle_size |
---|
| 63 | self.direction = inout |
---|
| 64 | id_var_nodims = bundle_size |
---|
| 65 | return_value = pyoasis.mod_oasis_var.def_var(_id_partition, |
---|
| 66 | self._name, |
---|
| 67 | id_var_nodims, |
---|
| 68 | self.direction.value) |
---|
| 69 | error = return_value[1] |
---|
| 70 | if error < 0: |
---|
| 71 | raise pyoasis.OasisException("Error in def_var", error) |
---|
| 72 | self._id = return_value[0] |
---|
| 73 | |
---|
| 74 | def _check_size(self, field): |
---|
| 75 | if self.bundle_size == 1: |
---|
| 76 | if field.ndim == 3: |
---|
| 77 | if field.shape[-1] == 1 and field.size == self._partition_local_size: |
---|
| 78 | return True |
---|
| 79 | else: |
---|
| 80 | return False |
---|
| 81 | else: |
---|
| 82 | return True |
---|
| 83 | else: |
---|
| 84 | if field.ndim >= 2: |
---|
| 85 | if field.shape[-1] == self.bundle_size: |
---|
| 86 | if numpy.prod(field.shape[:-1]) == self._partition_local_size: |
---|
| 87 | return True |
---|
| 88 | return False |
---|
| 89 | |
---|
| 90 | @property |
---|
| 91 | def name(self): |
---|
| 92 | """ |
---|
| 93 | :returns: name of variable data |
---|
| 94 | :rtype: string |
---|
| 95 | """ |
---|
| 96 | return self._name |
---|
| 97 | |
---|
| 98 | @property |
---|
| 99 | def is_active(self): |
---|
| 100 | """ |
---|
| 101 | :returns: flag indicating if the variable is activated in the namcouple |
---|
| 102 | :rtype: bool |
---|
| 103 | """ |
---|
| 104 | return self._id != -1 |
---|
| 105 | |
---|
| 106 | def put(self, time, field, write_restart=False): |
---|
| 107 | """ |
---|
| 108 | Sends data to another model. |
---|
| 109 | |
---|
| 110 | :param int time: model time (in seconds) [kstep in OASIS] |
---|
| 111 | :param pyoasis.asarray field: data |
---|
| 112 | :param bool write_restart: optional flag for writing a restart file |
---|
| 113 | |
---|
| 114 | :raises OasisException: if OASIS is unable to send \ |
---|
| 115 | data to the other component |
---|
| 116 | :raises PyOasisException: if an incorrect parameter is supplied |
---|
| 117 | """ |
---|
| 118 | pyoasis.check_types([int, numpy.ndarray, bool], [time, field, |
---|
| 119 | write_restart]) |
---|
| 120 | if(not self._check_size(field)): |
---|
| 121 | raise pyoasis.OasisException("Field of the wrong size", error) |
---|
| 122 | error = pyoasis.mod_oasis_getput_interface.put(self._id, time, |
---|
| 123 | field, write_restart) |
---|
| 124 | if error < 0: |
---|
| 125 | raise pyoasis.OasisException("Error in sending data to another component", error) |
---|
| 126 | |
---|
| 127 | try: |
---|
| 128 | return pyoasis.OasisParameters(error) |
---|
| 129 | except ValueError: |
---|
| 130 | raise pyoasis.OasisException("Error in put: returned value not mapped to Oasis parameters", -1) |
---|
| 131 | |
---|
| 132 | def get(self, time, field): |
---|
| 133 | """ |
---|
| 134 | Gets data from another model. |
---|
| 135 | |
---|
| 136 | :param int time: model time (in seconds) [kstep in OASIS] |
---|
| 137 | :param pyoasis.asarray field: data |
---|
| 138 | |
---|
| 139 | :raises OasisException: if OASIS is unable to receive \ |
---|
| 140 | data from the other component |
---|
| 141 | :raises PyOasisException: if an incorrect parameter is supplied |
---|
| 142 | """ |
---|
| 143 | pyoasis.check_types([int, numpy.ndarray], [time, field]) |
---|
| 144 | if(not self._check_size(field)): |
---|
| 145 | raise pyoasis.OasisException("Field of the wrong size", error) |
---|
| 146 | error = pyoasis.mod_oasis_getput_interface.get(self._id, time, field) |
---|
| 147 | if error < 0: |
---|
| 148 | raise pyoasis.OasisException("Error in getting data from another component", |
---|
| 149 | error) |
---|
| 150 | |
---|
| 151 | try: |
---|
| 152 | return pyoasis.OasisParameters(error) |
---|
| 153 | except ValueError: |
---|
| 154 | raise pyoasis.OasisException("Error in get: returned value not mapped to Oasis parameters", -1) |
---|
| 155 | |
---|
| 156 | def __str__(self): |
---|
| 157 | return "Variable data: name: " + self._name + ", id: " + str(self._id) |
---|
| 158 | |
---|
| 159 | def put_inquire(self, time): |
---|
| 160 | """ |
---|
| 161 | :param int time: model time (in seconds) [msec in OASIS] |
---|
| 162 | |
---|
| 163 | :returns: return value expected at a specified time |
---|
| 164 | for a given variable |
---|
| 165 | |
---|
| 166 | :rtype: pyoasis.OasisParameters |
---|
| 167 | |
---|
| 168 | :raises OasisException: if OASIS is unable to obtain |
---|
| 169 | the return code |
---|
| 170 | |
---|
| 171 | :raises PyOasisException: if an incorrect parameter is |
---|
| 172 | supplied |
---|
| 173 | """ |
---|
| 174 | pyoasis.check_types([int], [time]) |
---|
| 175 | rcode, ierr = pyoasis.mod_oasis_auxiliary_routines.put_inquire(self._id, |
---|
| 176 | time) |
---|
| 177 | |
---|
| 178 | if (pyoasis.OasisParameters(ierr) != pyoasis.OasisParameters.OASIS_OK): |
---|
| 179 | raise pyoasis.OasisException("Error in put_inquire: returned value not a valid Put inquire answer", -1) |
---|
| 180 | |
---|
| 181 | try: |
---|
| 182 | return pyoasis.OasisParameters(rcode) |
---|
| 183 | except ValueError: |
---|
| 184 | raise pyoasis.OasisException("Error in put_inquire: returned value not mapped to Oasis parameters", -1) |
---|
| 185 | |
---|
| 186 | @property |
---|
| 187 | def cpl_freqs(self): |
---|
| 188 | """ |
---|
| 189 | :returns: the coupling periods |
---|
| 190 | :rtype: array of integers |
---|
| 191 | :raises OasisException: if OASIS is unable to obtain the coupling periods |
---|
| 192 | """ |
---|
| 193 | freqs, error = pyoasis.mod_oasis_auxiliary_routines.get_freqs_array(self._id, |
---|
| 194 | self.direction.value) |
---|
| 195 | if error < 0: |
---|
| 196 | raise pyoasis.OasisException("Error in getting coupling frequencies", |
---|
| 197 | error) |
---|
| 198 | return freqs |
---|