[6328] | 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 | """OASIS send/receive (put/get) user interfaces""" |
---|
| 21 | |
---|
| 22 | import pyoasis |
---|
| 23 | import ctypes |
---|
| 24 | import numpy as np |
---|
| 25 | from numpy import float32, float64 |
---|
| 26 | from ctypes import c_int, cdll, CDLL |
---|
| 27 | |
---|
| 28 | cdll.LoadLibrary("liboasis.cbind.so") |
---|
| 29 | LIB = CDLL("liboasis.cbind.so") |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | def get_sizes(field): |
---|
| 33 | """Creates an array containing the dimensions of multidimensional fields""" |
---|
| 34 | sizes = np.ones(3, np.int32) |
---|
| 35 | sizes[:field.ndim] = field.shape |
---|
| 36 | return sizes |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | LIB.oasis_c_put.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int, |
---|
| 40 | ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_void_p, |
---|
| 41 | ctypes.c_bool, ctypes.POINTER(ctypes.c_int)] |
---|
| 42 | LIB.oasis_c_put.restype = ctypes.c_int |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | def put(var_id, kstep, field, write_restart): |
---|
| 46 | """Send 8-byte multidimensional field""" |
---|
| 47 | sizes = get_sizes(field) |
---|
| 48 | kinfo = c_int(0) |
---|
| 49 | ierr = c_int(0) |
---|
| 50 | p_field = field.ctypes.data |
---|
| 51 | storage = c_int(1) # colum-major storage cf. OASIS_COL_MAJOR in oasis_c.h |
---|
| 52 | if field.dtype == float32: |
---|
| 53 | fkind = c_int(4) |
---|
| 54 | elif field.dtype == float64: |
---|
| 55 | fkind = c_int(8) |
---|
| 56 | else: |
---|
| 57 | raise pyoasis.PyOasisException("Data type of field can only by float32 or float64") |
---|
| 58 | ierr = LIB.oasis_c_put(var_id, kstep, sizes[0], sizes[1], sizes[2], fkind, storage, |
---|
| 59 | p_field, write_restart, kinfo) |
---|
| 60 | if (pyoasis.OasisParameters(ierr) != pyoasis.OasisParameters.OASIS_OK): |
---|
| 61 | return ierr |
---|
| 62 | else: |
---|
| 63 | return kinfo.value |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | LIB.oasis_c_get.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, |
---|
| 67 | ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_void_p, |
---|
| 68 | ctypes.POINTER(ctypes.c_int)] |
---|
| 69 | LIB.oasis_c_get.restype = ctypes.c_int |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | def get(var_id, kstep, field): |
---|
| 73 | """Receive 8-byte multidimensional field""" |
---|
| 74 | sizes = get_sizes(field) |
---|
| 75 | kinfo = c_int(0) |
---|
| 76 | ierr = c_int(0) |
---|
| 77 | p_field = field.ctypes.data |
---|
| 78 | storage = c_int(1) # colum-major storage cf. OASIS_COL_MAJOR in oasis_c.h |
---|
| 79 | if field.dtype == float32: |
---|
| 80 | fkind = c_int(4) |
---|
| 81 | elif field.dtype == float64: |
---|
| 82 | fkind = c_int(8) |
---|
| 83 | else: |
---|
| 84 | raise pyoasis.PyOasisException("Data type of field can only by float32 or float64") |
---|
| 85 | ierr = LIB.oasis_c_get(var_id, kstep, sizes[0], sizes[1], sizes[2], fkind, storage, p_field, kinfo) |
---|
| 86 | if (pyoasis.OasisParameters(ierr) != pyoasis.OasisParameters.OASIS_OK): |
---|
| 87 | return ierr |
---|
| 88 | else: |
---|
| 89 | return kinfo.value |
---|