MOAB: Mesh Oriented datABase
(version 5.4.1)
|
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) [email protected] 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, 00176 size_t elem, 00177 Sample sample, 00178 MsqMatrix< 3, 2 >& W_out, 00179 MsqError& ) 00180 { 00181 CPPUNIT_ASSERT_EQUAL( 2u, TopologyInfo::dimension( pd.element_by_index( elem ).get_element_type() ) ); 00182 for( unsigned i = 0; i < 3; ++i ) 00183 for( unsigned j = 0; j < 2; ++j ) 00184 W_out( i, j ) = make_value( pd.get_element_handles_array()[elem], sample, 2 * i + j ); 00185 return true; 00186 } 00187 00188 bool FakeTargetCalc::get_2D_target( PatchData& pd, size_t elem, Sample sample, MsqMatrix< 2, 2 >& W_out, MsqError& ) 00189 { 00190 CPPUNIT_ASSERT_EQUAL( 2u, TopologyInfo::dimension( pd.element_by_index( elem ).get_element_type() ) ); 00191 for( unsigned i = 0; i < 2; ++i ) 00192 for( unsigned j = 0; j < 2; ++j ) 00193 W_out( i, j ) = make_value( pd.get_element_handles_array()[elem], sample, ( 2 - i ) * ( 2 - j ) ); 00194 return true; 00195 } 00196 00197 double FakeTargetCalc::get_weight( PatchData& pd, size_t elem, Sample sample, MsqError& ) 00198 { 00199 return make_value( pd.get_element_handles_array()[elem], sample, 0 ); 00200 } 00201 00202 unsigned long FakeTargetCalc::make_value( Mesh::ElementHandle elem, Sample sample, unsigned idx ) 00203 { 00204 const unsigned index_bits = 4; 00205 CPPUNIT_ASSERT( idx < ( 1 << index_bits ) ); 00206 unsigned long result = (unsigned long)elem; 00207 result = ( result << Sample::SIDE_DIMENSION_BITS ) | sample.dimension; 00208 result = ( result << Sample::SIDE_NUMBER_BITS ) | sample.number; 00209 result = ( result << index_bits ) | idx; 00210 return result; 00211 } 00212 00213 void TargetReadWriteTest::read_write_3D_targets() 00214 { 00215 const bool oriented = true; // doesn't matter which value for 3D 00216 00217 MsqPrintError err( std::cout ); 00218 FakeTargetCalc tc( oriented ); 00219 00220 // Write the targets 00221 TargetWriter writer( &tc ); 00222 MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( &myMesh, 0 ); 00223 writer.loop_over_mesh( &mesh_and_domain, &linearMaps, err ); 00224 CPPUNIT_ASSERT( !err ); 00225 00226 // Compare all target matrices 00227 bool checked_something = false; // make sure mesh actually contains volume elements 00228 TargetReader reader( oriented ); 00229 for( size_t i = 0; i < myPatch.num_elements(); ++i ) 00230 { 00231 const unsigned d = TopologyInfo::dimension( myPatch.element_by_index( i ).get_element_type() ); 00232 if( d != 3 ) continue; 00233 00234 checked_something = true; 00235 std::vector< Sample > samples; 00236 myPatch.get_samples( i, samples, err ); 00237 ASSERT_NO_ERROR( err ); 00238 for( size_t j = 0; j < samples.size(); ++j ) 00239 { 00240 MsqMatrix< 3, 3 > expected, read; 00241 tc.get_3D_target( myPatch, i, samples[j], expected, err ); 00242 CPPUNIT_ASSERT( !err ); 00243 reader.get_3D_target( myPatch, i, samples[j], read, err ); 00244 CPPUNIT_ASSERT( !err ); 00245 ASSERT_MATRICES_EQUAL( expected, read, 1e-12 ); 00246 } 00247 } 00248 00249 CPPUNIT_ASSERT( checked_something ); 00250 } 00251 00252 void TargetReadWriteTest::read_write_surface_targets() 00253 { 00254 const bool oriented = true; 00255 00256 MsqPrintError err( std::cout ); 00257 FakeTargetCalc tc( oriented ); 00258 00259 // Write the targets 00260 TargetWriter writer( &tc ); 00261 MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( &myMesh, 0 ); 00262 writer.loop_over_mesh( &mesh_and_domain, &linearMaps, err ); 00263 CPPUNIT_ASSERT( !err ); 00264 00265 // Compare all target matrices 00266 bool checked_something = false; // make sure mesh actually contains surface elements 00267 TargetReader reader( oriented ); 00268 for( size_t i = 0; i < myPatch.num_elements(); ++i ) 00269 { 00270 const unsigned d = TopologyInfo::dimension( myPatch.element_by_index( i ).get_element_type() ); 00271 if( d != 2 ) continue; 00272 00273 checked_something = true; 00274 std::vector< Sample > samples; 00275 myPatch.get_samples( i, samples, err ); 00276 ASSERT_NO_ERROR( err ); 00277 for( size_t j = 0; j < samples.size(); ++j ) 00278 { 00279 MsqMatrix< 3, 2 > expected, read; 00280 tc.get_surface_target( myPatch, i, samples[j], expected, err ); 00281 CPPUNIT_ASSERT( !err ); 00282 reader.get_surface_target( myPatch, i, samples[j], read, err ); 00283 CPPUNIT_ASSERT( !err ); 00284 ASSERT_MATRICES_EQUAL( expected, read, 1e-6 ); 00285 } 00286 } 00287 00288 CPPUNIT_ASSERT( checked_something ); 00289 } 00290 00291 void TargetReadWriteTest::read_write_2D_targets() 00292 { 00293 const bool oriented = false; 00294 00295 MsqPrintError err( std::cout ); 00296 FakeTargetCalc tc( oriented ); 00297 00298 // Write the targets 00299 TargetWriter writer( &tc ); 00300 MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( &myMesh, 0 ); 00301 writer.loop_over_mesh( &mesh_and_domain, &linearMaps, err ); 00302 CPPUNIT_ASSERT( !err ); 00303 00304 // Compare all target matrices 00305 bool checked_something = false; // make sure mesh actually contains surface elements 00306 TargetReader reader( oriented ); 00307 for( size_t i = 0; i < myPatch.num_elements(); ++i ) 00308 { 00309 const unsigned d = TopologyInfo::dimension( myPatch.element_by_index( i ).get_element_type() ); 00310 if( d != 2 ) continue; 00311 00312 checked_something = true; 00313 std::vector< Sample > samples; 00314 myPatch.get_samples( i, samples, err ); 00315 ASSERT_NO_ERROR( err ); 00316 for( size_t j = 0; j < samples.size(); ++j ) 00317 { 00318 MsqMatrix< 2, 2 > expected, read; 00319 tc.get_2D_target( myPatch, i, samples[j], expected, err ); 00320 CPPUNIT_ASSERT( !err ); 00321 reader.get_2D_target( myPatch, i, samples[j], read, err ); 00322 CPPUNIT_ASSERT( !err ); 00323 ASSERT_MATRICES_EQUAL( expected, read, 1e-6 ); 00324 } 00325 } 00326 00327 CPPUNIT_ASSERT( checked_something ); 00328 } 00329 00330 void TargetReadWriteTest::read_write_weights() 00331 { 00332 MsqPrintError err( std::cout ); 00333 FakeTargetCalc tc; 00334 00335 // Write the targets 00336 TargetWriter writer( 0, &tc ); 00337 MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( &myMesh, 0 ); 00338 writer.loop_over_mesh( &mesh_and_domain, &linearMaps, err ); 00339 CPPUNIT_ASSERT( !err ); 00340 00341 // Compare all target matrices 00342 WeightReader reader; 00343 for( size_t i = 0; i < myPatch.num_elements(); ++i ) 00344 { 00345 std::vector< Sample > samples; 00346 myPatch.get_samples( i, samples, err ); 00347 ASSERT_NO_ERROR( err ); 00348 for( size_t j = 0; j < samples.size(); ++j ) 00349 { 00350 double expected = tc.get_weight( myPatch, i, samples[j], err ); 00351 CPPUNIT_ASSERT( !err ); 00352 double read = reader.get_weight( myPatch, i, samples[j], err ); 00353 CPPUNIT_ASSERT( !err ); 00354 CPPUNIT_ASSERT_DOUBLES_EQUAL( expected, read, 1e-12 ); 00355 } 00356 } 00357 }