OPAL (Object Oriented Parallel Accelerator Library)  2024.1
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(nullptr), acc_m(nullptr) {
40  setSpline(splineOrder, times, values);
41 }
42 
44  : spline_m(nullptr), acc_m(nullptr) {
46 }
47 
48 SplineTimeDependence::SplineTimeDependence() : spline_m(nullptr), acc_m(nullptr) {
49 }
50 
52  if (spline_m != nullptr) {
53  gsl_spline_free(spline_m);
54  }
55  if (acc_m != nullptr) {
56  gsl_interp_accel_free(acc_m);
57  }
58 }
59 
63  return timeDep;
64 }
65 
67  if (spline_m == nullptr) {
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 != nullptr) {
102  gsl_spline_free(spline_m);
103  spline_m = nullptr;
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 == nullptr) {
114  acc_m = gsl_interp_accel_alloc();
115  } else {
116  gsl_interp_accel_reset(acc_m);
117  }
118 }
119 
void setSpline(size_t splineOrder, std::vector< double > times, std::vector< double > values)
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
std::vector< double > values_m
std::vector< double > times_m
Inform & print(Inform &os)
gsl_interp_accel * acc_m
Definition: Inform.h:42
SplineTimeDependence * clone()