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