MOAB: Mesh Oriented datABase  (version 5.2.1)
BCDTest.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     retain 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 BCDTest.cpp
00028  *  \brief
00029  *  \author Jason Kraftcheck
00030  */
00031 
00032 #include "Mesquite.hpp"
00033 #include "LPtoPTemplate.hpp"
00034 #include "PMeanPTemplate.hpp"
00035 #include "IdealWeightInverseMeanRatio.hpp"
00036 #include "InstructionQueue.hpp"
00037 #include "FeasibleNewton.hpp"
00038 #include "ConjugateGradient.hpp"
00039 #include "MeshImpl.hpp"
00040 #include "QualityAssessor.hpp"
00041 #include "TerminationCriterion.hpp"
00042 
00043 #include "UnitUtil.hpp"
00044 #include "meshfiles.h"
00045 #include "cppunit/extensions/HelperMacros.h"
00046 #include <iostream>
00047 #include <vector>
00048 #include <string>
00049 
00050 using namespace MBMesquite;
00051 using namespace std;
00052 
00053 const char HEX_MESH[] = MESH_FILES_DIR "3D/vtk/hexes/untangled/1000hex-block-internal-bias.vtk";
00054 const char TET_MESH[] = MESH_FILES_DIR "3D/vtk/tets/untangled/tire.vtk";
00055 typedef FeasibleNewton SolverType;
00056 
00057 class BCDTest : public CppUnit::TestFixture
00058 {
00059   private:
00060     CPPUNIT_TEST_SUITE( BCDTest );
00061 
00062     CPPUNIT_TEST( test_lp_to_p_hex );
00063     CPPUNIT_TEST( test_p_mean_p_hex );
00064     CPPUNIT_TEST( test_lp_to_p_tet );
00065     CPPUNIT_TEST( test_p_mean_p_tet );
00066 
00067     CPPUNIT_TEST_SUITE_END();
00068 
00069     IdealWeightInverseMeanRatio mMetric;
00070 
00071     void compare_bcd( ObjectiveFunction* of, string name, const char* file );
00072 
00073   public:
00074     void test_lp_to_p_hex()
00075     {
00076         LPtoPTemplate OF( 1, &mMetric );
00077         compare_bcd( &OF, "LPtoP-hex", HEX_MESH );
00078     }
00079 
00080     void test_p_mean_p_hex()
00081     {
00082         PMeanPTemplate OF( 1.0, &mMetric );
00083         compare_bcd( &OF, "PMeanP-hex", HEX_MESH );
00084     }
00085 
00086     void test_lp_to_p_tet()
00087     {
00088         LPtoPTemplate OF( 1, &mMetric );
00089         compare_bcd( &OF, "LPtoP-tet", TET_MESH );
00090     }
00091 
00092     void test_p_mean_p_tet()
00093     {
00094         PMeanPTemplate OF( 1.0, &mMetric );
00095         compare_bcd( &OF, "PMeanP-tet", TET_MESH );
00096     }
00097 };
00098 
00099 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( BCDTest, "BCDTest" );
00100 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( BCDTest, "Regression" );
00101 
00102 void BCDTest::compare_bcd( ObjectiveFunction* OF, string name, const char* mesh_file )
00103 {
00104     MsqPrintError err( cout );
00105     size_t i;
00106     vector< MsqVertex > initial_coords, global_coords, bcd_coords;
00107     vector< Mesh::VertexHandle > vertex_list;
00108 
00109     // set up a smoother
00110     TerminationCriterion iterations, vertex_movement;
00111     iterations.add_iteration_limit( 2 );
00112     vertex_movement.add_absolute_vertex_movement( 1e-3 );
00113 
00114     SolverType global_solver( OF );
00115     SolverType bcd_solver( OF );
00116     global_solver.use_global_patch();
00117     bcd_solver.use_element_on_vertex_patch();
00118     bcd_solver.do_block_coordinate_descent_optimization();
00119     global_solver.set_inner_termination_criterion( &vertex_movement );
00120     bcd_solver.set_inner_termination_criterion( &iterations );
00121     bcd_solver.set_outer_termination_criterion( &vertex_movement );
00122 
00123     QualityAssessor qa;
00124     qa.add_quality_assessment( &mMetric );
00125 
00126     InstructionQueue global_q, bcd_q;
00127     global_q.add_quality_assessor( &qa, err );
00128     global_q.set_master_quality_improver( &global_solver, err );
00129     global_q.add_quality_assessor( &qa, err );
00130     bcd_q.set_master_quality_improver( &bcd_solver, err );
00131     bcd_q.add_quality_assessor( &qa, err );
00132 
00133     // read mesh
00134     MeshImpl mesh;
00135     mesh.read_vtk( mesh_file, err );
00136     ASSERT_NO_ERROR( err );
00137     mesh.get_all_vertices( vertex_list, err );
00138     ASSERT_NO_ERROR( err );
00139     CPPUNIT_ASSERT( !vertex_list.empty() );
00140     initial_coords.resize( vertex_list.size() );
00141     mesh.vertices_get_coordinates( arrptr( vertex_list ), arrptr( initial_coords ), vertex_list.size(), err );
00142     ASSERT_NO_ERROR( err );
00143 
00144     // run global smoother
00145     global_q.run_instructions( &mesh, err );
00146     ASSERT_NO_ERROR( err );
00147     mesh.write_vtk( ( name + "-gbl.vtk" ).c_str(), err );
00148     global_coords.resize( vertex_list.size() );
00149     mesh.vertices_get_coordinates( arrptr( vertex_list ), arrptr( global_coords ), vertex_list.size(), err );
00150     ASSERT_NO_ERROR( err );
00151 
00152     // restore initial vertex positions
00153     for( i = 0; i < vertex_list.size(); ++i )
00154     {
00155         mesh.vertex_set_coordinates( vertex_list[i], initial_coords[i], err );
00156         ASSERT_NO_ERROR( err );
00157     }
00158 
00159     // run local smoother
00160     bcd_q.run_instructions( &mesh, err );
00161     ASSERT_NO_ERROR( err );
00162     mesh.write_vtk( ( name + "-bcd.vtk" ).c_str(), err );
00163     bcd_coords.resize( vertex_list.size() );
00164     mesh.vertices_get_coordinates( arrptr( vertex_list ), arrptr( bcd_coords ), vertex_list.size(), err );
00165     ASSERT_NO_ERROR( err );
00166 
00167     // compare results
00168     for( i = 0; i < bcd_coords.size(); ++i )
00169         CPPUNIT_ASSERT_VECTORS_EQUAL( global_coords[i], bcd_coords[i], 1e-2 );
00170 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines