Jpp 21.0.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JSphereND.cc
Go to the documentation of this file.
1#include <string>
2#include <iostream>
3#include <iomanip>
4
5#include "JTools/JMapList.hh"
10#include "JTools/JMultiGrid.hh"
13
14#include "Jeep/JPrint.hh"
15#include "Jeep/JTimer.hh"
16#include "Jeep/JParser.hh"
17#include "Jeep/JMessage.hh"
18
19
20namespace {
21
22 /**
23 * Auxiliary class to determine volume of N-dimensional sphere.
24 */
25 template<int N>
26 struct JVolume {
27 /**
28 * Get volume.
29 *
30 * \param R radius
31 * \return volume
32 */
33 static double get(const double R)
34 {
35 return 2*JTOOLS::PI*R*R * JVolume<N-2>::get(R) / N;
36 }
37 };
38
39
40 /**
41 * Specialisation of template class JVolume for 2D sphere.
42 */
43 template<>
44 struct JVolume<2> {
45 /**
46 * Get volume.
47 *
48 * \param R radius
49 * \return volume
50 */
51 static double get(const double R)
52 {
53 return JTOOLS::PI*R*R;
54 }
55 };
56
57
58 /**
59 * Specialisation of template class JVolume for 1D sphere.
60 */
61 template<>
62 struct JVolume<1> {
63 /**
64 * Get volume.
65 *
66 * \param R radius
67 * \return volume
68 */
69 static double get(const double R)
70 {
71 return 2*R;
72 }
73 };
74
75
76 /**
77 * Get length squared.
78 *
79 * \param key key
80 * \return square of length
81 */
82 template<class JKey_t>
83 inline double getLengthSquared(const JTOOLS::JMultiKey<1, JKey_t>& key)
84 {
85 return key.first*key.first;
86 }
87
88
89 /**
90 * Get length squared.
91 *
92 * \param key key
93 * \return square of length
94 */
95 template<unsigned int N, class JKey_t>
96 inline double getLengthSquared(const JTOOLS::JMultiKey<N, JKey_t>& key)
97 {
98 return key.first*key.first + getLengthSquared(key.second);
99 }
100}
101
102
103/**
104 * \file
105 *
106 * Example program to test integration of sphere in any number of dimensions.
107 * \author mdejong
108 */
109int main(int argc, char **argv)
110{
111 using namespace std;
112 using namespace JPP;
113
114 unsigned int numberOfBins;
115 double precision;
116 int debug;
117
118 try {
119
120 JParser<> zap("Example program to test integration of sphere in any number of dimensions.");
121
122 zap['N'] = make_field(numberOfBins) = 11;
123 zap['e'] = make_field(precision) = 1.0e-2;
124 zap['d'] = make_field(debug) = 2;
125
126 zap(argc, argv);
127 }
128 catch(const exception &error) {
129 FATAL(error.what() << endl);
130 }
131
132
133 const int N = 7;
134 const double R = 1.0;
135
136 typedef JGridPolint1Function1D_t JFunction1D_t;
137 typedef JMultipleMap<N-2, JPolint1FunctionalGridMap>::typelist JMaplist_t;
138 typedef JMultiFunction<JFunction1D_t, JMaplist_t> JMultiFunction_t;
139
140
141 const JGrid<double> grid(numberOfBins, -R * 1.05, +R * 1.05);
142
143 JMultiFunction_t gs;
144
145 gs.configure(make_multigrid<N-2>(grid));
146
147 for (JMultiFunction_t::super_iterator i = gs.super_begin(); i != gs.super_end(); ++i) {
148
149 const double x = sqrt(getLengthSquared(i.getKey()));
150
151 for (int __i = 0; __i != grid.getSize(); ++__i) {
152
153 const double y = grid.getX(__i);
154 const double z = R*R - x*x - y*y;
155
156 if (z > 0.0)
157 i.getValue()[y] = 2.0 * sqrt(z);
158 else
159 i.getValue()[y] = 0.0;
160 }
161 }
162
163 gs.compile();
164
165 const double U = JVolume<N>::get(R);
166
167 JTimer timer("integrator");
168
169 timer.start();
170
171 const double W = getIntegral(gs);
172
173 timer.stop();
174
175 NOTICE("Sphere " << N << "D" << endl);
176 NOTICE("Volume (real) " << SCIENTIFIC(12,4) << U << endl);
177 NOTICE("Volume (calc) " << SCIENTIFIC(12,4) << W << endl);
178
179 NOTICE(timer);
180
181 ASSERT(fabs(U - W) < precision * U);
182
183 return 0;
184}
Various implementations of functional maps.
General purpose messaging.
#define ASSERT(A,...)
Assert macro.
Definition JMessage.hh:90
#define NOTICE(A)
Definition JMessage.hh:64
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:74
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2140
I/O formatting auxiliaries.
int numberOfBins
number of bins for average CDF integral of optical module
Definition JSirene.cc:75
int main(int argc, char **argv)
Definition JSphereND.cc:109
This include file contains various recursive methods to operate on multi-dimensional collections.
Auxiliary class for CPU timing and usage.
Definition JTimer.hh:33
void stop()
Stop timer.
Definition JTimer.hh:127
void start()
Start timer.
Definition JTimer.hh:106
Utility class to parse command line options.
Definition JParser.hh:1697
Multidimensional interpolation method.
Multidimensional key.
Definition JMultiKey.hh:44
Template class for polynomial interpolation in 1D.
Definition JPolint.hh:1095
Functional map with polynomial interpolation.
Definition JPolint.hh:1153
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Simple data structure for an abstract collection of equidistant abscissa values.
Definition JGrid.hh:40
virtual int getSize() const override
Get number of elements.
Definition JGrid.hh:75
virtual abscissa_type getX(int index) const override
Get abscissa value.
Definition JGrid.hh:87
List of identical maps.
Auxiliary data structure for floating point format specification.
Definition JManip.hh:488