LifeV
lifev/electrophysiology/testsuite/test_0DFitzHughNagumoModel/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 0D test with the Fitz-Hugh Nagumo model.
30 
31  @date 01-03-2013
32  @author Giacomo Rosilho de Souza <giacomo.rosilhodesouza@epfl.ch>
33 
34  @contributor
35  @mantainer Giacomo Rosilho de Souza <giacomo.rosilhodesouza@epfl.ch>
36  */
37 
38 // Tell the compiler to ignore specific kind of warnings:
39 #pragma GCC diagnostic ignored "-Wunused-variable"
40 #pragma GCC diagnostic ignored "-Wunused-parameter"
41 
42 #include <Epetra_ConfigDefs.h>
43 #ifdef EPETRA_MPI
44 #include <mpi.h>
45 #include <Epetra_MpiComm.h>
46 #else
47 #include <Epetra_SerialComm.h>
48 #endif
49 
50 //Tell the compiler to restore the warning previously silented
51 #pragma GCC diagnostic warning "-Wunused-variable"
52 #pragma GCC diagnostic warning "-Wunused-parameter"
53 
54 
55 #include <fstream>
56 
57 #include <lifev/core/util/LifeChrono.hpp>
58 
59 #include <lifev/electrophysiology/solver/IonicModels/IonicFitzHughNagumo.hpp>
60 #include <lifev/core/LifeV.hpp>
61 
62 
63 using namespace LifeV;
64 
65 
66 Real EulerExplicit (Real& dt, const Real& TF, IonicFitzHughNagumo model, const Real& I, std::ofstream& output);
67 
68 //This is the norm of the precomputed solution
69 //we check the test against this value
70 #define SolutionTestNorm 456913.59773800277617
71 
72 
73 Int main ( Int argc, char** argv )
74 {
75  //! Initializing Epetra communicator
76  MPI_Init (&argc, &argv);
77  std::shared_ptr<Epetra_Comm> Comm ( new Epetra_MpiComm (MPI_COMM_WORLD) );
78  if ( Comm->MyPID() == 0 )
79  {
80  std::cout << "% using MPI" << std::endl;
81  }
82 
83  //********************************************//
84  // Import parameters from an xml list. Use //
85  // Teuchos to create a list from a given file //
86  // in the execution directory. //
87  //********************************************//
88 
89  std::cout << "Importing parameters list...";
90  Teuchos::ParameterList FHNParameterList = * ( Teuchos::getParametersFromXmlFile ( "FitzHughNagumoParameters.xml" ) );
91  std::cout << " Done!" << std::endl;
92 
93 
94  //********************************************//
95  // Creates a new model object representing the//
96  // model from Fitz-Hugh Nagumo. The //
97  // model input are the parameters. Pass the //
98  // parameter list in the constructor //
99  //********************************************//
100  std::cout << "Building Constructor for Fitz-Hugh Nagumo Model with parameters ... ";
101  IonicFitzHughNagumo model ( FHNParameterList );
102  std::cout << " Done!" << std::endl;
103 
104 
105  //********************************************//
106  // Show the parameters of the model as well as//
107  // other informations about the object. //
108  //********************************************//
109  model.showMe();
110 
111  //********************************************//
112  // Simulation starts on t=0 and ends on t=TF. //
113  // The timestep is given by dt //
114  //********************************************//
115 
116  Real TF ( FHNParameterList.get ("TF", 300.0) );
117  Real dt ( FHNParameterList.get ("dt", 0.01) );
118 
119  std::cout << "Time parameters : " << std::endl;
120  std::cout << "TF = " << TF << std::endl;
121  std::cout << "dt = " << dt << std::endl;
122 
123  std::string filename = "test_expeuler.txt";
124 
125 
126  std::ofstream output (filename.c_str() );
127 
128  //********************************************//
129  // Time loop starts. //
130  //********************************************//
131  std::cout << "Time loop starts...\n\n\n";
132 
133  LifeChrono chrono;
134  chrono.start();
135 
136  Real valueToTest;
137 
138  valueToTest = EulerExplicit (dt, TF, model, FHNParameterList.get ("Iapp", 2000.0), output);
139 
140 
141  chrono.stop();
142  std::cout << "\n...Time loop ends.\n";
143  std::cout << "\nElapsed time : " << chrono.diff() << std::endl;
144  std::cout << "Solution written on file: " << filename << "\n";
145  //********************************************//
146  // Close exported file. //
147  //********************************************//
148  output.close();
149 
150  //! Finalizing Epetra communicator
151  MPI_Finalize();
152 
153  Real returnValue;
154  Real err = std::abs (valueToTest - SolutionTestNorm) / std::abs (SolutionTestNorm);
155  std::cout << std::setprecision (20) << "\nError: " << err << "\nSolution norm: " << valueToTest << "\n";
156  if ( err > 1e-12 )
157  {
158  returnValue = EXIT_FAILURE; // Norm of solution did not match
159  }
160  else
161  {
162  returnValue = EXIT_SUCCESS;
163  }
164  return ( returnValue );
165 }
166 
167 Real EulerExplicit (Real& dt, const Real& TF, IonicFitzHughNagumo model, const Real& I, std::ofstream& output)
168 {
169  std::vector<Real> unknowns ( model.Size(), 0.0);
170  unknowns.at (0) = 1e-8;
171  unknowns.at (1) = 0.3;
172 
173  std::vector<Real> rhs ( model.Size(), 0.0);
174  Real Iapp;
175 
176  std::cout << "Computing using Explicit Euler" << std::endl;
177 
178  //********************************************//
179  // We record the norm of the solution to //
180  // check the failure of the test //
181  //********************************************//
182  Real SolutionNorm = unknowns[0];
183 
184  for ( Real t = 0; t < TF; )
185  {
186 
187  //********************************************//
188  // Compute Calcium concentration. Here it is //
189  // given as a function of time. //
190  //********************************************//
191  if ( t > 2.0 && t < 2.1 )
192  {
193  Iapp = I;
194  }
195  else
196  {
197  Iapp = 0;
198  }
199  model.setAppliedCurrent (Iapp);
200  std::cout << "\r " << t << " ms. " << std::flush;
201 
202  //********************************************//
203  // Compute the rhs using the model equations //
204  //********************************************//
205  model.computeRhs ( unknowns, rhs);
206  model.addAppliedCurrent (rhs);
207 
208  //********************************************//
209  // Use forward Euler method to advance the //
210  // solution in time. //
211  //********************************************//
212  unknowns.at (0) = unknowns.at (0) + dt * rhs.at (0);
213  unknowns.at (1) = unknowns.at (1) + dt * rhs.at (1);
214 
215 
216  //********************************************//
217  // Writes solution on file. //
218  //********************************************//
219  output << t << " " << unknowns.at (0) << " " << unknowns.at (1) << "\n";
220 
221  //********************************************//
222  // Update the time. //
223  //********************************************//
224  t = t + dt;
225 
226  //********************************************//
227  // Update the norm of the solution to check //
228  // test failure //
229  //********************************************//
230  SolutionNorm += unknowns[0];
231  }
232 
233  return SolutionNorm;
234 }
235 
236 
237 #undef SolutionTestNorm
void start()
Start the timer.
Definition: LifeChrono.hpp:93
int32_type Int
Generic integer data.
Definition: LifeV.hpp:188
void updateInverseJacobian(const UInt &iQuadPt)
Real EulerExplicit(Real &dt, const Real &TF, IonicFitzHughNagumo model, const Real &I, std::ofstream &output)
void showMe()
Display information about the model.
int main(int argc, char **argv)
Definition: dummy.cpp:5
double Real
Generic real data.
Definition: LifeV.hpp:175
void stop()
Stop the timer.
Definition: LifeChrono.hpp:100