TECA
The Toolkit for Extreme Climate Analysis
teca_derived_quantity_numerics.h
Go to the documentation of this file.
1 #ifndef teca_numerics_h
2 #define teca_numerics_h
3 
4 /// @file
5 
6 #include "teca_mesh.h"
7 #include "teca_variant_array.h"
9 #if !defined(SWIG)
11 using namespace teca_variant_array_util;
12 #endif
13 
14 #include <string>
15 #include <vector>
16 
17 /// Numeric code that could be reused by teca_derived_quantity
19 {
20 /** an execute function designed for use with teca_derived_quantity
21  * on a teca_mesh. shallow copies the input and computes the
22  * point-wise average of the named variables.
23  *
24  * for every i
25  * avg[i] = (v0[i] + v1[i])/2
26  */
28 {
29  // construct the class with two input array names, v0,v1
30  // and the output array name, avg.
31  point_wise_average(const std::string &v0,
32  const std::string &v1, const std::string &avg)
33  : m_v0(v0), m_v1(v1), m_avg(avg) {}
34 
35  point_wise_average() = delete;
36  ~point_wise_average() = default;
37 
38  // implements teca_algorithm::execute
39  const_p_teca_dataset operator()(unsigned int,
40  const std::vector<const_p_teca_dataset> &in_data, const teca_metadata &)
41  {
42  const_p_teca_mesh in_mesh;
44 
45  if (!(in_mesh = std::dynamic_pointer_cast<const teca_mesh>(in_data[0])) ||
46  !(v0 = in_mesh->get_point_arrays()->get(m_v0)) ||
47  !(v1 = in_mesh->get_point_arrays()->get(m_v1)))
48  {
49  TECA_ERROR("invalid inputs. mesh=" << in_mesh
50  << ", " << m_v0 << "=" << v0 << ", " << m_v1 << "=" << v1)
51  return nullptr;
52  }
53 
54  unsigned long n_pts = v0->size();
55  p_teca_variant_array avg = v0->new_instance();
56  avg->resize(n_pts);
57 
58  VARIANT_ARRAY_DISPATCH(v0.get(),
59 
60  assert_type<CTT>(v1);
61 
62  auto [sp_v0, p_v0, sp_v1, p_v1] = get_host_accessible<CTT>(v0, v1);
63  auto [p_avg] = data<TT>(avg);
64 
65  sync_host_access_any(v0, v1);
66 
67  for (unsigned long i = 0; i < n_pts; ++i)
68  p_avg[i] = (p_v0[i] + p_v1[i])/NT(2);
69  )
70 
71  p_teca_mesh out_mesh = std::static_pointer_cast<teca_mesh>(
72  in_mesh->new_instance());
73 
74  out_mesh->shallow_copy(std::const_pointer_cast<teca_mesh>(in_mesh));
75  out_mesh->get_point_arrays()->append(m_avg, avg);
76 
77  return out_mesh;
78  }
79 
80  std::string m_v0; // input variable name 1
81  std::string m_v1; // input variable name 2
82  std::string m_avg; // output variable name
83 };
84 
85 /** an execute function designed for use with teca_derived_quantity
86  * on a teca_mesh. compute the point-wise difference of two variables
87  *
88  * for every i
89  * diff[i] = v1[i] - v0[i]
90  */
92 {
93  // construct the class with two input array names, v0,v1
94  // and the output array name, diff.
95  point_wise_difference(const std::string &v0,
96  const std::string &v1, const std::string &diff)
97  : m_v0(v0), m_v1(v1), m_diff(diff) {}
98 
99  point_wise_difference() = delete;
100  ~point_wise_difference() = default;
101 
102  // implements teca_algorithm::execute, shallow copies the input and
103  // computes the difference of two variables.
104  const_p_teca_dataset operator()(unsigned int,
105  const std::vector<const_p_teca_dataset> &in_data, const teca_metadata &)
106  {
107  const_p_teca_mesh in_mesh;
109 
110  if (!(in_mesh = std::dynamic_pointer_cast<const teca_mesh>(in_data[0])) ||
111  !(v0 = in_mesh->get_point_arrays()->get(m_v0)) ||
112  !(v1 = in_mesh->get_point_arrays()->get(m_v1)))
113  {
114  TECA_ERROR("invalid inputs. mesh=" << in_mesh
115  << ", " << m_v0 << "=" << v0 << ", " << m_v1 << "=" << v1)
116  return nullptr;
117  }
118 
119  unsigned long n_pts = v0->size();
120  p_teca_variant_array diff = v0->new_instance();
121  diff->resize(n_pts);
122 
123  VARIANT_ARRAY_DISPATCH(v1.get(),
124 
125  assert_type<CTT>(v1);
126 
127  auto [sp_v0, p_v0, sp_v1, p_v1] = get_host_accessible<CTT>(v0, v1);
128  auto [p_diff] = data<TT>(diff);
129 
130  sync_host_access_any(v0, v1);
131 
132  for (unsigned long i = 0; i < n_pts; ++i)
133  p_diff[i] = p_v1[i] - p_v0[i];
134  )
135 
136  p_teca_mesh out_mesh = std::static_pointer_cast<teca_mesh>(
137  in_mesh->new_instance());
138 
139  out_mesh->shallow_copy(std::const_pointer_cast<teca_mesh>(in_mesh));
140  out_mesh->get_point_arrays()->append(m_diff, diff);
141 
142  return out_mesh;
143  }
144 
145  std::string m_v0; // input variable name 1
146  std::string m_v1; // input variable name 2
147  std::string m_diff; // output variable name
148 };
149 }
150 #endif
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:22
Numeric code that could be reused by teca_derived_quantity.
Definition: teca_derived_quantity_numerics.h:19
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.
some functions helping us manipulate teca_variant_array
Definition: teca_variant_array_util.h:14
void sync_host_access_any(const array_t &... arrays)
Definition: teca_variant_array_util.h:29
Definition: teca_derived_quantity_numerics.h:28
Definition: teca_derived_quantity_numerics.h:92
#define TECA_ERROR(_msg)
Constructs an error message and sends it to the stderr stream.
Definition: teca_common.h:161
std::shared_ptr< teca_variant_array > p_teca_variant_array
Definition: teca_variant_array.h:27
std::shared_ptr< const teca_variant_array > const_p_teca_variant_array
Definition: teca_variant_array.h:27
#define VARIANT_ARRAY_DISPATCH(p,...)
Definition: teca_variant_array_impl.h:329