00001 #include "mainappmpi.h"
00002 #include "unittests.h"
00003 #include "hdf5orthowriter.h"
00004 #include <stdarg.h>
00005 #include <string.h>
00006 #include <libgen.h>
00007 #include "netmpi.h"
00008 #include "fcompoundvector.h"
00009 #include "timer.h"
00010 #include "dummyflash.h"
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 MainAppMPI::MainAppMPI()
00025 :MainApp()
00026 {
00027 if (NetMPI::nProcess() == 1)
00028 setOutput(std::cout);
00029 else
00030 {
00031 char strOut[200];
00032 if (getRank() == 0)
00033 system("rm out-*");
00034 NetMPI::Barrier();
00035 sprintf(strOut,"out-%d",getRank());
00036 out.open(strOut);
00037 setOutput(out);
00038 NetMPI::setLog(out);
00039 }
00040 }
00041
00042
00043
00044
00045
00046 MainAppMPI::~MainAppMPI()
00047 {
00048
00049 }
00050
00051
00052
00053 void MainAppMPI::setLocalTriangulation()
00054 {
00055
00056 if (this->runningInParallel())
00057 {
00058 if (getLocalNElems()[0] < 2*configFile.getUnsigned("MESH_OVERLAP_SIZE"))
00059 {
00060 throw new Exception("Mesh Overlap gets the entire domain");
00061 }
00062 }
00063 Point<3> P0 = getLocalP0();
00064 Point<3> P1 = getLocalP1();
00065
00066 std::vector<unsigned> v = getLocalNElems();
00067 VecWellInfo wells = getWells();
00068
00069
00070
00071 m_pMesh = new OrthoMesh(P0,P1,v[0],v[1],v[2]);
00072 m_pMesh->putWells(wells);
00073
00074
00075 }
00076
00077
00078 int MainAppMPI::getNProcess()
00079 {
00080 int nP;
00081 MPI_Comm_size(MPI_COMM_WORLD,&nP);
00082 return nP;
00083 }
00084
00085 int MainAppMPI::getRank()
00086 {
00087 int rank;
00088 MPI_Comm_rank(MPI_COMM_WORLD,&rank);
00089 return rank;
00090 }
00091
00092
00093 Point3D MainAppMPI::getLocalP0()
00094 {
00095 std::vector<unsigned> nElems = getNElements();
00096 Point3D DX = getDX();
00097
00098 int rank = getRank();
00099 int nProcess = getNProcess();
00100
00101
00102 unsigned startElement = nElems[0]/nProcess*rank;
00103 int rest = nElems[0]%nProcess;
00104 startElement +=( (rank < rest) ? rank : rest);
00105
00106
00107 if (rank != 0)
00108 {
00109 startElement-=configFile.getInt("MESH_OVERLAP_SIZE");
00110 }
00111 return Point3D(startElement*DX(0),0,0);
00112 }
00113
00114
00115 Point3D MainAppMPI::getLocalP1()
00116 {
00117 Point3D LP1 = MainApp::getP1();
00118 Point3D DX = getDX();
00119 LP1(0) = getLocalP0()[0]+getLocalNElems()[0]*DX(0);
00120 return LP1;
00121 }
00122
00123
00124
00125 std::vector<unsigned> MainAppMPI::getLocalNElems()
00126 {
00127 std::vector<unsigned> v(3);
00128
00129 int rank = getRank();
00130 int nProcess = getNProcess();
00131 std::vector<unsigned> nElems = MainApp::getNElements();
00132
00133
00134 int rest = nElems[0]%nProcess;
00135 int nElements = nElems[0]/nProcess + (rank < rest ? 1 : 0);
00136
00137 if (nProcess == 1)
00138 nElements+=0;
00139 else if (rank == 0 || rank == (nProcess -1))
00140 nElements+=configFile.getInt("MESH_OVERLAP_SIZE");
00141 else
00142 nElements+=2*configFile.getInt("MESH_OVERLAP_SIZE");
00143
00144
00145
00146 v[0]=nElements;
00147 v[1]=nElems[1];
00148 v[2]=nElems[2];
00149 return v;
00150 }
00151
00152
00153 void MainAppMPI::execute(std::string fileName)
00154 {
00155 try {
00156
00157 configFile.readFile(fileName);
00158
00159 if (configFile.getInt("DEBUG_PRINT_TRIANGULATION",0) == 1)
00160 {
00161 getMesh().print();
00162 }
00163
00164 if (configFile.getInt("UNIT_TESTS",0))
00165 {
00166 UnitTests tests(*this);
00167 tests.execute();
00168 printBeginSection("END UNITS TESTS");
00169 return;
00170 }
00171
00172
00173 HDF5OrthoWriter &hdf5 = HDF5OrthoWriter::getHDF5OrthoWriter();
00174 hdf5.setOutputFile(getHDF5FileName());
00175 print("HDF5","HDF5 Ouput: %s",getHDF5FileName().c_str());
00176 hdf5.writeMesh("tria0",getMesh());
00177 hdf5.setVariable("Time",0);
00178
00179 m_pSequencer = getSequencer();
00180
00181 switch (configFile.getInt("SEQUENCE_ALGORITHM"))
00182 {
00183 case 0:
00184 print("Sequencer","Dynamic Module Test\n");
00185 printLog(std::cout);
00186 m_pTransportModule = getTransportModule();
00187 m_pDynamicModule = getDynamicModule();
00188 m_pSequencer->testDynamicModule(*m_pDynamicModule,*m_pTransportModule);
00189 break;
00190 case 1:
00191 {
00192 m_pTransportModule = getTransportModule();
00193 m_pDynamicModule = getDynamicModule();
00194
00195 print("Sequencer","Method: Transport Test\nEnd Time: %g\nOutputs: %g\n\n\n",
00196 configFile.getDouble("END_TIME"),
00197 configFile.getDouble("N_OUTPUTS"));
00198 printLog(std::cout);
00199
00200 m_pSequencer->testTransport(*m_pDynamicModule,*m_pTransportModule,
00201 configFile.getDouble("END_TIME"),
00202 configFile.getDouble("N_OUTPUTS"));
00203 break;
00204 }
00205 case 2:
00206 {
00207 m_pTransportModule = getTransportModule();
00208 m_pDynamicModule = getDynamicModule();
00209 m_pDiff = getDiffusiveStep();
00210 m_pDiff->setTransport(*m_pTransportModule);
00211
00212
00213 int nDyn = configFile.getInt("N_DYN_ITERATIONS");
00214 int nOuts = configFile.getInt("N_OUTPUTS");
00215 int nOutsPerDyn = nOuts/nDyn;
00216 if (nOutsPerDyn == 0)
00217 nOutsPerDyn = 1;
00218 print("Sequencer","Method: Staggered Iteration\nEnd Time: %g\nDynamic Modules Iterations: %d\nOutputs Per Dyn Iteration: %d\n",
00219 configFile.getDouble("END_TIME"),
00220 configFile.getInt("N_DYN_ITERATIONS"),
00221 nOutsPerDyn);
00222 printLog(std::cout);
00223 m_pSequencer->alternateIteration(*m_pDynamicModule,*m_pTransportModule,m_pDiff,
00224 configFile.getDouble("END_TIME"),
00225 configFile.getInt("N_DYN_ITERATIONS"),
00226 nOuts);
00227 break;
00228 }
00229 case 3:
00230 {
00231 m_pFlash = getFlash();
00232 m_pTransportModule = getTransportModule();
00233 m_pDynamicModule = getDynamicModule();
00234 m_pFlash->setTransport(*m_pTransportModule);
00235 m_pFlash->setDynamic(*m_pDynamicModule);
00236
00237 m_pDiff = getDiffusiveStep();
00238 m_pDiff->setTransport(*m_pTransportModule);
00239
00240
00241
00242
00243
00244
00245
00246
00247 if (typeid(*m_pFlash) != typeid(DummyFlash))
00248 {
00249 print("Sequencer",
00250 "Adjusting Initial Conditions for Compressible Model\n");
00251 adjustInitialConditionForCompressibleModel(*m_pDynamicModule,*dynamic_cast<ConservativeMethodForSystem*>(m_pTransportModule),*m_pFlash);
00252 }
00253
00254
00255 print("Sequencer","Method: Staggered Iteration with Flash calculation\n");
00256 print("Sequencer","End Time: %g\n" ,configFile.getDouble("END_TIME"));
00257 print("Sequencer","Dynamic Iterations: %g\n",configFile.getDouble("N_DYN_ITERATIONS"));
00258 ConservativeMethodForSystem* pTransportSystem = dynamic_cast<ConservativeMethodForSystem*>(m_pTransportModule);
00259
00260
00261 if (configFile.isDefined("TRANSPORT_PER_DYNAMIC_STEPS"))
00262 {
00263 CompressibleDynamic *pCompDynamic;
00264 pCompDynamic = dynamic_cast<CompressibleDynamic*>(m_pDynamicModule);
00265 if (!pCompDynamic)
00266 {
00267 throw new Exception("The proportion control sequence alghorithm demands a module that implements CompressibleDynamicBase\n");
00268 }
00269 print("Sequencer","Max Transport Steps per Cycle: %g\n",configFile.getDouble("TRANSPORT_PER_DYNAMIC_STEPS"));
00270 printLog(std::cout);
00271 m_pSequencer->alternateIterationProportionControl(*pCompDynamic,
00272 *pTransportSystem,
00273 *m_pFlash,m_pDiff,
00274 configFile.getDouble("END_TIME"),
00275 configFile.getDouble("TRANSPORT_PER_DYNAMIC_STEPS"),
00276 configFile.getDouble("TRANSPORT_PER_DYNAMIC_STEPS_TOLERANCE"),
00277 getDynamicTimeStep(),
00278 configFile.getDouble("N_OUTPUTS"));
00279
00280 }
00281 else
00282 {
00283
00284 printLog(std::cout);
00285 m_pSequencer->alternateIteration(*m_pDynamicModule,
00286 *pTransportSystem,
00287 *m_pFlash,
00288 m_pDiff,
00289 configFile.getDouble("END_TIME"),
00290 configFile.getDouble("N_DYN_ITERATIONS"),
00291 configFile.getDouble("N_OUTPUTS"));
00292 }
00293 }
00294 break;
00295 case 4:
00296 {
00297 m_pTransportModule = getTransportModule();
00298 m_pDiff = getDiffusiveStep();
00299 m_pDiff->setTransport(*m_pTransportModule);
00300
00301 int nDyn = configFile.getInt("N_DYN_ITERATIONS");
00302 unsigned nOuts = configFile.getInt("N_OUTPUTS");
00303 int nOutsPerDyn = nOuts/nDyn;
00304 if (nOutsPerDyn == 0)
00305 nOutsPerDyn = 1;
00306 printLog(std::cout);
00307 m_pSequencer->diffusiveStepTest(*m_pDiff, configFile.getInt("N_DYN_ITERATIONS"),
00308 configFile.getDouble("END_TIME"),
00309 nOuts);
00310
00311 break;
00312
00313
00314 }
00315 default:
00316 throw new Exception("Wrong option for the SEQUENCE_ALGORITHM key\n");
00317 return;
00318 }
00319 printf("\n\nSimulation Done\n");
00320 hdf5.close();
00321 return;
00322
00323 }
00324 catch(Exception *e)
00325 {
00326 printf("Error");
00327 printBeginSection("ERROR MESSAGE");
00328 printf("%s\n\nQuitting.....\n",e->getError().c_str());
00329 abort();
00330 }
00331
00332 }
00333
00334
00335
00336
00337
00338 void MainAppMPI::printLocalTriangulation()
00339 {
00340 Point3D P0 = getLocalP0();
00341 Point3D P1 = getLocalP1();
00342 Point3D DX = getDX();
00343 std::vector<unsigned> el = getLocalNElems();
00344 print("Mesh","\
00345 Local Triangulation:\n\
00346 Elements: %d x %d x %d\n\
00347 Dimensions: %g x %g %g\n\
00348 Steps: %g,%g,%g\n\
00349 Domain <%g, %g, %g> - <%g,%g,%g>\n",
00350 el[0],el[1],el[2],
00351 P1[0]-P0[0],P1[1]-P0[1],P1[2]-P0[2],
00352 DX[0],DX[1],DX[2],
00353 P0[0],P0[1],P0[2],
00354 P1[0],P1[1],P1[2]);
00355 print("Mesh","Total: %u cells, %u faces\n",getMesh().numCells(),getMesh().numFaces());
00356 }
00357
00358
00359
00360
00361
00362
00363
00364 std::string MainAppMPI::appendRankInFileName(std::string fileName)
00365 {
00366
00367 char strHDF5[500];
00368 sprintf(strHDF5,"%s_%d.hdf",
00369 strtok( basename((char*) fileName.c_str()),"."),getRank());
00370
00371 string result = dirname((char*) fileName.c_str());
00372 result+="/";
00373 result+=strHDF5;
00374 return result;
00375 }
00376
00377
00382 bool MainAppMPI::runningInParallel()
00383 {
00384 if (NetMPI::nProcess() == 1)
00385 return false;
00386 else
00387 return true;
00388
00389
00390
00391
00392
00393
00394
00395 }
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511