* add the CVS Makefile.vc
[hkl.git] / test / pseudoaxe_twoC_test.cpp
blob8495c7e8ba22be75b4f81c2b2684f0a0a1ef29ad
1 #include "pseudoaxe_twoC_test.h"
2 #include "pseudoaxe.h"
3 #include <fstream>
5 CPPUNIT_TEST_SUITE_REGISTRATION( PseudoAxe_TwoC_Vertical_Test );
7 void
8 PseudoAxe_TwoC_Vertical_Test::setUp(void)
10 m_geometry = geometry::twoC::Vertical();
13 void
14 PseudoAxe_TwoC_Vertical_Test::tearDown(void)
17 void
18 PseudoAxe_TwoC_Vertical_Test::Th2th(void)
20 hkl::pseudoAxeEngine::twoC::vertical::Th2th pseudoAxeEngine(m_geometry);
21 hkl::PseudoAxe & pseudoAxe = *pseudoAxeEngine.pseudoAxes()["th2th"];
23 // this pseudoAxe is always valid.
24 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.get_current());
25 // exception if the source is not initialized
26 CPPUNIT_ASSERT_THROW(pseudoAxe.set_current(1), HKLException);
27 CPPUNIT_ASSERT_EQUAL(false, pseudoAxe.is_writable());
28 CPPUNIT_ASSERT_EQUAL(true, pseudoAxe.is_readable());
29 CPPUNIT_ASSERT_EQUAL(m_geometry.tth()->get_min(), pseudoAxe.get_min());
30 CPPUNIT_ASSERT_EQUAL(m_geometry.tth()->get_max(), pseudoAxe.get_max());
32 // no more exception after initialization of the source.
33 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.initialize());
34 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.get_current());
35 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.set_current(1 * constant::math::degToRad));
36 CPPUNIT_ASSERT_EQUAL(true, pseudoAxe.is_writable());
37 CPPUNIT_ASSERT_EQUAL(true, pseudoAxe.is_readable());
38 CPPUNIT_ASSERT_EQUAL(m_geometry.tth()->get_min(), pseudoAxe.get_min());
39 CPPUNIT_ASSERT_EQUAL(m_geometry.tth()->get_max(), pseudoAxe.get_max());
41 // set a non valid geometry an test
42 CPPUNIT_ASSERT_NO_THROW(m_geometry.setAngles(0, 1));
43 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.get_current());
44 CPPUNIT_ASSERT_THROW(pseudoAxe.set_current(1), HKLException);
45 CPPUNIT_ASSERT_EQUAL(false, pseudoAxe.is_writable());
46 CPPUNIT_ASSERT_EQUAL(true, pseudoAxe.is_readable());
47 CPPUNIT_ASSERT_EQUAL(m_geometry.tth()->get_min(), pseudoAxe.get_min());
48 CPPUNIT_ASSERT_EQUAL(m_geometry.tth()->get_max(), pseudoAxe.get_max());
50 // test the uninitialize
51 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.uninitialize());
52 // this pseudoAxe can be read all the time when the source is well set.
53 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.get_current());
54 CPPUNIT_ASSERT_THROW(pseudoAxe.set_current(1), HKLException);
55 CPPUNIT_ASSERT_EQUAL(false, pseudoAxe.is_writable());
56 CPPUNIT_ASSERT_EQUAL(true, pseudoAxe.is_readable());
57 CPPUNIT_ASSERT_EQUAL(m_geometry.tth()->get_min(), pseudoAxe.get_min());
58 CPPUNIT_ASSERT_EQUAL(m_geometry.tth()->get_max(), pseudoAxe.get_max());
60 //set_current
61 m_geometry.setAngles(45 * constant::math::degToRad,
62 34 * constant::math::degToRad);
63 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.initialize());
64 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.set_current(34. * constant::math::degToRad));
65 CPPUNIT_ASSERT_EQUAL(Value(45 * constant::math::degToRad),
66 m_geometry.get_axe("omega").get_current());
67 CPPUNIT_ASSERT_EQUAL(Value(34 * constant::math::degToRad),
68 m_geometry.get_axe("2theta").get_current());
69 //get_value
70 CPPUNIT_ASSERT_EQUAL(Value(34. * constant::math::degToRad), pseudoAxe.get_current());
73 //set_current
74 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.set_current(36. * constant::math::degToRad));
75 CPPUNIT_ASSERT_EQUAL(Value(46 * constant::math::degToRad),
76 m_geometry.get_axe("omega").get_current());
77 CPPUNIT_ASSERT_EQUAL(Value(36 * constant::math::degToRad),
78 m_geometry.get_axe("2theta").get_current());
80 // random test
81 unsigned int i;
82 unsigned int j;
83 for(i=0;i<100;i++)
85 double omega0 = constant::math::pi * (2. * rand() / (RAND_MAX + 1.) - 1.);
86 double tth0 = constant::math::pi * (2. * rand() / (RAND_MAX + 1.) - 1.);
87 m_geometry.setAngles(omega0, tth0);
88 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.initialize());
89 double min = pseudoAxe.get_min().get_value();
90 double max = pseudoAxe.get_max().get_value();
91 for(j=0;j<100;j++)
93 double angle0 = (max - min) * rand() / (RAND_MAX + 1.) + min;
94 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.set_current(Value(angle0)));
95 double angle = pseudoAxe.get_current().get_value();
96 CPPUNIT_ASSERT_EQUAL(fmod(angle0, constant::math::pi), fmod(angle, constant::math::pi));
100 #ifdef PROFILE
101 // profiling
102 Value v(36. * constant::math::degToRad);
103 for(unsigned int i=0;i<1000000; i++)
105 pseudoAxe.set_current(v);
106 pseudoAxe.get_current();
107 pseudoAxe.get_min();
108 pseudoAxe.get_max();
110 #endif
113 void
114 PseudoAxe_TwoC_Vertical_Test::Q2th(void)
116 hkl::pseudoAxeEngine::twoC::vertical::Q2th pseudoAxeEngine(m_geometry);
117 hkl::PseudoAxe & pseudoAxe = *pseudoAxeEngine.pseudoAxes()["q2th"];
119 // exception if not initialize
120 // This pseudoAxe can be read all the time.
121 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.get_current());
122 CPPUNIT_ASSERT_THROW(pseudoAxe.set_current(0), HKLException);
123 CPPUNIT_ASSERT_EQUAL(false, pseudoAxe.is_writable());
124 CPPUNIT_ASSERT_EQUAL(true, pseudoAxe.is_readable());
125 CPPUNIT_ASSERT_EQUAL(Value(-2 * constant::physic::tau / 1.54), pseudoAxe.get_min());
126 CPPUNIT_ASSERT_EQUAL(Value(2 * constant::physic::tau / 1.54), pseudoAxe.get_max());
128 // no more exception after the source initialisation
129 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.initialize());
130 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.get_current());
131 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.set_current(0));
132 CPPUNIT_ASSERT_EQUAL(true, pseudoAxe.is_writable());
133 CPPUNIT_ASSERT_EQUAL(true, pseudoAxe.is_readable());
134 CPPUNIT_ASSERT_EQUAL(Value(-2 * constant::physic::tau / 1.54), pseudoAxe.get_min());
135 CPPUNIT_ASSERT_EQUAL(Value(2 * constant::physic::tau / 1.54), pseudoAxe.get_max());
137 // uninitialize
138 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.uninitialize());
139 // This pseudoAxe can be read all the time one the source is well set.
140 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.get_current());
141 CPPUNIT_ASSERT_THROW(pseudoAxe.set_current(0), HKLException);
142 CPPUNIT_ASSERT_EQUAL(false, pseudoAxe.is_writable());
143 CPPUNIT_ASSERT_EQUAL(true, pseudoAxe.is_readable());
144 CPPUNIT_ASSERT_EQUAL(Value(-2 * constant::physic::tau / 1.54), pseudoAxe.get_min());
145 CPPUNIT_ASSERT_EQUAL(Value(2 * constant::physic::tau / 1.54), pseudoAxe.get_max());
147 //set_current
148 double lambda = m_geometry.get_source().get_waveLength().get_value();
149 double theta = 34 / 2;
150 double value = 2 * constant::physic::tau * sin(theta * constant::math::degToRad) / lambda;
151 m_geometry.setAngles(45 * constant::math::degToRad,
152 34 * constant::math::degToRad);
153 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.initialize());
154 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.set_current(value));
155 CPPUNIT_ASSERT_EQUAL(Value(45 * constant::math::degToRad),
156 m_geometry.get_axe("omega").get_current());
157 CPPUNIT_ASSERT_EQUAL(Value(34 * constant::math::degToRad),
158 m_geometry.get_axe("2theta").get_current());
159 //get_value
160 CPPUNIT_ASSERT_EQUAL((Value)value, pseudoAxe.get_current());
163 //set_current
164 theta = 36 / 2;
165 value = 2 * constant::physic::tau * sin(theta* constant::math::degToRad) / lambda;
166 pseudoAxe.set_current(value);
167 CPPUNIT_ASSERT_EQUAL(Value(46 * constant::math::degToRad),
168 m_geometry.get_axe("omega").get_current());
169 CPPUNIT_ASSERT_EQUAL(Value(36 * constant::math::degToRad),
170 m_geometry.get_axe("2theta").get_current());
172 #ifdef PROFILE
173 // profiling
174 Value v(36. * constant::math::degToRad);
175 for(unsigned int i=0;i<1000000; i++)
177 pseudoAxe.set_current(v);
178 pseudoAxe.get_current();
179 pseudoAxe.get_min();
180 pseudoAxe.get_max();
182 #endif
184 // if put a non valid geometry can not set the value.
185 m_geometry.setAngles(40. * constant::math::degToRad,
186 30. * constant::math::degToRad);
187 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.get_current());
188 CPPUNIT_ASSERT_THROW(pseudoAxe.set_current(1. * constant::math::degToRad), HKLException);
189 CPPUNIT_ASSERT_EQUAL(false, pseudoAxe.is_writable());
190 CPPUNIT_ASSERT_EQUAL(true, pseudoAxe.is_readable());
191 CPPUNIT_ASSERT_EQUAL(Value(-2 * constant::physic::tau / 1.54), pseudoAxe.get_min());
192 CPPUNIT_ASSERT_EQUAL(Value(2 * constant::physic::tau / 1.54), pseudoAxe.get_max());
196 void
197 PseudoAxe_TwoC_Vertical_Test::Q(void)
199 hkl::pseudoAxeEngine::twoC::vertical::Q pseudoAxeEngine(m_geometry);
200 hkl::PseudoAxe & pseudoAxe = *pseudoAxeEngine.pseudoAxes()["q"];
202 // exception if not initialized.
203 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.get_current());
204 CPPUNIT_ASSERT_THROW(pseudoAxe.set_current(0), HKLException);
205 CPPUNIT_ASSERT_EQUAL(false, pseudoAxe.is_writable());
206 CPPUNIT_ASSERT_EQUAL(true, pseudoAxe.is_readable());
207 CPPUNIT_ASSERT_EQUAL(Value(-2 * constant::physic::tau / 1.54), pseudoAxe.get_min());
208 CPPUNIT_ASSERT_EQUAL(Value(2 * constant::physic::tau / 1.54), pseudoAxe.get_max());
210 // no more exception after initialisation
211 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.initialize());
212 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.get_current());
213 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.set_current(0));
214 CPPUNIT_ASSERT_EQUAL(true, pseudoAxe.is_writable());
215 CPPUNIT_ASSERT_EQUAL(true, pseudoAxe.is_readable());
216 CPPUNIT_ASSERT_EQUAL(Value(-2 * constant::physic::tau / 1.54), pseudoAxe.get_min());
217 CPPUNIT_ASSERT_EQUAL(Value(2 * constant::physic::tau / 1.54), pseudoAxe.get_max());
219 // uninitialize
220 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.uninitialize());
221 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.get_current());
222 CPPUNIT_ASSERT_THROW(pseudoAxe.set_current(0), HKLException);
223 CPPUNIT_ASSERT_EQUAL(false, pseudoAxe.is_writable());
224 CPPUNIT_ASSERT_EQUAL(true, pseudoAxe.is_readable());
225 CPPUNIT_ASSERT_EQUAL(Value(-2 * constant::physic::tau / 1.54), pseudoAxe.get_min());
226 CPPUNIT_ASSERT_EQUAL(Value(2 * constant::physic::tau / 1.54), pseudoAxe.get_max());
228 //set_current
229 m_geometry.setAngles(45 * constant::math::degToRad, 34 * constant::math::degToRad);
230 double lambda = m_geometry.get_source().get_waveLength().get_value();
231 double theta = 34 / 2 * constant::math::degToRad;
232 double value = 2 * constant::physic::tau * sin(theta) / lambda;
233 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.initialize());
234 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.set_current(value));
235 CPPUNIT_ASSERT_EQUAL(Value(45 * constant::math::degToRad),
236 m_geometry.get_axe("omega").get_current());
237 CPPUNIT_ASSERT_EQUAL(Value(34 * constant::math::degToRad),
238 m_geometry.get_axe("2theta").get_current());
239 //get_value
240 CPPUNIT_ASSERT_EQUAL((Value)value, pseudoAxe.get_current());
242 //set_current
243 theta = 36 / 2;
244 value = 2 * constant::physic::tau * sin(theta* constant::math::degToRad) / lambda;
245 CPPUNIT_ASSERT_NO_THROW(pseudoAxe.set_current(value));
246 CPPUNIT_ASSERT_EQUAL(Value(45 * constant::math::degToRad),
247 m_geometry.get_axe("omega").get_current());
248 CPPUNIT_ASSERT_EQUAL(Value(36 * constant::math::degToRad),
249 m_geometry.get_axe("2theta").get_current());
251 #ifdef PROFILE
252 // profiling
253 Value v(36. * constant::math::degToRad);
254 for(unsigned int i=0;i<1000000; i++)
256 pseudoAxe.set_current(v);
257 pseudoAxe.get_current();
258 pseudoAxe.get_min();
259 pseudoAxe.get_max();
261 #endif
264 void
265 PseudoAxe_TwoC_Vertical_Test::persistanceIO(void)
267 hkl::pseudoAxeEngine::twoC::vertical::Th2th th2th_ref(m_geometry);
268 hkl::pseudoAxeEngine::twoC::vertical::Th2th th2th(m_geometry);
269 hkl::pseudoAxeEngine::twoC::vertical::Q2th q2th_ref(m_geometry);
270 hkl::pseudoAxeEngine::twoC::vertical::Q2th q2th(m_geometry);
271 hkl::pseudoAxeEngine::twoC::vertical::Q q_ref(m_geometry);
272 hkl::pseudoAxeEngine::twoC::vertical::Q q(m_geometry);
274 stringstream flux;
276 th2th_ref.toStream(flux);
277 q2th_ref.toStream(flux);
278 q_ref.toStream(flux);
280 th2th.fromStream(flux);
281 q2th.fromStream(flux);
282 q.fromStream(flux);
284 CPPUNIT_ASSERT_EQUAL(th2th_ref, th2th);
285 CPPUNIT_ASSERT_EQUAL(q2th_ref, q2th);
286 CPPUNIT_ASSERT_EQUAL(q_ref, q);