Reference Guide  2.5.0
inter_grid_vector_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 InterGridVectorArgMetadata class which captures the
37 metadata associated with a intergrid vector argument. Supports the
38 creation, modification and Fortran output of an InterGridVector
39 argument.
40 
41 '''
43  InterGridArgMetadata
44 
45 
47  '''Class to capture LFRic kernel metadata information for an
48  InterGridVector argument.
49 
50  :param str datatype: the datatype of this InterGridVector argument \
51  (GH_INTEGER, ...).
52  :param str access: the way the kernel accesses this InterGridVector \
53  argument (GH_WRITE, ...).
54  :param str function_space: the function space that this \
55  InterGridVector argument is on (W0, ...).
56  :param str mesh_arg: the type of mesh that this InterGrid arg \
57  is on (coarse or fine).
58  :param str vector_length: the size of the vector.
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 vector length metadata. Metadata
64  # for an inter-grid vector argument is provided in the following
65  # format 'arg_type(form*vector_length, datatype, access,
66  # function_space, [stencil], mesh)'. Therefore, the index of the
67  # vector_length argument (vector_length_arg_index) is 0. Index
68  # values not provided here are common to the parent classes and
69  # are inherited from them.
70  vector_length_arg_index = 0
71  # The name to use for any exceptions.
72  check_name = "inter-grid-vector"
73  # Whether the class captures vector metadata.
74  vector = True
75 
76  def __init__(self, datatype, access, function_space, mesh_arg,
77  vector_length, stencil=None):
78  super().__init__(
79  datatype, access, function_space, mesh_arg, stencil=stencil)
80  self.vector_lengthvector_lengthvector_lengthvector_length = vector_length
81 
82  @classmethod
83  def _get_metadata(cls, fparser2_tree):
84  '''Extract the required metadata from the fparser2 tree and return it
85  as strings. Also check that the metadata is in the expected
86  form (but do not check the metadata values as that is done
87  separately).
88 
89  :param fparser2_tree: fparser2 tree containing the metadata \
90  for this argument.
91  :type fparser2_tree: \
92  :py:class:`fparser.two.Fortran2003.Structure_Constructor`
93 
94  :returns: a tuple containing the datatype, access, function \
95  space, mesh, vector-length and stencil metadata.
96  :rtype: Tuple[str, str, str, str, str, Optional[str]]
97 
98  '''
99  datatype, access, function_space, mesh_arg, stencil = \
100  super()._get_metadata(fparser2_tree)
101  vector_length = cls.get_vector_lengthget_vector_length(fparser2_tree)
102  return (datatype, access, function_space, mesh_arg, vector_length,
103  stencil)
104 
105  def fortran_string(self):
106  '''
107  :returns: the metadata represented by this class as Fortran.
108  :rtype: str
109  '''
110  if self.stencilstencilstencilstencil:
111  return (f"arg_type({self.form}*{self.vector_length}, "
112  f"{self.datatype}, {self.access}, {self.function_space}, "
113  f"stencil({self.stencil}), mesh_arg={self.mesh_arg})")
114  return (f"arg_type({self.form}*{self.vector_length}, "
115  f"{self.datatype}, {self.access}, {self.function_space}, "
116  f"mesh_arg={self.mesh_arg})")
117 
118  @property
119  def vector_length(self):
120  '''
121  :returns: the vector length of this intergrid vector \
122  argument.
123  :rtype: str
124  '''
125  return self._vector_length_vector_length
126 
127  @vector_length.setter
128  def vector_length(self, value):
129  '''
130  :param str value: set the intergrid vector length to the specified \
131  value.
132 
133  :raises TypeError: if the provided value is not of type str.
134  :raises ValueError: if the provided value is not a string \
135  containing an integer.
136  :raises ValueError: if the provided value is not greater than 1.
137 
138  '''
139  if not isinstance(value, str):
140  raise TypeError(f"The vector size should be a string but found "
141  f"{type(value).__name__}.")
142  try:
143  int_value = int(value)
144  except ValueError as info:
145  raise ValueError(
146  f"The vector size should be a string containing an integer, "
147  f"but found '{value}'.") from info
148 
149  if int_value <= 1:
150  raise ValueError(f"The vector size should be an integer greater "
151  f"than 1 but found {value}.")
152  self._vector_length_vector_length = value
153 
154 
155 __all__ = ["InterGridVectorArgMetadata"]