b2api
B2000++ API Reference Manual, VERSION 4.6
 
Loading...
Searching...
No Matches
b2integration1d.H
1//------------------------------------------------------------------------
2// b2integration1d.H --
3//
4//
5// written by Mathias Doreille
6//
7// Copyright (c) 2007-2012 SMR Engineering & Development SA
8// 2502 Bienne, Switzerland
9//
10// All Rights Reserved. Proprietary source code. The contents of
11// this file may not be disclosed to third parties, copied or
12// duplicated in any form, in whole or in part, without the prior
13// written permission of SMR.
14//------------------------------------------------------------------------
15
16#ifndef B2INTEGRATION1D_H_
17#define B2INTEGRATION1D_H_
18
19#include "b2exception.H"
20
21typedef double(quadpack_function_t)(const double*);
22
23extern "C" void FC_GLOBAL(dqagse, DQAGSE)(
24 quadpack_function_t f, double* a, double* b, double* epsabs, double* epsrel, int* limit,
25 double* result, double* abserr, int* neval, int* ier, double* alist, double* blist,
26 double* rlist, double* elist, int* iord, int* last);
27
28namespace b2000 { namespace quadpack {
29
30template <typename F>
31double simpson_integration(const F& f, double a, double b, int n) {
32 if (n % 2 != 0) { n += 1; }
33 double r = (f(a) + f(b)) / 2;
34 const double h = (b - a) / n;
35 a += h;
36 for (int i = 1; i != n; ++i, a += h) { r += (1 + i % 2) * f(a); }
37 return 2 * r * h / 3;
38}
39
40template <typename F>
41class FOToF {
42public:
43 static void set(const F* f) { f_static = f; }
44 static double f(const double* x) { return (*f_static)(*x); }
45
46private:
47 // Put it in thread local variable for a thread safe implementation.
48 static const F* f_static;
49};
50
51template <typename F>
52const F* FOToF<F>::f_static;
53
54class Exception : public b2000::Exception {
55public:
56 Exception(int ier) {
57 switch (ier) {
58 case 1:
59 set_msg("Maximum number of subdivisions allowed has been achieved");
60 return;
61 case 2:
62 set_msg(
63 "The occurrence of roundoff error is detected, "
64 "which prevents the requested tolerance from being achieved.");
65 return;
66 case 3:
67 set_msg(
68 "Extremely bad integrand behaviour occurs at some "
69 "points of the integration interval.");
70 return;
71 case 4:
72 set_msg("The algorithm does not converge.");
73 return;
74 case 5:
75 set_msg("The integral is probably divergent, or slowly convergent.");
76 return;
77 case 6:
78 set_msg("The input is invali.");
79 return;
80 default:
81 set_msg("The integral is probably divergent, or slowly convergent.");
82 return;
83 }
84 }
85};
86
87template <typename F>
88double qags(
89 const F& f, double a, double b, double epsabs, double epsrel, int subintevals_limit,
90 double& abserr, int& nb_subinterval) {
91 double res;
92 int neval;
93 int ier;
94 double* work = new double[subintevals_limit * 4];
95 int* iwork = new int[subintevals_limit];
96 FOToF<F>::set(&f);
97 FC_GLOBAL(dqagse, DQAGSE)
98 (FOToF<F>::f, &a, &b, &epsabs, &epsrel, &subintevals_limit, &res, &abserr, &neval, &ier, work,
99 work + subintevals_limit, work + 2 * subintevals_limit, work + 3 * subintevals_limit, iwork,
100 &nb_subinterval);
101 nb_subinterval = neval;
102 delete[] work;
103 delete[] iwork;
104 if (ier > 1) { Exception(ier) << THROW; }
105 return res;
106}
107
108}} // namespace b2000::quadpack
109
110#endif /*B2INTEGRATION1D_H_*/
#define THROW
Definition b2exception.H:198
Definition b2exception.H:131
Exception(const std::string &msg_=std::string(), const char *file_=nullptr, int line_=-1)
Definition b2exception.H:143
Contains the base classes for implementing Finite Elements.
Definition b2boundary_condition.H:32