Nektar++
FilterBenchmark.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File FilterBenchmark.cpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Outputs times when solution crosses a threshold value.
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
37 
38 namespace Nektar
39 {
40 std::string FilterBenchmark::className =
42  "Benchmark",
44 
45 /**
46  * @class FilterBenchmark
47  *
48  * This class records the sequence of activation and repolarisation times across
49  * the entire domain into a two-dimensional storage structure. At each
50  * timestep, the voltage at each point in the domain is examined to identify if
51  * it has crossed the threshold value. If so, the time of crossing is recorded.
52  * Auxiliary arrays hold the current index of each point (i.e. the number of
53  * crossings of the threshold) and the type of the last crossing (activation or
54  * repolarisation).
55  */
56 
57 /**
58  * @param pSession Session reader for IO
59  * @param pParams Parameters of filter
60  */
63  const ParamMap &pParams)
64  : Filter(pSession)
65 {
66  ParamMap::const_iterator it;
67 
68  // ThresholdValue
69  it = pParams.find("ThresholdValue");
70  ASSERTL0(it != pParams.end(), "Missing parameter 'ThresholdValue'.");
71  LibUtilities::Equation equ1(m_session, it->second);
72  m_thresholdValue = floor(equ1.Evaluate());
73 
74  // InitialValue
75  it = pParams.find("InitialValue");
76  ASSERTL0(it != pParams.end(), "Missing parameter 'InitialValue'.");
77  LibUtilities::Equation equ2(m_session, it->second);
78  m_initialValue = floor(equ2.Evaluate());
79 
80  // OutputFile
81  it = pParams.find("OutputFile");
82  ASSERTL0(it->second.length() > 0, "Missing parameter 'OutputFile'.");
83  m_outputFile = it->second;
84 
85  // StartTime
86  m_startTime = 0.0;
87  it = pParams.find("StartTime");
88  if (it != pParams.end())
89  {
90  LibUtilities::Equation equ(m_session, it->second);
91  m_startTime = floor(equ.Evaluate());
92  }
93 
95  pSession->GetComm());
96 
97 }
98 
99 
100 /**
101  *
102  */
104 {
105 
106 }
107 
108 
109 /*
110  * Initialises the storage.
111  * @param pFields Field storage expansion lists
112  * @param time Current time
113  */
116  const NekDouble &time)
117 {
119  pFields[0]->GetNpoints(), m_initialValue));
120 
121  m_idx = Array<OneD, int> (pFields[0]->GetNpoints(), 0);
122  m_polarity = Array<OneD, int> (pFields[0]->GetNpoints(), -1);
123 }
124 
125 
126 /**
127  * Checks each point in the domain to determine if it has crossed the threshold.
128  * The direction of crossing is determined. Additional storage is allocated if
129  * needed.
130  * @param pFields Field storage expansion lists
131  * @param time Current time
132  */
135  const NekDouble &time)
136 {
137  // Only proceed if the start time has passed
138  if (time < m_startTime)
139  {
140  return;
141  }
142 
143  // Examine each point in turn
144  for (int i = 0; i < pFields[0]->GetNpoints(); ++i)
145  {
146  if ((m_polarity[i] == -1 &&
147  pFields[0]->GetPhys()[i] > m_thresholdValue) ||
148  (m_polarity[i] == 1 &&
149  pFields[0]->GetPhys()[i] < m_thresholdValue))
150  {
151  // If APD less than 50ms, remove last activation
152  if (m_polarity[i] == 1 &&
153  time - m_threshold[m_idx[i]][i] < 50)
154  {
155  m_idx[i]--;
157  }
158  else
159  {
160  m_threshold[m_idx[i]][i] = time;
161  m_idx[i]++;
162  }
163  // Update polarity of last crossing
164  m_polarity[i] *= -1;
165  }
166  }
167 
168  // Allocate additional storage if any point has as many crossings as
169  // current storage permits.
170  int max_idx = Vmath::Vmax(pFields[0]->GetNpoints(), m_idx, 1);
171  pFields[0]->GetSession()->GetComm()->AllReduce(max_idx,
173  if (m_threshold.size() == max_idx)
174  {
176  pFields[0]->GetNpoints(), m_initialValue));
177  }
178 
179 }
180 
181 
182 /**
183  * Writes out the crossings to file.
184  * @param pFields Field storage expansion list.
185  * @param time Current time.
186  */
189  const NekDouble &time)
190 {
191  for (int i = 0; i < m_threshold.size() - 1; ++i)
192  {
193  std::stringstream vOutputFilename;
194  vOutputFilename << m_outputFile << "_" << i << ".fld";
195 
196  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef
197  = pFields[0]->GetFieldDefinitions();
198  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
199 
200  Array<OneD, NekDouble> vCoeffs(pFields[0]->GetNcoeffs());
201  pFields[0]->FwdTrans_IterPerExp(m_threshold[i], vCoeffs);
202 
203  // copy Data into FieldData and set variable
204  for(int i = 0; i < FieldDef.size(); ++i)
205  {
206  // Could do a search here to find correct variable
207  FieldDef[i]->m_fields.push_back("m");
208  pFields[0]->AppendFieldData(FieldDef[i], FieldData[i], vCoeffs);
209  }
210 
211  m_fld->Write(vOutputFilename.str(),FieldDef,FieldData);
212  }
213 }
214 
215 
216 /**
217  * @return This filter is time dependent.
218  */
220 {
221  return true;
222 }
223 
224 }
virtual void v_Update(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
Update recorded times.
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:161
virtual ~FilterBenchmark()
Destructor for the benchmark filter.
NekDouble m_initialValue
Initial time to use in storage array.
static boost::shared_ptr< DataType > AllocateSharedPtr()
Allocate a shared pointer from the memory pool.
virtual bool v_IsTimeDependent()
Identifies that the benchmark filter is time dependent.
std::string m_outputFile
Filename of output files.
T Vmax(int n, const T *x, const int incx)
Return the maximum element in x – called vmax to avoid conflict with max.
Definition: Vmath.cpp:756
boost::shared_ptr< SessionReader > SessionReaderSharedPtr
Definition: MeshPartition.h:50
NekDouble Evaluate() const
Definition: Equation.h:102
NekDouble m_startTime
Time at which to start detecting activations and repolarisations.
virtual void v_Initialise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
Initialises the benchmark filter and allocates storage.
double NekDouble
LibUtilities::FieldIOSharedPtr m_fld
FieldIO object used for writing output files.
NekDouble m_thresholdValue
Value at which tissue is considered active.
std::map< std::string, std::string > ParamMap
Definition: Filter.h:67
LibUtilities::SessionReaderSharedPtr m_session
Definition: Filter.h:84
std::vector< Array< OneD, NekDouble > > m_threshold
Storage for activation and repolarisation times.
Array< OneD, int > m_idx
Number of activations and repolarisations detected for each point.
FilterFactory & GetFilterFactory()
Definition: Filter.cpp:42
static std::string className
Name of the class.
virtual void v_Finalise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
Finalises the benchmark filter and write out recorded data.
Array< OneD, int > m_polarity
Indicates if the previous event was an activation or repolarisation.
static SolverUtils::FilterSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const ParamMap &pParams)
Creates an instance of this class.
FilterBenchmark(const LibUtilities::SessionReaderSharedPtr &pSession, const ParamMap &pParams)
Construct the benchmark filter.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, tDescription pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:215