Loading...
Searching...
No Matches
ithacaInterpolator.C
1/*---------------------------------------------------------------------------*\
2 ██╗████████╗██╗ ██╗ █████╗ ██████╗ █████╗ ███████╗██╗ ██╗
3 ██║╚══██╔══╝██║ ██║██╔══██╗██╔════╝██╔══██╗ ██╔════╝██║ ██║
4 ██║ ██║ ███████║███████║██║ ███████║█████╗█████╗ ██║ ██║
5 ██║ ██║ ██╔══██║██╔══██║██║ ██╔══██║╚════╝██╔══╝ ╚██╗ ██╔╝
6 ██║ ██║ ██║ ██║██║ ██║╚██████╗██║ ██║ ██║ ╚████╔╝
7 ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═╝ ╚═══╝
8
9 * In real Time Highly Advanced Computational Applications for Finite Volumes
10 * Copyright (C) 2017 by the ITHACA-FV authors
11-------------------------------------------------------------------------------
12
13License
14 This file is part of ITHACA-FV
15
16 ITHACA-FV is free software: you can redistribute it and/or modify
17 it under the terms of the GNU Lesser General Public License as published by
18 the Free Software Foundation, either version 3 of the License, or
19 (at your option) any later version.
20
21 ITHACA-FV is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU Lesser General Public License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with ITHACA-FV. If not, see <http://www.gnu.org/licenses/>.
28
29\*---------------------------------------------------------------------------*/
30
31#include "ithacaInterpolator.H"
32#include "mtbGPR.H"
33#include "mtbRBF.H"
34#include "splinterRBF.H"
35#include "error.H"
36
37ithacaInterpolator::ithacaInterpolator(const Foam::dictionary& dict)
38{
39 package_ = dict.lookupOrDefault<Foam::word>("package", "mathtoolbox");
40 algorithm_ = dict.lookupOrDefault<Foam::word>("algorithm", "RBF");
41
42 if (package_ == "mathtoolbox")
43 {
44 if (algorithm_ == "RBF" || algorithm_ == "rbf")
45 {
46 mtb_ = std::make_unique<mtbRBF>(dict);
47 }
48 else if (algorithm_ == "GPR" || algorithm_ == "gpr")
49 {
50 mtbGPR_ = std::make_unique<mtbGPR>(dict);
51 }
52 else
53 {
54 FatalErrorInFunction
55 << "Unknown algorithm for mathtoolbox package: " << algorithm_
56 << ". Valid options are: RBF, GPR"
57 << Foam::exit(Foam::FatalError);
58 }
59 }
60 else if (package_ == "splinter")
61 {
62 if (algorithm_ != "RBF" && algorithm_ != "rbf")
63 {
64 FatalErrorInFunction
65 << "Unknown algorithm for splinter package: " << algorithm_
66 << ". Valid option is: RBF"
67 << Foam::exit(Foam::FatalError);
68 }
69 splinter_ = std::make_unique<splinterRBF>(dict);
70 }
71 else
72 {
73 FatalErrorInFunction
74 << "Unknown package: " << package_
75 << ". Valid options are: mathtoolbox, splinter"
76 << Foam::exit(Foam::FatalError);
77 }
78}
79
80ithacaInterpolator::~ithacaInterpolator() = default;
81
82void ithacaInterpolator::fit(const Eigen::MatrixXd& X, const Eigen::VectorXd& y)
83{
84 if (package_ == "mathtoolbox")
85 {
86 if (mtb_)
87 {
88 mtb_->fit(X, y);
89 }
90 else if (mtbGPR_)
91 {
92 mtbGPR_->fit(X, y);
93 }
94 else
95 {
96 FatalErrorInFunction
97 << "mathtoolbox algorithm not initialized"
98 << Foam::exit(Foam::FatalError);
99 }
100 }
101 else if (package_ == "splinter")
102 {
103 if (algorithm_ != "RBF" && algorithm_ != "rbf")
104 {
105 FatalErrorInFunction
106 << "Unknown algorithm for splinter package: " << algorithm_
107 << ". Valid option is: RBF"
108 << Foam::exit(Foam::FatalError);
109 }
110 splinter_->fit(X, y);
111 }
112 else
113 {
114 FatalErrorInFunction
115 << "Unknown package: " << package_
116 << Foam::exit(Foam::FatalError);
117 }
118}
119
120double ithacaInterpolator::predict(const Eigen::VectorXd& x)
121{
122 if (package_ == "mathtoolbox")
123 {
124 if (mtb_)
125 {
126 return mtb_->predict(x);
127 }
128 else if (mtbGPR_)
129 {
130 return mtbGPR_->predict(x);
131 }
132 else
133 {
134 FatalErrorInFunction
135 << "mathtoolbox algorithm not initialized"
136 << Foam::exit(Foam::FatalError);
137 }
138 }
139 else if (package_ == "splinter")
140 {
141 if (algorithm_ != "RBF" && algorithm_ != "rbf")
142 {
143 FatalErrorInFunction
144 << "Unknown algorithm for splinter package: " << algorithm_
145 << ". Valid option is: RBF"
146 << Foam::exit(Foam::FatalError);
147 }
148 return splinter_->predict(x);
149 }
150 else
151 {
152 FatalErrorInFunction
153 << "Unknown package: " << package_
154 << Foam::exit(Foam::FatalError);
155 }
156 return 0.0; // unreachable, keeps compiler happy
157}
158
159Eigen::VectorXd ithacaInterpolator::predict(const Eigen::MatrixXd& X)
160{
161 if (package_ == "mathtoolbox")
162 {
163 if (mtb_)
164 {
165 return mtb_->predict(X);
166 }
167 else if (mtbGPR_)
168 {
169 return mtbGPR_->predict(X);
170 }
171 else
172 {
173 FatalErrorInFunction
174 << "mathtoolbox algorithm not initialized"
175 << Foam::exit(Foam::FatalError);
176 }
177 }
178 else if (package_ == "splinter")
179 {
180 if (algorithm_ != "RBF" && algorithm_ != "rbf")
181 {
182 FatalErrorInFunction
183 << "Unknown algorithm for splinter package: " << algorithm_
184 << ". Valid option is: RBF"
185 << Foam::exit(Foam::FatalError);
186 }
187 return splinter_->predict(X);
188 }
189 else
190 {
191 FatalErrorInFunction
192 << "Unknown package: " << package_
193 << Foam::exit(Foam::FatalError);
194 }
195 return Eigen::VectorXd(); // unreachable, keeps compiler happy
196}
197
198void ithacaInterpolator::printInfo() const
199{
200 if (package_ == "mathtoolbox")
201 {
202 if (mtb_)
203 {
204 mtb_->printInfo();
205 }
206 else if (mtbGPR_)
207 {
208 mtbGPR_->printInfo();
209 }
210 else
211 {
212 FatalErrorInFunction
213 << "mathtoolbox algorithm not initialized"
214 << Foam::exit(Foam::FatalError);
215 }
216 }
217 else if (package_ == "splinter")
218 {
219 if (algorithm_ != "RBF" && algorithm_ != "rbf")
220 {
221 FatalErrorInFunction
222 << "Unknown algorithm for splinter package: " << algorithm_
223 << ". Valid option is: RBF"
224 << Foam::exit(Foam::FatalError);
225 }
226 splinter_->printInfo();
227 }
228 else
229 {
230 FatalErrorInFunction
231 << "Unknown package: " << package_
232 << Foam::exit(Foam::FatalError);
233 }
234}