Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
96 }
97 
98 
99 /**
100  *
101  */
103 {
104 
105 }
106 
107 
108 /*
109  * Initialises the storage.
110  * @param pFields Field storage expansion lists
111  * @param time Current time
112  */
115  const NekDouble &time)
116 {
118  pFields[0]->GetNpoints(), m_initialValue));
119 
120  m_idx = Array<OneD, int> (pFields[0]->GetNpoints(), 0);
121  m_polarity = Array<OneD, int> (pFields[0]->GetNpoints(), -1);
122 }
123 
124 
125 /**
126  * Checks each point in the domain to determine if it has crossed the threshold.
127  * The direction of crossing is determined. Additional storage is allocated if
128  * needed.
129  * @param pFields Field storage expansion lists
130  * @param time Current time
131  */
134  const NekDouble &time)
135 {
136  // Only proceed if the start time has passed
137  if (time < m_startTime)
138  {
139  return;
140  }
141 
142  // Examine each point in turn
143  for (int i = 0; i < pFields[0]->GetNpoints(); ++i)
144  {
145  if ((m_polarity[i] == -1 &&
146  pFields[0]->GetPhys()[i] > m_thresholdValue) ||
147  (m_polarity[i] == 1 &&
148  pFields[0]->GetPhys()[i] < m_thresholdValue))
149  {
150  // If APD less than 50ms, remove last activation
151  if (m_polarity[i] == 1 &&
152  time - m_threshold[m_idx[i]][i] < 50)
153  {
154  m_idx[i]--;
156  }
157  else
158  {
159  m_threshold[m_idx[i]][i] = time;
160  m_idx[i]++;
161  }
162  // Update polarity of last crossing
163  m_polarity[i] *= -1;
164  }
165  }
166 
167  // Allocate additional storage if any point has as many crossings as
168  // current storage permits.
169  int max_idx = Vmath::Vmax(pFields[0]->GetNpoints(), m_idx, 1);
170  pFields[0]->GetSession()->GetComm()->AllReduce(max_idx,
172  if (m_threshold.size() == max_idx)
173  {
175  pFields[0]->GetNpoints(), m_initialValue));
176  }
177 
178 }
179 
180 
181 /**
182  * Writes out the crossings to file.
183  * @param pFields Field storage expansion list.
184  * @param time Current time.
185  */
188  const NekDouble &time)
189 {
190  for (int i = 0; i < m_threshold.size() - 1; ++i)
191  {
192  std::stringstream vOutputFilename;
193  vOutputFilename << m_outputFile << "_" << i << ".fld";
194 
195  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef
196  = pFields[0]->GetFieldDefinitions();
197  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
198 
199  Array<OneD, NekDouble> vCoeffs(pFields[0]->GetNcoeffs());
200  pFields[0]->FwdTrans_IterPerExp(m_threshold[i], vCoeffs);
201 
202  // copy Data into FieldData and set variable
203  for(int i = 0; i < FieldDef.size(); ++i)
204  {
205  // Could do a search here to find correct variable
206  FieldDef[i]->m_fields.push_back("m");
207  pFields[0]->AppendFieldData(FieldDef[i], FieldData[i], vCoeffs);
208  }
209 
210  m_fld->Write(vOutputFilename.str(),FieldDef,FieldData);
211  }
212 }
213 
214 
215 /**
216  * @return This filter is time dependent.
217  */
219 {
220  return true;
221 }
222 
223 }
virtual void v_Update(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
Update recorded times.
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
virtual ~FilterBenchmark()
Destructor for the benchmark filter.
NekDouble m_initialValue
Initial time to use in storage array.
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:779
boost::shared_ptr< SessionReader > SessionReaderSharedPtr
Definition: MeshPartition.h:51
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
static boost::shared_ptr< FieldIO > CreateDefault(const LibUtilities::SessionReaderSharedPtr session)
Returns an object for the default FieldIO method.
Definition: FieldIO.cpp:181
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