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
/* *****************************************************************
    MESQUITE -- The Mesh Quality Improvement Toolkit

    Copyright 2006 Sandia National Laboratories.  Developed at the
    University of Wisconsin--Madison under SNL contract number
    624796.  The U.S. Government and the University of Wisconsin
    retian certain rights to 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 TargetReadWriteTest.cpp
 *  \brief
 *  \author Jason Kraftcheck
 */

#include "Mesquite.hpp"
#include "TargetWriter.hpp"
#include "TargetReader.hpp"
#include "WeightReader.hpp"
#include "MeshImpl.hpp"
#include "PatchData.hpp"
#include "Settings.hpp"
#include "ElemSampleQM.hpp"

#include "cppunit/extensions/HelperMacros.h"
#include "UnitUtil.hpp"

#include <iostream>

using namespace MBMesquite;

class TargetReadWriteTest : public CppUnit::TestFixture
{
  private:
    CPPUNIT_TEST_SUITE( TargetReadWriteTest );
    CPPUNIT_TEST( read_write_3D_targets );
    CPPUNIT_TEST( read_write_2D_targets );
    CPPUNIT_TEST( read_write_surface_targets );
    CPPUNIT_TEST( read_write_weights );
    CPPUNIT_TEST_SUITE_END();

    MeshImpl myMesh;    // mesh data
    PatchData myPatch;  // global patch for mesh data
    Settings linearMaps;

  public:
    void setUp();
    void tearDown();
    void read_write_3D_targets();
    void read_write_2D_targets();
    void read_write_surface_targets();
    void read_write_weights();
};

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TargetReadWriteTest, "TargetReadWriteTest" );
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TargetReadWriteTest, "Unit" );

static const char vtk_file_data[] = "# vtk DataFile Version 2.0\n"
                                    "Mesquite Mesh\n"
                                    "ASCII\n"
                                    "DATASET UNSTRUCTURED_GRID\n"
                                    "POINTS 11 float\n"
                                    "-1 -1 -1\n"
                                    " 1 -1 -1\n"
                                    " 1  1 -1\n"
                                    "-1  1 -1\n"
                                    "-1 -1  1\n"
                                    " 1 -1  1\n"
                                    " 1  1  1\n"
                                    "-1  1  1\n"
                                    " 0  0  2\n"
                                    "-2  0  2\n"
                                    "-2  0 -1\n"
                                    "CELLS 6 36\n"
                                    "3 0 1 10\n"
                                    "4 0 1 2 3\n"
                                    "4 4 5 8 9\n"
                                    "5 4 5 6 7 8\n"
                                    "6 4 5 9 1 0 10\n"
                                    "8 0 1 2 3 4 5 6 7\n"
                                    "CELL_TYPES 6\n"
                                    "5 9 10 14 13 12\n"
                                    "\n";

void TargetReadWriteTest::setUp()
{
    // create input file
    const char filename[] = "target_reader_test.vtk";
    FILE* file            = fopen( filename, "w" );
    CPPUNIT_ASSERT( file );
    int rval = fputs( vtk_file_data, file );
    fclose( file );
    CPPUNIT_ASSERT( rval != EOF );

    // read input file
    MsqError err;
    myMesh.read_vtk( filename, err );
    remove( filename );
    if( err ) std::cout << err << std::endl;
    CPPUNIT_ASSERT( !err );

    // Construct global patch
    std::vector< Mesh::ElementHandle > elems;
    std::vector< Mesh::VertexHandle > verts;
    myMesh.get_all_elements( elems, err );
    CPPUNIT_ASSERT( !err );
    myMesh.get_all_vertices( verts, err );
    CPPUNIT_ASSERT( !err );
    myPatch.set_mesh( &myMesh );
    myPatch.attach_settings( &linearMaps );
    myPatch.set_mesh_entities( elems, verts, err );
    CPPUNIT_ASSERT( !err );
}

void TargetReadWriteTest::tearDown()
{
    myMesh.clear();
}

class FakeTargetCalc : public TargetCalculator, public WeightCalculator
{
    bool surfOrient;

  public:
    FakeTargetCalc( bool surface_orient = true ) : surfOrient( surface_orient ) {}

    ~FakeTargetCalc() {}

    bool get_3D_target( PatchData& pd, size_t element, Sample sample, MsqMatrix< 3, 3 >& W_out, MsqError& err );

    bool get_2D_target( PatchData& pd, size_t element, Sample sample, MsqMatrix< 2, 2 >& W_out, MsqError& err );

    bool get_surface_target( PatchData& pd, size_t element, Sample sample, MsqMatrix< 3, 2 >& W_out, MsqError& err );

    double get_weight( PatchData& pd, size_t element, Sample sample, MsqError& err );

    bool have_surface_orient() const
    {
        return surfOrient;
    }

    unsigned long make_value( Mesh::ElementHandle elem, Sample sample, unsigned idx );
};

bool FakeTargetCalc::get_3D_target( PatchData& pd, size_t elem, Sample sample, MsqMatrix< 3, 3 >& W_out, MsqError& )<--- Parameter 'W_out' can be declared with const
{
    CPPUNIT_ASSERT_EQUAL( 3u, TopologyInfo::dimension( pd.element_by_index( elem ).get_element_type() ) );
    unsigned i, j;
    for( i = 0; i < 3; ++i )
    {
        for( j = 0; j < i; ++j )
            W_out( i, j ) = 0.0;
        for( j = i; j < 3; ++j )
            W_out( i, j ) = make_value( pd.get_element_handles_array()[elem], sample, 3 * i + j + 1 );
    }
    return true;
}

bool FakeTargetCalc::get_surface_target( PatchData& pd,
                                         size_t elem,
                                         Sample sample,
                                         MsqMatrix< 3, 2 >& W_out,<--- Parameter 'W_out' can be declared with const
                                         MsqError& )
{
    CPPUNIT_ASSERT_EQUAL( 2u, TopologyInfo::dimension( pd.element_by_index( elem ).get_element_type() ) );
    for( unsigned i = 0; i < 3; ++i )
        for( unsigned j = 0; j < 2; ++j )
            W_out( i, j ) = make_value( pd.get_element_handles_array()[elem], sample, 2 * i + j );
    return true;
}

bool FakeTargetCalc::get_2D_target( PatchData& pd, size_t elem, Sample sample, MsqMatrix< 2, 2 >& W_out, MsqError& )<--- Parameter 'W_out' can be declared with const
{
    CPPUNIT_ASSERT_EQUAL( 2u, TopologyInfo::dimension( pd.element_by_index( elem ).get_element_type() ) );
    for( unsigned i = 0; i < 2; ++i )
        for( unsigned j = 0; j < 2; ++j )
            W_out( i, j ) = make_value( pd.get_element_handles_array()[elem], sample, ( 2 - i ) * ( 2 - j ) );
    return true;
}

double FakeTargetCalc::get_weight( PatchData& pd, size_t elem, Sample sample, MsqError& )
{
    return make_value( pd.get_element_handles_array()[elem], sample, 0 );
}

unsigned long FakeTargetCalc::make_value( Mesh::ElementHandle elem, Sample sample, unsigned idx )
{
    const unsigned index_bits = 4;
    CPPUNIT_ASSERT( idx < ( 1 << index_bits ) );
    unsigned long result = (unsigned long)elem;
    result               = ( result << Sample::SIDE_DIMENSION_BITS ) | sample.dimension;
    result               = ( result << Sample::SIDE_NUMBER_BITS ) | sample.number;
    result               = ( result << index_bits ) | idx;
    return result;
}

void TargetReadWriteTest::read_write_3D_targets()
{
    const bool oriented = true;  // doesn't matter which value for 3D

    MsqPrintError err( std::cout );
    FakeTargetCalc tc( oriented );

    // Write the targets
    TargetWriter writer( &tc );
    MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( &myMesh, 0 );
    writer.loop_over_mesh( &mesh_and_domain, &linearMaps, err );
    CPPUNIT_ASSERT( !err );

    // Compare all target matrices
    bool checked_something = false;  // make sure mesh actually contains volume elements
    TargetReader reader( oriented );
    for( size_t i = 0; i < myPatch.num_elements(); ++i )
    {
        const unsigned d = TopologyInfo::dimension( myPatch.element_by_index( i ).get_element_type() );
        if( d != 3 ) continue;

        checked_something = true;
        std::vector< Sample > samples;
        myPatch.get_samples( i, samples, err );
        ASSERT_NO_ERROR( err );
        for( size_t j = 0; j < samples.size(); ++j )
        {
            MsqMatrix< 3, 3 > expected, read;
            tc.get_3D_target( myPatch, i, samples[j], expected, err );
            CPPUNIT_ASSERT( !err );
            reader.get_3D_target( myPatch, i, samples[j], read, err );
            CPPUNIT_ASSERT( !err );
            ASSERT_MATRICES_EQUAL( expected, read, 1e-12 );
        }
    }

    CPPUNIT_ASSERT( checked_something );
}

void TargetReadWriteTest::read_write_surface_targets()
{
    const bool oriented = true;

    MsqPrintError err( std::cout );
    FakeTargetCalc tc( oriented );

    // Write the targets
    TargetWriter writer( &tc );
    MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( &myMesh, 0 );
    writer.loop_over_mesh( &mesh_and_domain, &linearMaps, err );
    CPPUNIT_ASSERT( !err );

    // Compare all target matrices
    bool checked_something = false;  // make sure mesh actually contains surface elements
    TargetReader reader( oriented );
    for( size_t i = 0; i < myPatch.num_elements(); ++i )
    {
        const unsigned d = TopologyInfo::dimension( myPatch.element_by_index( i ).get_element_type() );
        if( d != 2 ) continue;

        checked_something = true;
        std::vector< Sample > samples;
        myPatch.get_samples( i, samples, err );
        ASSERT_NO_ERROR( err );
        for( size_t j = 0; j < samples.size(); ++j )
        {
            MsqMatrix< 3, 2 > expected, read;
            tc.get_surface_target( myPatch, i, samples[j], expected, err );
            CPPUNIT_ASSERT( !err );
            reader.get_surface_target( myPatch, i, samples[j], read, err );
            CPPUNIT_ASSERT( !err );
            ASSERT_MATRICES_EQUAL( expected, read, 1e-6 );
        }
    }

    CPPUNIT_ASSERT( checked_something );
}

void TargetReadWriteTest::read_write_2D_targets()
{
    const bool oriented = false;

    MsqPrintError err( std::cout );
    FakeTargetCalc tc( oriented );

    // Write the targets
    TargetWriter writer( &tc );
    MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( &myMesh, 0 );
    writer.loop_over_mesh( &mesh_and_domain, &linearMaps, err );
    CPPUNIT_ASSERT( !err );

    // Compare all target matrices
    bool checked_something = false;  // make sure mesh actually contains surface elements
    TargetReader reader( oriented );
    for( size_t i = 0; i < myPatch.num_elements(); ++i )
    {
        const unsigned d = TopologyInfo::dimension( myPatch.element_by_index( i ).get_element_type() );
        if( d != 2 ) continue;

        checked_something = true;
        std::vector< Sample > samples;
        myPatch.get_samples( i, samples, err );
        ASSERT_NO_ERROR( err );
        for( size_t j = 0; j < samples.size(); ++j )
        {
            MsqMatrix< 2, 2 > expected, read;
            tc.get_2D_target( myPatch, i, samples[j], expected, err );
            CPPUNIT_ASSERT( !err );
            reader.get_2D_target( myPatch, i, samples[j], read, err );
            CPPUNIT_ASSERT( !err );
            ASSERT_MATRICES_EQUAL( expected, read, 1e-6 );
        }
    }

    CPPUNIT_ASSERT( checked_something );
}

void TargetReadWriteTest::read_write_weights()
{
    MsqPrintError err( std::cout );
    FakeTargetCalc tc;

    // Write the targets
    TargetWriter writer( 0, &tc );
    MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( &myMesh, 0 );
    writer.loop_over_mesh( &mesh_and_domain, &linearMaps, err );
    CPPUNIT_ASSERT( !err );

    // Compare all target matrices
    WeightReader reader;
    for( size_t i = 0; i < myPatch.num_elements(); ++i )
    {
        std::vector< Sample > samples;
        myPatch.get_samples( i, samples, err );
        ASSERT_NO_ERROR( err );
        for( size_t j = 0; j < samples.size(); ++j )
        {
            double expected = tc.get_weight( myPatch, i, samples[j], err );
            CPPUNIT_ASSERT( !err );
            double read = reader.get_weight( myPatch, i, samples[j], err );
            CPPUNIT_ASSERT( !err );
            CPPUNIT_ASSERT_DOUBLES_EQUAL( expected, read, 1e-12 );
        }
    }
}