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
37namespace Nektar
38{
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::shared_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 [[maybe_unused]] 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 size_t nPts = pFields[0]->GetNpoints();
136 for (size_t i = 0; i < nPts; ++i)
137 {
138 if ((m_polarity[i] == -1 &&
139 pFields[0]->GetPhys()[i] > m_thresholdValue) ||
140 (m_polarity[i] == 1 && pFields[0]->GetPhys()[i] < m_thresholdValue))
141 {
142 // If APD less than 50ms, remove last activation
143 if (m_polarity[i] == 1 && time - m_threshold[m_idx[i]][i] < 50)
144 {
145 m_idx[i]--;
147 }
148 else
149 {
150 m_threshold[m_idx[i]][i] = time;
151 m_idx[i]++;
152 }
153 // Update polarity of last crossing
154 m_polarity[i] *= -1;
155 }
156 }
157
158 // Allocate additional storage if any point has as many crossings as
159 // current storage permits.
160 size_t max_idx = Vmath::Vmax(pFields[0]->GetNpoints(), m_idx, 1);
161 pFields[0]->GetSession()->GetComm()->AllReduce(max_idx,
163 if (m_threshold.size() == max_idx)
164 {
165 m_threshold.push_back(
166 Array<OneD, NekDouble>(pFields[0]->GetNpoints(), m_initialValue));
167 }
168}
169
170/**
171 * Writes out the crossings to file.
172 * @param pFields Field storage expansion list.
173 * @param time Current time.
174 */
177 [[maybe_unused]] const NekDouble &time)
178{
179 for (size_t j = 0; j < m_threshold.size() - 1; ++j)
180 {
181 std::stringstream vOutputFilename;
182 vOutputFilename << m_outputFile << "_" << j << ".fld";
183
184 std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef =
185 pFields[0]->GetFieldDefinitions();
186 std::vector<std::vector<NekDouble>> FieldData(FieldDef.size());
187
188 Array<OneD, NekDouble> vCoeffs(pFields[0]->GetNcoeffs());
189 pFields[0]->FwdTransLocalElmt(m_threshold[j], vCoeffs);
190
191 // copy Data into FieldData and set variable
192 for (size_t i = 0; i < FieldDef.size(); ++i)
193 {
194 // Could do a search here to find correct variable
195 FieldDef[i]->m_fields.push_back("m");
196 pFields[0]->AppendFieldData(FieldDef[i], FieldData[i], vCoeffs);
197 }
198
199 m_fld->Write(vOutputFilename.str(), FieldDef, FieldData);
200 }
201}
202
203/**
204 * @return This filter is time dependent.
205 */
207{
208 return true;
209}
210
211} // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
std::vector< Array< OneD, NekDouble > > m_threshold
Storage for activation and repolarisation times.
LibUtilities::FieldIOSharedPtr m_fld
FieldIO object used for writing output files.
void v_Finalise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) override
Finalises the benchmark filter and write out recorded data.
~FilterBenchmark() override
Destructor for the benchmark filter.
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.
FilterBenchmark(const LibUtilities::SessionReaderSharedPtr &pSession, const std::shared_ptr< SolverUtils::EquationSystem > &pEquation, const ParamMap &pParams)
Construct the benchmark filter.
std::string m_outputFile
Filename of output files.
static SolverUtils::FilterSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const std::shared_ptr< SolverUtils::EquationSystem > &pEquation, const ParamMap &pParams)
Creates an instance of this class.
void v_Update(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) override
Update recorded times.
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.
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:195
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
LibUtilities::SessionReaderSharedPtr m_session
Definition: Filter.h:83
std::map< std::string, std::string > ParamMap
Definition: Filter.h:65
std::shared_ptr< SessionReader > SessionReaderSharedPtr
FilterFactory & GetFilterFactory()
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.hpp:644