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  boost::ignore_unused(time);
111 
112  m_threshold.push_back(
113  Array<OneD, NekDouble>(pFields[0]->GetNpoints(), m_initialValue));
114 
115  m_idx = Array<OneD, int>(pFields[0]->GetNpoints(), 0);
116  m_polarity = Array<OneD, int>(pFields[0]->GetNpoints(), -1);
117 }
118 
119 /**
120  * Checks each point in the domain to determine if it has crossed the threshold.
121  * The direction of crossing is determined. Additional storage is allocated if
122  * needed.
123  * @param pFields Field storage expansion lists
124  * @param time Current time
125  */
128  const NekDouble &time)
129 {
130  // Only proceed if the start time has passed
131  if (time < m_startTime)
132  {
133  return;
134  }
135 
136  // Examine each point in turn
137  size_t nPts = pFields[0]->GetNpoints();
138  for (size_t i = 0; i < nPts; ++i)
139  {
140  if ((m_polarity[i] == -1 &&
141  pFields[0]->GetPhys()[i] > m_thresholdValue) ||
142  (m_polarity[i] == 1 && pFields[0]->GetPhys()[i] < m_thresholdValue))
143  {
144  // If APD less than 50ms, remove last activation
145  if (m_polarity[i] == 1 && time - m_threshold[m_idx[i]][i] < 50)
146  {
147  m_idx[i]--;
149  }
150  else
151  {
152  m_threshold[m_idx[i]][i] = time;
153  m_idx[i]++;
154  }
155  // Update polarity of last crossing
156  m_polarity[i] *= -1;
157  }
158  }
159 
160  // Allocate additional storage if any point has as many crossings as
161  // current storage permits.
162  size_t max_idx = Vmath::Vmax(pFields[0]->GetNpoints(), m_idx, 1);
163  pFields[0]->GetSession()->GetComm()->AllReduce(max_idx,
165  if (m_threshold.size() == max_idx)
166  {
167  m_threshold.push_back(
168  Array<OneD, NekDouble>(pFields[0]->GetNpoints(), m_initialValue));
169  }
170 }
171 
172 /**
173  * Writes out the crossings to file.
174  * @param pFields Field storage expansion list.
175  * @param time Current time.
176  */
179  const NekDouble &time)
180 {
181  boost::ignore_unused(time);
182 
183  for (size_t j = 0; j < m_threshold.size() - 1; ++j)
184  {
185  std::stringstream vOutputFilename;
186  vOutputFilename << m_outputFile << "_" << j << ".fld";
187 
188  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef =
189  pFields[0]->GetFieldDefinitions();
190  std::vector<std::vector<NekDouble>> FieldData(FieldDef.size());
191 
192  Array<OneD, NekDouble> vCoeffs(pFields[0]->GetNcoeffs());
193  pFields[0]->FwdTransLocalElmt(m_threshold[j], vCoeffs);
194 
195  // copy Data into FieldData and set variable
196  for (size_t i = 0; i < FieldDef.size(); ++i)
197  {
198  // Could do a search here to find correct variable
199  FieldDef[i]->m_fields.push_back("m");
200  pFields[0]->AppendFieldData(FieldDef[i], FieldData[i], vCoeffs);
201  }
202 
203  m_fld->Write(vOutputFilename.str(), FieldDef, FieldData);
204  }
205 }
206 
207 /**
208  * @return This filter is time dependent.
209  */
211 {
212  return true;
213 }
214 
215 } // 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.
virtual void v_Finalise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) override
Finalises the benchmark filter and write out recorded data.
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 void v_Initialise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) override
Initialises the benchmark filter and allocates storage.
NekDouble m_thresholdValue
Value at which tissue is considered active.
std::string m_outputFile
Filename of output files.
virtual void v_Update(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) override
Update recorded times.
virtual bool v_IsTimeDependent() override
Identifies that the benchmark filter is time dependent.
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.
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.
static std::shared_ptr< FieldIO > CreateDefault(const LibUtilities::SessionReaderSharedPtr session)
Returns an object for the default FieldIO method.
Definition: FieldIO.cpp:197
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:2
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