tode.h.html - numeric - C++ library with numerical algorithms
(HTM) git clone git://src.adamsgaard.dk/numeric
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
tode.h.html (10451B)
---
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <title>~/code/numeric/exam/ode.h.html</title>
6 <meta name="Generator" content="Vim/7.4">
7 <meta name="plugin-version" content="vim7.4_v1">
8 <meta name="syntax" content="cpp">
9 <meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
10 <meta name="colorscheme" content="desert">
11 <style type="text/css">
12 <!--
13 pre { white-space: pre-wrap; font-family: monospace; color: #ffffff; background-color: #000000; }
14 body { font-family: monospace; color: #ffffff; background-color: #000000; }
15 * { font-size: 1em; }
16 .Type { color: #008000; }
17 .Statement { color: #804000; }
18 .LineNr { color: #804000; }
19 .Comment { color: #008080; }
20 .Constant { color: #af5f00; }
21 .PreProc { color: #c000c0; }
22 -->
23 </style>
24
25 <script type='text/javascript'>
26 <!--
27
28 /* function to open any folds containing a jumped-to line before jumping to it */
29 function JumpToLine()
30 {
31 var lineNum;
32 lineNum = window.location.hash;
33 lineNum = lineNum.substr(1); /* strip off '#' */
34
35 if (lineNum.indexOf('L') == -1) {
36 lineNum = 'L'+lineNum;
37 }
38 lineElem = document.getElementById(lineNum);
39 /* Always jump to new location even if the line was hidden inside a fold, or
40 * we corrected the raw number to a line ID.
41 */
42 if (lineElem) {
43 lineElem.scrollIntoView(true);
44 }
45 return true;
46 }
47 if ('onhashchange' in window) {
48 window.onhashchange = JumpToLine;
49 }
50
51 -->
52 </script>
53 </head>
54 <body onload='JumpToLine();'>
55 <pre id='vimCodeElement'>
56 <span id="L1" class="LineNr"> 1 </span><span class="Comment">// Make sure header is only included once</span>
57 <span id="L2" class="LineNr"> 2 </span><span class="PreProc">#ifndef ODE_H_</span>
58 <span id="L3" class="LineNr"> 3 </span><span class="PreProc">#define ODE_H_</span>
59 <span id="L4" class="LineNr"> 4 </span>
60 <span id="L5" class="LineNr"> 5 </span><span class="PreProc">#include </span><span class="Constant"><vector></span>
61 <span id="L6" class="LineNr"> 6 </span><span class="PreProc">#include </span><span class="Constant"><complex></span>
62 <span id="L7" class="LineNr"> 7 </span><span class="PreProc">#include </span><span class="Constant">"typedefs.h"</span>
63 <span id="L8" class="LineNr"> 8 </span>
64 <span id="L9" class="LineNr"> 9 </span><span class="Comment">// ODE class</span>
65 <span id="L10" class="LineNr">10 </span><span class="Type">class</span> ODE {
66 <span id="L11" class="LineNr">11 </span>
67 <span id="L12" class="LineNr">12 </span> <span class="Comment">// Values and functions only accessible from the class internally</span>
68 <span id="L13" class="LineNr">13 </span> <span class="Statement">private</span>:
69 <span id="L14" class="LineNr">14 </span>
70 <span id="L15" class="LineNr">15 </span> <span class="Comment">// System of ordinary differential equations to solve</span>
71 <span id="L16" class="LineNr">16 </span> std::vector<std::<span class="Type">complex</span><Floattype> >
72 <span id="L17" class="LineNr">17 </span> (*f)(<span class="Type">const</span> std::<span class="Type">complex</span><Floattype> x,
73 <span id="L18" class="LineNr">18 </span> <span class="Type">const</span> std::vector<std::<span class="Type">complex</span><Floattype> > &y);
74 <span id="L19" class="LineNr">19 </span>
75 <span id="L20" class="LineNr">20 </span> <span class="Comment">// Points to be evaluated</span>
76 <span id="L21" class="LineNr">21 </span> std::vector<std::<span class="Type">complex</span><Floattype> > x_list;
77 <span id="L22" class="LineNr">22 </span>
78 <span id="L23" class="LineNr">23 </span> <span class="Comment">// Limits of range to evaluate</span>
79 <span id="L24" class="LineNr">24 </span> <span class="Type">const</span> std::<span class="Type">complex</span><Floattype> a; <span class="Comment">// Lower</span>
80 <span id="L25" class="LineNr">25 </span> <span class="Type">const</span> std::<span class="Type">complex</span><Floattype> b; <span class="Comment">// Upper</span>
81 <span id="L26" class="LineNr">26 </span>
82 <span id="L27" class="LineNr">27 </span> <span class="Comment">// Step size</span>
83 <span id="L28" class="LineNr">28 </span> std::<span class="Type">complex</span><Floattype> h;
84 <span id="L29" class="LineNr">29 </span>
85 <span id="L30" class="LineNr">30 </span> <span class="Comment">// Results stored in 2D: vector of vectors</span>
86 <span id="L31" class="LineNr">31 </span> std::vector<std::vector<std::<span class="Type">complex</span><Floattype> > > y_list;
87 <span id="L32" class="LineNr">32 </span>
88 <span id="L33" class="LineNr">33 </span> <span class="Comment">// Maximum number of steps to evaluate, defined by y size</span>
89 <span id="L34" class="LineNr">34 </span> <span class="Type">const</span> Inttype n_max;
90 <span id="L35" class="LineNr">35 </span>
91 <span id="L36" class="LineNr">36 </span> <span class="Comment">// Accuracy requirement values</span>
92 <span id="L37" class="LineNr">37 </span> <span class="Type">const</span> Floattype delta; <span class="Comment">// Absolute</span>
93 <span id="L38" class="LineNr">38 </span> <span class="Type">const</span> Floattype epsilon; <span class="Comment">// Relative</span>
94 <span id="L39" class="LineNr">39 </span>
95 <span id="L40" class="LineNr">40 </span> <span class="Comment">// Tolerance estimator</span>
96 <span id="L41" class="LineNr">41 </span> Floattype tau(<span class="Type">const</span> std::vector<std::<span class="Type">complex</span><Floattype> > &y,
97 <span id="L42" class="LineNr">42 </span> <span class="Type">const</span> std::<span class="Type">complex</span><Floattype> h);
98 <span id="L43" class="LineNr">43 </span>
99 <span id="L44" class="LineNr">44 </span> <span class="Comment">// Runge-Kutta mid-point stepper prototype</span>
100 <span id="L45" class="LineNr">45 </span> <span class="Type">void</span> rkstep12(<span class="Type">const</span> std::<span class="Type">complex</span><Floattype> x0,
101 <span id="L46" class="LineNr">46 </span> <span class="Type">const</span> std::vector<std::<span class="Type">complex</span><Floattype> > &y0,
102 <span id="L47" class="LineNr">47 </span> std::vector<std::<span class="Type">complex</span><Floattype> > &y1,
103 <span id="L48" class="LineNr">48 </span> std::vector<std::<span class="Type">complex</span><Floattype> > &dy);
104 <span id="L49" class="LineNr">49 </span>
105 <span id="L50" class="LineNr">50 </span> <span class="Comment">// Runge-Kutta driver function parameters</span>
106 <span id="L51" class="LineNr">51 </span> <span class="Type">const</span> Floattype power;
107 <span id="L52" class="LineNr">52 </span> <span class="Type">const</span> Floattype safety;
108 <span id="L53" class="LineNr">53 </span>
109 <span id="L54" class="LineNr">54 </span> <span class="Comment">// Runge-Kutta driver prototype</span>
110 <span id="L55" class="LineNr">55 </span> <span class="Type">void</span> rkdriver();
111 <span id="L56" class="LineNr">56 </span>
112 <span id="L57" class="LineNr">57 </span>
113 <span id="L58" class="LineNr">58 </span> <span class="Comment">// Values and functions accessible from the outside</span>
114 <span id="L59" class="LineNr">59 </span> <span class="Statement">public</span>:
115 <span id="L60" class="LineNr">60 </span>
116 <span id="L61" class="LineNr">61 </span> <span class="Comment">// Constructor, some parameters with default values</span>
117 <span id="L62" class="LineNr">62 </span> ODE(std::vector<std::<span class="Type">complex</span><Floattype> >
118 <span id="L63" class="LineNr">63 </span> (*f_in)(<span class="Type">const</span> std::<span class="Type">complex</span><Floattype> x,
119 <span id="L64" class="LineNr">64 </span> <span class="Type">const</span> std::vector<std::<span class="Type">complex</span><Floattype> > &y),
120 <span id="L65" class="LineNr">65 </span> <span class="Type">const</span> std::vector<std::<span class="Type">complex</span><Floattype> > y_start,
121 <span id="L66" class="LineNr">66 </span> <span class="Type">const</span> std::<span class="Type">complex</span><Floattype> a_in,
122 <span id="L67" class="LineNr">67 </span> <span class="Type">const</span> std::<span class="Type">complex</span><Floattype> b_in,
123 <span id="L68" class="LineNr">68 </span> <span class="Type">const</span> Floattype h_start = <span class="Constant">0.01f</span>,
124 <span id="L69" class="LineNr">69 </span> <span class="Type">const</span> Inttype max_steps = <span class="Constant">1e4</span>,
125 <span id="L70" class="LineNr">70 </span> <span class="Type">const</span> Floattype delta_in = <span class="Constant">1e-3f</span>,
126 <span id="L71" class="LineNr">71 </span> <span class="Type">const</span> Floattype epsilon_in = <span class="Constant">1e-3f</span>,
127 <span id="L72" class="LineNr">72 </span> <span class="Type">const</span> Floattype power_in = <span class="Constant">0.25f</span>,
128 <span id="L73" class="LineNr">73 </span> <span class="Type">const</span> Floattype safety_in = <span class="Constant">0.95f</span>
129 <span id="L74" class="LineNr">74 </span> );
130 <span id="L75" class="LineNr">75 </span>
131 <span id="L76" class="LineNr">76 </span> <span class="Comment">// Return the number of steps taken</span>
132 <span id="L77" class="LineNr">77 </span> Inttype steps();
133 <span id="L78" class="LineNr">78 </span>
134 <span id="L79" class="LineNr">79 </span> <span class="Comment">// Print the x- and y-values to stdout</span>
135 <span id="L80" class="LineNr">80 </span> <span class="Type">void</span> print();
136 <span id="L81" class="LineNr">81 </span>
137 <span id="L82" class="LineNr">82 </span> <span class="Comment">// Write the x- and y-values to file</span>
138 <span id="L83" class="LineNr">83 </span> <span class="Type">void</span> write(<span class="Type">const</span> <span class="Type">char</span>* filename);
139 <span id="L84" class="LineNr">84 </span>
140 <span id="L85" class="LineNr">85 </span>};
141 <span id="L86" class="LineNr">86 </span>
142 <span id="L87" class="LineNr">87 </span><span class="PreProc">#endif</span>
143 </pre>
144 </body>
145 </html>
146 <!-- vim: set foldmethod=manual : -->