MOAB: Mesh Oriented datABase  (version 5.2.1)
main_moab.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2004 Sandia Corporation and Argonne National
00005     Laboratory.  Under the terms of Contract DE-AC04-94AL85000
00006     with Sandia Corporation, the U.S. Government retains certain
00007     rights in 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     diachin2@llnl.gov, djmelan@sandia.gov, mbrewer@sandia.gov,
00024     pknupp@sandia.gov, tleurent@mcs.anl.gov, tmunson@mcs.anl.gov
00025 
00026   ***************************************************************** */
00027 // -*- Mode : c++; tab-width: 3; c-tab-always-indent: t; indent-tabs-mode: nil; c-basic-offset: 3
00028 // -*-
00029 //
00030 //   SUMMARY:
00031 //     USAGE:
00032 //
00033 // ORIG-DATE: 19-Feb-02 at 10:57:52
00034 //  LAST-MOD: 10-Feb-04 at 22:44:58 by Thomas Leurent
00035 //
00036 //
00037 // DESCRIPTION:
00038 // ============
00039 /*! \file main.cpp
00040 
00041 describe main.cpp here
00042 
00043  */
00044 // DESCRIP-END.
00045 //
00046 
00047 #include "Mesquite.hpp"
00048 #include "MsqMOAB.hpp"
00049 #include "MeshImpl.hpp"
00050 #include "MsqError.hpp"
00051 #include "InstructionQueue.hpp"
00052 #include "TerminationCriterion.hpp"
00053 #include "QualityAssessor.hpp"
00054 #include "PlanarDomain.hpp"
00055 #include "MeshWriter.hpp"
00056 #include "TestUtil.hpp"
00057 
00058 // algorithms
00059 #include "IdealWeightInverseMeanRatio.hpp"
00060 #include "EdgeLengthQualityMetric.hpp"
00061 #include "LPtoPTemplate.hpp"
00062 #include "FeasibleNewton.hpp"
00063 #include "ConjugateGradient.hpp"
00064 #include "SmartLaplacianSmoother.hpp"
00065 
00066 #include <iostream>
00067 using std::cerr;
00068 using std::cout;
00069 using std::endl;
00070 
00071 #include "iBase.h"
00072 
00073 using namespace MBMesquite;
00074 
00075 std::string default_file_name = TestDir + "/3D/vtk/large_box_hex_1000.vtk";
00076 
00077 void usage()
00078 {
00079     cout << "main [-N] [filename]" << endl;
00080     cout << "  -N : Use native representation instead of TSTT implementation\n";
00081     cout << "  If no file name is specified, will use \"" << default_file_name << '"' << endl;
00082     exit( 1 );
00083 }
00084 
00085 // Construct a MeshTSTT from the file
00086 Mesh* get_imesh_mesh( const char* file_name );
00087 
00088 // Construct a MeshImpl from the file
00089 Mesh* get_native_mesh( const char* file_name );
00090 
00091 // Run FeasibleNewton solver
00092 int run_global_smoother( Mesh* mesh, MsqError& err );
00093 
00094 // Run SmoothLaplacian solver
00095 int run_local_smoother( Mesh* mesh, MsqError& err );
00096 
00097 int main( int argc, char* argv[] )
00098 {
00099     MBMesquite::MsqPrintError err( cout );
00100 
00101     // command line arguments
00102     const char* file_name = 0;
00103     bool use_native = false, opts_done = false;
00104     for( int arg = 1; arg < argc; ++arg )
00105     {
00106         if( !opts_done && argv[arg][0] == '-' )
00107         {
00108             if( !strcmp( argv[arg], "-N" ) )
00109                 use_native = true;
00110             else if( !strcmp( argv[arg], "--" ) )
00111                 opts_done = true;
00112             else
00113                 usage();
00114         }
00115         else if( !file_name )
00116             file_name = argv[arg];
00117         else
00118             usage();
00119     }
00120     if( !file_name )
00121     {
00122         file_name = default_file_name.c_str();
00123         cout << "No file specified: using default: " << default_file_name << endl;
00124     }
00125 
00126     // Try running a global smoother on the mesh
00127     Mesh* mesh = use_native ? get_native_mesh( file_name ) : get_imesh_mesh( file_name );
00128     if( !mesh )
00129     {
00130         std::cerr << "Failed to load input file.  Aborting." << std::endl;
00131         return 1;
00132     }
00133 
00134     MeshWriter::write_vtk( mesh, "original.vtk", err );
00135     if( err ) return 1;
00136     cout << "Wrote \"original.vtk\"" << endl;
00137     run_global_smoother( mesh, err );
00138     if( err ) return 1;
00139 
00140     // Try running a local smoother on the mesh
00141     mesh = use_native ? get_native_mesh( file_name ) : get_imesh_mesh( file_name );
00142     if( !mesh )
00143     {
00144         std::cerr << "Failed to load input file.  Aborting." << std::endl;
00145         return 1;
00146     }
00147 
00148     run_local_smoother( mesh, err );
00149     if( err ) return 1;
00150 
00151     return 0;
00152 }
00153 
00154 int run_global_smoother( Mesh* mesh, MsqError& err )
00155 {
00156     double OF_value = 0.0001;
00157 
00158     // creates an intruction queue
00159     InstructionQueue queue1;
00160 
00161     // creates a mean ratio quality metric ...
00162     IdealWeightInverseMeanRatio* mean_ratio = new IdealWeightInverseMeanRatio( err );
00163     if( err ) return 1;
00164     mean_ratio->set_averaging_method( QualityMetric::SUM, err );
00165     if( err ) return 1;
00166 
00167     // ... and builds an objective function with it
00168     LPtoPTemplate* obj_func = new LPtoPTemplate( mean_ratio, 1, err );
00169     if( err ) return 1;
00170 
00171     // creates the feas newt optimization procedures
00172     FeasibleNewton* pass1 = new FeasibleNewton( obj_func );
00173     pass1->use_global_patch();
00174     if( err ) return 1;
00175 
00176     QualityAssessor stop_qa( mean_ratio );
00177 
00178     // **************Set stopping criterion****************
00179     TerminationCriterion tc_inner;
00180     tc_inner.add_absolute_vertex_movement( OF_value );
00181     if( err ) return 1;
00182     TerminationCriterion tc_outer;
00183     tc_outer.add_iteration_limit( 1 );
00184     pass1->set_inner_termination_criterion( &tc_inner );
00185     pass1->set_outer_termination_criterion( &tc_outer );
00186 
00187     queue1.add_quality_assessor( &stop_qa, err );
00188     if( err ) return 1;
00189 
00190     // adds 1 pass of pass1 to mesh_set1
00191     queue1.set_master_quality_improver( pass1, err );
00192     if( err ) return 1;
00193 
00194     queue1.add_quality_assessor( &stop_qa, err );
00195     if( err ) return 1;
00196 
00197     // launches optimization on mesh_set
00198     queue1.run_instructions( mesh, err );
00199     if( err ) return 1;
00200 
00201     MeshWriter::write_vtk( mesh, "feasible-newton-result.vtk", err );
00202     if( err ) return 1;
00203     cout << "Wrote \"feasible-newton-result.vtk\"" << endl;
00204 
00205     // print_timing_diagnostics( cout );
00206     return 0;
00207 }
00208 
00209 int run_local_smoother( Mesh* mesh, MsqError& err )
00210 {
00211     double OF_value = 0.0001;
00212 
00213     // creates an intruction queue
00214     InstructionQueue queue1;
00215 
00216     // creates a mean ratio quality metric ...
00217     IdealWeightInverseMeanRatio* mean_ratio = new IdealWeightInverseMeanRatio( err );
00218     if( err ) return 1;
00219     mean_ratio->set_averaging_method( QualityMetric::SUM, err );
00220     if( err ) return 1;
00221 
00222     // ... and builds an objective function with it
00223     LPtoPTemplate* obj_func = new LPtoPTemplate( mean_ratio, 1, err );
00224     if( err ) return 1;
00225 
00226     // creates the smart laplacian optimization procedures
00227     SmartLaplacianSmoother* pass1 = new SmartLaplacianSmoother( obj_func );
00228 
00229     QualityAssessor stop_qa( mean_ratio );
00230 
00231     // **************Set stopping criterion****************
00232     TerminationCriterion tc_inner;
00233     tc_inner.add_absolute_vertex_movement( OF_value );
00234     TerminationCriterion tc_outer;
00235     tc_outer.add_iteration_limit( 1 );
00236     pass1->set_inner_termination_criterion( &tc_inner );
00237     pass1->set_outer_termination_criterion( &tc_outer );
00238 
00239     queue1.add_quality_assessor( &stop_qa, err );
00240     if( err ) return 1;
00241 
00242     // adds 1 pass of pass1 to mesh_set
00243     queue1.set_master_quality_improver( pass1, err );
00244     if( err ) return 1;
00245 
00246     queue1.add_quality_assessor( &stop_qa, err );
00247     if( err ) return 1;
00248 
00249     // launches optimization on mesh_set
00250     queue1.run_instructions( mesh, err );
00251     if( err ) return 1;
00252 
00253     MeshWriter::write_vtk( mesh, "smart-laplacian-result.vtk", err );
00254     if( err ) return 1;
00255     cout << "Wrote \"smart-laplacian-result.vtk\"" << endl;
00256 
00257     // print_timing_diagnostics( cout );
00258     return 0;
00259 }
00260 
00261 Mesh* get_imesh_mesh( const char* file_name )
00262 {
00263     moab::Core* mb = new( std::nothrow ) moab::Core;
00264     if( NULL == mb ) return 0;
00265 
00266     moab::ErrorCode rval;
00267     // This file is in the mesh files directory
00268     rval = mb->load_file( file_name );MB_CHK_SET_ERR_RET_VAL( rval, "Failed to read", 0 );
00269 
00270     moab::Tag fixed_tag;
00271     rval = mb->tag_get_handle( "fixed", fixed_tag );
00272 
00273     moab::EntityHandle root_set = 0;
00274     MsqError err;
00275     Mesh* result = new MBMesquite::MsqMOAB( mb, root_set, moab::MBHEX, err, &fixed_tag );
00276     if( MSQ_CHKERR( err ) )
00277     {
00278         delete result;
00279         cerr << err << endl;
00280         return 0;
00281     }
00282 
00283     return result;
00284 }
00285 
00286 Mesh* get_native_mesh( const char* file_name )
00287 {
00288     MsqError err;
00289     MeshImpl* mesh = new MeshImpl;
00290     if( !mesh )
00291     {
00292         cerr << "Failed during MeshImpl construction.\n";
00293         exit( 2 );
00294     }
00295     mesh->read_vtk( file_name, err );
00296     if( err )
00297     {
00298         cerr << err << endl;
00299         exit( 3 );
00300     }
00301 
00302     return mesh;
00303 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines