PLaSK library

Loading...
Searching...
No Matches
rootdigger.hpp
Go to the documentation of this file.
1/*
2 * This file is part of PLaSK (https://plask.app) by Photonics Group at TUL
3 * Copyright (c) 2022 Lodz University of Technology
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14#ifndef PLASK__OPTICAL_EFFECTIVE_ROOTDIGGER_H
15#define PLASK__OPTICAL_EFFECTIVE_ROOTDIGGER_H
16
17#include <functional>
18#include <plask/plask.hpp>
19
20 namespace plask { namespace optical { namespace effective {
21
22 struct RootDigger {
23
24 typedef std::function<dcomplex(dcomplex)> function_type;
25
27 enum Method {
28 ROOT_MULLER,
29 ROOT_BROYDEN,
30 ROOT_BRENT,
31 };
32
33 struct Params {
34 Method method;
35 double tolx,
36 tolf_min,
37 tolf_max,
38 maxstep;
39 int maxiter;
40 double alpha,
41 lambda_min;
42 dcomplex initial_dist;
43 int stairs;
44
45 Params():
46 method(ROOT_BROYDEN),
47 tolx(1e-6),
48 tolf_min(1e-7),
49 tolf_max(1e-5),
50 maxstep(0.1),
51 maxiter(500),
52 alpha(1e-7),
53 lambda_min(1e-8),
54 initial_dist(1e-3),
55 stairs(3)
56 {}
57 };
58
59 protected:
60
61 // Solver
62 Solver& solver;
63
64 // Solver method computing the value to zero
65 function_type val_function;
66
67 // Value writelog
68 DataLog<dcomplex,dcomplex>& log_value;
69
70 inline dcomplex valFunction(dcomplex x) const {
71 try {
72 return val_function(x);
73 } catch (...) {
74 log_value.throwError(x);
75 }
76 return NAN;
77 }
78
79 public:
80
81 // Rootdigger parameters
82 Params params;
83
84 // Constructor
85 RootDigger(Solver& solver, const function_type& val_fun, DataLog<dcomplex,dcomplex>& log_value,
86 const Params& pars) :
87 solver(solver),
88 val_function(val_fun),
89 log_value(log_value),
90 params(pars)
91 {};
92
93 virtual ~RootDigger() {}
94
100 virtual dcomplex find(dcomplex start) const = 0;
101
109 static std::unique_ptr<RootDigger> get(Solver* solver, const function_type& func, DataLog<dcomplex,dcomplex>& detlog, const Params& params);
110
112 static void readRootDiggerConfig(XMLReader& reader, Params& params);
113};
114
115}}} // namespace plask::optical::effective
116
117#endif // PLASK__OPTICAL_EFFECTIVE_ROOTDIGGER_H