Reference Guide  2.5.0
operator_arg_metadata.py
1 # -----------------------------------------------------------------------------
2 # BSD 3-Clause License
3 #
4 # Copyright (c) 2022-2024, Science and Technology Facilities Council
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 #
10 # * Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
12 #
13 # * Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 #
17 # * Neither the name of the copyright holder nor the names of its
18 # contributors may be used to endorse or promote products derived from
19 # this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 # -----------------------------------------------------------------------------
34 # Author R. W. Ford, STFC Daresbury Lab
35 
36 '''Module containing the OperatorArgMetadata class which captures the metadata
37 associated with an operator argument. Supports the creation, modification
38 and Fortran output of a Operator argument.
39 
40 '''
41 from psyclone.domain.lfric import LFRicConstants
42 from psyclone.domain.lfric.kernel.scalar_arg_metadata import ScalarArgMetadata
43 
44 
46  '''Class to capture LFRic kernel metadata information for an operator
47  argument.
48 
49  :param str datatype: the datatype of this operator (GH_INTEGER, ...).
50  :param str access: the way the kernel accesses this operator \
51  (GH_WRITE, ...).
52  :param str function_space_to: the function space that this operator \
53  maps to (W0, ...).
54  :param str function_space_from: the function space that this \
55  operator maps from (W0, ...).
56 
57  '''
58  # The name used to specify an operator argument in LFRic metadata.
59  form = "gh_operator"
60  # The relative positions of LFRic function-space-to and
61  # function-space-from metadata. Metadata for an operator argument
62  # is provided in the following format 'arg_type(form, datatype,
63  # access, function_space_to, function_space_from)'. Therefore, for
64  # example, the index of the function_space_to argument
65  # (function_space_to_arg_index) is 3. Index values not provided
66  # here are common to the parent classes and are inherited from
67  # them.
68  function_space_to_arg_index = 3
69  function_space_from_arg_index = 4
70  # The name to use for any exceptions.
71  check_name = "operator"
72  # The number of arguments in the language-level metadata.
73  nargs = 5
74 
75  def __init__(self, datatype, access, function_space_to,
76  function_space_from):
77  super().__init__(datatype, access)
78  self.function_space_tofunction_space_tofunction_space_tofunction_space_to = function_space_to
79  self.function_space_fromfunction_space_fromfunction_space_fromfunction_space_from = function_space_from
80 
81  @classmethod
82  def _get_metadata(cls, fparser2_tree):
83  '''Extract the required metadata from the fparser2 tree and return it
84  as strings. Also check that the metadata is in the expected
85  form (but do not check the metadata values as that is done
86  separately).
87 
88  :param fparser2_tree: fparser2 tree containing the metadata \
89  for this argument.
90  :type fparser2_tree: :py:class:`fparser.two.Fortran2003.Part_Ref`
91 
92  :returns: a tuple containing the datatype, access \
93  function_space_to and function_space_from metadata.
94  :rtype: Tuple[str, str, str, str]
95 
96  '''
97  datatype, access = super()._get_metadata(fparser2_tree)
98  function_space_to = cls.get_argget_arg(
99  fparser2_tree, cls.function_space_to_arg_indexfunction_space_to_arg_index)
100  function_space_from = cls.get_argget_arg(
101  fparser2_tree, cls.function_space_from_arg_indexfunction_space_from_arg_index)
102  return (datatype, access, function_space_to, function_space_from)
103 
104  def fortran_string(self):
105  '''
106  :returns: the metadata represented by this class as Fortran.
107  :rtype: str
108  '''
109  return (f"arg_type({self.form}, {self.datatype}, {self.access}, "
110  f"{self.function_space_to}, {self.function_space_from})")
111 
112  @staticmethod
113  def check_datatype(value):
114  '''
115  :param str value: the datatype to check for validity.
116  '''
117  const = LFRicConstants()
118  OperatorArgMetadata.validate_scalar_value(
119  value, const.VALID_OPERATOR_DATA_TYPES, "datatype descriptor")
120 
121  @staticmethod
122  def check_access(value):
123  '''
124  :param str value: the access descriptor to validate.
125  '''
126  const = LFRicConstants()
127  OperatorArgMetadata.validate_scalar_value(
128  value, const.VALID_OPERATOR_ACCESS_TYPES, "access descriptor")
129 
130  @property
131  def function_space_to(self):
132  '''
133  :returns: the first function space for this operator \
134  argument (that this operator maps to).
135  :rtype: str
136  '''
137  return self._function_space_to_function_space_to
138 
139  @function_space_to.setter
140  def function_space_to(self, value):
141  '''
142  :param str value: set the function space to the \
143  specified value.
144 
145  raises ValueError: if the provided value is not a valid \
146  function space.
147 
148  '''
149  const = LFRicConstants()
150  self.validate_scalar_valuevalidate_scalar_value(
151  value, const.VALID_FUNCTION_SPACE_NAMES, "function_space_to")
152  self._function_space_to_function_space_to = value.lower()
153 
154  @property
155  def function_space_from(self):
156  '''
157  :returns: the second function space for this operator \
158  argument (that this operator maps from).
159  :rtype: str
160  '''
161  return self._function_space_from_function_space_from
162 
163  @function_space_from.setter
164  def function_space_from(self, value):
165  '''
166  :param str value: set the function space to the \
167  specified value.
168  '''
169  const = LFRicConstants()
170  self.validate_scalar_valuevalidate_scalar_value(
171  value, const.VALID_FUNCTION_SPACE_NAMES, "function_space_from")
172  self._function_space_from_function_space_from = value.lower()
173 
174 
175 __all__ = ["OperatorArgMetadata"]