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