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