Reference Guide  2.5.0
inter_grid_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 InterGridArgMetadata class which captures the metadata
37 associated with an intergrid argument. Supports the creation, modification
38 and Fortran output of an intergrid argument.
39 
40 '''
41 from fparser.two import Fortran2003
42 
43 from psyclone.domain.lfric import LFRicConstants
44 from psyclone.domain.lfric.kernel.field_arg_metadata import FieldArgMetadata
45 
46 
48  '''Class to capture LFRic kernel metadata information for an intergrid
49  argument.
50 
51  :param str datatype: the datatype of this InterGrid argument \
52  (GH_INTEGER, ...).
53  :param str access: the way the kernel accesses this intergrid \
54  argument (GH_WRITE, ...).
55  :param str function_space: the function space that this \
56  InterGrid is on (W0, ...).
57  :param str mesh_arg: the type of mesh that this InterGrid arg \
58  is on (coarse or fine).
59  :param Optional[str] stencil: the type of stencil used by the \
60  kernel when accessing this InterGrid arg.
61 
62  '''
63  # The relative position of LFRic mesh metadata. Metadata for an
64  # inter-grid argument is provided in the following format
65  # 'arg_type(form, datatype, access, function_space, [stencil],
66  # mesh)'. The stencil argument is optional and its index
67  # (stencil_arg_index) is therefore 4 if it exists and the index of
68  # the mesh argument is 4 or 5 depending on whether there is a
69  # stencil argument. As the mesh argument index value is not known
70  # beforehand, it is not set. Fixed index values not provided here
71  # are common to the parent classes and are inherited from them.
72  stencil_arg_index = 4
73  # The name to use for any exceptions.
74  check_name = "inter-grid"
75  # The number of arguments in the language-level metadata (min and
76  # max values).
77  nargs = (5, 6)
78 
79  # The fparser2 class that captures this metadata.
80  fparser2_class = Fortran2003.Structure_Constructor
81 
82  def __init__(self, datatype, access, function_space, mesh_arg,
83  stencil=None):
84  super().__init__(datatype, access, function_space, stencil=stencil)
85  self.mesh_argmesh_argmesh_argmesh_arg = mesh_arg
86 
87  @classmethod
88  def _get_metadata(cls, fparser2_tree):
89  '''Extract the required metadata from the fparser2 tree and return it
90  as strings. Also check that the metadata is in the expected
91  form (but do not check the metadata values as that is done
92  separately).
93 
94  :param fparser2_tree: fparser2 tree containing the metadata \
95  for this argument.
96  :type fparser2_tree: \
97  :py:class:`fparser.two.Fortran2003.Structure_Constructor`
98 
99  :returns: a tuple containing the datatype, access, function \
100  space, mesh and stencil metadata.
101  :rtype: Tuple[str, str, str, str, Optional[str]]
102 
103  '''
104  datatype, access = cls._get_datatype_access_metadata_get_datatype_access_metadata(fparser2_tree)
105  function_space = cls.get_argget_arg(
106  fparser2_tree, cls.function_space_arg_indexfunction_space_arg_indexfunction_space_arg_index)
107  try:
108  stencil = cls.get_stencilget_stencil(fparser2_tree)
109  mesh_arg = cls.get_mesh_argget_mesh_arg(fparser2_tree, 5)
110  except TypeError:
111  stencil = None
112  mesh_arg = cls.get_mesh_argget_mesh_arg(fparser2_tree, 4)
113 
114  return (datatype, access, function_space, mesh_arg, stencil)
115 
116  @staticmethod
117  def get_mesh_arg(fparser2_tree, mesh_arg_index):
118  '''Retrieves the mesh_arg metadata value from the supplied fparser2
119  tree.
120 
121  :param fparser2_tree: fparser2 tree capturing the metadata for \
122  an InterGrid argument.
123  :type fparser2_tree: \
124  :py:class:`fparser.two.Fortran2003.Structure_Constructor`
125  :param int mesh_arg_index: the index at which to find the metadata.
126 
127  :returns: the metadata mesh value extracted from the fparser2 tree.
128  :rtype: str
129 
130  raises ValueError: if the metadata is not in the form \
131  'mesh_arg = <value>'.
132 
133  '''
134  try:
135  mesh_arg_lhs = fparser2_tree.children[1].\
136  children[mesh_arg_index].children[0].tostr()
137  except IndexError as info:
138  raise ValueError(
139  f"At argument index {mesh_arg_index} for metadata "
140  f"'{fparser2_tree}' expected the metadata to be in the form "
141  f"'mesh_arg=value' but found "
142  f"'{fparser2_tree.children[1].children[mesh_arg_index]}'.") \
143  from info
144 
145  if not mesh_arg_lhs.lower() == "mesh_arg":
146  raise ValueError(
147  f"At argument index {mesh_arg_index} for metadata "
148  f"'{fparser2_tree}' expected the left hand side "
149  f"to be 'mesh_arg' but found '{mesh_arg_lhs}'.")
150  mesh_arg = fparser2_tree.children[1].\
151  children[mesh_arg_index].children[1].tostr()
152  return mesh_arg
153 
154  def fortran_string(self):
155  '''
156  :returns: the metadata represented by this class as Fortran.
157  :rtype: str
158  '''
159  if self.stencilstencilstencilstencil:
160  return (f"arg_type({self.form}, {self.datatype}, {self.access}, "
161  f"{self.function_space}, stencil({self.stencil}), "
162  f"mesh_arg={self.mesh_arg})")
163  return (f"arg_type({self.form}, {self.datatype}, {self.access}, "
164  f"{self.function_space}, mesh_arg={self.mesh_arg})")
165 
166  @property
167  def mesh_arg(self):
168  '''
169  :returns: the mesh type for this intergrid argument.
170  :rtype: str
171  '''
172  return self._mesh_arg_mesh_arg
173 
174  @mesh_arg.setter
175  def mesh_arg(self, value):
176  '''
177  :param str value: set the mesh type to the \
178  specified value.
179  '''
180  const = LFRicConstants()
181  InterGridArgMetadata.validate_scalar_value(
182  value, const.VALID_MESH_TYPES, "mesh_arg")
183  self._mesh_arg_mesh_arg = value.lower()
184 
185 
186 __all__ = ["InterGridArgMetadata"]