MeshKit  1.0
CoreGen.cpp
Go to the documentation of this file.
00001 #include "meshkit/CoreGen.hpp"
00002 #define ERROR(a) {if (iBase_SUCCESS != err) std::cerr << a << std::endl;}
00003 #define ERRORR(a,b) {if (iBase_SUCCESS != err) {std::cerr << a << std::endl; return b;}}
00004 namespace MeshKit
00005 {
00006 // static registration of this  mesh scheme
00007 moab::EntityType CoreGen_tps[] = { moab::MBVERTEX,
00008                                    moab::MBEDGE,
00009                                    moab::MBTRI,
00010                                    moab::MBHEX,
00011                                    moab::MBMAXTYPE};
00012 const moab::EntityType* CoreGen::output_types()
00013 { return CoreGen_tps; }
00014 
00015 CoreGen::CoreGen( MKCore *mk, const MEntVector &me_vec)
00016     : MeshScheme( mk, me_vec),
00017       igeom(mk->igeom_instance()), imesh(mk->imesh_instance()),
00018       mb (mk->moab_instance())
00019 {
00020     err = 0;
00021     run_flag = 1;
00022     UNITCELL_DUCT = 0;
00023     ASSY_TYPES = 1;
00024     pack_type = 1;
00025     symm = 1;
00026     z_height = 1;
00027     z_divisions = 2;
00028     set_DIM = 3; // default is 3D
00029     PII = acos(-1.0);
00030     comment = "!";
00031     MAXCHARS = 300;
00032     extrude_flag = false;
00033     mem_tflag = false;
00034     global_ids = true;
00035     merge_tol = 1.0e-4;
00036     do_merge = 1;
00037     update_sets = 0;
00038     merge_tag = NULL;
00039     nst_flag = false;
00040     nsb_flag = false;
00041     nss_flag = false;
00042     nst_Id = 9997;
00043     nsb_Id = 9998;
00044     prob_type = "mesh";
00045     savefiles = "one";
00046     num_nsside = 0;
00047     linenumber = 0;
00048     info = "off";
00049     minfo = "off";
00050 
00051     // initialize more memory/time related variables
00052     ctload = 0, ctcopymove = 0, ctmerge = 0, ctextrude = 0, ctns = 0, ctgid = 0, ctsave = 0;
00053     tload = 0, tcopymove = 0, tmerge = 0, textrude = 0, tns = 0, tgid = 0, tsave = 0;
00054     ld_t = 0, ld_tload = 0, ld_tcopymove = 0, ld_tsave = 0, ld_tgid = 0, ld_tmerge = 0, ld_tns = 0;
00055     mem1 = 0, mem2 = 0, mem3 = 0, mem4 = 0, mem5 = 0, mem6 = 0, mem7 = 0;
00056 
00057 
00058 }
00059 
00060 CoreGen::~CoreGen()
00061 {}
00062 
00063 bool CoreGen::add_modelent(ModelEnt *model_ent)
00064 {
00065     return MeshOp::add_modelent(model_ent);
00066 }
00067 
00068 void CoreGen::setup_this()
00069 {
00070     logfile << "Setting-up in CoreGen meshop.." << std::endl;
00071     if(run_flag != 0){
00072         double ctload = 0;
00073         clock_t tload = 0;
00074         CClock ld_time;
00075         int ld_t = 0, ld_tload = 0;
00076         unsigned long mem1 = 0;
00077         if (prob_type == "mesh") {
00078             if (procs == 1) {
00079                 err = load_meshes();
00080                 //ERRORR("Failed to load meshes.", 1);
00081             }
00082             else {
00083 #ifdef USE_MPI
00084                 err = load_meshes_parallel(rank, procs);
00085                 //ERRORR("Failed to load meshes.", 1);
00086 
00087 #ifdef USE_MPI
00088                 MPI::COMM_WORLD.Barrier();
00089 #endif
00090                 if(procs > (int) files.size()){
00091                     // if there are more procs than files distribute the copy/move work on each proc
00092                     err = distribute_mesh(rank, procs);
00093                     // ERRORR("Failed to load meshes.", 1);
00094                 }
00095                 //Get a pcomm object
00096                 pc = new moab::ParallelComm(mk_core()->moab_instance(), MPI::COMM_WORLD, &err);
00097 #endif
00098             }
00099 
00100             mk_core()->moab_instance()->estimated_memory_use(0, 0, 0, &mem1);
00101             ld_tload = ld_time.DiffTime();
00102             tload = clock();
00103             ctload = (double) (tload - sTime)/(60*CLOCKS_PER_SEC);
00104 
00105             if (mem_tflag == true && procs == 1) {
00106                 logfile << "\n" << " Clock time taken to load mesh files = " << ld_tload
00107                         << " seconds" << std::endl;
00108                 logfile << " CPU time = " << ctload << " mins" << std::endl;
00109                 logfile << " Memory used: " << mem1/1e6 << " Mb\n" << std::endl;
00110             }
00111         }
00112 
00113         /*********************************************/
00114         // load geometry files
00115         /*********************************************/
00116         else if (prob_type == "geometry" && procs == 1) {
00117             err = load_geometries();
00118             //   ERRORR("Failed to load geometries", 1);
00119         }
00120         else if(prob_type == "geometry" && procs > 1){
00121             logfile << " Parallel mode not supported for problem-type: Geometry " << std::endl;
00122             exit(1);
00123         }
00124 
00125 
00126 
00127 #ifdef USE_MPI
00128         MPI::COMM_WORLD.Barrier();
00129 #endif
00130         /*********************************************/
00131         // copy move
00132         /*********************************************/
00133         CClock ld_cm;
00134         err = copymove(rank, procs);
00135         if (prob_type == "mesh"){
00136             mk_core()->moab_instance()->estimated_memory_use(0, 0, 0, &mem2);
00137             ld_tcopymove = ld_cm.DiffTime();
00138             tcopymove = clock();
00139             ctcopymove = (double) (tcopymove - tload)/(60*CLOCKS_PER_SEC);
00140 
00141             if (mem_tflag == true && (strcmp(prob_type.c_str(), "mesh") == 0) && procs == 1) {
00142                 logfile << "\n" << " Clock time taken to copy/move mesh files = " << ld_tcopymove
00143                         << " seconds" << std::endl;
00144                 logfile << " CPU time = " << ctcopymove << " mins" << std::endl;
00145                 logfile << " Memory used: " << mem2/1e6 << " Mb\n" << std::endl;
00146             }
00147         }
00148 #ifdef USE_MPI
00149         MPI::COMM_WORLD.Barrier();
00150 #endif
00151         for (unsigned int i = 0; i < assys.size(); i++) {
00152             if(prob_type =="mesh")
00153                 cm[i]->setup_called(true);
00154             if(prob_type =="geometry")
00155                 cg[i]->setup_called(true);
00156         }
00157 
00158         if (prob_type == "mesh") {
00159             /*********************************************/
00160             // merge
00161             /*********************************************/
00162             CClock ld_mm;
00163             if (procs == 1){
00164                 // merge mesh now
00165                 //std::vector<iBase_EntityHandle> ents;
00166                 moab::Range ents;
00167                 int dim = imesh->getGeometricDimension();
00168                 mb->get_entities_by_dimension(0, dim, ents);
00169 
00170                 moab::MergeMesh mm(mb);
00171                 moab::ErrorCode err = mm.merge_entities(ents, merge_tol, true);
00172                 if (MB_SUCCESS != err) {
00173                     std::cerr << "Error in MergeMesh during merging entities" << std::endl;
00174                     exit(2);
00175                 }
00176             }
00177             else if(procs > 1){
00178                 if (rank == 0) {
00179                     logfile << "Merging nodes in parallel. " << std::endl;
00180                 }
00181 
00182 #ifdef USE_MPI
00183                 //Call the resolve parallel function
00184                 moab::ParallelMergeMesh pm(pc, merge_tol);
00185                 err = pm.merge();
00186                 if (err != moab::MB_SUCCESS) {
00187                     std::cerr << "Merge Failed" << std::endl;
00188                     //MPI_Abort(MPI_COMM_WORLD, 1);
00189                 }
00190 #endif
00191             }
00192             mk_core()->moab_instance()->estimated_memory_use(0, 0, 0, &mem3);
00193             ld_tmerge = ld_mm.DiffTime();
00194             tmerge = clock();
00195             ctmerge = (double) (tmerge - tcopymove)/(60*CLOCKS_PER_SEC);
00196 
00197             if (mem_tflag == true && procs == 1 ) {
00198                 logfile << "\n" << " Clock time taken to merge nodes = " << ld_tmerge
00199                         << " seconds" << std::endl;
00200                 logfile << " CPU time = " << ctmerge << " mins" << std::endl;
00201                 logfile << " Memory used: " << mem3/1e6 << " Mb\n" << std::endl;
00202             }
00203 #ifdef USE_MPI
00204             MPI::COMM_WORLD.Barrier();
00205 #endif
00206             /*********************************************/
00207             // extrude
00208             /*********************************************/
00209             if(procs == 1){
00210                 if (extrude_flag == true) {
00211 
00212                     // assign global ids after copy/move step
00213                     if (rank == 0)
00214                         err = assign_gids();
00215 
00216                     CClock ld_em;
00217                     err = extrude();
00218                     //     ERRORR("Failed to extrude.", 1);
00219 
00220                     mk_core()->moab_instance()->estimated_memory_use(0, 0, 0, &mem4);
00221                     ld_t = ld_em.DiffTime();
00222                     textrude = clock();
00223                     ctextrude = (double) (textrude - tmerge)/(60*CLOCKS_PER_SEC);
00224 
00225                     if (mem_tflag == true && procs == 1) {
00226                         logfile << "\n" << " Clock time taken to extrude = " << ld_t
00227                                 << " seconds" << std::endl;
00228                         logfile << " CPU time = " << ctextrude << " mins" << std::endl;
00229                         logfile << " Memory used: " << mem4/1e6 << " Mb\n"
00230                                 << std::endl;
00231                     }
00232                 }
00233             }
00234             if(extrude_flag == true)
00235                 em->setup_called(true);
00236 #ifdef USE_MPI
00237             MPI::COMM_WORLD.Barrier();
00238 #endif
00239         }
00240     }
00241 }
00242 
00243 
00244 void CoreGen::execute_this()
00245 {
00246     logfile << "Executing in CoreGen meshop.." << std::endl;
00247 
00248     if(run_flag != 0){
00249         for (unsigned int i = 0; i < assys.size(); i++) {
00250             cm[i]->execute_called(true);
00251         }
00252         /*********************************************/
00253         // assign gids
00254         /*********************************************/
00255         CClock ld_gid;
00256         if (procs == 1) {
00257             err = assign_gids();
00258             //      ERRORR("Failed to assign global ids.", 1);
00259         }
00260         //        else{
00261         //          // err = assign_gids_parallel(rank, procs);
00262         //          // ERRORR("Failed to assign global ids.", 1);
00263         //        }
00264 
00265         mk_core()->moab_instance()->estimated_memory_use(0, 0, 0, &mem5);
00266         ld_tgid = ld_gid.DiffTime();
00267         tgid = clock();
00268         ctgid = (double) (tgid-tmerge)/(60*CLOCKS_PER_SEC);
00269 
00270         if (mem_tflag == true && procs == 1) {
00271             logfile << "\n" << " Clock time taken to assign gids = " << ld_tgid
00272                     << " seconds" << std::endl;
00273             logfile << " CPU time = " << ctgid << " mins" << std::endl;
00274             logfile << " Memory used: " << mem5/1e6 << " Mb\n" << std::endl;
00275         }
00276         /*********************************************/
00277         // create neumann sets on the core model
00278         /*********************************************/
00279         if((nss_flag == true || nsb_flag == true
00280             || nst_flag == true) && procs == 1){
00281             CClock ld_ns;
00282             err = create_neumannset();
00283             //    ERRORR("Failed to create neumann set.", 1);
00284 
00285             mk_core()->moab_instance()->estimated_memory_use(0, 0, 0, &mem6);
00286             ld_tns = ld_ns.DiffTime();
00287             tns = clock();
00288             ctns = (double) (tns-tgid)/(60*CLOCKS_PER_SEC);
00289             if (mem_tflag == true && procs == 1) {
00290                 logfile << "\n" << " Clock time taken to create neumann sets = " << ld_tns
00291                         << " seconds" << std::endl;
00292                 logfile << " CPU time = " << ctns << " mins" << std::endl;
00293                 logfile << " Memory used: " << mem6/1e6 << " Mb\n" << std::endl;
00294             }
00295         }
00296 #ifdef USE_MPI
00297         MPI::COMM_WORLD.Barrier();
00298 #endif
00299 
00300     }
00301     if (prob_type == "mesh") {
00302 
00303 
00304         /*********************************************/
00305         // save
00306         /*********************************************/
00307         CClock ld_sv;
00308         if (procs == 1) {
00309             err = save_mesh();
00310             //  ERRORR("Failed to save o/p file.", 1);
00311         } else {
00312             if(savefiles != "one" && (savefiles == "multiple" || savefiles == "both")){
00313                 err = save_mesh(rank); // uncomment to save the meshes with each proc
00314                 //ERRORR("Failed to save o/p file.", 1);
00315             }
00316             if(savefiles != "multiple"){
00317 #ifdef USE_MPI
00318                 double write_time = MPI_Wtime();
00319                 err = save_mesh_parallel(rank, procs);
00320                 //ERRORR("Failed to save o/p file.", 1);
00321                 write_time = MPI_Wtime() - write_time;
00322                 if (rank == 0)
00323                     logfile << "Parallel write time = " << write_time/60.0 << " mins" << std::endl;
00324 #endif
00325             }
00326         }
00327 
00328         mk_core()->moab_instance()->estimated_memory_use(0, 0, 0, &mem7);
00329         ld_tsave = ld_sv.DiffTime();
00330         tsave = clock();
00331         ctsave = (double) (tsave - tgid)/(60*CLOCKS_PER_SEC);
00332 
00333         if (mem_tflag == true && procs == 1 ) {
00334             logfile << "\n" << " Clock time taken to save = " << ld_tsave << " seconds"
00335                     << std::endl;
00336             logfile << " CPU time = " << ctsave << " mins" << std::endl;
00337             logfile << " Memory used: " << mem7/1e6 << " Mb\n" << std::endl;
00338         }
00339     }
00340     /*********************************************/
00341     // geometry operations
00342     /*********************************************/
00343     else if (prob_type == "geometry") {
00344         err = save_geometry();
00345         //ERRORR("Failed to save o/p file.", 1);
00346     }
00347 
00348 
00349 
00350 
00351     /*********************************************/
00352     // print memory and timing if using mpi
00353     /*********************************************/
00354     mem1/=1e6;
00355     mem2/=1e6;
00356     mem3/=1e6;
00357     mem5/=1e6;
00358     mem7/=1e6;
00359 
00360 #ifdef USE_MPI
00361     unsigned long max_mem7 = 1.0;
00362     MPI::COMM_WORLD.Reduce( &mem7, &max_mem7, 1, MPI::UNSIGNED_LONG, MPI::MAX, 0);
00363 #endif
00364 
00365 #ifdef USE_MPI
00366     if (mem_tflag == true) {
00367 
00368         unsigned long max_mem1 = 1.0, max_mem2 = 1.0, max_mem3 = 1.0, max_mem5 = 1.0;
00369 
00370         MPI::COMM_WORLD.Reduce( &mem1, &max_mem1, 1, MPI::UNSIGNED_LONG, MPI::MAX, 0);
00371         MPI::COMM_WORLD.Reduce( &mem2, &max_mem2, 1, MPI::UNSIGNED_LONG, MPI::MAX, 0);
00372         MPI::COMM_WORLD.Reduce( &mem3, &max_mem3, 1, MPI::UNSIGNED_LONG, MPI::MAX, 0);
00373         MPI::COMM_WORLD.Reduce( &mem5, &max_mem5, 1, MPI::UNSIGNED_LONG, MPI::MAX, 0);
00374 
00375         double max_ctload = -1.0, max_ctcopymove = -1.0, max_ctgid = -1.0, max_ctsave = -1.0, max_ctmerge = -1.0;
00376         MPI::COMM_WORLD.Reduce( &ctload, &max_ctload, 1, MPI::DOUBLE, MPI::MAX, 0);
00377         MPI::COMM_WORLD.Reduce( &ctcopymove, &max_ctcopymove, 1, MPI::DOUBLE, MPI::MAX, 0);
00378         MPI::COMM_WORLD.Reduce( &ctmerge, &max_ctmerge, 1, MPI::DOUBLE, MPI::MAX, 0);
00379         MPI::COMM_WORLD.Reduce( &ctgid, &max_ctgid, 1, MPI::DOUBLE, MPI::MAX, 0);
00380         MPI::COMM_WORLD.Reduce( &ctsave, &max_ctsave, 1, MPI::DOUBLE, MPI::MAX, 0);
00381 
00382         int max_tload = -1.0, max_tcopymove = -1.0, max_tgid = -1.0, max_tsave = -1.0, max_tmerge = -1.0;
00383         MPI::COMM_WORLD.Reduce( &ld_tload, &max_tload, 1, MPI::INT, MPI::MAX, 0);
00384         MPI::COMM_WORLD.Reduce( &ld_tcopymove, &max_tcopymove, 1, MPI::INT, MPI::MAX, 0);
00385         MPI::COMM_WORLD.Reduce( &ld_tmerge, &max_tmerge, 1, MPI::INT, MPI::MAX, 0);
00386         MPI::COMM_WORLD.Reduce( &ld_tgid, &max_tgid, 1, MPI::INT, MPI::MAX, 0);
00387         MPI::COMM_WORLD.Reduce( &ld_tsave, &max_tsave, 1, MPI::INT, MPI::MAX, 0);
00388 
00389         if(rank == 0 && procs > 1){
00390             logfile << "\nMAXIMUM TIME TAKEN OVER ALL PROCS\nCLOCK TIME:-";
00391             logfile << "\n**r = " << rank<< " Time taken to load mesh files = " << max_tload
00392                     << " secs" << std::endl;
00393             logfile << "***r = : " << rank<< " Memory used: " << max_mem1 << " Mb" << std::endl;
00394 
00395             // copymove
00396             logfile << "\n**r = " << rank<< " Time taken to copy/move mesh files = " << max_tcopymove
00397                     << " secs" << std::endl;
00398             logfile << "***r = " << rank<< " Memory used: " << max_mem2 << " Mb" << std::endl;
00399 
00400             // merge
00401             logfile << "\n**r = " << rank<< " Time taken to merge nodes = " << max_tmerge
00402                     << " secs" << std::endl;
00403             logfile << "***r = " << rank<< " Memory used: " << max_mem3 << " kb" << std::endl;
00404 
00405             // assign gid
00406             logfile << "\n**r = " << rank<< " Time taken to assign gids = " << max_tgid
00407                     << " secs" << std::endl;
00408             logfile << "*** r = " << rank<< " Memory used: " << max_mem5 << " Mb" << std::endl;
00409 
00410             // save
00411             logfile << "\n**r = " << rank<< " Time taken to save = " <<  max_tsave << " secs"
00412                     << std::endl;
00413             logfile << "***r = " << rank<< " Memory used: " << max_mem7 << " Mb" << std::endl;
00414 
00415             // cpu times
00416             logfile << "\n CPU TIME:-\n" << " r = " << rank<< " Time taken to load mesh files = " << ctload
00417                     << " mins" << std::endl;
00418 
00419             logfile << " r = " << rank << " Time taken to copy/move files = " << ctcopymove
00420                     << " mins" << std::endl;
00421 
00422             logfile << " r = " << rank << " Time taken to merge = " << ctmerge
00423                     << " mins" << std::endl;
00424 
00425             logfile << " r = " << rank <<  " Time taken to assign gids = " << ctgid
00426                     << " mins" << std::endl;
00427 
00428             logfile  << " r = " << rank << " Time taken to save mesh = " << ctsave
00429                      << " mins" << std::endl;
00430         }
00431     }
00432 #endif
00433 
00434     if (rank == 0) {
00435         Timer.GetDateTime(szDateTime);
00436         logfile << "\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"  << std::endl;
00437         logfile << "Ending at : " << szDateTime;
00438         logfile << "Elapsed wall clock time: " << Timer.DiffTime()
00439                 << " seconds or " << (Timer.DiffTime()) / 60.0 << " mins\n";
00440 
00441         logfile << "Total CPU time used: " <<  (double) (clock() - sTime)/(CLOCKS_PER_SEC) << " seconds or " <<
00442                    (double) (clock() - sTime)/(60*CLOCKS_PER_SEC)
00443                 << " mins" << std::endl;
00444 #ifdef USE_MPI
00445         logfile << "Maximum memory used by a processor: " << max_mem7 <<  " Mb" << std::endl;
00446 #endif
00447         if(procs == 1)
00448             logfile << "Maximum memory used: " << mem7 <<  " Mb" << std::endl;
00449         logfile << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"  << std::endl;
00450 
00451     }
00452     /*********************************************/
00453     // close
00454     /*********************************************/
00455     if (run_flag == 1 && procs == 1) {
00456         //err = close();
00457         //ERRORR("Failed to dellocate.", 1);
00458     } else {
00459         //err = close_parallel(rank, procs);
00460         //ERRORR("Failed to dellocate.", 1);
00461     }
00462 }
00463 
00464 int CoreGen::save_mesh_parallel(const int nrank, const int numprocs)
00465 // -------------------------------------------------------------------------------------------
00466 // Function: save mesh file in parallel (hdf5 only)
00467 // Input:    none
00468 // Output:   none
00469 // -------------------------------------------------------------------------------------------
00470 {
00471 
00472 #ifdef USE_MPI
00473     // write file
00474     if (nrank == 0) {
00475         logfile << "Saving mesh file in parallel. " << std::endl;
00476     }
00477 
00478     //  All explicit sharing data must be updated in ParallelComm instance before save
00479     // moab::Range entities, sets, faces, edges;
00480     // moab::Tag gid_tag;
00481     // mb->tag_get_handle( "GLOBAL_ID",0, MB_TYPE_INTEGER, gid_tag);
00482     // mb->get_entities_by_type( 0, MBQUAD, faces );
00483     // mb->get_entities_by_type( 0, MBEDGE, edges );
00484 
00485     // err = pc->resolve_shared_ents( 0, edges, 0, -1, &gid_tag );
00486     // if (err != moab::MB_SUCCESS) {
00487     //   std::cerr << "failed to resolve shared ents" << std::endl;
00488     //   MPI_Abort(MPI_COMM_WORLD, 1);
00489     // }
00490 
00491     // err = pc->resolve_shared_ents( 0, faces, 0, -1, &gid_tag );
00492     // if (err != moab::MB_SUCCESS) {
00493     //   std::cerr << "failed to resolve shared ents" << std::endl;
00494     //   MPI_Abort(MPI_COMM_WORLD, 1);
00495     // }
00496 
00497     moab::Tag mattag;
00498     mb->tag_get_handle( "MATERIAL_SET", 1, MB_TYPE_INTEGER, mattag );
00499     moab::Range matsets;
00500     mb->get_entities_by_type_and_tag( 0, MBENTITYSET, &mattag, 0, 1, matsets );
00501     pc->resolve_shared_sets( matsets, mattag );
00502 
00503     moab::Tag nstag;
00504     mb->tag_get_handle( "NEUMANN_SET", 1, MB_TYPE_INTEGER, nstag );
00505     moab::Range nssets;
00506     mb->get_entities_by_type_and_tag( 0, MBENTITYSET, &nstag, 0, 1, nssets );
00507     pc->resolve_shared_sets( nssets, nstag );
00508 
00509 #ifdef HAVE_MOAB
00510     //  int rval = mb->write_file(outfile.c_str() , 0,"PARALLEL=WRITE_PART;DEBUG_IO=5");
00511     int rval = mb->write_file(outfile.c_str() , 0,"PARALLEL=WRITE_PART");
00512     if(rval != moab::MB_SUCCESS) {
00513         std::cerr<<"Writing output file failed Code:";
00514         std::string foo = ""; mb->get_last_error(foo);
00515         std::cerr<<"File Error: "<<foo<<std::endl;
00516         return 1;
00517     }
00518     if (nrank == 0) {
00519         logfile << "Done saving mesh file: " << outfile << std::endl;
00520     }
00521 #endif
00522 #endif
00523     return iBase_SUCCESS;
00524 
00525 }
00526 
00527 int CoreGen::save_mesh(int nrank) {
00528     // export proc- nrank mesh
00529     std::ostringstream os;
00530     std::string fname;
00531     fname = iname;
00532     os << fname << nrank << ".h5m";
00533     fname = os.str();
00534     iMesh_save(imesh->instance(), root_set, fname.c_str(), NULL, &err, strlen(fname.c_str()), 0);
00535     ERRORR("Trouble writing output mesh.", err);
00536     logfile << "Saved mesh file: " << fname.c_str() << std::endl;
00537 
00538     return iBase_SUCCESS;
00539 }
00540 
00541 int CoreGen::save_mesh() {
00542     // ---------------------------------------------------------------------------
00543     // Function: save mesh serially
00544     // Input:    none
00545     // Output:   none
00546     // ---------------------------------------------------------------------------
00547     // export
00548     logfile << "Saving mesh file." << std::endl;
00549     iMesh_save(imesh->instance(), root_set, outfile.c_str(), NULL, &err, strlen(
00550                    outfile.c_str()), 0);
00551     ERRORR("Trouble writing output mesh.", err);
00552     logfile << "Saved mesh file: " << outfile.c_str() << std::endl;
00553 
00554     return iBase_SUCCESS;
00555 }
00556 
00557 
00558 int CoreGen::save_geometry() {
00559     // ---------------------------------------------------------------------------
00560     // Function: save geometry serially
00561     // Input:    none
00562     // Output:   none
00563     // ---------------------------------------------------------------------------
00564     double dTol = 1e-3;
00565 
00566     // getting all entities for merge and imprint
00567     SimpleArray<iBase_EntityHandle> entities_merge, entities_imprint;
00568     iGeom_getEntities(igeom->instance(), root_set, iBase_REGION,
00569                       ARRAY_INOUT(entities_merge), &err );
00570     ERRORR("Trouble writing output geometry.", err);
00571 
00572     // merge and imprint before save
00573     logfile << "Merging.." << std::endl;
00574     iGeom_mergeEnts(igeom->instance(), ARRAY_IN(entities_merge), dTol, &err);
00575     ERRORR("Trouble writing output geometry.", err);
00576 
00577     iGeom_getEntities( igeom->instance(), root_set, iBase_REGION, ARRAY_INOUT(entities_imprint),&err );
00578     ERRORR("Trouble writing output geometry.", err);
00579 
00580     // logfile << "Imprinting.." << std::endl;
00581     // iGeom_imprintEnts(igeom->instance(), ARRAY_IN(entities_imprint),&err);
00582     // ERRORR("Trouble writing output geometry.", err);
00583     // export
00584     logfile << "Saving geometry file: " <<  outfile << std::endl;
00585 
00586     iGeom_save(igeom->instance(), outfile.c_str(), NULL, &err,
00587                strlen(outfile.c_str()), 0);
00588     ERRORR("Trouble writing output geometry.", err);
00589     logfile << "Saved geometry file: "<< outfile.c_str() <<std::endl;
00590 
00591     return iBase_SUCCESS;
00592 }
00593 
00594 int CoreGen::assign_gids_parallel(const int nrank, const int numprocs) {
00595 #ifdef USE_MPI
00596     // assign new global ids
00597     if (global_ids == true) {
00598         if(nrank==0)
00599             logfile << "Assigning global ids in parallel" << std::endl;
00600         err = pc->assign_global_ids(0, 3, 1, false, true, false);
00601         ERRORR("Error assigning global ids.", err);
00602     }
00603     // // assign global ids for all entity sets
00604     // SimpleArray<iBase_EntitySetHandle> sets;
00605     // iBase_TagHandle gid_tag;
00606     // const char *tag_name = "GLOBAL_ID";
00607     // iMesh_getTagHandle(imesh->instance(), tag_name, &gid_tag, &err, 9);
00608     // if (iBase_TAG_NOT_FOUND == err) {
00609     //   iMesh_createTag(imesh->instance(), tag_name, 1, iBase_INTEGER,
00610     //                   &gid_tag, &err, strlen(tag_name));
00611     //   ERRORR("Couldn't create global id tag", err);
00612     // }
00613 
00614 
00615 
00616     // iMesh_getEntSets(imesh->instance(),root_set, 1, ARRAY_INOUT(sets), &err);
00617     // ERRORR("Failed to get contained sets.", err);
00618 
00619     // for(int id = 1; id <= sets.size(); id++){
00620     //   iMesh_setEntSetIntData(imesh->instance(), sets[id-1], gid_tag, id, &err);
00621     //   ERRORR("Failed to set tags on sets.", err);
00622     // }
00623 #endif
00624     return iBase_SUCCESS;
00625 }
00626 
00627 int CoreGen::close_parallel(const int nrank, const int numprocs)
00628 // ---------------------------------------------------------------------------
00629 // Function: dellocating
00630 // Input:    none
00631 // Output:   none
00632 // ---------------------------------------------------------------------------
00633 {
00634 #ifdef USE_MPI
00635     // deallocate ... deallocate ... deallocate
00636     if (prob_type == "mesh") {
00637         //        for (unsigned int i = 0; i < assys.size(); i++) {
00638         //            delete cm[i];
00639         //          }
00640 
00641         iMesh_dtor(imesh->instance(), &err);
00642         ERRORR("Failed in call iMesh_dtor", err);
00643 
00644     }
00645 
00646     if (prob_type == "geometry") {
00647         //        for (unsigned int i = 0; i < 1; i++) {
00648         //            delete cg[i];
00649         //          }
00650         iGeom_dtor(igeom->instance(), &err);
00651         ERRORR("Failed in call iGeom_dtor", err);
00652     }
00653 #endif
00654     return 0;
00655 }
00656 
00657 int CoreGen::distribute_mesh(const int nrank, int numprocs)
00658 // -------------------------------------------------------------------------------------------
00659 // Function: merge the nodes within a set tolerance in the model
00660 // Input:    none
00661 // Output:   none
00662 // -------------------------------------------------------------------------------------------
00663 {
00664     int nback = files.size() - nassys;
00665     if(nrank < ((int) core_alias.size() + nback)){
00666         if(numprocs > (int) core_alias.size()){
00667             numprocs =  core_alias.size() + nback;
00668         }
00669 #ifdef USE_MPI
00670         std::vector<int> rank_load;
00671         rank_load.resize(numprocs);
00672         int extra_procs = numprocs - files.size();
00673         if(numprocs >= (int) files.size() && numprocs <= (tot_assys + nback)){
00674             // again fill assm_meshfiles
00675             for(int p=0; p<nassys; p++)
00676                 assm_meshfiles[p]=0;
00677             for(int p=0; p<tot_assys; p++){
00678                 for(int q=0; q<nassys; q++){
00679                     if(strcmp(core_alias[p].c_str(), assm_alias[q].c_str()) ==0) {
00680                         assm_meshfiles[q]+=1;
00681                     }
00682                 }
00683             }
00684             //distribute
00685             for(int i=0; i<  (int)files.size(); i++){
00686                 rank_load[i] = i;
00687             }
00688 
00689             int temp = 0;
00690             int assm_load = - 1;
00691             int e= 0;
00692             // compute the rank, mf vector for extra procs
00693             while(e < extra_procs){
00694                 for(int i = 0; i < nassys; i++){
00695                     if (assm_meshfiles[i] > temp){
00696                         temp = assm_meshfiles[i];
00697                         assm_load = i;
00698                     }
00699                     else if (assm_load == -1){
00700                         logfile << "No assemblies mesh files used in core" << std::endl;
00701                         exit(0);
00702                     }
00703                 }
00704                 assm_meshfiles[assm_load]-=1;
00705                 int temp_rank = files.size()+ e;
00706                 rank_load[temp_rank] = assm_load;
00707                 e++;
00708                 temp = 0;
00709             }
00710         }
00711         else{
00712             logfile << "Warning: #procs <= #assys in core, some processor will be idle" << std::endl;
00713         }
00714 
00715         std::vector<int> times_loaded(nassys);
00716         std::vector<std::vector<int> > meshfiles_rank (files.size());
00717         for(int i=0; i < (int) files.size(); i++){
00718             for(int j=0; j < (int) rank_load.size(); j++){
00719                 if(rank_load[j]==i){
00720                     meshfiles_rank[i].push_back(j);
00721                     times_loaded[i]+=1;
00722                 }
00723             }
00724         }
00725 
00726         position_core.resize(numprocs);
00727 
00728         for(int i=0; i < (int) files.size(); i++){
00729             int k = 0;
00730             if(i < (nassys) ){
00731                 for(int j=0; j < (int) assm_location[i].size(); j++){
00732                     if (k >= (int) meshfiles_rank[i].size()){
00733                         k = 0;
00734                     }
00735                     int p = meshfiles_rank[i][k];
00736                     int q = assm_location[i][j];
00737                     position_core[p].push_back(q);
00738 
00739                     ++k;
00740                 }
00741             }
00742             else{
00743                 // this is background mesh set it -2, no meshfile to copy/move
00744                 position_core[i].push_back(-2);
00745             }
00746         }
00747 
00748         if(nrank == 0){
00749             logfile << " copy/move task distribution " << std::endl;
00750             for(int i =0; i< numprocs; i++){
00751                 logfile << "rank: " << i <<  " positions : ";
00752                 for(int j=0; j< (int) position_core[i].size(); j++){
00753                     logfile << (int) position_core[i][j] << " ";
00754                 }
00755                 logfile << "\n" << std::endl;
00756             }
00757         }
00758 #endif
00759     }
00760     return 0;
00761 }
00762 
00763 int CoreGen::load_meshes_parallel(const int nrank, int numprocs)
00764 // ---------------------------------------------------------------------------
00765 // Function: loads all the meshes and initializes copymesh object
00766 // Input:    none
00767 // Output:   none
00768 // ---------------------------------------------------------------------------
00769 {
00770     int nback = files.size() - nassys;
00771     cm.resize(files.size());
00772 
00773     iMesh_getRootSet(imesh->instance(), &root_set, &err);
00774     ERRORR("Couldn't get the root set", err);
00775     if(nrank < ((int) core_alias.size() + nback)){
00776         if(numprocs > (int) core_alias.size()){
00777             numprocs =  core_alias.size() + nback;
00778         }
00779 
00780 #ifdef USE_MPI
00781         if(numprocs > ((int) core_alias.size() + nback)){
00782             logfile << "Warning: #procs <= #assys in core, some processor will be idle" << std::endl;
00783         }
00784 
00785         iBase_EntitySetHandle orig_set;
00786         int temp_index;
00787         int extra_procs = numprocs - files.size();
00788         std::vector<int> rank_load;
00789         for(int i = 0; i< (int) files.size(); i++){
00790             temp_index = numprocs*i + nrank;
00791             if(temp_index >= (int) files.size()){
00792                 if (nrank >= (int) files.size() && nrank <= (tot_assys + nback)){
00793                     int temp = 1;
00794                     int p = extra_procs;
00795                     // compute the rank, mf vector for extra procs
00796                     while(p !=0){
00797                         int assm_load = - 1;
00798                         for(int i = 0; i < nassys; i++){
00799                             if ((int) assm_meshfiles[i] > temp){
00800                                 temp = assm_meshfiles[i];
00801                                 assm_load = i;
00802                             }
00803                         }
00804                         if (assm_load == -1){
00805                             continue;
00806                             logfile << "Warning: #procs <= #assys in core, some processor will be idle" << std::endl;
00807                         }
00808                         assm_meshfiles[assm_load]-=1;
00809                         rank_load.push_back(assm_load);
00810                         --p;
00811                         temp = 1;
00812                     }
00813 
00814                     temp_index = nrank - files.size();
00815                     iMesh_createEntSet(imesh->instance(), 0, &orig_set, &err);
00816                     ERRORR("Couldn't create file set.", err);
00817 
00818                     // load this file
00819                     iMesh_load(imesh->instance(), orig_set, files[rank_load[temp_index]].c_str(), NULL, &err, strlen(files[rank_load[temp_index]].c_str()), 0);
00820                     ERRORR("Couldn't read mesh file.", err);
00821                     logfile << "Loaded mesh file " << rank_load[temp_index] << " in processor: " << nrank << std::endl;
00822 
00823                     ModelEnt *me;
00824                     me = NULL;
00825                     me = new ModelEnt(mk_core(), iBase_EntitySetHandle(0), /*igeom instance*/0, (moab::EntityHandle)orig_set, 0);
00826                     MEntVector assm_set;
00827                     assm_set.push_back(me);
00828                     cm[i] = (CopyMesh*) mk_core()->construct_meshop("CopyMesh", assm_set);
00829                     cm[i]->set_name("copy_move_mesh");
00830 
00831                     assys.push_back(orig_set);
00832                     assys_index.push_back(rank_load[temp_index]);
00833                     break;
00834                 }
00835             }
00836             else{
00837                 iMesh_createEntSet(imesh->instance(), 0, &orig_set, &err);
00838                 ERRORR("Couldn't create file set.", err);
00839 
00840                 // load this file
00841                 iMesh_load(imesh->instance(), orig_set, files[temp_index].c_str(), NULL, &err, strlen(files[temp_index].c_str()), 0);
00842                 ERRORR("Couldn't read mesh file.", err);
00843                 logfile << "Loaded mesh file " << temp_index << " in processor: " << nrank << std::endl;
00844 
00845                 ModelEnt *me;
00846                 me = NULL;
00847                 me = new ModelEnt(mk_core(), iBase_EntitySetHandle(0), /*igeom instance*/0, (moab::EntityHandle)orig_set, 0);
00848                 MEntVector assm_set;
00849                 assm_set.push_back(me);
00850                 cm[i] = (CopyMesh*) mk_core()->construct_meshop("CopyMesh", assm_set);
00851                 cm[i]->set_name("copy_move_mesh");
00852 
00853                 assys.push_back(orig_set);
00854                 assys_index.push_back(temp_index);
00855             }
00856         }
00857 
00858         // create cm instances for each mesh file
00859         //cm = new CopyMesh*[assys.size()];
00860         //        for (unsigned int i = 0; i < assys.size(); i++) {
00861         //            cm[i] = new CopyMesh(impl);
00862         //          }
00863 #endif
00864     }
00865     return iBase_SUCCESS;
00866 }
00867 
00868 int CoreGen::close()
00869 // ---------------------------------------------------------------------------
00870 // Function: dellocating
00871 // Input:    none
00872 // Output:   none
00873 // ---------------------------------------------------------------------------
00874 {
00875     // deallocate ... deallocate ... deallocate
00876     if (prob_type == "mesh") {
00877         //        for (unsigned int i = 0; i < files.size(); i++) {
00878         //            delete cm[i];
00879         //          }
00880 
00881         iMesh_dtor(imesh->instance(), &err);
00882         ERRORR("Failed in call iMesh_dtor", err);
00883 
00884     }
00885     if (prob_type == "geometry") {
00886         //        for (unsigned int i = 0; i < files.size(); i++) {
00887         //            delete cg[i];
00888         //          }
00889         iGeom_dtor(igeom->instance(), &err);
00890         ERRORR("Failed in call iGeom_dtor", err);
00891     }
00892     return 0;
00893 }
00894 
00895 int CoreGen::load_meshes()
00896 // ---------------------------------------------------------------------------
00897 // Function: loads all the meshes and initializes copymesh and merge mesh objects
00898 // Input:    none
00899 // Output:   none
00900 // ---------------------------------------------------------------------------
00901 {
00902     // make a mesh instance
00903     // iMesh_newMesh("MOAB", imesh->instance(), &err, 4);
00904     // ERRORR("Failed to create instance.", 1);
00905 
00906     //    iMesh_getRootSet(imesh->instance(), &root_set, &err);
00907     //    ERRORR("Couldn't get the root set", err);
00908 
00909     // create cm instances for each mesh file
00910     cm.resize(files.size());
00911     //    for (unsigned int i = 0; i < files.size(); i++) {
00912     //        cm[i] = new CopyMesh(impl);
00913     //      }
00914 
00915     iBase_EntitySetHandle orig_set;
00916 
00917     // loop reading all mesh files
00918     for (unsigned int i = 0; i < files.size(); i++) {
00919         iMesh_createEntSet(imesh->instance(), 0, &orig_set, &err);
00920         ERRORR("Couldn't create file set.", err);
00921         logfile << "Loading File: " << files[i].c_str() << std::endl;
00922         iMesh_load(imesh->instance(), orig_set, files[i].c_str(), NULL, &err, strlen(files[i].c_str()), 0);
00923         ERRORR("Couldn't read mesh file.", err);
00924         mk_core()->populate_model_ents(0,0,0);
00925         ModelEnt *me;
00926         me = NULL;
00927         me = new ModelEnt(mk_core(), iBase_EntitySetHandle(0), /*igeom instance*/0, (moab::EntityHandle)orig_set, 0);
00928         MEntVector assm_set;
00929         //assm_set.clear();
00930         assm_set.push_back(me);
00931         cm[i] = (CopyMesh*) mk_core()->construct_meshop("CopyMesh", assm_set);
00932         cm[i]->set_name("copy_move_mesh");
00933         // cm[i]->copy_sets().add_set(orig_set);
00934         assys.push_back(orig_set);
00935         assys_index.push_back(i);
00936     }
00937     logfile << "Loaded mesh files." << std::endl;
00938 
00939     return iBase_SUCCESS;
00940 }
00941 
00942 int CoreGen::load_geometries()
00943 // ---------------------------------------------------------------------------
00944 // Function: loads all the meshes and initializes copymesh and merge mesh objects
00945 // Input:    none
00946 // Output:   none
00947 // ---------------------------------------------------------------------------
00948 {
00949     logfile << "\n--Loading geometry files." << std::endl;
00950     // make a mesh instance
00951     //    iGeom_newGeom("GEOM", igeom->instance(), &err, 4);
00952     //    ERRORR("Failed to create instance.", 1);
00953 
00954     iGeom_getRootSet(igeom->instance(), &root_set, &err);
00955     ERRORR("Couldn't get the root set", err);
00956 
00957     // create cm instances for each mesh file
00958     cg.resize(files.size());
00959 
00960     iBase_EntitySetHandle orig_set, temp_set, temp_set1;
00961 
00962     iGeom_createEntSet(igeom->instance(), 0, &temp_set1, &err);
00963     ERRORR( "Problem creating entity set.",err );
00964 
00965     // loop reading all igeom->instance() files
00966     for (unsigned int i = 0; i < files.size(); i++) {
00967         iGeom_createEntSet(igeom->instance(), 0, &orig_set, &err);
00968         ERRORR( "Problem creating entity set.", err);
00969 
00970         iGeom_createEntSet(igeom->instance(), 0, &temp_set, &err);
00971         ERRORR( "Problem creating entity set.",err );
00972 
00973         iGeom_load(igeom->instance(), files[i].c_str(), NULL, &err,
00974                    strlen(files[i].c_str()), 0);
00975         ERRORR("Couldn't read geometry file.", err);
00976 
00977         iBase_EntityHandle *entities = NULL;
00978         int entities_ehsize = 0, entities_ehallocated = 0;
00979 
00980         iGeom_getEntities(igeom->instance(), root_set, iBase_REGION, &entities,
00981                           &entities_ehsize, &entities_ehallocated, &err);
00982         ERRORR( "Problem getting entities." , err);
00983 
00984         // add the entity
00985         for (int j = 0; j < entities_ehsize; j++) {
00986             iGeom_addEntToSet(igeom->instance(), entities[j], temp_set, &err);
00987             ERRORR( "Problem adding to set.", err );
00988         }
00989 
00990         iGeom_subtract(igeom->instance(), temp_set, temp_set1, &orig_set, &err);
00991         ERRORR( "Unable to subtract entity sets.", err );
00992 
00993         MEntVector vols;
00994         cg[i] = (CopyGeom*) mk_core()->construct_meshop("CopyGeom", vols);
00995         cg[i]->set_name("copy_move_geom");
00996 
00997         assys.push_back(orig_set);
00998 
00999         assys_index.push_back(i);
01000 
01001         // store this set for subtraction with next entity set
01002         temp_set1 = temp_set;
01003     }
01004     logfile << "\n--Loaded geometry files.\n" << std::endl;
01005 
01006     return iBase_SUCCESS;
01007 }
01008 
01009 int CoreGen::move_verts(iBase_EntitySetHandle set, const double *dx)
01010 // ---------------------------------------------------------------------------
01011 // Function: Change the coordinates for moving the assembly to its first loc.
01012 // Input:    none
01013 // Output:   none
01014 // ---------------------------------------------------------------------------
01015 {
01016 
01017     int verts_ents_alloc = 0, verts_ents_size = 0;
01018     iBase_EntityHandle *verts_ents = NULL;
01019 
01020     iMesh_getEntities(imesh->instance(), set, iBase_VERTEX, iMesh_ALL_TOPOLOGIES,
01021                       &verts_ents, &verts_ents_alloc, &verts_ents_size, &err);
01022     ERRORR("Failed to get any entities from original set.", iBase_FAILURE);
01023 
01024     double *coords = 0;
01025     int coords_alloc = 0, coords_size = 0;
01026 
01027     iMesh_getVtxArrCoords(imesh->instance(), verts_ents, verts_ents_size, iBase_INTERLEAVED,
01028                           &coords, &coords_alloc, &coords_size, &err);
01029     ERRORR("Failed to get vtx coords from set.", iBase_FAILURE);
01030 
01031     for (int i = 0; i < verts_ents_size; i++) {
01032         coords[3 * i] += dx[0];
01033         coords[3 * i + 1] += dx[1];
01034         coords[3 * i + 2] += dx[2];
01035     }
01036 
01037     iMesh_setVtxArrCoords(imesh->instance(), verts_ents, verts_ents_size, iBase_INTERLEAVED,
01038                           coords, coords_size, &err);
01039     ERRORR("Failed to set vtx coords.", iBase_FAILURE);
01040 
01041     return iBase_SUCCESS;
01042 }
01043 
01044 int CoreGen::move_geoms(iBase_EntitySetHandle set, const double *dx)
01045 // ---------------------------------------------------------------------------
01046 // Function: Change the coordinates for moving the assembly to its first loc.
01047 // Input:    none
01048 // Output:   none
01049 // ---------------------------------------------------------------------------
01050 {
01051     int entities_ehsize = 0, entities_ehallocated = 0;
01052     iBase_EntityHandle *entities = NULL;
01053 
01054     iGeom_getEntities(igeom->instance(), set, iBase_ALL_TYPES, &entities, &entities_ehsize,
01055                       &entities_ehallocated, &err);
01056     ERRORR("Failed to get entities from set.", iBase_FAILURE);
01057 
01058     for (int i = 0; i < entities_ehsize; i++) {
01059         iGeom_moveEnt(igeom->instance(), entities[i], dx[0], dx[1], dx[2], &err);
01060         ERRORR("Failed to move geometries.", iBase_FAILURE);
01061     }
01062     return iBase_SUCCESS;
01063 }
01064 
01065 int CoreGen::assign_gids() {
01066     // ---------------------------------------------------------------------------
01067     // Function: assign global ids
01068     // Input:    none
01069     // Output:   none
01070     // ---------------------------------------------------------------------------
01071     // assign new global ids
01072     //    if (global_ids == true){
01073     //        logfile << "Assigning global ids." << std::endl;
01074     //        mu = new MKUtils(impl);
01075     //        err = mu->assign_global_ids(root_set, 3, 1, true, false,
01076     //                                    "GLOBAL_ID");
01077     //        ERRORR("Error assigning global ids.", err);
01078     //      }
01079     //    delete mu;
01080     return iBase_SUCCESS;
01081 
01082 
01083     // // assign new global ids
01084     // if (global_ids == true) {
01085     //   logfile << "Assigning global ids." << std::endl;
01086     //   err = pc->assign_global_ids(0, 3, 1, false, false, false);
01087     //   ERRORR("Error assigning global ids.", err);
01088     // }
01089 
01090     // // assign global ids for all entity sets
01091     // SimpleArray<iBase_EntitySetHandle> sets;
01092     // iBase_TagHandle gid_tag;
01093     // const char *tag_name = "GLOBAL_ID";
01094     // iMesh_getTagHandle(imesh->instance(), tag_name, &gid_tag, &err, 9);
01095     // if (iBase_TAG_NOT_FOUND == err) {
01096     //   iMesh_createTag(imesh->instance(), tag_name, 1, iBase_INTEGER,
01097     //                   &gid_tag, &err, strlen(tag_name));
01098     //   ERRORR("Couldn't create global id tag", err);
01099     // }
01100 
01101     // iMesh_getEntSets(imesh->instance(),root_set, 1, ARRAY_INOUT(sets), &err);
01102     // ERRORR("Failed to get contained sets.", err);
01103 
01104     // for(int id = 1; id <= sets.size(); id++){
01105     //   iMesh_setEntSetIntData(imesh->instance(), sets[id-1], gid_tag, id, &err);
01106     //   ERRORR("Failed to set tags on sets.", err);
01107     // }
01108     // return iBase_SUCCESS;
01109 }
01110 
01111 int CoreGen::extrude() {
01112     // ---------------------------------------------------------------------------
01113     // Function: extrude 2D surface core
01114     // Input:    none
01115     // Output:   none
01116     // ---------------------------------------------------------------------------
01117     // extrude if this is a surface mesh
01118     if (set_DIM == 2 && extrude_flag == true) { // if surface geometry and extrude
01119         logfile << "Extruding surface mesh." << std::endl;
01120 
01121         double v[] = { 0, 0, z_height };
01122         int steps = z_divisions;
01123 
01124         ModelEnt me(mk_core(), iBase_EntitySetHandle(0), /*igeom instance*/0,
01125                     0);
01126         MEntVector selection;
01127         selection.push_back(&me);
01128 
01129         em = (ExtrudeMesh*) mk_core()->construct_meshop("ExtrudeMesh",
01130                                                         selection);
01131 
01132         em->set_transform(Extrude::Translate(v, steps));
01133         em->copy_faces(true);
01134 
01135         em->execute_this();
01136 
01137     }
01138 
01139     return iBase_SUCCESS;
01140 }
01141 
01142 int CoreGen::create_neumannset() {
01143     // ---------------------------------------------------------------------------
01144     // Function: create Neumann set on the whole core
01145     // Input:    none
01146     // Output:   none
01147     // ---------------------------------------------------------------------------
01148     if (nss_flag == true || nsb_flag == true || nst_flag == true) {
01149         logfile << "Creating NeumannSet." << std::endl;
01150 
01151         if (extrude_flag == true)
01152             set_DIM = 3;
01153 
01154 #ifdef HAVE_MOAB
01155         int err = 0, z_flag = 0, i, ents_alloc = 0, ents_size;
01156         double z1 = 0.0;
01157         iBase_TagHandle ntag1, gtag1;
01158         iBase_EntityHandle *ents = NULL;
01159         iBase_EntitySetHandle set = NULL, set_z1 = NULL, set_z2 = NULL;
01160         std::vector<iBase_EntitySetHandle> set_side;
01161 
01162         //get entities for skinner
01163         if(set_DIM ==2) { // if surface geometry specified
01164             iMesh_getEntities(imesh->instance(), root_set,
01165                               iBase_FACE, iMesh_ALL_TOPOLOGIES,
01166                               &ents, &ents_alloc, &ents_size, &err);
01167         }
01168         else {
01169             iMesh_getEntities(imesh->instance(), root_set,
01170                               iBase_REGION, iMesh_ALL_TOPOLOGIES,
01171                               &ents, &ents_alloc, &ents_size, &err);
01172         }
01173         ERRORR("Trouble getting entities for specifying neumannsets via skinner.", err);
01174 
01175         // get tag handle
01176         const char *tag_neumann1 = "NEUMANN_SET";
01177         const char *global_id1 = "GLOBAL_ID";
01178 
01179         iMesh_getTagHandle(imesh->instance(), tag_neumann1, &ntag1, &err, 12);
01180         ERRORR("Trouble getting handle.", err);
01181 
01182         iMesh_getTagHandle(imesh->instance(), global_id1, &gtag1, &err, 9);
01183         ERRORR("Trouble getting handle.", err);
01184 
01185         iMesh_createEntSet(imesh->instance(),0, &set, &err); // for all other sides
01186         ERRORR("Trouble creating set handle.", err);
01187 
01188         if (set_DIM == 3) { // sets for collecting top and bottom surface
01189             iMesh_createEntSet(imesh->instance(),0, &set_z1, &err);
01190             ERRORR("Trouble creating set handle.", err);
01191 
01192             iMesh_createEntSet(imesh->instance(),0, &set_z2, &err);
01193             ERRORR("Trouble creating set handle.", err);
01194 
01195             set_side.resize(num_nsside);
01196             for(int i=0; i<num_nsside; i++){
01197                 iMesh_createEntSet(imesh->instance(),0, &set_side[i], &err);
01198                 ERRORR("Trouble creating set handle.", err);
01199             }
01200         }
01201 
01202         MBRange tmp_elems;
01203         tmp_elems.insert((MBEntityHandle*)ents, (MBEntityHandle*)ents + ents_size);
01204 
01205         // get the skin of the entities
01206         MBSkinner skinner(mb);
01207         MBRange skin_range;
01208         MBErrorCode result;
01209         MBRange::iterator rit;
01210 
01211         result = skinner.find_skin(0, tmp_elems, set_DIM-1, skin_range);
01212         if (MB_SUCCESS != result) return result;
01213 
01214         for (rit = skin_range.begin(), i = 0; rit != skin_range.end(); rit++, i++) {
01215             if(set_DIM == 3) { // filter top and bottom
01216                 int num_vertex=0, size_vertex =0;
01217                 iBase_EntityHandle *vertex = NULL;
01218                 iMesh_getEntAdj(imesh->instance(), (iBase_EntityHandle)(*rit), iBase_VERTEX, &vertex,
01219                                 &num_vertex, &size_vertex, &err);
01220                 ERRORR("Trouble getting number of entities after merge.", err);
01221 
01222                 double *coords = NULL;
01223                 int coords_alloc = 0, coords_size=0;
01224                 iMesh_getVtxArrCoords(imesh->instance(), vertex, size_vertex, iBase_INTERLEAVED,
01225                                       &coords, &coords_alloc, &coords_size, &err);
01226                 ERRORR("Trouble getting number of entities after merge.", err);
01227                 double ztemp = coords[2];
01228                 int flag = 0;
01229                 for (int p=1; p<num_vertex; p++) {
01230                     double z1 = coords[3*p+2];
01231                     if( ztemp != z1) {
01232                         flag = 1;
01233                         continue;
01234                     }
01235                 }
01236                 if(flag == 0) { // this is top or bottom surface
01237                     if (z_flag == 0) { // store z-coord this is the first call
01238                         z_flag = 1;
01239                         z1 = ztemp;
01240                     }
01241                     if( ztemp == z1) {
01242                         iMesh_addEntToSet(imesh->instance(), (iBase_EntityHandle)(*rit), set_z1, &err);
01243                         ERRORR("Trouble getting number of entities after merge.", err);
01244                     }
01245                     else {
01246                         iMesh_addEntToSet(imesh->instance(), (iBase_EntityHandle)(*rit), set_z2, &err);
01247                         ERRORR("Trouble getting number of entities after merge.", err);
01248                     }
01249                 }
01250                 else if (flag == 1) { // add the faces that are not top or bottom surface
01251                     // filter the sidesets based on their x and y coords
01252 
01253                     for(int k=0; k<num_nsside; k++){
01254                         if ( fabs((coords[0])*nsx[k] + (coords[1])*nsy[k] + nsc[k]) <= merge_tol
01255                              && fabs((coords[3])*nsx[k] + (coords[4])*nsy[k] + nsc[k]) <= merge_tol
01256                              && fabs((coords[6])*nsx[k] + (coords[7])*nsy[k] + nsc[k]) <= merge_tol) {
01257                             iMesh_addEntToSet(imesh->instance(), (iBase_EntityHandle)(*rit), (iBase_EntitySetHandle) set_side[k], &err);
01258                             ERRORR("Trouble getting number of entities after merge.", err);
01259                             continue;
01260                         }
01261                         else{ // outside the specified
01262                             iMesh_addEntToSet(imesh->instance(), (iBase_EntityHandle)(*rit), set, &err);
01263                             ERRORR("Trouble getting number of entities after merge.", err);
01264                             continue;
01265                         }
01266 
01267                     }
01268                 }
01269             }
01270             else if(set_DIM == 2) { // edges add all for sideset
01271                 iMesh_addEntToSet(imesh->instance(), (iBase_EntityHandle)(*rit), set, &err);
01272                 ERRORR("Trouble getting number of entities after merge.", err);
01273             }
01274         }
01275 
01276         iMesh_setEntSetIntData( imesh->instance(), set, ntag1, 99999, &err);
01277         ERRORR("Trouble getting handle.", err);
01278 
01279         iMesh_setEntSetIntData( imesh->instance(), set, gtag1, 99999, &err);
01280         ERRORR("Trouble getting handle.", err);
01281 
01282         if (set_DIM == 3) {
01283             if (nst_flag == true || nsb_flag == true) {
01284 
01285                 iMesh_setEntSetIntData( imesh->instance(), set_z1, ntag1, nst_Id, &err);
01286                 ERRORR("Trouble getting handle.", err);
01287 
01288                 iMesh_setEntSetIntData( imesh->instance(), set_z1, gtag1, nst_Id, &err);
01289                 ERRORR("Trouble getting handle.", err);
01290 
01291                 iMesh_setEntSetIntData( imesh->instance(), set_z2, ntag1, nsb_Id, &err);
01292                 ERRORR("Trouble getting handle.", err);
01293 
01294                 iMesh_setEntSetIntData( imesh->instance(), set_z2, gtag1, nsb_Id, &err);
01295                 ERRORR("Trouble getting handle.", err);
01296 
01297                 for(int j=0; j<num_nsside; j++){
01298                     iMesh_setEntSetIntData( imesh->instance(), set_side[j], ntag1, nss_Id[j], &err);
01299                     ERRORR("Trouble getting handle.", err);
01300 
01301                     iMesh_setEntSetIntData( imesh->instance(), set_side[j], gtag1, nss_Id[j], &err);
01302                     ERRORR("Trouble getting handle.", err);
01303                 }
01304             }
01305         }
01306 #endif
01307     }
01308     return iBase_SUCCESS;
01309 }
01310 
01311 void CoreGen::IOErrorHandler (ErrorStates ECode) const
01312 // ---------------------------------------------------------------------------
01313 // Function: displays error messages related to input file parsing data
01314 // Input:    error code
01315 // Output:   none
01316 // ---------------------------------------------------------------------------
01317 {
01318     std::cerr << '\n';
01319     if (ECode == INVALIDINPUT) // invalid input
01320         std::cerr << "Invalid input.";
01321     else if (ECode == ENEGATIVE) // invalid input
01322         std::cerr << "Unexpected negative value.";
01323     else
01324         std::cerr << "Unknown error ...?";
01325 
01326     std::cerr << '\n' << "Error reading input file, line : " << linenumber;
01327     std::cerr << std::endl;
01328     exit (1);
01329 }
01330 
01331 int CoreGen::write_minfofile()
01332 // ---------------------------------------------------------------------------
01333 // Function: write the spreadsheet mesh info file based on inputs read from mesh & input file
01334 // Input:    none
01335 // Output:   none
01336 // ---------------------------------------------------------------------------
01337 {
01338     logfile << "Writing mesh info file indicating elements and pin number" << std::endl;
01339 
01340     moab::Tag ntag;
01341     mb->tag_get_handle(NAME_TAG_NAME, NAME_TAG_SIZE, MB_TYPE_OPAQUE, ntag);
01342     moab::Tag mattag;
01343     mb->tag_get_handle( "MATERIAL_SET", 1, MB_TYPE_INTEGER, mattag );
01344     int rval = 0;
01345     moab::Range matsets;
01346     std::vector <EntityHandle> set_ents;
01347 
01348     mb->get_entities_by_type_and_tag( 0, MBENTITYSET, &mattag, 0, 1, matsets );
01349 
01350     moab::Range::iterator set_it;
01351     for (set_it = matsets.begin(); set_it != matsets.end(); set_it++)  {
01352         moab::EntityHandle this_set = *set_it;
01353 
01354         // get the id for this set
01355         int set_id;
01356         rval = mb->tag_get_data(mattag, &this_set, 1, &set_id);
01357         if(rval != moab::MB_SUCCESS) {
01358             std::cerr<<"getting tag data failed Code:";
01359             std::string foo = ""; mb->get_last_error(foo);
01360             std::cerr<<"File Error: "<<foo<<std::endl;
01361             return 1;
01362         }
01363         char name[NAME_TAG_SIZE];
01364         rval = mb->tag_get_data(ntag, &this_set, 1, &name);
01365         if(rval != moab::MB_SUCCESS) {
01366             std::cerr<<"getting tag data failed Code:";
01367             std::string foo = ""; mb->get_last_error(foo);
01368             std::cerr<<"File Error: "<<foo<<std::endl;
01369             return 1;
01370         }
01371         // check for the special block _xp created in AssyGen stage
01372         // now print out elements for each pin on the mesh info file
01373         if(name[0]=='_' && name[1]=='x' && name[2] == 'p'){
01374 
01375             // get the entities in the set, recursively
01376             rval = mb->get_entities_by_dimension(this_set, 3, set_ents, true);
01377 
01378             logfile << "Block: " << set_id << " has "
01379                     << set_ents.size() << " entities. Name = " << name;
01380 
01381             // loop thro' all the elements in all the sets
01382             for (int i=0;i<int(set_ents.size());i++){
01383 
01384                 std::vector<EntityHandle> conn;
01385                 EntityHandle handle = set_ents[i];
01386 
01387                 // get the connectivity of this element
01388                 rval = mb->get_connectivity(&handle, 1, conn);
01389                 double coords[3];
01390                 double x_sum = 0.0, y_sum = 0.0, z_sum = 0.0;
01391                 for (int j = 0; j<int(conn.size()); ++j){
01392                     rval = mb->get_coords(&conn[j], 1, coords);
01393                     x_sum+=coords[0];
01394                     y_sum+=coords[1];
01395                     z_sum+=coords[2];
01396                 }
01397                 int p = 3;
01398                 while(name[p]!='\0'){
01399                     minfo_file << name[p];
01400                     ++p;
01401                 }
01402                 minfo_file << " \t" << x_sum/conn.size() << " \t" << y_sum/conn.size() << " \t" <<  z_sum/conn.size() <<  std::endl;
01403             }
01404             logfile << ". Deleting block " << set_id << std::endl;
01405             mb->delete_entities(&this_set, 1);
01406             set_ents.clear();
01407         }
01408 
01409     }
01410     return 0;
01411 }
01412 
01413 
01414 int CoreGen::prepareIO(int argc, char *argv[], int nrank, int nprocs, std::string  TestDir)
01415 // -----------------------------------------------------------------------------------
01416 // Function: Obtains file names and opens input/output files and then read/write them
01417 // Input:    command line arguments
01418 // Output:   none
01419 // -----------------------------------------------------------------------------------
01420 {
01421     // set rank and total number of processors
01422     rank = nrank;
01423     procs = nprocs;
01424     sTime = clock();
01425     testdir = TestDir;
01426     std::cout << testdir << std::endl;
01427     if (argc > 1) {
01428         if (argv[1][0] == '-' && argv[1][1] == 'm') {
01429             // set to zero, when run_flag = 1, program runs and does copy, move, merge, extrude, assign gids, save and close
01430             run_flag = 0;
01431         }
01432     }
01433 
01434     bool bDone = false;
01435     do {
01436         if (2 == argc) {
01437             if (argv[1][0] == '-' && nrank == 0) {
01438                 if (argv[1][1] == 'h') {
01439                     logfile << "Usage: coregen [-t -m -h] <coregen input file>"
01440                             << std::endl;
01441                     logfile << "        -t print timing and memory usage info in each step"
01442                             << std::endl;
01443                     logfile << "        -m create makefile only" << std::endl;
01444                     logfile << "        -h print help" << std::endl;
01445                     logfile << "\nInstruction on writing coregen input file can be found at: "
01446                             << std::endl;
01447                     logfile << "        https://trac.mcs.anl.gov/projects/fathom/browser/MeshKit/trunk/rgg/README"
01448                             << std::endl;
01449                     exit(0);
01450                 }
01451             }
01452 
01453             iname = argv[1];
01454             ifile = iname + ".inp";
01455             outfile = iname + ".h5m";
01456             mfile = iname + ".makefile";
01457             infofile = iname + "_info.csv";
01458             minfofile = iname + "_mesh_info.csv";
01459             logfilename = iname + ".log";
01460         } else if (3 == argc) {
01461             int i = 1;// will loop through arguments, and process them
01462             for (i = 1; i < argc - 1; i++) {
01463                 if (argv[i][0] == '-') {
01464                     switch (argv[i][1]) {
01465                     case 'm': {
01466                         if (nrank == 0) {
01467                             logfile << "Creating Make/Info file Only" << std::endl;
01468                         }
01469                         // only makefile creation specified
01470                         iname = argv[2];
01471                         ifile = iname + ".inp";
01472                         outfile = iname + ".h5m";
01473                         mfile = iname + ".makefile";
01474                         infofile = iname + "_info.csv";
01475                         minfofile = iname + "_mesh_info.csv";
01476                         logfilename = iname + ".log";
01477                         break;
01478                     }
01479                     case 't': {
01480                         mem_tflag = true;
01481                         iname = argv[2];
01482                         ifile = iname + ".inp";
01483                         outfile = iname + ".h5m";
01484                         mfile = iname + ".makefile";
01485                         infofile = iname + "_info.csv";
01486                         minfofile = iname + "_mesh_info.csv";
01487                         logfilename = iname + ".log";
01488                         break;
01489                     }
01490                     case 'h': {
01491                         if (nrank == 0) {
01492                             logfile << "Usage: coregen [-t -m -h] <coregen input file>"
01493                                     << std::endl;
01494                             logfile << "        -t print timing and memory usage info in each step"
01495                                     << std::endl;
01496                             logfile << "        -m create makefile only"
01497                                     << std::endl;
01498                             logfile << "        -h print help" << std::endl;
01499                             logfile << "\nInstruction on writing coregen input file can also be found at: "
01500                                     << std::endl;
01501                             logfile << "        https://trac.mcs.anl.gov/projects/fathom/browser/MeshKit/trunk/rgg/README"
01502                                     << std::endl;
01503                             exit(0);
01504                             break;
01505                         }
01506                     }
01507                     }
01508                 }
01509             }
01510         } else { //default case
01511             if (nrank == 0) {
01512                 std::cerr << "Usage: " << argv[0]
01513                           << " <input file> WITHOUT EXTENSION" << std::endl;
01514                 logfile << "  No file specified.  Defaulting to: "
01515                         << COREGEN_DEFAULT_TEST_FILE << std::endl;
01516             }
01517             iname = TestDir + "/" + COREGEN_DEFAULT_TEST_FILE;
01518             ifile = iname + ".inp";
01519             std::string temp = CTEST_FILE_NAME;
01520             outfile = temp + ".h5m";
01521             mfile = temp + ".makefile";
01522             infofile = temp + "_info.csv";
01523             minfofile = temp + "_mesh_info.csv";
01524             logfilename = temp + ".log";
01525         }
01526 
01527         // open the file
01528         file_input.open(ifile.c_str(), std::ios::in);
01529         logfile.coss.open(logfilename.c_str(), std::ios::out);
01530 
01531         /*********************************************/
01532         // Print banner on logfile and std out
01533         /*********************************************/
01534         if (rank == 0) {
01535             banner();
01536             Timer.GetDateTime(szDateTime);
01537             logfile << "\nStarting out at : " << szDateTime << "\n";
01538         }
01539 
01540         if (!file_input) {
01541             if (nrank == 0) {
01542                 logfile << "Unable to open file" << std::endl;
01543                 logfile << "Usage: coregen [-t -m -h] <coregen input file>"
01544                         << std::endl;
01545                 logfile << "        -t print timing and memory usage info in each step"
01546                         << std::endl;
01547                 logfile << "        -m create makefile only" << std::endl;
01548                 logfile << "        -h print help" << std::endl;
01549                 logfile << "\nInstruction on writing coregen input file can be found at: "
01550                         << std::endl;
01551                 logfile << "        https://trac.mcs.anl.gov/projects/fathom/browser/MeshKit/trunk/rgg/README"
01552                         << std::endl;
01553             }
01554             file_input.clear();
01555             exit(1);
01556         } else
01557             bDone = true; // file opened successfully
01558     } while (!bDone);
01559 
01560     // open Makefile-rgg
01561     do {
01562         make_file.open(mfile.c_str(), std::ios::out);
01563         if (!make_file) {
01564             if (nrank == 0) {
01565                 logfile << "Unable to open makefile for writing" << std::endl;
01566             }
01567             make_file.clear();
01568         } else
01569             bDone = true; // file opened successfully
01570     } while (!bDone);
01571 
01572     if (nrank == 0) {
01573         logfile << "\nEntered input file name: " << ifile << std::endl;
01574     }
01575 
01576     // now call the functions to read and write
01577     err = read_inputs_phase1();
01578     ERRORR("Failed to read inputs in phase1.", 1);
01579 
01580     err = read_inputs_phase2();
01581     ERRORR("Failed to read inputs in phase2.", 1);
01582 
01583     // open info file
01584     if(strcmp(info.c_str(),"on") == 0 && nrank == 0){
01585         do {
01586             info_file.open(infofile.c_str(), std::ios::out);
01587             if (!info_file) {
01588                 if (nrank == 0) {
01589                     logfile << "Unable to open makefile for writing" << std::endl;
01590                 }
01591                 info_file.clear();
01592             } else
01593                 bDone = true; // file opened successfully
01594             logfile << "Created core info file: " << infofile << std::endl;
01595         } while (!bDone);
01596 
01597         info_file << "assm index"  << " \t" << "assm number" << " \t" << "dX" << " \t" << "dY" << " \t" << "dZ"  << " \t" << "rank" << std::endl;
01598     }
01599 
01600     // open mesh info file
01601     if(strcmp(minfo.c_str(),"on") == 0 && nrank == 0){
01602         do {
01603             minfo_file.open(minfofile.c_str(), std::ios::out);
01604             if (!info_file) {
01605                 if (nrank == 0) {
01606                     logfile << "Unable to open minfofile for writing" << std::endl;
01607                 }
01608                 minfo_file.clear();
01609             } else
01610                 bDone = true; // file opened successfully
01611             logfile << "Created mesh details info file: " << minfofile << std::endl;
01612         } while (!bDone);
01613         minfo_file << "pin_number"  << " \t" << "x_centroid" << " \t" << "y_centroid" << " \t" << "z_centroid" << std::endl;
01614     }
01615     if (nrank == 0) {
01616         err = write_makefile();
01617         ERRORR("Failed to write a makefile.", 1);
01618     }
01619     return 0;
01620 }
01621 
01622 
01623 int CoreGen::read_inputs_phase1() {
01624     // ---------------------------------------------------------------------------
01625     // Function: Reads the dimension and symmetry of the problem
01626     // Input:    none
01627     // Output:   none
01628     // ---------------------------------------------------------------------------
01629     CParser parse;
01630     for (;;) {
01631         if (!parse.ReadNextLine(file_input, linenumber, input_string, MAXCHARS,
01632                                 comment))
01633             ERRORR("Reading input file failed",1);
01634         //    logfile << input_string << std::endl;
01635         if (input_string.substr(0, 11) == "problemtype") {
01636             std::istringstream formatString(input_string);
01637             formatString >> card >> prob_type;
01638             if(((strcmp (prob_type.c_str(), "geometry") != 0)
01639                 && (strcmp (prob_type.c_str(), "mesh") != 0)) || formatString.fail())
01640                 IOErrorHandler (INVALIDINPUT);
01641             if ((strcmp(prob_type.c_str(), "geometry") == 0)) {
01642                 prob_type = "geometry";
01643             }
01644         }
01645         if (input_string.substr(0, 8) == "geometry" && input_string.substr(0, 12) != "geometrytype") {
01646             std::istringstream formatString(input_string);
01647             formatString >> card >> geometry;
01648             if(((strcmp (geometry.c_str(), "volume") != 0)
01649                 && (strcmp (geometry.c_str(), "surface") != 0)) || formatString.fail())
01650                 IOErrorHandler (INVALIDINPUT);
01651             if ((strcmp(geometry.c_str(), "surface") == 0)) {
01652                 set_DIM = 2;
01653             }
01654         }
01655 
01656         // igeom->instance() engine
01657         if (input_string.substr(0, 10) == "geomengine") {
01658             std::istringstream formatString(input_string);
01659             formatString >> card >> geom_engine;
01660             if(((strcmp (geom_engine.c_str(), "acis") != 0)
01661                 && (strcmp (geom_engine.c_str(), "occ") != 0)) || formatString.fail())
01662                 IOErrorHandler (INVALIDINPUT);
01663         }
01664 
01665         // symmetry
01666         if (input_string.substr(0, 8) == "symmetry") {
01667             std::istringstream formatString(input_string);
01668             formatString >> card >> symm;
01669             if((symm !=1 && symm !=6 && symm !=12) || formatString.fail())
01670                 IOErrorHandler (INVALIDINPUT);
01671         }
01672 
01673         // merge tolerance
01674         if (input_string.substr(0, 14) == "mergetolerance") {
01675             std::istringstream formatString(input_string);
01676             formatString >> card >> merge_tol;
01677             if(merge_tol < 0 || formatString.fail())
01678                 IOErrorHandler (ENEGATIVE);
01679         }
01680 
01681         // save onefile for each proc (multiple) flag
01682         if (input_string.substr(0, 12) == "saveparallel") {
01683             std::istringstream formatString(input_string);
01684             formatString >> card >> savefiles;
01685             if(formatString.fail())
01686                 IOErrorHandler (INVALIDINPUT);
01687         }
01688         // info flag
01689         if (input_string.substr(0, 4) == "info") {
01690             std::istringstream formatString(input_string);
01691             formatString >> card >> info;
01692             if(formatString.fail())
01693                 IOErrorHandler (INVALIDINPUT);
01694         }
01695         // info flag
01696         if (input_string.substr(0, 8) == "meshinfo") {
01697             std::istringstream formatString(input_string);
01698             formatString >> card >> minfo;
01699             if(formatString.fail())
01700                 IOErrorHandler (INVALIDINPUT);
01701         }
01702         // neumannset card
01703         if (input_string.substr(0, 10) == "neumannset") {
01704             std::istringstream formatString(input_string);
01705             std::string nsLoc = "", temp1, temp2, temp3;
01706             double x, y, c;
01707             int nsId = 0;
01708             formatString >> card >> nsLoc >> nsId;
01709             if(nsId < 0 || formatString.fail())
01710                 IOErrorHandler (INVALIDINPUT);
01711             if ((strcmp(nsLoc.c_str(), "top") == 0)) {
01712                 nst_flag = true;
01713                 nst_Id = nsId;
01714             } else if ((strcmp(nsLoc.c_str(), "bot") == 0)) {
01715                 nsb_flag = true;
01716                 nsb_Id = nsId;
01717             } else if ((strcmp(nsLoc.c_str(), "side") == 0)) {
01718                 nss_Id.push_back(nsId);
01719 
01720                 formatString >> temp1 >> x >> temp2 >> y >> temp3 >> c;
01721                 if(formatString.fail())
01722                     IOErrorHandler (INVALIDINPUT);
01723                 nsx.push_back(x);
01724                 nsy.push_back(y);
01725                 nsc.push_back(c);
01726 
01727                 ++num_nsside;
01728                 nss_flag = true;
01729             } else {
01730                 logfile << "Invalid Neumann set specification" << std::endl;
01731             }
01732         }
01733 
01734         // breaking condition
01735         if (input_string.substr(0, 3) == "end") {
01736             std::istringstream formatstring(input_string);
01737             break;
01738         }
01739     }
01740     return iBase_SUCCESS;
01741 }
01742 
01743 int CoreGen::read_inputs_phase2()
01744 // ---------------------------------------------------------------------------
01745 // Function: read all the inputs
01746 // Input:    command line arguments
01747 // Output:   none
01748 // ---------------------------------------------------------------------------
01749 {
01750     //Rewind the input file
01751     file_input.clear(std::ios_base::goodbit);
01752     file_input.seekg(0L, std::ios::beg);
01753     linenumber = 0;
01754 
01755     CParser parse;
01756     for (;;) {
01757         if (!parse.ReadNextLine(file_input, linenumber, input_string, MAXCHARS,
01758                                 comment))
01759             ERRORR("Reading input file failed",1);
01760 
01761         if (input_string.substr(0, 12) == "geometrytype" ) {
01762             std::istringstream formatString(input_string);
01763             formatString >> card >> geom_type;
01764             if(formatString.fail())
01765                 IOErrorHandler (INVALIDINPUT);
01766 
01767             if (geom_type == "hexvertex" && symm == 6) {
01768 
01769                 // reading pitch info
01770                 if (!parse.ReadNextLine(file_input, linenumber, input_string,
01771                                         MAXCHARS, comment))
01772                     ERRORR("Reading input file failed",1);
01773                 if (input_string.substr(0, 10) == "assemblies") {
01774                     std::istringstream formatString(input_string);
01775                     formatString >> card >> nassys >> pitch;
01776                     if(nassys < 0 || formatString.fail())
01777                         IOErrorHandler (INVALIDINPUT);
01778                 }
01779 
01780                 // reading file and alias names
01781                 for (int i = 1; i <= nassys; i++) {
01782                     if (!parse.ReadNextLine(file_input, linenumber,
01783                                             input_string, MAXCHARS, comment))
01784                         ERRORR("Reading input file failed",1);
01785                     std::istringstream formatString(input_string);
01786                     formatString >> meshfile >> mf_alias;
01787                     if(formatString.fail())
01788                         IOErrorHandler (INVALIDINPUT);
01789 
01790                     all_meshfiles.push_back(meshfile);
01791                     if (iname == COREGEN_DEFAULT_TEST_FILE){
01792                         meshfile = testdir + meshfile;
01793                     }
01794                     files.push_back(meshfile);
01795                     assm_alias.push_back(mf_alias);
01796                 }
01797                 // reading lattice
01798                 if (!parse.ReadNextLine(file_input, linenumber, input_string,
01799                                         MAXCHARS, comment))
01800                     ERRORR("Reading input file failed",1);
01801                 if (input_string.substr(0, 7) == "lattice") {
01802                     std::istringstream formatString(input_string);
01803                     formatString >> card >> nrings;
01804                     if(nrings < 0 || formatString.fail())
01805                         IOErrorHandler (INVALIDINPUT);
01806                     if (nrings % 2 == 0)
01807                         tot_assys = (nrings * (nrings)) / 2;
01808                     else
01809                         tot_assys = ((nrings * (nrings - 1)) / 2)
01810                                 + (nrings + 1) / 2;
01811                 }
01812 
01813                 // now reading the arrangement
01814                 if (!parse.ReadNextLine(file_input, linenumber, input_string,
01815                                         MAXCHARS, comment))
01816                     ERRORR("Reading input file failed",1);
01817                 std::istringstream formatString(input_string);
01818                 for (int i = 1; i <= tot_assys; i++) {
01819                     formatString >> temp_alias;
01820                     if(formatString.fail())
01821                         IOErrorHandler (INVALIDINPUT);
01822                     core_alias.push_back(temp_alias);
01823                 }
01824             }
01825 
01826             else if (geom_type == "rectangular" && symm == 1) {
01827 
01828                 // reading pitch info
01829                 if (!parse.ReadNextLine(file_input, linenumber, input_string,
01830                                         MAXCHARS, comment))
01831                     ERRORR("Reading input file failed",1);
01832                 if (input_string.substr(0, 10) == "assemblies") {
01833                     std::istringstream formatString(input_string);
01834                     formatString >> card >> nassys >> pitchx >> pitchy;
01835                     if(nassys < 0 || pitchx < 0 || pitchy< 0 || formatString.fail())
01836                         IOErrorHandler (INVALIDINPUT);
01837                 }
01838 
01839                 // reading file and alias names
01840                 for (int i = 1; i <= nassys; i++) {
01841                     if (!parse.ReadNextLine(file_input, linenumber,
01842                                             input_string, MAXCHARS, comment))
01843                         ERRORR("Reading input file failed",1);
01844                     std::istringstream formatString(input_string);
01845                     formatString >> meshfile >> mf_alias;
01846                     if(formatString.fail())
01847                         IOErrorHandler (INVALIDINPUT);
01848 
01849                     all_meshfiles.push_back(meshfile);
01850 
01851                     if (iname.find(COREGEN_DEFAULT_TEST_FILE) != std::string::npos){
01852                         meshfile = testdir + "/" + meshfile;
01853                     }
01854                     files.push_back(meshfile);
01855                     assm_alias.push_back(mf_alias);
01856                 }
01857 
01858                 // reading lattice
01859                 if (!parse.ReadNextLine(file_input, linenumber, input_string,
01860                                         MAXCHARS, comment))
01861                     ERRORR("Reading input file failed",1);
01862                 if (input_string.substr(0, 7) == "lattice") {
01863                     std::istringstream formatString(input_string);
01864                     formatString >> card >> nringsx >> nringsy;
01865                     if(formatString.fail())
01866                         IOErrorHandler (INVALIDINPUT);
01867                     tot_assys = nringsx * nringsy;
01868                 }
01869 
01870                 // now reading the arrangement
01871                 if (!parse.ReadNextLine(file_input, linenumber, input_string,
01872                                         MAXCHARS, comment))
01873                     ERRORR("Reading input file failed",1);
01874                 std::istringstream formatString(input_string);
01875                 for (int i = 1; i <= tot_assys; i++) {
01876                     formatString >> temp_alias;
01877                     if(formatString.fail())
01878                         IOErrorHandler (INVALIDINPUT);
01879                     core_alias.push_back(temp_alias);
01880                 }
01881             }
01882 
01883             else if (geom_type == "hexflat" && symm == 6) {
01884                 // reading pitch info
01885                 if (!parse.ReadNextLine(file_input, linenumber, input_string,
01886                                         MAXCHARS, comment))
01887                     ERRORR("Reading input file failed",1);
01888                 if (input_string.substr(0, 10) == "assemblies") {
01889                     std::istringstream formatString(input_string);
01890                     formatString >> card >> nassys >> pitch;
01891                     if(formatString.fail())
01892                         IOErrorHandler (INVALIDINPUT);
01893                 }
01894 
01895                 // reading file and alias names
01896                 for (int i = 1; i <= nassys; i++) {
01897                     if (!parse.ReadNextLine(file_input, linenumber,
01898                                             input_string, MAXCHARS, comment))
01899                         ERRORR("Reading input file failed",1);
01900                     std::istringstream formatString(input_string);
01901                     formatString >> meshfile >> mf_alias;
01902                     if(formatString.fail())
01903                         IOErrorHandler (INVALIDINPUT);
01904 
01905                     all_meshfiles.push_back(meshfile);
01906 
01907                     if (iname == COREGEN_DEFAULT_TEST_FILE){
01908                         meshfile = testdir + meshfile;
01909                     }
01910                     files.push_back(meshfile);
01911                     assm_alias.push_back(mf_alias);
01912                 }
01913 
01914                 // reading lattice
01915                 if (!parse.ReadNextLine(file_input, linenumber, input_string,
01916                                         MAXCHARS, comment))
01917                     ERRORR("Reading input file failed",1);
01918                 if (input_string.substr(0, 7) == "lattice") {
01919                     std::istringstream formatString(input_string);
01920                     formatString >> card >> nrings;
01921                     if(nrings < 0 || formatString.fail())
01922                         IOErrorHandler (INVALIDINPUT);
01923                     tot_assys = (nrings * (nrings + 1)) / 2;
01924                 }
01925 
01926                 // now reading the arrangement
01927                 if (!parse.ReadNextLine(file_input, linenumber, input_string,
01928                                         MAXCHARS, comment))
01929                     ERRORR("Reading input file failed",1);
01930                 std::istringstream formatString(input_string);
01931                 for (int i = 1; i <= tot_assys; i++) {
01932                     formatString >> temp_alias;
01933                     if(formatString.fail())
01934                         IOErrorHandler (INVALIDINPUT);
01935                     core_alias.push_back(temp_alias);
01936                 }
01937             } else if (geom_type == "hexflat" && symm == 1) {
01938                 // reading pitch info
01939                 if (!parse.ReadNextLine(file_input, linenumber, input_string,
01940                                         MAXCHARS, comment))
01941                     ERRORR("Reading input file failed",1);
01942                 if (input_string.substr(0, 10) == "assemblies") {
01943                     std::istringstream formatString(input_string);
01944                     formatString >> card >> nassys >> pitch;
01945                     if(nassys < 0 || formatString.fail())
01946                         IOErrorHandler (INVALIDINPUT);
01947                 }
01948 
01949                 // reading file and alias names
01950                 for (int i = 1; i <= nassys; i++) {
01951                     if (!parse.ReadNextLine(file_input, linenumber,
01952                                             input_string, MAXCHARS, comment))
01953                         ERRORR("Reading input file failed",1);
01954                     std::istringstream formatString(input_string);
01955                     formatString >> meshfile >> mf_alias;
01956                     if(formatString.fail())
01957                         IOErrorHandler (INVALIDINPUT);
01958 
01959                     all_meshfiles.push_back(meshfile);
01960 
01961                     if (iname == COREGEN_DEFAULT_TEST_FILE){
01962                         meshfile = testdir + meshfile;
01963                     }
01964                     files.push_back(meshfile);
01965                     assm_alias.push_back(mf_alias);
01966                 }
01967 
01968                 // reading lattice
01969                 if (!parse.ReadNextLine(file_input, linenumber, input_string,
01970                                         MAXCHARS, comment))
01971                     ERRORR("Reading input file failed",1);
01972                 if (input_string.substr(0, 7) == "lattice") {
01973                     std::istringstream formatString(input_string);
01974                     formatString >> card >> nrings;
01975                     if(nrings < 0 || formatString.fail())
01976                         IOErrorHandler (INVALIDINPUT);
01977                     tot_assys = 3 * (nrings * (nrings - 1)) + 1;
01978                 }
01979 
01980                 // now reading the arrangement
01981                 if (!parse.ReadNextLine(file_input, linenumber, input_string,
01982                                         MAXCHARS, comment))
01983                     ERRORR("Reading input file failed",1);
01984                 std::istringstream formatString(input_string);
01985                 for (int i = 1; i <= tot_assys; i++) {
01986                     formatString >> temp_alias;
01987                     if(formatString.fail())
01988                         IOErrorHandler (INVALIDINPUT);
01989                     core_alias.push_back(temp_alias);
01990                 }
01991             } else if (geom_type == "hexflat" && symm == 12) {
01992 
01993                 // reading pitch info
01994                 if (!parse.ReadNextLine(file_input, linenumber, input_string,
01995                                         MAXCHARS, comment))
01996                     ERRORR("Reading input file failed",1);
01997                 if (input_string.substr(0, 10) == "assemblies") {
01998                     std::istringstream formatString(input_string);
01999                     formatString >> card >> nassys >> pitch;
02000                     if(nassys < 0 || formatString.fail())
02001                         IOErrorHandler (INVALIDINPUT);
02002                 }
02003 
02004                 // reading file and alias names
02005                 for (int i = 1; i <= nassys; i++) {
02006                     if (!parse.ReadNextLine(file_input, linenumber,
02007                                             input_string, MAXCHARS, comment))
02008                         ERRORR("Reading input file failed",1);
02009                     std::istringstream formatString(input_string);
02010                     formatString >> meshfile >> mf_alias;
02011                     if(formatString.fail())
02012                         IOErrorHandler (INVALIDINPUT);
02013 
02014                     all_meshfiles.push_back(meshfile);
02015 
02016                     if (iname == COREGEN_DEFAULT_TEST_FILE){
02017                         meshfile = testdir + meshfile;
02018                     }
02019                     files.push_back(meshfile);
02020                     assm_alias.push_back(mf_alias);
02021                 }
02022 
02023                 // reading lattice
02024                 if (!parse.ReadNextLine(file_input, linenumber, input_string,
02025                                         MAXCHARS, comment))
02026                     ERRORR("Reading input file failed",1);
02027                 if (input_string.substr(0, 7) == "lattice") {
02028                     std::istringstream formatString(input_string);
02029                     formatString >> card >> nrings;
02030                     if(nrings < 0 || formatString.fail())
02031                         IOErrorHandler (INVALIDINPUT);
02032                     if (nrings % 2 == 0)
02033                         tot_assys = (nrings * (nrings + 2)) / 4;
02034                     else
02035                         tot_assys = ((nrings + 1) * (nrings + 1)) / 4;
02036                 }
02037 
02038                 // now reading the arrangement
02039                 if (!parse.ReadNextLine(file_input, linenumber, input_string,
02040                                         MAXCHARS, comment))
02041                     ERRORR("Reading input file failed",1);
02042                 std::istringstream formatString(input_string);
02043                 for (int i = 1; i <= tot_assys; i++) {
02044                     formatString >> temp_alias;
02045                     if(formatString.fail())
02046                         IOErrorHandler (INVALIDINPUT);
02047                     core_alias.push_back(temp_alias);
02048                 }
02049             }
02050 
02051             else {
02052                 ERRORR("Invalid geometry type",1);
02053             }
02054         }
02055         // background mesh
02056         if (input_string.substr(0, 10) == "background") {
02057             std::istringstream formatString(input_string);
02058             formatString >> card >> back_meshfile;
02059             if(formatString.fail())
02060                 IOErrorHandler (INVALIDINPUT);
02061 
02062             all_meshfiles.push_back(back_meshfile);
02063 
02064             if (iname == COREGEN_DEFAULT_TEST_FILE){
02065                 back_meshfile = testdir + back_meshfile;
02066             }
02067             files.push_back(back_meshfile);
02068             back_mesh = true;
02069         }
02070         // z-height and z-divisions
02071         if (input_string.substr(0, 7) == "extrude") {
02072             extrude_flag = true;
02073             std::istringstream formatString(input_string);
02074             formatString >> card >> z_height >> z_divisions;
02075             if(z_divisions < 0 || formatString.fail())
02076                 IOErrorHandler (INVALIDINPUT);
02077         }
02078 
02079         // // neumannset card
02080         // if (input_string.substr(0, 10) == "neumannset") {
02081         //   std::istringstream formatString(input_string);
02082         //   std::string nsLoc = "", temp;
02083         //   int nsId = 0;
02084         //   formatString >> card >> nsLoc >> nsId;
02085         //   if ((strcmp(nsLoc.c_str(), "side") == 0)) {
02086         //      formatString >> temp >> nsx[ns] >> temp >> nsy[ns] >> temp >> nsc[ns];
02087         //      ++ns
02088         //   }
02089         // }
02090         // OutputFileName
02091         if (input_string.substr(0, 14) == "outputfilename") {
02092             std::istringstream formatString(input_string);
02093             formatString >> card >> outfile;
02094             if(formatString.fail())
02095                 IOErrorHandler (INVALIDINPUT);
02096         }
02097 
02098         // breaking condition
02099         if (input_string.substr(0, 3) == "end") {
02100             std::istringstream formatstring(input_string);
02101             break;
02102         }
02103     }
02104     // set some variables
02105     assm_meshfiles.resize(nassys);
02106     assm_location.resize(nassys);
02107 
02108     for(int i = 0; i < tot_assys; i++){
02109         for (int j = 0; j < nassys; j++){
02110             if (strcmp(core_alias[i].c_str(), assm_alias[j].c_str()) == 0) {
02111                 assm_meshfiles[j]+=1;
02112                 assm_location[j].push_back(i);
02113                 break;
02114             }
02115         }
02116     }
02117     return iBase_SUCCESS;
02118 }
02119 
02120 int CoreGen::find_assm(const int i, int &assm_index)
02121 // ---------------------------------------------------------------------------
02122 // Function: find the assembly index (0 to n) for n assemblies for core alias i
02123 // Input:    none
02124 // Output:   none
02125 // ---------------------------------------------------------------------------
02126 {
02127     int flag = 0;
02128     for (int j = 0; j < nassys; j++)
02129         if (strcmp(core_alias[i].c_str(), assm_alias[j].c_str()) == 0) {
02130             assm_index = j;
02131             flag = 1;
02132             break;
02133         }
02134     if (flag == 0)//nothing found return -1 or no assembly
02135         assm_index = -1;
02136     return iBase_SUCCESS;
02137 }
02138 
02139 int CoreGen::write_makefile()
02140 // ---------------------------------------------------------------------------
02141 // Function: write the makefile based on inputs read from input file
02142 // Input:    none
02143 // Output:   none
02144 // ---------------------------------------------------------------------------
02145 {
02146     std::string name;
02147     std::vector<std::string> f_no_ext, f_sat, f_inp, f_jou, f_injou;
02148     make_file << "##" << std::endl;
02149     make_file
02150             << "## This makefile is automatically generated by coregen program"
02151             << std::endl;
02152     make_file << "##" << std::endl;
02153     make_file << "## Check your coregen, assygen and cubit location"
02154               << std::endl;
02155     make_file << "##" << std::endl;
02156     make_file << "\nCUBIT = cubit\n" << std::endl;
02157     make_file << "COREGEN = ../../coregen\n" << std::endl;
02158     make_file << "ASSYGEN = ../../assygen\n" << std::endl;
02159 
02160     // remove the ./ if run from the current working TestDirectory
02161     make_file << "MESH_FILES = ";
02162     std::string filename;
02163     for (unsigned int i = 0; i < files.size(); i++) {
02164         if (files[i][0] == '.' && files[i][1] == '/') {
02165             filename = files[i].substr(2, files[i].length());
02166         } else if (files[i][0] != '.' || files[i][1] != '/') {
02167             int loc1 = files[i].find_last_of(".");
02168             int loc2 = files[i].find_last_of("/");
02169             filename = files[i].substr(loc2 + 1, loc1);
02170         } else {
02171             filename = files[i];
02172         }
02173         mk_files.push_back(filename);
02174         make_file << all_meshfiles[i] << "  ";
02175     }
02176 
02177     // get file names without extension
02178     for (unsigned int i = 0; i < mk_files.size(); i++) {
02179         int loc = mk_files[i].find_first_of(".");
02180         f_no_ext.push_back(mk_files[i].substr(0, loc));
02181     }
02182 
02183     make_file << "\n\nGEOM_FILES = ";
02184     for (unsigned int i = 0; i < mk_files.size(); i++) {
02185         if (geom_engine == "occ")
02186             name = f_no_ext[i] + ".stp";
02187         else
02188             name = f_no_ext[i] + ".sat";
02189         f_sat.push_back(name);
02190         make_file << name << "  ";
02191         name = "";
02192     }
02193 
02194     make_file << "\n\nJOU_FILES = ";
02195     for (unsigned int i = 0; i < mk_files.size(); i++) {
02196         name = f_no_ext[i] + ".jou";
02197         f_jou.push_back(name);
02198         make_file << name << "  ";
02199         name = "";
02200     }
02201 
02202     make_file << "\n\nINJOU_FILES = ";
02203     for (unsigned int i = 0; i < mk_files.size(); i++) {
02204         name = f_no_ext[i] + ".template.jou";
02205         f_injou.push_back(name);
02206         make_file << name << "  ";
02207         name = "";
02208     }
02209 
02210     make_file << "\n\nASSYGEN_FILES = ";
02211     for (unsigned int i = 0; i < mk_files.size(); i++) {
02212         name = f_no_ext[i] + ".inp";
02213         f_inp.push_back(name);
02214         make_file << name << "  ";
02215         name = "";
02216     }
02217 
02218     make_file << "\n\n" << outfile << " : ${MESH_FILES} " << ifile << std::endl;
02219     make_file << "\t" << "${COREGEN} " << iname << std::endl;
02220     for (unsigned int i = 0; i < mk_files.size(); i++) {
02221         make_file << all_meshfiles[i] << " : " << f_sat[i] << "  " << f_jou[i]
02222                      << "  " << f_injou[i] << std::endl;
02223         make_file << "\t" << "${CUBIT} -batch " << f_jou[i] << "\n"
02224                   << std::endl;
02225 
02226         make_file << f_sat[i] << " " << f_jou[i] << " " << f_injou[i] << " : "
02227                   << f_inp[i] << std::endl;
02228         make_file << "\t" << "${ASSYGEN} " << f_no_ext[i] << "\n" << std::endl;
02229     }
02230 
02231     make_file.close();
02232     logfile << "Created makefile: " << mfile << std::endl;
02233     return 0;
02234 }
02235 
02236 void CoreGen::banner()
02237 // ---------------------------------------------------------------------------
02238 // Function: display the program banner
02239 // Input:    none
02240 // Output:   none
02241 // ---------------------------------------------------------------------------
02242 {
02243     logfile << '\n';
02244     logfile
02245             << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
02246             << '\n';
02247     logfile
02248             << "Program to Assemble Nuclear Reactor Assembly Meshes and Form a Core     "
02249             << '\n';
02250     logfile << "\t\t\tArgonne National Laboratory" << '\n';
02251     logfile << "\t\t\t        2011-2014         " << '\n';
02252     logfile
02253             << "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
02254             << '\n';
02255 }
02256 
02257 
02258 } // namespace MeshKit
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines