PolyFEM
Loading...
Searching...
No Matches
MiscProblem.cpp
Go to the documentation of this file.
1#include "MiscProblem.hpp"
2
3#include <iostream>
4
5namespace polyfem
6{
7 namespace problem
8 {
9 namespace
10 {
11 template <typename T>
12 T linear_fun(T x, T y)
13 {
14 return x;
15 }
16
17 template <typename T>
18 T quadratic_fun(T x, T y)
19 {
20 T v = x;
21 return v * v;
22 }
23
24 template <typename T>
25 T cubic_fun(T x, T y)
26 {
27 T v = (2 * y - 0.9);
28 return v * v * v * v + 0.1;
29 }
30
31 template <typename T>
32 T sine_fun(T x, T y)
33 {
34 return sin(x * 10) * sin(y * 10);
35 }
36
37 template <typename T>
38 T sine_fun(T x, T y, T z)
39 {
40 return sin(x * 10) * sin(y * 10) * sin(z * 10);
41 }
42
43 template <typename T>
44 T zero_bc(T x, T y)
45 {
46 return (1 - x) * x * x * y * (1 - y) * (1 - y);
47 }
48
49 template <typename T>
50 T zero_bc(T x, T y, T z)
51 {
52 return (1 - x) * x * x * y * (1 - y) * (1 - y) * z * (1 - z);
53 }
54 } // namespace
55
56 LinearProblem::LinearProblem(const std::string &name)
58 {
59 }
60
61 VectorNd LinearProblem::eval_fun(const VectorNd &pt, const double t) const
62 {
63 VectorNd res(1);
64 res(0) = linear_fun(pt(0), pt(1));
65
66 return res;
67 }
68
69 AutodiffGradPt LinearProblem::eval_fun(const AutodiffGradPt &pt, const double t) const
70 {
71 AutodiffGradPt res(1);
72 res(0) = linear_fun(pt(0), pt(1));
73
74 return res;
75 }
76
78 {
79 AutodiffHessianPt res(1);
80 res(0) = linear_fun(pt(0), pt(1));
81
82 return res;
83 }
84
85 QuadraticProblem::QuadraticProblem(const std::string &name)
87 {
88 }
89
90 VectorNd QuadraticProblem::eval_fun(const VectorNd &pt, const double t) const
91 {
92 VectorNd res(1);
93 res(0) = quadratic_fun(pt(0), pt(1));
94
95 return res;
96 }
97
99 {
100 AutodiffGradPt res(1);
101 res(0) = quadratic_fun(pt(0), pt(1));
102
103 return res;
104 }
105
107 {
108 AutodiffHessianPt res(1);
109 res(0) = quadratic_fun(pt(0), pt(1));
110
111 return res;
112 }
113
114 CubicProblem::CubicProblem(const std::string &name)
115 : ProblemWithSolution(name)
116 {
117 }
118
119 VectorNd CubicProblem::eval_fun(const VectorNd &pt, const double t) const
120 {
121 VectorNd res(1);
122 res(0) = cubic_fun(pt(0), pt(1));
123
124 return res;
125 }
126
127 AutodiffGradPt CubicProblem::eval_fun(const AutodiffGradPt &pt, const double t) const
128 {
129 AutodiffGradPt res(1);
130 res(0) = cubic_fun(pt(0), pt(1));
131
132 return res;
133 }
134
136 {
137 AutodiffHessianPt res(1);
138 res(0) = cubic_fun(pt(0), pt(1));
139
140 return res;
141 }
142
143 SineProblem::SineProblem(const std::string &name)
144 : ProblemWithSolution(name)
145 {
146 }
147
148 VectorNd SineProblem::eval_fun(const VectorNd &pt, const double t) const
149 {
150 VectorNd res(1);
151 if (pt.size() == 2)
152 res(0) = sine_fun(pt(0), pt(1));
153 else if (pt.size() == 3)
154 res(0) = sine_fun(pt(0), pt(1), pt(2));
155 else
156 assert(false);
157
158 return res;
159 }
160
161 AutodiffGradPt SineProblem::eval_fun(const AutodiffGradPt &pt, const double t) const
162 {
163 AutodiffGradPt res(1);
164
165 if (pt.size() == 2)
166 res(0) = sine_fun(pt(0), pt(1));
167 else if (pt.size() == 3)
168 res(0) = sine_fun(pt(0), pt(1), pt(2));
169 else
170 assert(false);
171
172 return res;
173 }
174
176 {
177 AutodiffHessianPt res(1);
178
179 if (pt.size() == 2)
180 res(0) = sine_fun(pt(0), pt(1));
181 else if (pt.size() == 3)
182 res(0) = sine_fun(pt(0), pt(1), pt(2));
183 else
184 assert(false);
185
186 return res;
187 }
188
189 ZeroBCProblem::ZeroBCProblem(const std::string &name)
190 : ProblemWithSolution(name)
191 {
192 }
193
194 VectorNd ZeroBCProblem::eval_fun(const VectorNd &pt, const double t) const
195 {
196 VectorNd res(1);
197 if (pt.size() == 2)
198 res(0) = zero_bc(pt(0), pt(1));
199 else if (pt.size() == 3)
200 res(0) = zero_bc(pt(0), pt(1), pt(2));
201 else
202 assert(false);
203
204 return res;
205 }
206
208 {
209 AutodiffGradPt res(1);
210 if (pt.size() == 2)
211 res(0) = zero_bc(pt(0), pt(1));
212 else if (pt.size() == 3)
213 res(0) = zero_bc(pt(0), pt(1), pt(2));
214 else
215 assert(false);
216
217 return res;
218 }
219
221 {
222 AutodiffHessianPt res(1);
223 if (pt.size() == 2)
224 res(0) = zero_bc(pt(0), pt(1));
225 else if (pt.size() == 3)
226 res(0) = zero_bc(pt(0), pt(1), pt(2));
227 else
228 assert(false);
229
230 return res;
231 }
232
233 MinSurfProblem::MinSurfProblem(const std::string &name)
234 : Problem(name)
235 {
236 }
237
238 void MinSurfProblem::rhs(const assembler::Assembler &assembler, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const
239 {
240 val = -10 * Eigen::MatrixXd::Ones(pts.rows(), 1);
241 }
242
243 void MinSurfProblem::dirichlet_bc(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &uv, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const
244 {
245 val = Eigen::MatrixXd::Zero(pts.rows(), 1);
246 }
247
249 : Problem(name)
250 {
251 }
252
253 void TimeDependentProblem::rhs(const assembler::Assembler &assembler, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const
254 {
255 val = Eigen::MatrixXd::Ones(pts.rows(), 1);
256 }
257
258 void TimeDependentProblem::dirichlet_bc(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &uv, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const
259 {
260 val = Eigen::MatrixXd::Zero(pts.rows(), 1);
261 }
262
263 void TimeDependentProblem::initial_solution(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &pts, Eigen::MatrixXd &val) const
264 {
265 val = Eigen::MatrixXd::Zero(pts.rows(), 1);
266 }
267
269 : ProblemWithSolution(name), func_(0)
270 {
271 }
272
273 void GenericScalarProblemExact::initial_solution(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &pts, Eigen::MatrixXd &val) const
274 {
275 exact(pts, 0, val);
276 }
277
279 {
280
281 if (params.contains("func"))
282 {
283 func_ = params["func"];
284 }
285 }
286
288 {
289 VectorNd res(1);
290 const double tt = func_ == 0 ? t : t * t;
291 res(0) = pt(0) * pt(0) + pt(1) * pt(1) + tt;
292 return res;
293 }
295 {
296 AutodiffGradPt res(1);
297 const double tt = func_ == 0 ? t : t * t;
298 res(0) = pt(0) * pt(0) + pt(1) * pt(1) + tt;
299 return res;
300 }
302 {
303 AutodiffHessianPt res(1);
304 const double tt = func_ == 0 ? t : t * t;
305 res(0) = pt(0) * pt(0) + pt(1) * pt(1) + tt;
306 return res;
307 }
308
309 void GenericScalarProblemExact::rhs(const assembler::Assembler &assembler, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const
310 {
311 ProblemWithSolution::rhs(assembler, pts, t, val);
312 if (func_ == 0)
313 val.array() -= 1;
314 else
315 val.array() -= 2 * t;
316 }
317 } // namespace problem
318} // namespace polyfem
double val
Definition Assembler.cpp:86
int y
int z
int x
Abstract mesh class to capture 2d/3d conforming and non-conforming meshes.
Definition Mesh.hpp:39
VectorNd eval_fun(const VectorNd &pt, const double t) const override
CubicProblem(const std::string &name)
GenericScalarProblemExact(const std::string &name)
VectorNd eval_fun(const VectorNd &pt, double t) const override
void rhs(const assembler::Assembler &assembler, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
void set_parameters(const json &params) override
void initial_solution(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &pts, Eigen::MatrixXd &val) const override
LinearProblem(const std::string &name)
VectorNd eval_fun(const VectorNd &pt, const double t) const override
MinSurfProblem(const std::string &name)
void dirichlet_bc(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &uv, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
void rhs(const assembler::Assembler &assembler, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
virtual void rhs(const assembler::Assembler &assembler, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
virtual void exact(const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
QuadraticProblem(const std::string &name)
VectorNd eval_fun(const VectorNd &pt, const double t) const override
VectorNd eval_fun(const VectorNd &pt, const double t) const override
SineProblem(const std::string &name)
void rhs(const assembler::Assembler &assembler, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
TimeDependentProblem(const std::string &name)
void initial_solution(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &pts, Eigen::MatrixXd &val) const override
void dirichlet_bc(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &uv, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
ZeroBCProblem(const std::string &name)
VectorNd eval_fun(const VectorNd &pt, const double t) const override
Eigen::Matrix< AutodiffScalarHessian, Eigen::Dynamic, 1, 0, 3, 1 > AutodiffHessianPt
Eigen::Matrix< double, Eigen::Dynamic, 1, 0, 3, 1 > VectorNd
Definition Types.hpp:11
nlohmann::json json
Definition Common.hpp:9
Eigen::Matrix< AutodiffScalarGrad, Eigen::Dynamic, 1, 0, 3, 1 > AutodiffGradPt