LifeV
lifev/zero_dimensional/testsuite/basic_test/main.cpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  * @file
29  * @brief File containing the ZeroDimensional test
30  *
31  * @date 10-02-2012
32  * @author Cristiano Malossi <cristiano.malossi@epfl.ch>
33  *
34  * @maintainer Cristiano Malossi <cristiano.malossi@epfl.ch>
35  */
36 
37 
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 
41 #include <Epetra_ConfigDefs.h>
42 #ifdef EPETRA_MPI
43 #include <mpi.h>
44 #include <Epetra_MpiComm.h>
45 #else
46 #include <Epetra_SerialComm.h>
47 #endif
48 
49 
50 // LifeV includes
51 #include <lifev/core/LifeV.hpp>
52 #include <lifev/bc_interface/0D/bc/BCInterface0D.hpp>
53 #include <lifev/zero_dimensional/solver/ZeroDimensionalData.hpp>
54 #include <lifev/zero_dimensional/solver/ZeroDimensionalSolver.hpp>
55 
56 using namespace LifeV;
57 
58 bool checkValue (const Real val, const Real test, const Real tol = 1.e-5, const bool verbose = true)
59 {
60  Real norm = std::abs (val - test);
61 
62  if ( verbose )
63  {
64  std::cout << " value = " << val << " computed value = " << test << " diff = " << norm << std::endl;
65  }
66 
67  return (norm < tol);
68 }
69 
70 Int
71 main ( Int argc, char** argv )
72 {
73  //Setup main communicator
74  std::shared_ptr< Epetra_Comm > comm;
75 
76  //Setup MPI variables
77  Int numberOfProcesses (1);
78  Int rank (0);
79 
80 #ifdef HAVE_MPI
81  MPI_Init ( &argc, &argv );
82 
83  MPI_Comm_size ( MPI_COMM_WORLD, &numberOfProcesses );
84  MPI_Comm_rank ( MPI_COMM_WORLD, &rank );
85 #endif
86 
87  if ( rank == 0 )
88  {
89  std::cout << std::endl;
90  std::cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << std::endl;
91  std::cout << " THE ZERO DIMENSIONAL SOLVER IS AN ALPHA VERSION UNDER STRONG DEVELOPMENT" << std::endl;
92  std::cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << std::endl << std::endl;
93 
94  std::cout << "MPI Processes: " << numberOfProcesses << std::endl;
95  }
96 
97 #ifdef HAVE_MPI
98  if ( numberOfProcesses > 1 )
99  {
100  if ( rank == 0 )
101  {
102  std::cout << "test_ZeroDimensional not enabled in parallel, failing gracefully." << std::endl;
103  std::cout << "MPI Finalization" << std::endl;
104  }
105  MPI_Finalize();
106  return EXIT_FAILURE;
107  }
108 #endif
109 
110 #ifdef EPETRA_MPI
111  if ( rank == 0 )
112  {
113  std::cout << "MPI Epetra Initialization ... " << std::endl;
114  }
115  comm.reset ( new Epetra_MpiComm ( MPI_COMM_WORLD ) );
116 #else
117  std::cout << "SERIAL Epetra Initialization ... " << std::endl;
118  comm.reset ( new Epetra_SerialComm() );
119 #endif
120 
121  bool exitFlag = EXIT_SUCCESS;
122 
123 #if ( defined(HAVE_NOX_THYRA) && defined(HAVE_TRILINOS_RYTHMOS) )
124  // Command line parameters
125  GetPot commandLine ( argc, argv );
126  const bool check = commandLine.search ( 2, "-c", "--check" );
127  string fileName = commandLine.follow ( "data", 2, "-f", "--file" );
128 
129  // SetupData
130  GetPot dataFile ( fileName + ".dat" );
131 
132  std::string circuitDataFile = dataFile ( "0D_Model/CircuitDataFile", "./inputFile.dat" );
133  BCInterface0D< ZeroDimensionalBCHandler, ZeroDimensionalData > zeroDimensionalBC;
134  zeroDimensionalBC.createHandler();
135  zeroDimensionalBC.fillHandler ( circuitDataFile, "Files" );
136 
137  std::shared_ptr< ZeroDimensionalData > zeroDimensionalData ( new ZeroDimensionalData );
138  zeroDimensionalData->setup ( dataFile, zeroDimensionalBC.handler() );
139 
140  std::shared_ptr< ZeroDimensionalSolver > zeroDimensionalSolver ( new ZeroDimensionalSolver ( zeroDimensionalData->unknownCounter(), comm, zeroDimensionalData->circuitData() ) );
141  zeroDimensionalSolver->setup ( zeroDimensionalData->solverData() );
142 
143  zeroDimensionalData->showMe();
144 
145  // SetupModel
146  zeroDimensionalData->dataTime()->setInitialTime (0);
147  zeroDimensionalData->initializeSolution();
148 
149  // Create output folder
150  if ( comm->MyPID() == 0 )
151  {
152  mkdir ( "output", 0777 );
153  }
154 
155  // Save initial solution
156  zeroDimensionalData->saveSolution();
157 
158  zeroDimensionalData->dataTime()->updateTime();
159  zeroDimensionalData->dataTime()->setInitialTime (zeroDimensionalData->dataTime()->time() );
160 
161  // Definitions for the time loop
162  LifeChrono chronoTotal;
163  LifeChrono chronoSystem;
164  LifeChrono chronoIteration;
165 
166  Int count = 0;
167  chronoTotal.start();
168 
169  for ( ; zeroDimensionalData->dataTime()->canAdvance() ; zeroDimensionalData->dataTime()->updateTime(), ++count )
170  {
171  std::cout << std::endl << "--------- Iteration " << count << " time = " << zeroDimensionalData->dataTime()->time() << std::endl;
172 
173  chronoIteration.start();
174  chronoSystem.start();
175 
176  zeroDimensionalSolver->takeStep ( zeroDimensionalData->dataTime()->previousTime(), zeroDimensionalData->dataTime()->time() );
177 
178  chronoSystem.stop();
179 
180  //Save solution
181  zeroDimensionalData->saveSolution();
182 
183  chronoIteration.stop();
184 
185  std::cout << " System solved in " << chronoSystem.diff() << " s, (total time " << chronoIteration.diff() << " s)." << std::endl;
186  }
187 
188  chronoTotal.stop();
189  std::cout << std::endl << " Simulation ended successfully in " << chronoTotal.diff() << " s" << std::endl;
190 
191  // Final check
192  if ( check )
193  {
194  bool ok = true;
195 
196  ok = ok && checkValue ( 0.001329039627, zeroDimensionalData->circuitData()->Nodes()->nodeListAt (1)->voltage() );
197  ok = ok && checkValue ( 0.000787475119, zeroDimensionalData->circuitData()->Elements()->elementListAt (1)->current() );
198  if (ok)
199  {
200  std::cout << " Test succesful" << std::endl;
201  exitFlag = EXIT_SUCCESS;
202  }
203  else
204  {
205  std::cout << " Test unsuccesful" << std::endl;
206  exitFlag = EXIT_FAILURE;
207  }
208  }
209 #else
210  std::cout << "ZeroDimensional requires configuring Trilinos with Rythmos/NOX/Thyra. Skipping test." << std::endl;
211  exitFlag = EXIT_SUCCESS;
212 #endif /* HAVE_NOX_THYRA && HAVE_TRILINOS_RYTHMOS */
213 
214  if (rank == 0)
215  {
216  std::cout << "End Result: TEST PASSED" << std::endl;
217  }
218 
219 #ifdef HAVE_MPI
220  if ( rank == 0 )
221  {
222  std::cout << "MPI Finalization" << std::endl;
223  }
224  MPI_Finalize();
225 #endif
226 
227  return exitFlag;
228 }
int32_type Int
Generic integer data.
Definition: LifeV.hpp:188
void updateInverseJacobian(const UInt &iQuadPt)
int main(int argc, char **argv)
Definition: dummy.cpp:5
double Real
Generic real data.
Definition: LifeV.hpp:175
bool checkValue(const Real val, const Real test, const Real tol=1.e-5, const bool verbose=true)