dune-composites (2.5.1)

KLFunctions.hh
1#ifndef KLFunctions_h
2#define KLFunctions_h
3
4namespace Dune{
5 namespace Composites{
6
7 template<class X>
8 void inline evaluate_eigenValues(double ell, X& lambda, X& freq){
10 double c = 1. / ell;
11 for (int i = 0; i < lambda.size(); i++){
12 lambda[i] = 2.0 * c /(freq[i] * freq[i] + c * c);
13 }
14 }
15
16
17 double res(double x, double ell){
18 double c = 1.0 / ell;
19 double g = std::tan(x) - 2.0 * c * x / (x * x - c * c);
20 return g;
21 }
22
23
24 double findRoot(double ell, double a, double b){
26 double fa, fb, x, fx, error, m;
27 error = 1.0;
28 int iter = 0;
29 while (error > 1e-6){
30 fa = res(a,ell);
31 fb = res(b,ell);
32 m = (fb - fa) / (b - a);
33 x = a - fa / m;
34 fx = res(x,ell);
35 if (((fa < 0) & (fx < 0)) | ((fa > 0) & (fx > 0))) { a = x; }
36 else { b = x; }
37 error = std::abs(fx);
38 if(iter > 100000){
39 std::cout << "Warning: root finder hasn't converged. Check number of KL modes and correlation length." << std::endl;
40 break;
41 }
42 iter++;
43 }
44 return x;
45 }
46
47 void rootFinder(int M, double ell, std::vector<double>& answer){
48 double c = 1.0 / ell;
49 std::vector<double> freq(M+2);
50
51 // For all intervals
52
53 int m = -1;
54 //Should go to M because first interval is added additionally!
55 for (int i = 0; i < M+1; i++){
56
57 // std::cout << "Root i = " << i << std::endl;
58
59 double w_min = (i - 0.4999) * M_PI;
60 double w_max = (i + 0.4999) * M_PI;
61
62 // std::cout << "w_min = " << w_min << std::endl;
63 //std::cout << "w_max = " << w_max << std::endl;
64 if ((w_min <= c) && (w_max >= c)){
65
66 // If not first interval look for solution near left boundary
67 if (w_min > 0.0){
68 m += 1;
69 freq[m] = findRoot(ell,w_min,0.5*(c+w_min));
70 }
71 // Always look for solution near right boundary
72 m += 1;
73 freq[m] = findRoot(ell,0.5*(c + w_max),w_max);
74 }
75 else{
76 m += 1;
77 freq[m] = findRoot(ell,w_min,w_max);
78 }
79
80 }
81
82 for (int i = 0; i < M; i++){
83 answer[i] = freq[i+1];
84 }
85
86 }
87
88 }
89}
90
91#endif /* KLFunctions_h */
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Sep 5, 22:35, 2025)