1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560 | /* *****************************************************************
MESQUITE -- The Mesh Quality Improvement Toolkit
Copyright 2006 Lawrence Livermore National Laboratory. Under
the terms of Contract B545069 with the University of Wisconsin --
Madison, Lawrence Livermore National Laboratory retains certain
rights in this software.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
(lgpl.txt) along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
(2006) [email protected]
***************************************************************** */
/** \file CachingTargetTest.cpp
* \brief Unit tests for CachingTargetCalculator class
* \author Jason Kraftcheck
*/
#include "Mesquite.hpp"
#include "CachingTargetCalculator.hpp"
#include "PatchData.hpp"
#include "UnitUtil.hpp"
#include "PatchDataInstances.hpp"
#include <cppunit/extensions/HelperMacros.h>
#include <algorithm>
using namespace MBMesquite;
class CachedTargetCalculator;
class CachingTargetTest : public CppUnit::TestFixture<--- The class 'CachingTargetTest' does not have a constructor although it has private member variables. [+]The class 'CachingTargetTest' does not have a constructor although it has private member variables. Member variables of builtin types are left uninitialized when the class is instantiated. That may cause bugs or undefined behavior.
{
private:
CPPUNIT_TEST_SUITE( CachingTargetTest );
CPPUNIT_TEST( test_surface_orient_flag );
CPPUNIT_TEST( test_3d_targets_cached );
CPPUNIT_TEST( test_2d_targets_cached );
CPPUNIT_TEST( test_surface_targets_cached );
CPPUNIT_TEST( test_3d_target_values );
CPPUNIT_TEST( test_2d_target_values );
CPPUNIT_TEST( test_surface_target_values );
CPPUNIT_TEST( test_3d_target_subpatch );
CPPUNIT_TEST( test_2d_target_subpatch );
CPPUNIT_TEST( test_surface_target_subpatch );
CPPUNIT_TEST( test_cache_cleared );
CPPUNIT_TEST_SUITE_END();
PatchData patch_3d, patch_2d;
CachedTargetCalculator* cached;
CachingTargetCalculator* cacher;
unsigned request_all_targets_3d();
unsigned request_all_targets_2d();
unsigned request_all_targets_surf();
public:
void setUp();
void tearDown();
void test_surface_orient_flag();
void test_3d_targets_cached();
void test_2d_targets_cached();
void test_surface_targets_cached();
void test_3d_target_values();
void test_2d_target_values();
void test_surface_target_values();
void test_3d_target_subpatch();
void test_2d_target_subpatch();
void test_surface_target_subpatch();
void test_cache_cleared();
};
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CachingTargetTest, "CachingTargetTest" );
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CachingTargetTest, "Unit" );
class CachedTargetCalculator : public TargetCalculator
{
private:
unsigned called_3d, called_2d, called_surf;
bool surfOrientFlag;
public:
static MsqMatrix< 3, 3 > make_3d( size_t elem, Sample sample );
static MsqMatrix< 2, 2 > make_2d( size_t elem, Sample sample );
static MsqMatrix< 3, 2 > make_surf( size_t elem, Sample sample );
CachedTargetCalculator() : called_3d( 0 ), called_2d( 0 ), called_surf( 0 ), surfOrientFlag( true ) {}
virtual bool get_3D_target( PatchData&, size_t elem, Sample sample, MsqMatrix< 3, 3 >& result, MsqError& )
{
++called_3d;
result = make_3d( elem, sample );
return true;
}
virtual bool get_2D_target( PatchData&, size_t elem, Sample sample, MsqMatrix< 2, 2 >& result, MsqError& )
{
++called_2d;
result = make_2d( elem, sample );
return true;
}
virtual bool get_surface_target( PatchData&, size_t elem, Sample sample, MsqMatrix< 3, 2 >& result, MsqError& )
{
++called_surf;
result = make_surf( elem, sample );
return true;
}
void clear()
{
called_3d = 0;
called_2d = 0;
called_surf = 0;
}
unsigned calls_3d() const
{
return called_3d;
}
unsigned calls_2d() const
{
return called_2d;
}
unsigned calls_surf() const
{
return called_surf;
}
void surf_orient( bool value )
{
surfOrientFlag = value;
}
virtual bool have_surface_orient() const
{
return surfOrientFlag;
}
};
MsqMatrix< 3, 3 > CachedTargetCalculator::make_3d( size_t elem, Sample sample )
{
double v = 100. * elem + 4 * sample.number + sample.dimension + 1;
const double values[] = { v, 0, 0, 0, v, 0, 0, 0, 1 / v };
return MsqMatrix< 3, 3 >( values );
}
MsqMatrix< 2, 2 > CachedTargetCalculator::make_2d( size_t elem, Sample sample )
{
double v = 100. * elem + 4 * sample.number + sample.dimension + 1;
const double values[] = { v, 0, 0, 1 / v };
return MsqMatrix< 2, 2 >( values );
}
MsqMatrix< 3, 2 > CachedTargetCalculator::make_surf( size_t elem, Sample sample )
{
double v = 100. * elem + 4 * sample.number + sample.dimension + 1;
const double values[] = { v, 0, 0, v, 0.5 * v, 0.5 * v };
return MsqMatrix< 3, 2 >( values );
}
void CachingTargetTest::setUp()
{
// make sure these are null so that if we fail within setUp,
// tearDown doesn't try to delete stale pointers
cached = 0;
cacher = 0;
MsqError err;
create_four_quads_patch( patch_2d, err );
CPPUNIT_ASSERT( !err );
create_qm_two_hex_patch( patch_3d, err );
CPPUNIT_ASSERT( !err );
cached = new CachedTargetCalculator();
cacher = new CachingTargetCalculator( cached );
}
unsigned CachingTargetTest::request_all_targets_3d()
{
unsigned total = 0;
MsqMatrix< 3, 3 > W;
MsqPrintError err( std::cout );
std::vector< Sample > locations;
for( size_t i = 0; i < patch_3d.num_elements(); ++i )
{
patch_3d.get_samples( i, locations, err );
ASSERT_NO_ERROR( err );
total += locations.size();
for( unsigned j = 0; j < locations.size(); ++j )
{
bool rval = cacher->get_3D_target( patch_3d, i, locations[j], W, err );
CPPUNIT_ASSERT( rval );
CPPUNIT_ASSERT( !err );
}
}
return total;
}
unsigned CachingTargetTest::request_all_targets_2d()
{
unsigned total = 0;
MsqMatrix< 2, 2 > W;
MsqPrintError err( std::cout );
std::vector< Sample > locations;
for( size_t i = 0; i < patch_2d.num_elements(); ++i )
{
patch_2d.get_samples( i, locations, err );
ASSERT_NO_ERROR( err );
total += locations.size();
for( unsigned j = 0; j < locations.size(); ++j )
{
bool rval = cacher->get_2D_target( patch_2d, i, locations[j], W, err );
CPPUNIT_ASSERT( rval );
CPPUNIT_ASSERT( !err );
}
}
return total;
}
unsigned CachingTargetTest::request_all_targets_surf()
{
unsigned total = 0;
MsqMatrix< 3, 2 > W;
MsqPrintError err( std::cout );
std::vector< Sample > locations;
for( size_t i = 0; i < patch_2d.num_elements(); ++i )
{
patch_2d.get_samples( i, locations, err );
ASSERT_NO_ERROR( err );
total += locations.size();
for( unsigned j = 0; j < locations.size(); ++j )
{
bool rval = cacher->get_surface_target( patch_2d, i, locations[j], W, err );
CPPUNIT_ASSERT( rval );
CPPUNIT_ASSERT( !err );
}
}
return total;
}
void CachingTargetTest::tearDown()
{
delete cacher;
delete cached;
}
void CachingTargetTest::test_surface_orient_flag()
{
cached->surf_orient( true );
CPPUNIT_ASSERT( cacher->have_surface_orient() );
cached->surf_orient( false );
CPPUNIT_ASSERT( !cacher->have_surface_orient() );
}
void CachingTargetTest::test_3d_targets_cached()
{
CPPUNIT_ASSERT_EQUAL( cached->calls_3d(), 0u );
CPPUNIT_ASSERT_EQUAL( cached->calls_surf(), 0u );
CPPUNIT_ASSERT_EQUAL( cached->calls_2d(), 0u );
unsigned count = request_all_targets_3d();
CPPUNIT_ASSERT_EQUAL( cached->calls_3d(), count );
CPPUNIT_ASSERT_EQUAL( cached->calls_surf(), 0u );
CPPUNIT_ASSERT_EQUAL( cached->calls_2d(), 0u );
cached->clear();
request_all_targets_3d();
CPPUNIT_ASSERT_EQUAL( cached->calls_3d(), 0u );
}
void CachingTargetTest::test_2d_targets_cached()
{
cached->surf_orient( false );
CPPUNIT_ASSERT_EQUAL( cached->calls_2d(), 0u );
CPPUNIT_ASSERT_EQUAL( cached->calls_surf(), 0u );
CPPUNIT_ASSERT_EQUAL( cached->calls_3d(), 0u );
unsigned count = request_all_targets_2d();
CPPUNIT_ASSERT_EQUAL( cached->calls_2d(), count );
CPPUNIT_ASSERT_EQUAL( cached->calls_surf(), 0u );
CPPUNIT_ASSERT_EQUAL( cached->calls_3d(), 0u );
cached->clear();
request_all_targets_2d();
CPPUNIT_ASSERT_EQUAL( cached->calls_2d(), 0u );
}
void CachingTargetTest::test_surface_targets_cached()
{
cached->surf_orient( true );
CPPUNIT_ASSERT_EQUAL( cached->calls_2d(), 0u );
CPPUNIT_ASSERT_EQUAL( cached->calls_surf(), 0u );
CPPUNIT_ASSERT_EQUAL( cached->calls_3d(), 0u );
unsigned count = request_all_targets_surf();
CPPUNIT_ASSERT_EQUAL( cached->calls_2d(), 0u );
CPPUNIT_ASSERT_EQUAL( cached->calls_surf(), count );
CPPUNIT_ASSERT_EQUAL( cached->calls_3d(), 0u );
cached->clear();
request_all_targets_surf();
CPPUNIT_ASSERT_EQUAL( cached->calls_surf(), 0u );
}
void CachingTargetTest::test_3d_target_values()
{
MsqPrintError err( std::cout );
std::vector< Sample > locations;
// evaluate all once to make sure we test the cached values
request_all_targets_3d();
// test each value
for( size_t i = 0; i < patch_3d.num_elements(); ++i )
{
patch_3d.get_samples( i, locations, err );
ASSERT_NO_ERROR( err );
for( unsigned j = 0; j < locations.size(); ++j )
{
MsqMatrix< 3, 3 > W;
bool rval = cacher->get_3D_target( patch_3d, i, locations[j], W, err );
CPPUNIT_ASSERT( rval && !err );
MsqMatrix< 3, 3 > M = CachedTargetCalculator::make_3d( i, locations[j] );
ASSERT_MATRICES_EQUAL( W, M, DBL_EPSILON );
}
}
}
void CachingTargetTest::test_2d_target_values()
{
MsqPrintError err( std::cout );
std::vector< Sample > locations;
// evaluate all once to make sure we test the cached values
cached->surf_orient( false );
request_all_targets_2d();
// test each value
for( size_t i = 0; i < patch_2d.num_elements(); ++i )
{
patch_2d.get_samples( i, locations, err );
ASSERT_NO_ERROR( err );
for( unsigned j = 0; j < locations.size(); ++j )
{
MsqMatrix< 2, 2 > W;
bool rval = cacher->get_2D_target( patch_2d, i, locations[j], W, err );
CPPUNIT_ASSERT( rval && !err );
MsqMatrix< 2, 2 > M = CachedTargetCalculator::make_2d( i, locations[j] );
ASSERT_MATRICES_EQUAL( W, M, DBL_EPSILON );
}
}
}
void CachingTargetTest::test_surface_target_values()
{
MsqPrintError err( std::cout );
std::vector< Sample > locations;
// evaluate all once to make sure we test the cached values
cached->surf_orient( true );
request_all_targets_surf();
// test each value
for( size_t i = 0; i < patch_2d.num_elements(); ++i )
{
patch_2d.get_samples( i, locations, err );
ASSERT_NO_ERROR( err );
for( unsigned j = 0; j < locations.size(); ++j )
{
MsqMatrix< 3, 2 > W;
bool rval = cacher->get_surface_target( patch_2d, i, locations[j], W, err );
CPPUNIT_ASSERT( rval && !err );
MsqMatrix< 3, 2 > M = CachedTargetCalculator::make_surf( i, locations[j] );
ASSERT_MATRICES_EQUAL( W, M, DBL_EPSILON );
}
}
}
void CachingTargetTest::test_3d_target_subpatch()
{
MsqPrintError err( std::cout );
std::vector< Sample > locations;
// cache some values on the main patch
request_all_targets_3d();
// clear the count so we know if any additional
// evalutions of the base target calculator are
// done during subpatch creation.
cached->clear();
// create a sub-patch
CPPUNIT_ASSERT( patch_3d.num_nodes() > 1 );
PatchData subpatch;
patch_3d.get_subpatch( 1, 1, subpatch, err );
CPPUNIT_ASSERT( !err );
// make sure we copied the cached values onto the subpatch
CPPUNIT_ASSERT_EQUAL( cached->calls_3d(), 0u );
// Test the values for each cached matrix on the subpatch
// NOTE: This test takes advantange of the fact that the
// "handles" in the subpatch are indices into the main patch.
// test each value
for( size_t i = 0; i < subpatch.num_elements(); ++i )
{
subpatch.get_samples( i, locations, err );
ASSERT_NO_ERROR( err );
for( unsigned j = 0; j < locations.size(); ++j )
{
MsqMatrix< 3, 3 > W;
bool rval = cacher->get_3D_target( subpatch, i, locations[j], W, err );
CPPUNIT_ASSERT( rval && !err );
Mesh::ElementHandle h = subpatch.get_element_handles_array()[i];
Mesh::ElementHandle* old_h = patch_3d.get_element_handles_array();
size_t old_idx = std::find( old_h, old_h + patch_3d.num_elements(), h ) - old_h;
CPPUNIT_ASSERT( old_idx < patch_3d.num_elements() );
MsqMatrix< 3, 3 > M = CachedTargetCalculator::make_3d( old_idx, locations[j] );
ASSERT_MATRICES_EQUAL( W, M, DBL_EPSILON );
}
}
}
void CachingTargetTest::test_2d_target_subpatch()
{
MsqPrintError err( std::cout );
std::vector< Sample > locations;
// cache some values on the main patch
cached->surf_orient( false );
request_all_targets_2d();
// clear the count so we know if any additional
// evalutions of the base target calculator are
// done during subpatch creation.
cached->clear();
// create a sub-patch
CPPUNIT_ASSERT( patch_2d.num_nodes() > 1 );
PatchData subpatch;
patch_2d.get_subpatch( 1, 1, subpatch, err );
CPPUNIT_ASSERT( !err );
// make sure we copied the cached values onto the subpatch
CPPUNIT_ASSERT_EQUAL( cached->calls_2d(), 0u );
// Test the values for each cached matrix on the subpatch
// NOTE: This test takes advantange of the fact that the
// "handles" in the subpatch are indices into the main patch.
// test each value
for( size_t i = 0; i < subpatch.num_elements(); ++i )
{
subpatch.get_samples( i, locations, err );
ASSERT_NO_ERROR( err );
for( unsigned j = 0; j < locations.size(); ++j )
{
MsqMatrix< 2, 2 > W;
bool rval = cacher->get_2D_target( subpatch, i, locations[j], W, err );
CPPUNIT_ASSERT( rval && !err );
Mesh::ElementHandle h = subpatch.get_element_handles_array()[i];
Mesh::ElementHandle* old_h = patch_2d.get_element_handles_array();
size_t old_idx = std::find( old_h, old_h + patch_2d.num_elements(), h ) - old_h;
CPPUNIT_ASSERT( old_idx < patch_2d.num_elements() );
MsqMatrix< 2, 2 > M = CachedTargetCalculator::make_2d( old_idx, locations[j] );
ASSERT_MATRICES_EQUAL( W, M, DBL_EPSILON );
}
}
}
void CachingTargetTest::test_surface_target_subpatch()
{
MsqPrintError err( std::cout );
std::vector< Sample > locations;
// cache some values on the main patch
cached->surf_orient( true );
request_all_targets_surf();
// clear the count so we know if any additional
// evalutions of the base target calculator are
// done during subpatch creation.
cached->clear();
// create a sub-patch
CPPUNIT_ASSERT( patch_2d.num_nodes() > 1 );
PatchData subpatch;
patch_2d.get_subpatch( 1, 1, subpatch, err );
CPPUNIT_ASSERT( !err );
// make sure we copied the cached values onto the subpatch
CPPUNIT_ASSERT_EQUAL( cached->calls_2d(), 0u );
// Test the values for each cached matrix on the subpatch
// NOTE: This test takes advantange of the fact that the
// "handles" in the subpatch are indices into the main patch.
// test each value
for( size_t i = 0; i < subpatch.num_elements(); ++i )
{
subpatch.get_samples( i, locations, err );
ASSERT_NO_ERROR( err );
for( unsigned j = 0; j < locations.size(); ++j )
{
MsqMatrix< 3, 2 > W;
bool rval = cacher->get_surface_target( subpatch, i, locations[j], W, err );
CPPUNIT_ASSERT( rval && !err );
Mesh::ElementHandle h = subpatch.get_element_handles_array()[i];
Mesh::ElementHandle* old_h = patch_2d.get_element_handles_array();
size_t old_idx = std::find( old_h, old_h + patch_2d.num_elements(), h ) - old_h;
CPPUNIT_ASSERT( old_idx < patch_2d.num_elements() );
MsqMatrix< 3, 2 > M = CachedTargetCalculator::make_surf( old_idx, locations[j] );
ASSERT_MATRICES_EQUAL( W, M, DBL_EPSILON );
}
}
}
void CachingTargetTest::test_cache_cleared()
{
MsqPrintError err( std::cout );
// cache some values on the main patch
request_all_targets_3d();
// clear the count so we know if any additional
// evalutions of the base target calculator are
// done.
cached->clear();
// now re-create the patch, which should result in the
// cached data being notified that the mesh has changed
create_twelve_hex_patch( patch_3d, err );
CPPUNIT_ASSERT( !err );
// now get cached values for each element
unsigned count = request_all_targets_3d();
// and check that they were all re-calculated
CPPUNIT_ASSERT_EQUAL( cached->calls_3d(), count );
}
|