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 
10 #include <string>
11 #include <vector>
12 
13 /// Numeric code that could be reused by teca_derived_quantity
15 {
16 /** an execute function designed for use with teca_derived_quantity
17  * on a teca_mesh. shallow copies the input and computes the
18  * point-wise average of the named variables.
19  *
20  * for every i
21  * avg[i] = (v0[i] + v1[i])/2
22  */
24 {
25  // construct the class with two input array names, v0,v1
26  // and the output array name, avg.
27  point_wise_average(const std::string &v0,
28  const std::string &v1, const std::string &avg)
29  : m_v0(v0), m_v1(v1), m_avg(avg) {}
30 
31  point_wise_average() = delete;
32  ~point_wise_average() = default;
33 
34  // implements teca_algorithm::execute
35  const_p_teca_dataset operator()(unsigned int,
36  const std::vector<const_p_teca_dataset> &in_data, const teca_metadata &)
37  {
38  const_p_teca_mesh in_mesh;
40 
41  if (!(in_mesh = std::dynamic_pointer_cast<const teca_mesh>(in_data[0])) ||
42  !(v0 = in_mesh->get_point_arrays()->get(m_v0)) ||
43  !(v1 = in_mesh->get_point_arrays()->get(m_v1)))
44  {
45  TECA_ERROR("invalid inputs. mesh=" << in_mesh
46  << ", " << m_v0 << "=" << v0 << ", " << m_v1 << "=" << v1)
47  return nullptr;
48  }
49 
50  unsigned long n_pts = v0->size();
51  p_teca_variant_array avg = v0->new_instance();
52  avg->resize(n_pts);
53 
55  avg.get(),
56  auto sp_v0 = static_cast<const TT*>(v0.get())->get_cpu_accessible();
57  const NT *p_v0 = sp_v0.get();
58 
59  auto sp_v1 = dynamic_cast<const TT*>(v1.get())->get_cpu_accessible();
60  const NT *p_v1 = sp_v1.get();
61 
62  auto sp_avg = static_cast<TT*>(avg.get())->get_cpu_accessible();
63  NT *p_avg = sp_avg.get();
64 
65  for (unsigned long i = 0; i < n_pts; ++i)
66  p_avg[i] = (p_v0[i] + p_v1[i])/NT(2);
67  )
68 
69  p_teca_mesh out_mesh = std::static_pointer_cast<teca_mesh>(
70  in_mesh->new_instance());
71 
72  out_mesh->shallow_copy(std::const_pointer_cast<teca_mesh>(in_mesh));
73  out_mesh->get_point_arrays()->append(m_avg, avg);
74 
75  return out_mesh;
76  }
77 
78  std::string m_v0; // input variable name 1
79  std::string m_v1; // input variable name 2
80  std::string m_avg; // output variable name
81 };
82 
83 /** an execute function designed for use with teca_derived_quantity
84  * on a teca_mesh. compute the point-wise difference of two variables
85  *
86  * for every i
87  * diff[i] = v1[i] - v0[i]
88  */
90 {
91  // construct the class with two input array names, v0,v1
92  // and the output array name, diff.
93  point_wise_difference(const std::string &v0,
94  const std::string &v1, const std::string &diff)
95  : m_v0(v0), m_v1(v1), m_diff(diff) {}
96 
97  point_wise_difference() = delete;
98  ~point_wise_difference() = default;
99 
100  // implements teca_algorithm::execute, shallow copies the input and
101  // computes the difference of two variables.
102  const_p_teca_dataset operator()(unsigned int,
103  const std::vector<const_p_teca_dataset> &in_data, const teca_metadata &)
104  {
105  const_p_teca_mesh in_mesh;
107 
108  if (!(in_mesh = std::dynamic_pointer_cast<const teca_mesh>(in_data[0])) ||
109  !(v0 = in_mesh->get_point_arrays()->get(m_v0)) ||
110  !(v1 = in_mesh->get_point_arrays()->get(m_v1)))
111  {
112  TECA_ERROR("invalid inputs. mesh=" << in_mesh
113  << ", " << m_v0 << "=" << v0 << ", " << m_v1 << "=" << v1)
114  return nullptr;
115  }
116 
117  unsigned long n_pts = v0->size();
118  p_teca_variant_array diff = v0->new_instance();
119  diff->resize(n_pts);
120 
122  diff.get(),
123  auto sp_v0 = static_cast<const TT*>(v0.get())->get_cpu_accessible();
124  const NT *p_v0 = sp_v0.get();
125 
126  auto sp_v1 = dynamic_cast<const TT*>(v1.get())->get_cpu_accessible();
127  const NT *p_v1 = sp_v1.get();
128 
129  auto sp_diff = static_cast<TT*>(diff.get())->get_cpu_accessible();
130  NT *p_diff = sp_diff.get();
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
teca_variant_array.h
teca_variant_array_impl::get
void get(size_t i, U &val) const
Definition: teca_variant_array_impl.h:444
teca_metadata
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:21
teca_variant_array_impl
The concrete implementation of our type agnostic container for contiguous arrays.
Definition: teca_variant_array_impl.h:39
teca_derived_quantity_numerics::point_wise_difference
Definition: teca_derived_quantity_numerics.h:89
const_p_teca_variant_array
std::shared_ptr< const teca_variant_array > const_p_teca_variant_array
Definition: teca_variant_array.h:27
teca_derived_quantity_numerics::point_wise_average
Definition: teca_derived_quantity_numerics.h:23
p_teca_variant_array
std::shared_ptr< teca_variant_array > p_teca_variant_array
Definition: teca_variant_array.h:27
teca_error::TECA_EXPORT
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.
TECA_ERROR
#define TECA_ERROR(_msg)
Constructs an error message and sends it to the stderr stream.
Definition: teca_common.h:146
teca_derived_quantity_numerics
Numeric code that could be reused by teca_derived_quantity.
Definition: teca_derived_quantity_numerics.h:14
teca_variant_array_impl.h
TEMPLATE_DISPATCH
#define TEMPLATE_DISPATCH(t, p, body)
Definition: teca_variant_array_impl.h:209