Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DriverArnoldi.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File DriverArnoldi.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: Base Driver class for the stability solver
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 #include <iomanip>
36 
38 
39 namespace Nektar
40 {
41  namespace SolverUtils
42  {
43  /**
44  *
45  */
47  : Driver(pSession)
48  {
49  m_session->LoadParameter("IO_InfoSteps", m_infosteps, 1);
50  };
51 
52 
53  /**
54  *
55  */
57  {
58  };
59 
60 
61  /**
62  *
63  */
64  void DriverArnoldi::v_InitObject(ostream &out)
65  {
67  m_session->MatchSolverInfo("SolverType","VelocityCorrectionScheme",m_timeSteppingAlgorithm, false);
68 
70  {
71  m_period = m_session->GetParameter("TimeStep")* m_session->GetParameter("NumSteps");
72  m_nfields = m_equ[0]->UpdateFields().num_elements() - 1;
73 
74  if(m_session->DefinesSolverInfo("ModeType") &&
75  (m_session->GetSolverInfo("ModeType")=="SingleMode"|| m_session->GetSolverInfo("ModeType")=="HalfMode") )
76  {
77  for(int i = 0; i < m_nfields; ++i)
78  {
79  m_equ[0]->UpdateFields()[i]->SetWaveSpace(true);
80  }
81  }
82 
83  }
84  else
85  {
86  m_period = 1.0;
87  ASSERTL0(m_session->DefinesFunction("BodyForce"),"A BodyForce section needs to be defined for this solver type");
88  m_nfields = m_equ[0]->UpdateFields().num_elements();
89  }
90 
91  m_session->LoadParameter("kdim", m_kdim, 16);
92  m_session->LoadParameter("nvec", m_nvec, 2);
93  m_session->LoadParameter("nits", m_nits, 500);
94  m_session->LoadParameter("evtol", m_evtol, 1e-06);
95 
96  m_session->LoadParameter("realShift", m_realShift, 0.0);
97  m_equ[0]->SetLambda(m_realShift);
98 
99  m_session->LoadParameter("imagShift", m_imagShift, 0.0);
100  }
101 
102  void DriverArnoldi::ArnoldiSummary(std::ostream &out)
103  {
104 
105  if(m_session->DefinesSolverInfo("SingleMode"))
106  {
107  out << "\tSingle Fourier mode : true " << endl;
108  ASSERTL0(m_session->DefinesSolverInfo("Homogeneous"),"Expected a homogeneous expansion to be defined with single mode");
109  }
110  else
111  {
112  out << "\tSingle Fourier mode : false " << endl;
113  }
114  if(m_session->DefinesSolverInfo("BetaZero"))
115  {
116  out << "\tBeta set to Zero : true (overrides LHom)" << endl;
117  }
118  else
119  {
120  out << "\tBeta set to Zero : false " << endl;
121  }
122 
124  {
125  out << "\tEvolution operator : " << m_session->GetSolverInfo("EvolutionOperator") << endl;
126  }
127  else
128  {
129  out << "\tShift (Real,Imag) : " << m_realShift <<"," << m_imagShift << endl;
130  }
131  out << "\tKrylov-space dimension : " << m_kdim << endl;
132  out << "\tNumber of vectors : " << m_nvec << endl;
133  out << "\tMax iterations : " << m_nits << endl;
134  out << "\tEigenvalue tolerance : " << m_evtol << endl;
135  out << "=======================================================================" << endl;
136  }
137 
138  /**
139  * Copy Arnoldi array to field variables which depend from
140  * either the m_fields or m_forces
141  */
142  void DriverArnoldi::CopyArnoldiArrayToField(Array<OneD, NekDouble> &array)
143  {
144 
145  Array<OneD, MultiRegions::ExpListSharedPtr>& fields = m_equ[0]->UpdateFields();
146  int nq = fields[0]->GetNcoeffs();
147 
148  for (int k = 0; k < m_nfields; ++k)
149  {
150  Vmath::Vcopy(nq, &array[k*nq], 1, &fields[k]->UpdateCoeffs()[0], 1);
151  fields[k]->SetPhysState(false);
152  }
153  };
154 
155  /**
156  * Copy field variables which depend from either the m_fields
157  * or m_forces array the Arnoldi array
158  */
159  void DriverArnoldi::CopyFieldToArnoldiArray(Array<OneD, NekDouble> &array)
160  {
161 
162  Array<OneD, MultiRegions::ExpListSharedPtr> fields;
163  fields = m_equ[m_nequ-1]->UpdateFields();
164  for (int k = 0; k < m_nfields; ++k)
165  {
166  int nq = fields[0]->GetNcoeffs();
167  Vmath::Vcopy(nq, &fields[k]->GetCoeffs()[0], 1, &array[k*nq], 1);
168  fields[k]->SetPhysState(false);
169 
170  }
171  };
172 
173 
174  /**
175  * Initialisation for the transient growth
176  */
178  {
179  Array<OneD, MultiRegions::ExpListSharedPtr> fields;
180 
182  {
183  fields = m_equ[0]->UpdateFields();
184  int nq = fields[0]->GetNcoeffs();
185 
186 
187  for (int k=0 ; k < m_nfields; ++k)
188  {
189  Vmath::Vcopy(nq, &fields[k]->GetCoeffs()[0], 1,&m_equ[1]->UpdateFields()[k]->UpdateCoeffs()[0], 1);
190 
191  }
192  }
193  else
194  {
195  ASSERTL0(false,"Transient Growth non available for Coupled Solver");
196 
197  }
198  };
199 
200  void DriverArnoldi::WriteFld(std::string file, std::vector<Array<OneD, NekDouble> > coeffs)
201  {
202 
203  std::vector<std::string> variables(m_nfields);
204 
205  ASSERTL1(coeffs.size() >= m_nfields, "coeffs is not of the correct length");
206  for(int i = 0; i < m_nfields; ++i)
207  {
208  variables[i] = m_equ[0]->GetVariable(i);
209  }
210 
211  m_equ[0]->WriteFld(file,m_equ[0]->UpdateFields()[0], coeffs, variables);
212  }
213 
214 
215  void DriverArnoldi::WriteFld(std::string file, Array<OneD, NekDouble> coeffs)
216  {
217 
218  std::vector<std::string> variables(m_nfields);
219  std::vector<Array<OneD, NekDouble> > fieldcoeffs(m_nfields);
220 
221  int ncoeffs = m_equ[0]->UpdateFields()[0]->GetNcoeffs();
222  ASSERTL1(coeffs.num_elements() >= ncoeffs*m_nfields,"coeffs is not of sufficient size");
223 
224  for(int i = 0; i < m_nfields; ++i)
225  {
226  variables[i] = m_equ[0]->GetVariable(i);
227  fieldcoeffs[i] = coeffs + i*ncoeffs;
228  }
229 
230  m_equ[0]->WriteFld(file,m_equ[0]->UpdateFields()[0], fieldcoeffs, variables);
231  }
232 
233  void DriverArnoldi::WriteEvs(ostream &evlout, const int i, const NekDouble re_ev, const NekDouble im_ev, NekDouble resid)
234  {
236  {
237  NekDouble abs_ev = hypot (re_ev, im_ev);
238  NekDouble ang_ev = atan2 (im_ev, re_ev);
239 
240  evlout << setw(2) << i
241  << setw(12) << abs_ev
242  << setw(12) << ang_ev
243  << setw(12) << log (abs_ev) / m_period
244  << setw(12) << ang_ev / m_period;
245 
246  if(resid != NekConstants::kNekUnsetDouble)
247  {
248  evlout << setw(12) << resid;
249  }
250  evlout << endl;
251  }
252  else
253  {
254  NekDouble invmag = 1.0/(re_ev*re_ev + im_ev*im_ev);
255 
256  evlout << setw(2) << i
257  << setw(14) << re_ev
258  << setw(14) << im_ev
259  << setw(14) << -re_ev*invmag + m_realShift
260  << setw(14) << im_ev*invmag;
261 
262  if(resid != NekConstants::kNekUnsetDouble)
263  {
264  evlout << setw(12) << resid;
265  }
266  evlout << endl;
267  }
268  }
269  }
270 }