OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
SplineTimeDependence.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, Chris Rogers
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * 1. Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * 2. Redistributions in binary form must reproduce the above copyright notice,
9  * this list of conditions and the following disclaimer in the documentation
10  * and/or other materials provided with the distribution.
11  * 3. Neither the name of STFC nor the names of its contributors may be used to
12  * endorse or promote products derived from this software without specific
13  * prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <gsl/gsl_errno.h>
29 #include <gsl/gsl_spline.h>
30 
33 
35  std::vector<double> times,
36  std::vector<double> values)
37  : spline_m(NULL), acc_m(NULL) {
38  setSpline(splineOrder, times, values);
39 }
40 
42  : spline_m(NULL), acc_m(NULL) {
44 }
45 
46 SplineTimeDependence::SplineTimeDependence() : spline_m(NULL), acc_m(NULL) {
47 }
48 
50  if (spline_m != NULL) {
51  gsl_spline_free(spline_m);
52  }
53  if (acc_m != NULL) {
54  gsl_interp_accel_free(acc_m);
55  }
56 }
57 
61  return timeDep;
62 }
63 
65  if (spline_m == NULL) {
66  os << "Uninitiaised SplineTimeDependence" << endl;
67  return os;
68  }
69  os << "SplineTimeDependence of order " << splineOrder_m
70  << " with " << times_m.size() << " entries" << endl;
71  return os;
72 }
73 
74 void SplineTimeDependence::setSpline(size_t splineOrder,
75  std::vector<double> times,
76  std::vector<double> values) {
77  if (times.size() != values.size()) {
79  "SplineTimeDependence::SplineTimeDependence",
80  "Times and values should be of equal length");
81  }
82  if (times.size() < splineOrder+1) {
84  "SplineTimeDependence::SplineTimeDependence",
85  "Times and values should be of length > splineOrder+1");
86  }
87  if (splineOrder != 1 and splineOrder != 3) {
89  "SplineTimeDependence::SplineTimeDependence",
90  "Only linear or cubic interpolation is supported");
91  }
92  for (int i = 0; i < int(times.size())-1; ++i) {
93  if (times[i] >= times[i+1]) {
95  "SplineTimeDependence::SplineTimeDependence",
96  "Times should increase monotonically");
97  }
98  }
99  if (spline_m != NULL) {
100  gsl_spline_free(spline_m);
101  spline_m = NULL;
102  }
103  if (splineOrder == 1) {
104  spline_m = gsl_spline_alloc (gsl_interp_linear, times.size());
105  } else if (splineOrder == 3) {
106  spline_m = gsl_spline_alloc (gsl_interp_cspline, times.size());
107  }
108  times_m = times;
109  values_m = values;
110  gsl_spline_init(spline_m, &times[0], &values[0], times.size());
111  if (acc_m == NULL) {
112  acc_m = gsl_interp_accel_alloc();
113  } else {
114  gsl_interp_accel_reset(acc_m);
115  }
116 }
117 
gsl_interp_accel * acc_m
std::vector< double > times_m
Inform & print(Inform &os)
void setSpline(size_t splineOrder, std::vector< double > times, std::vector< double > values)
SplineTimeDependence * clone()
std::vector< double > values_m
Definition: Inform.h:41
Inform & endl(Inform &inf)
Definition: Inform.cpp:42