OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
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 
34 #include "Utility/Inform.h"
35 
37  std::vector<double> times,
38  std::vector<double> values)
39  : spline_m(NULL), acc_m(NULL) {
40  setSpline(splineOrder, times, values);
41 }
42 
44  : spline_m(NULL), acc_m(NULL) {
46 }
47 
48 SplineTimeDependence::SplineTimeDependence() : spline_m(NULL), acc_m(NULL) {
49 }
50 
52  if (spline_m != NULL) {
53  gsl_spline_free(spline_m);
54  }
55  if (acc_m != NULL) {
56  gsl_interp_accel_free(acc_m);
57  }
58 }
59 
63  return timeDep;
64 }
65 
67  if (spline_m == NULL) {
68  os << "Uninitiaised SplineTimeDependence" << endl;
69  return os;
70  }
71  os << "SplineTimeDependence of order " << splineOrder_m
72  << " with " << times_m.size() << " entries" << endl;
73  return os;
74 }
75 
76 void SplineTimeDependence::setSpline(size_t splineOrder,
77  std::vector<double> times,
78  std::vector<double> values) {
79  if (times.size() != values.size()) {
81  "SplineTimeDependence::SplineTimeDependence",
82  "Times and values should be of equal length");
83  }
84  if (times.size() < splineOrder+1) {
86  "SplineTimeDependence::SplineTimeDependence",
87  "Times and values should be of length > splineOrder+1");
88  }
89  if (splineOrder != 1 and splineOrder != 3) {
91  "SplineTimeDependence::SplineTimeDependence",
92  "Only linear or cubic interpolation is supported");
93  }
94  for (int i = 0; i < int(times.size())-1; ++i) {
95  if (times[i] >= times[i+1]) {
97  "SplineTimeDependence::SplineTimeDependence",
98  "Times should increase monotonically");
99  }
100  }
101  if (spline_m != NULL) {
102  gsl_spline_free(spline_m);
103  spline_m = NULL;
104  }
105  if (splineOrder == 1) {
106  spline_m = gsl_spline_alloc (gsl_interp_linear, times.size());
107  } else if (splineOrder == 3) {
108  spline_m = gsl_spline_alloc (gsl_interp_cspline, times.size());
109  }
110  times_m = times;
111  values_m = values;
112  gsl_spline_init(spline_m, &times[0], &values[0], times.size());
113  if (acc_m == NULL) {
114  acc_m = gsl_interp_accel_alloc();
115  } else {
116  gsl_interp_accel_reset(acc_m);
117  }
118 }
119 
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
SplineTimeDependence * clone()
gsl_interp_accel * acc_m
std::vector< double > values_m
void setSpline(size_t splineOrder, std::vector< double > times, std::vector< double > values)
Inform & print(Inform &os)
std::vector< double > times_m
Definition: Inform.h:42