MOAB: Mesh Oriented datABase  (version 5.2.1)
cslam_par_test.cpp
Go to the documentation of this file.
00001 /*
00002  * cslam_par_test.cpp
00003  *  test to trigger intersection on a sphere in parallel
00004  *  it will start from an eulerian mesh (mesh + velocity from Mark Taylor)
00005  *   file: VELO00.h5m; mesh + velo at 850 milibar, in an unstructured mesh refined from
00006  *   a cube sphere grid
00007  *  the mesh is read in parallel (euler mesh);
00008  *
00009  *   lagrangian mesh is obtained using
00010  *   pos (t-dt) = pos(t) -Velo(t)*dt
00011  *   then, project on the sphere with a given radius
00012  *
00013  *  Created on: Apr 22, 2013
00014  */
00015 
00016 #include <iostream>
00017 #include <sstream>
00018 #include <time.h>
00019 #include <stdlib.h>
00020 #include <stdio.h>
00021 #include <string.h>
00022 #include "moab/Core.hpp"
00023 #include "moab/Interface.hpp"
00024 #include "moab/IntxMesh/Intx2MeshOnSphere.hpp"
00025 #include <math.h>
00026 #include "TestUtil.hpp"
00027 #include "moab/ParallelComm.hpp"
00028 #include "moab/ProgOptions.hpp"
00029 #include "MBParallelConventions.h"
00030 #include "moab/ReadUtilIface.hpp"
00031 #include "MBTagConventions.hpp"
00032 
00033 #include "moab/IntxMesh/IntxUtils.hpp"
00034 #include "IntxUtilsCSLAM.hpp"
00035 
00036 // for M_PI
00037 #include <math.h>
00038 
00039 using namespace moab;
00040 // some input data
00041 double EPS1 = 0.2;                                // this is for box error
00042 std::string input_mesh_file( "VELO00_16p.h5m" );  // input file, partitioned correctly
00043 double Radius = 1.0;                              // change to radius
00044 double deltaT = 1.e-6;
00045 void test_intx_in_parallel_elem_based();
00046 
00047 int main( int argc, char** argv )
00048 {
00049     MPI_Init( &argc, &argv );
00050     EPS1       = 0.000002;
00051     int result = 0;
00052 
00053     if( argc > 1 )
00054     {
00055         int index = 1;
00056         while( index < argc )
00057         {
00058             if( !strcmp( argv[index], "-eps" ) )  // this is for box error
00059             { EPS1 = atof( argv[++index] ); }
00060             if( !strcmp( argv[index], "-input" ) ) { input_mesh_file = argv[++index]; }
00061             if( !strcmp( argv[index], "-radius" ) ) { Radius = atof( argv[++index] ); }
00062             if( !strcmp( argv[index], "-deltaT" ) ) { deltaT = atof( argv[++index] ); }
00063             index++;
00064         }
00065     }
00066     std::cout << " run: -input " << input_mesh_file << "  -eps " << EPS1 << " -radius " << Radius << " -deltaT "
00067               << deltaT << "\n";
00068 
00069     result += RUN_TEST( test_intx_in_parallel_elem_based );
00070 
00071     MPI_Finalize();
00072     return result;
00073 }
00074 // will save the LOC tag on the euler nodes
00075 ErrorCode compute_lagrange_mesh_on_sphere( Interface* mb, EntityHandle euler_set )
00076 {
00077     /*
00078      * get all quads first, then vertices, then move them on the surface of the sphere
00079      *  radius is 1, usually
00080      *  pos (t-dt) = pos(t) -Velo(t)*dt; this will be lagrange mesh, on each processor
00081      */
00082     Range quads;
00083     ErrorCode rval = mb->get_entities_by_type( euler_set, MBQUAD, quads );MB_CHK_ERR( rval );
00084 
00085     Range connecVerts;
00086     rval = mb->get_connectivity( quads, connecVerts );MB_CHK_ERR( rval );
00087 
00088     // the LOC tag, should be provided by the user?
00089     Tag tagh = 0;
00090     std::string tag_name( "DP" );
00091     rval = mb->tag_get_handle( tag_name.c_str(), 3, MB_TYPE_DOUBLE, tagh, MB_TAG_DENSE | MB_TAG_CREAT );MB_CHK_ERR( rval );
00092     void* data;  // pointer to the DP in memory, for each vertex
00093     int count;
00094 
00095     rval = mb->tag_iterate( tagh, connecVerts.begin(), connecVerts.end(), count, data );MB_CHK_ERR( rval );
00096     // here we are checking contiguity
00097     assert( count == (int)connecVerts.size() );
00098     double* ptr_DP = (double*)data;
00099     // get the coordinates of the old mesh, and move it around using velocity tag
00100 
00101     Tag tagv = 0;
00102     std::string velo_tag_name( "VELO" );
00103     rval = mb->tag_get_handle( velo_tag_name.c_str(), 3, MB_TYPE_DOUBLE, tagv, MB_TAG_DENSE );MB_CHK_ERR( rval );
00104 
00105     /*void *datavelo; // pointer to the VELO in memory, for each vertex
00106 
00107     rval = mb->tag_iterate(tagv, connecVerts.begin(), connecVerts.end(), count, datavelo);MB_CHK_ERR(rval);*/
00108     // here we are checking contiguity
00109     assert( count == (int)connecVerts.size() );
00110     // now put the vertices in the right place....
00111     // int vix=0; // vertex index in new array
00112 
00113     for( Range::iterator vit = connecVerts.begin(); vit != connecVerts.end(); ++vit )
00114     {
00115         EntityHandle oldV = *vit;
00116         CartVect posi;
00117         rval = mb->get_coords( &oldV, 1, &( posi[0] ) );MB_CHK_ERR( rval );
00118         CartVect velo;
00119         rval = mb->tag_get_data( tagv, &oldV, 1, (void*)&( velo[0] ) );MB_CHK_ERR( rval );
00120         // do some mumbo jumbo, as in python script
00121         CartVect newPos = posi - deltaT * velo;
00122         double len1     = newPos.length();
00123         newPos          = Radius * newPos / len1;
00124 
00125         ptr_DP[0] = newPos[0];
00126         ptr_DP[1] = newPos[1];
00127         ptr_DP[2] = newPos[2];
00128         ptr_DP += 3;  // increment to the next node
00129     }
00130 
00131     return rval;
00132 }
00133 
00134 void test_intx_in_parallel_elem_based()
00135 {
00136     std::string opts = std::string( "PARALLEL=READ_PART;PARTITION=PARALLEL_PARTITION" ) +
00137                        std::string( ";PARALLEL_RESOLVE_SHARED_ENTS" );
00138     Core moab;
00139     Interface& mb = moab;
00140     EntityHandle euler_set;
00141     ErrorCode rval;
00142     rval = mb.create_meshset( MESHSET_SET, euler_set );MB_CHK_ERR_RET( rval );
00143     std::string example( TestDir + "/" + input_mesh_file );
00144 
00145     rval = mb.load_file( example.c_str(), &euler_set, opts.c_str() );
00146 
00147     ParallelComm* pcomm = ParallelComm::get_pcomm( &mb, 0 );MB_CHK_ERR_RET( rval );
00148 
00149     rval = pcomm->check_all_shared_handles();MB_CHK_ERR_RET( rval );
00150 
00151     // everybody will get a DP tag, including the non owned entities; so exchange tags is not
00152     // required for LOC (here)
00153     rval = compute_lagrange_mesh_on_sphere( &mb, euler_set );MB_CHK_ERR_RET( rval );
00154 
00155     int rank = pcomm->proc_config().proc_rank();
00156 
00157     std::stringstream ste;
00158     ste << "initial" << rank << ".vtk";
00159     mb.write_file( ste.str().c_str(), 0, 0, &euler_set, 1 );
00160 
00161     Intx2MeshOnSphere worker( &mb );
00162 
00163     worker.set_radius_source_mesh( Radius );
00164     worker.set_radius_destination_mesh( Radius );
00165     worker.set_box_error( EPS1 );  //
00166     // worker.SetEntityType(MBQUAD);
00167 
00168     worker.set_error_tolerance( Radius * 1.e-8 );
00169     std::cout << "error tolerance epsilon_1=" << Radius * 1.e-8 << "\n";
00170     //  worker.locate_departure_points(euler_set);
00171 
00172     rval = worker.FindMaxEdges( euler_set, euler_set );  // departure will be the same max_edges
00173     // we need to make sure the covering set is bigger than the euler mesh
00174     EntityHandle covering_lagr_set;
00175     rval = mb.create_meshset( MESHSET_SET, covering_lagr_set );MB_CHK_ERR_RET( rval );
00176 
00177     rval = worker.create_departure_mesh_2nd_alg( euler_set, covering_lagr_set );MB_CHK_ERR_RET( rval );
00178 
00179     std::stringstream ss;
00180     ss << "partial" << rank << ".vtk";
00181     mb.write_file( ss.str().c_str(), 0, 0, &covering_lagr_set, 1 );
00182     EntityHandle outputSet;
00183     rval = mb.create_meshset( MESHSET_SET, outputSet );MB_CHK_ERR_RET( rval );
00184     rval = worker.intersect_meshes( covering_lagr_set, euler_set, outputSet );MB_CHK_ERR_RET( rval );
00185 
00186     // std::string opts_write("PARALLEL=WRITE_PART");
00187     // rval = mb.write_file("manuf.h5m", 0, opts_write.c_str(), &outputSet, 1);
00188     // std::string opts_write("");
00189     std::stringstream outf;
00190     outf << "intersect" << rank << ".h5m";
00191     rval = mb.write_file( outf.str().c_str(), 0, 0, &outputSet, 1 );MB_CHK_ERR_RET( rval );
00192 
00193     moab::IntxAreaUtils sphAreaUtils;
00194     double intx_area    = sphAreaUtils.area_on_sphere( &mb, outputSet, Radius );
00195     double arrival_area = sphAreaUtils.area_on_sphere( &mb, euler_set, Radius );
00196     std::cout << "On rank : " << rank << " arrival area: " << arrival_area << "  intersection area:" << intx_area
00197               << " rel error: " << fabs( ( intx_area - arrival_area ) / arrival_area ) << "\n";
00198 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines