MOAB: Mesh Oriented datABase  (version 5.2.1)
TargetReadWriteTest.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2006 Sandia National Laboratories.  Developed at the
00005     University of Wisconsin--Madison under SNL contract number
00006     624796.  The U.S. Government and the University of Wisconsin
00007     retian certain rights to this software.
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Lesser General Public
00011     License as published by the Free Software Foundation; either
00012     version 2.1 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     (lgpl.txt) along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023     (2006) kraftche@cae.wisc.edu
00024 
00025   ***************************************************************** */
00026 
00027 /** \file TargetReadWriteTest.cpp
00028  *  \brief
00029  *  \author Jason Kraftcheck
00030  */
00031 
00032 #include "Mesquite.hpp"
00033 #include "TargetWriter.hpp"
00034 #include "TargetReader.hpp"
00035 #include "WeightReader.hpp"
00036 #include "MeshImpl.hpp"
00037 #include "PatchData.hpp"
00038 #include "Settings.hpp"
00039 #include "ElemSampleQM.hpp"
00040 
00041 #include "cppunit/extensions/HelperMacros.h"
00042 #include "UnitUtil.hpp"
00043 
00044 #include <iostream>
00045 
00046 using namespace MBMesquite;
00047 
00048 class TargetReadWriteTest : public CppUnit::TestFixture
00049 {
00050   private:
00051     CPPUNIT_TEST_SUITE( TargetReadWriteTest );
00052     CPPUNIT_TEST( read_write_3D_targets );
00053     CPPUNIT_TEST( read_write_2D_targets );
00054     CPPUNIT_TEST( read_write_surface_targets );
00055     CPPUNIT_TEST( read_write_weights );
00056     CPPUNIT_TEST_SUITE_END();
00057 
00058     MeshImpl myMesh;    // mesh data
00059     PatchData myPatch;  // global patch for mesh data
00060     Settings linearMaps;
00061 
00062   public:
00063     void setUp();
00064     void tearDown();
00065     void read_write_3D_targets();
00066     void read_write_2D_targets();
00067     void read_write_surface_targets();
00068     void read_write_weights();
00069 };
00070 
00071 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TargetReadWriteTest, "TargetReadWriteTest" );
00072 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TargetReadWriteTest, "Unit" );
00073 
00074 static const char vtk_file_data[] = "# vtk DataFile Version 2.0\n"
00075                                     "Mesquite Mesh\n"
00076                                     "ASCII\n"
00077                                     "DATASET UNSTRUCTURED_GRID\n"
00078                                     "POINTS 11 float\n"
00079                                     "-1 -1 -1\n"
00080                                     " 1 -1 -1\n"
00081                                     " 1  1 -1\n"
00082                                     "-1  1 -1\n"
00083                                     "-1 -1  1\n"
00084                                     " 1 -1  1\n"
00085                                     " 1  1  1\n"
00086                                     "-1  1  1\n"
00087                                     " 0  0  2\n"
00088                                     "-2  0  2\n"
00089                                     "-2  0 -1\n"
00090                                     "CELLS 6 36\n"
00091                                     "3 0 1 10\n"
00092                                     "4 0 1 2 3\n"
00093                                     "4 4 5 8 9\n"
00094                                     "5 4 5 6 7 8\n"
00095                                     "6 4 5 9 1 0 10\n"
00096                                     "8 0 1 2 3 4 5 6 7\n"
00097                                     "CELL_TYPES 6\n"
00098                                     "5 9 10 14 13 12\n"
00099                                     "\n";
00100 
00101 void TargetReadWriteTest::setUp()
00102 {
00103     // create input file
00104     const char filename[] = "target_reader_test.vtk";
00105     FILE* file            = fopen( filename, "w" );
00106     CPPUNIT_ASSERT( file );
00107     int rval = fputs( vtk_file_data, file );
00108     fclose( file );
00109     CPPUNIT_ASSERT( rval != EOF );
00110 
00111     // read input file
00112     MsqError err;
00113     myMesh.read_vtk( filename, err );
00114     remove( filename );
00115     if( err ) std::cout << err << std::endl;
00116     CPPUNIT_ASSERT( !err );
00117 
00118     // Construct global patch
00119     std::vector< Mesh::ElementHandle > elems;
00120     std::vector< Mesh::VertexHandle > verts;
00121     myMesh.get_all_elements( elems, err );
00122     CPPUNIT_ASSERT( !err );
00123     myMesh.get_all_vertices( verts, err );
00124     CPPUNIT_ASSERT( !err );
00125     myPatch.set_mesh( &myMesh );
00126     myPatch.attach_settings( &linearMaps );
00127     myPatch.set_mesh_entities( elems, verts, err );
00128     CPPUNIT_ASSERT( !err );
00129 }
00130 
00131 void TargetReadWriteTest::tearDown()
00132 {
00133     myMesh.clear();
00134 }
00135 
00136 class FakeTargetCalc : public TargetCalculator, public WeightCalculator
00137 {
00138     bool surfOrient;
00139 
00140   public:
00141     FakeTargetCalc( bool surface_orient = true ) : surfOrient( surface_orient ) {}
00142 
00143     ~FakeTargetCalc() {}
00144 
00145     bool get_3D_target( PatchData& pd, size_t element, Sample sample, MsqMatrix< 3, 3 >& W_out, MsqError& err );
00146 
00147     bool get_2D_target( PatchData& pd, size_t element, Sample sample, MsqMatrix< 2, 2 >& W_out, MsqError& err );
00148 
00149     bool get_surface_target( PatchData& pd, size_t element, Sample sample, MsqMatrix< 3, 2 >& W_out, MsqError& err );
00150 
00151     double get_weight( PatchData& pd, size_t element, Sample sample, MsqError& err );
00152 
00153     bool have_surface_orient() const
00154     {
00155         return surfOrient;
00156     }
00157 
00158     unsigned long make_value( Mesh::ElementHandle elem, Sample sample, unsigned idx );
00159 };
00160 
00161 bool FakeTargetCalc::get_3D_target( PatchData& pd, size_t elem, Sample sample, MsqMatrix< 3, 3 >& W_out, MsqError& )
00162 {
00163     CPPUNIT_ASSERT_EQUAL( 3u, TopologyInfo::dimension( pd.element_by_index( elem ).get_element_type() ) );
00164     unsigned i, j;
00165     for( i = 0; i < 3; ++i )
00166     {
00167         for( j = 0; j < i; ++j )
00168             W_out( i, j ) = 0.0;
00169         for( j = i; j < 3; ++j )
00170             W_out( i, j ) = make_value( pd.get_element_handles_array()[elem], sample, 3 * i + j + 1 );
00171     }
00172     return true;
00173 }
00174 
00175 bool FakeTargetCalc::get_surface_target( PatchData& pd, size_t elem, Sample sample, MsqMatrix< 3, 2 >& W_out,
00176                                          MsqError& )
00177 {
00178     CPPUNIT_ASSERT_EQUAL( 2u, TopologyInfo::dimension( pd.element_by_index( elem ).get_element_type() ) );
00179     for( unsigned i = 0; i < 3; ++i )
00180         for( unsigned j = 0; j < 2; ++j )
00181             W_out( i, j ) = make_value( pd.get_element_handles_array()[elem], sample, 2 * i + j );
00182     return true;
00183 }
00184 
00185 bool FakeTargetCalc::get_2D_target( PatchData& pd, size_t elem, Sample sample, MsqMatrix< 2, 2 >& W_out, MsqError& )
00186 {
00187     CPPUNIT_ASSERT_EQUAL( 2u, TopologyInfo::dimension( pd.element_by_index( elem ).get_element_type() ) );
00188     for( unsigned i = 0; i < 2; ++i )
00189         for( unsigned j = 0; j < 2; ++j )
00190             W_out( i, j ) = make_value( pd.get_element_handles_array()[elem], sample, ( 2 - i ) * ( 2 - j ) );
00191     return true;
00192 }
00193 
00194 double FakeTargetCalc::get_weight( PatchData& pd, size_t elem, Sample sample, MsqError& )
00195 {
00196     return make_value( pd.get_element_handles_array()[elem], sample, 0 );
00197 }
00198 
00199 unsigned long FakeTargetCalc::make_value( Mesh::ElementHandle elem, Sample sample, unsigned idx )
00200 {
00201     const unsigned index_bits = 4;
00202     CPPUNIT_ASSERT( idx < ( 1 << index_bits ) );
00203     unsigned long result = (unsigned long)elem;
00204     result               = ( result << Sample::SIDE_DIMENSION_BITS ) | sample.dimension;
00205     result               = ( result << Sample::SIDE_NUMBER_BITS ) | sample.number;
00206     result               = ( result << index_bits ) | idx;
00207     return result;
00208 }
00209 
00210 void TargetReadWriteTest::read_write_3D_targets()
00211 {
00212     const bool oriented = true;  // doesn't matter which value for 3D
00213 
00214     MsqPrintError err( std::cout );
00215     FakeTargetCalc tc( oriented );
00216 
00217     // Write the targets
00218     TargetWriter writer( &tc );
00219     MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( &myMesh, 0 );
00220     writer.loop_over_mesh( &mesh_and_domain, &linearMaps, err );
00221     CPPUNIT_ASSERT( !err );
00222 
00223     // Compare all target matrices
00224     bool checked_something = false;  // make sure mesh actually contains volume elements
00225     TargetReader reader( oriented );
00226     for( size_t i = 0; i < myPatch.num_elements(); ++i )
00227     {
00228         const unsigned d = TopologyInfo::dimension( myPatch.element_by_index( i ).get_element_type() );
00229         if( d != 3 ) continue;
00230 
00231         checked_something = true;
00232         std::vector< Sample > samples;
00233         myPatch.get_samples( i, samples, err );
00234         ASSERT_NO_ERROR( err );
00235         for( size_t j = 0; j < samples.size(); ++j )
00236         {
00237             MsqMatrix< 3, 3 > expected, read;
00238             tc.get_3D_target( myPatch, i, samples[j], expected, err );
00239             CPPUNIT_ASSERT( !err );
00240             reader.get_3D_target( myPatch, i, samples[j], read, err );
00241             CPPUNIT_ASSERT( !err );
00242             ASSERT_MATRICES_EQUAL( expected, read, 1e-12 );
00243         }
00244     }
00245 
00246     CPPUNIT_ASSERT( checked_something );
00247 }
00248 
00249 void TargetReadWriteTest::read_write_surface_targets()
00250 {
00251     const bool oriented = true;
00252 
00253     MsqPrintError err( std::cout );
00254     FakeTargetCalc tc( oriented );
00255 
00256     // Write the targets
00257     TargetWriter writer( &tc );
00258     MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( &myMesh, 0 );
00259     writer.loop_over_mesh( &mesh_and_domain, &linearMaps, err );
00260     CPPUNIT_ASSERT( !err );
00261 
00262     // Compare all target matrices
00263     bool checked_something = false;  // make sure mesh actually contains surface elements
00264     TargetReader reader( oriented );
00265     for( size_t i = 0; i < myPatch.num_elements(); ++i )
00266     {
00267         const unsigned d = TopologyInfo::dimension( myPatch.element_by_index( i ).get_element_type() );
00268         if( d != 2 ) continue;
00269 
00270         checked_something = true;
00271         std::vector< Sample > samples;
00272         myPatch.get_samples( i, samples, err );
00273         ASSERT_NO_ERROR( err );
00274         for( size_t j = 0; j < samples.size(); ++j )
00275         {
00276             MsqMatrix< 3, 2 > expected, read;
00277             tc.get_surface_target( myPatch, i, samples[j], expected, err );
00278             CPPUNIT_ASSERT( !err );
00279             reader.get_surface_target( myPatch, i, samples[j], read, err );
00280             CPPUNIT_ASSERT( !err );
00281             ASSERT_MATRICES_EQUAL( expected, read, 1e-6 );
00282         }
00283     }
00284 
00285     CPPUNIT_ASSERT( checked_something );
00286 }
00287 
00288 void TargetReadWriteTest::read_write_2D_targets()
00289 {
00290     const bool oriented = false;
00291 
00292     MsqPrintError err( std::cout );
00293     FakeTargetCalc tc( oriented );
00294 
00295     // Write the targets
00296     TargetWriter writer( &tc );
00297     MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( &myMesh, 0 );
00298     writer.loop_over_mesh( &mesh_and_domain, &linearMaps, err );
00299     CPPUNIT_ASSERT( !err );
00300 
00301     // Compare all target matrices
00302     bool checked_something = false;  // make sure mesh actually contains surface elements
00303     TargetReader reader( oriented );
00304     for( size_t i = 0; i < myPatch.num_elements(); ++i )
00305     {
00306         const unsigned d = TopologyInfo::dimension( myPatch.element_by_index( i ).get_element_type() );
00307         if( d != 2 ) continue;
00308 
00309         checked_something = true;
00310         std::vector< Sample > samples;
00311         myPatch.get_samples( i, samples, err );
00312         ASSERT_NO_ERROR( err );
00313         for( size_t j = 0; j < samples.size(); ++j )
00314         {
00315             MsqMatrix< 2, 2 > expected, read;
00316             tc.get_2D_target( myPatch, i, samples[j], expected, err );
00317             CPPUNIT_ASSERT( !err );
00318             reader.get_2D_target( myPatch, i, samples[j], read, err );
00319             CPPUNIT_ASSERT( !err );
00320             ASSERT_MATRICES_EQUAL( expected, read, 1e-6 );
00321         }
00322     }
00323 
00324     CPPUNIT_ASSERT( checked_something );
00325 }
00326 
00327 void TargetReadWriteTest::read_write_weights()
00328 {
00329     MsqPrintError err( std::cout );
00330     FakeTargetCalc tc;
00331 
00332     // Write the targets
00333     TargetWriter writer( 0, &tc );
00334     MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( &myMesh, 0 );
00335     writer.loop_over_mesh( &mesh_and_domain, &linearMaps, err );
00336     CPPUNIT_ASSERT( !err );
00337 
00338     // Compare all target matrices
00339     WeightReader reader;
00340     for( size_t i = 0; i < myPatch.num_elements(); ++i )
00341     {
00342         std::vector< Sample > samples;
00343         myPatch.get_samples( i, samples, err );
00344         ASSERT_NO_ERROR( err );
00345         for( size_t j = 0; j < samples.size(); ++j )
00346         {
00347             double expected = tc.get_weight( myPatch, i, samples[j], err );
00348             CPPUNIT_ASSERT( !err );
00349             double read = reader.get_weight( myPatch, i, samples[j], err );
00350             CPPUNIT_ASSERT( !err );
00351             CPPUNIT_ASSERT_DOUBLES_EQUAL( expected, read, 1e-12 );
00352         }
00353     }
00354 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines