G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsINSSolver.hpp
Go to the documentation of this file.
1
12#pragma once
14
15namespace gismo
16{
17
18template<class T, int MatOrder>
20{
21 GISMO_ASSERT(getAssembler()->isInitialized(), "Assembler must be initialized first, call initialize()");
22 gsWriteOutputLine(m_outFile, "Computing the steady Stokes problem...", m_fileOutput, m_dispOutput);
23
25 gsMatrix<T> stokesRhs;
26
27 getAssembler()->fillStokesSystem(stokesMat, stokesRhs);
28
29 if (!m_iterationNumber)
30 this->initIteration(stokesMat);
31
32 this->getLinSolver()->applySolver(stokesMat, stokesRhs, m_solution);
33}
34
35// ===================================================================================================================
36
37template<class T, int MatOrder>
39{
40 GISMO_ASSERT(this->getAssembler()->isInitialized(), "Assembler must be initialized first, call initialize()");
41
42 this->updateAssembler();
43
44 if (!m_iterationNumber)
45 this->initIteration();
46
47 this->applySolver(m_solution);
48
49 m_iterationNumber++;
50}
51
52// ===================================================================================================================
53
54template<class T, int MatOrder>
56{
57 Base::initMembers();
58 m_time = 0;
59 m_timeStepSize = m_params.options().getReal("timeStep");
60 m_innerIter = m_params.options().getInt("nonlin.maxIt");
61 m_innerTol = m_params.options().getReal("nonlin.tol");
62 m_avgPicardIter = 0;
63}
64
65
66template<class T, int MatOrder>
67void gsINSSolverUnsteady<T, MatOrder>::plotCurrentTimeStep(std::ofstream& fileU, std::ofstream& fileP, std::string fileNameSuffix, unsigned plotPts)
68{
69 int numPatches = m_params.getPde().patches().nPatches();
70
71 gsField<T> uSol = this->constructSolution(0);
72 std::stringstream filenameU;
73 filenameU << "velocity" + fileNameSuffix + "_" << m_iterationNumber << "it";
74 gsWriteParaview<T>(uSol, filenameU.str(), plotPts);
75
76 gsField<T> pSol = this->constructSolution(1);
77 std::stringstream filenameP;
78 filenameP << "pressure" + fileNameSuffix + "_" << m_iterationNumber << "it";
79 gsWriteParaview<T>(pSol, filenameP.str(), plotPts);
80
81 for (int p = 0; p < numPatches; p++)
82 {
83 std::stringstream fnU;
84 fnU << filenameU.str() << p << ".vts";
85 fileU << "<DataSet timestep = \"" << m_iterationNumber << "\" part = \"" << p << "\" file = \"" << fnU.str() << "\"/>\n";
86
87 std::stringstream fnP;
88 fnP << filenameP.str() << p << ".vts";
89 fileP << "<DataSet timestep = \"" << m_iterationNumber << "\" part = \"" << p << "\" file = \"" << fnP.str() << "\"/>\n";
90 }
91}
92
93
94template<class T, int MatOrder>
96{
97 GISMO_ASSERT(this->getAssembler()->isInitialized(), "Assembler must be initialized first, call initialize()");
98
99 this->updateAssembler();
100
101 if (!m_iterationNumber)
102 this->initIteration();
103
104 gsMatrix<T> tmpSolution = m_solution;
105
106 this->applySolver(tmpSolution);
107
108 this->writeSolChangeRelNorm(m_solution, tmpSolution);
109
110 index_t picardIter = 0;
111 T relNorm = this->solutionChangeRelNorm(m_solution, tmpSolution);
112
113 gsWriteOutputLine(m_outFile, " [u, p] Picard's iterations...", m_fileOutput, m_dispOutput);
114
115 while((relNorm > m_innerTol) && (picardIter < m_innerIter))
116 {
117 gsWriteOutput(m_outFile, " ", m_fileOutput, m_dispOutput);
118
119 gsMatrix<T> oldSol = tmpSolution;
120
121 this->updateAssembler(tmpSolution, false);
122 this->applySolver(tmpSolution);
123 this->writeSolChangeRelNorm(oldSol, tmpSolution);
124
125 relNorm = this->solutionChangeRelNorm(oldSol, tmpSolution);
126 picardIter++;
127 }
128
129 m_solution = tmpSolution;
130
131 m_time += m_timeStepSize;
132 m_avgPicardIter += picardIter;
133 m_iterationNumber++;
134}
135
136
137template<class T, int MatOrder>
138void gsINSSolverUnsteady<T, MatOrder>::solveWithAnimation(const int totalIter, const int iterStep, std::string fileNameSuffix, const T epsilon, unsigned plotPts, const int minIterations)
139{
140 // prepare plotting
141 std::string fileNameU = "velocity" + fileNameSuffix + "_animation.pvd";
142 std::ofstream fileU(fileNameU.c_str());
143 GISMO_ASSERT(fileU.is_open(), "Error creating " << fileNameU);
144
145 std::string fileNameP = "pressure" + fileNameSuffix + "_animation.pvd";
146 std::ofstream fileP(fileNameP.c_str());
147 GISMO_ASSERT(fileP.is_open(), "Error creating " << fileNameP);
148
149 startAnimationFile(fileU);
150 startAnimationFile(fileP);
151
152 plotCurrentTimeStep(fileU, fileP, fileNameSuffix, plotPts);
153
154 for (int i = 0; i < totalIter; i += iterStep)
155 {
156 this->solve(math::min(iterStep, totalIter), epsilon, minIterations);
157
158 plotCurrentTimeStep(fileU, fileP, fileNameSuffix, plotPts);
159 }
160
161 endAnimationFile(fileU);
162 endAnimationFile(fileP);
163}
164
165// from old version of the solver (TODO):
166
167// template<class T>
168// void gsINSSolverUnsteady<T>::initGeneralizedStokesSolution(gsSparseMatrix<T>& stokesMatrix, gsMatrix<T>& stokesRhs)
169// {
170// GISMO_ASSERT(getAssembler()->isInitialized(), "Assembler must be initialized first, call initialize()");
171
172// getAssembler()->fillStokesSystem(stokesMatrix, stokesRhs);
173
174// const int uDofs = getAssembler()->getUdofs();
175// const T invTimeStep = 1. / m_timeStepSize;
176
177// #pragma omp parallel for num_threads(getAssembler()->getBlockAssembler().getNumThreads())
178// for (index_t col = 0; col < uDofs; ++col)
179// for (typename gsSparseMatrix<T>::InnerIterator it(getAssembler()->getVelocityMassMatrix(), col); it; ++it)
180// for (index_t s = 0; s < getAssembler()->getTarDim(); ++s)
181// stokesMatrix.coeffRef(it.row() + s * uDofs, it.col() + s * uDofs) += invTimeStep * it.value();
182
183// m_clock.restart();
184// m_solver.analyzePattern(stokesMatrix);
185// m_solver.factorize(stokesMatrix);
186// m_solsetupT += m_clock.stop();
187// }
188
189
190// template<class T>
191// void gsINSSolverUnsteady<T>::solveGeneralizedStokes(const int maxIterations, const T epsilon, const int minIterations)
192// {
193// gsSparseMatrix<T> stokesMatrix;
194// gsMatrix<T> stokesRhs;
195
196// initGeneralizedStokesSolution(stokesMatrix, stokesRhs);
197
198// const int uDofs = getAssembler()->getUdofs();
199// const T invTimeStep = 1. / m_timeStepSize;
200// int iter = 0;
201// T relNorm = std::numeric_limits<T>::infinity();
202
203// while ((iter < minIterations) || ((relNorm > epsilon) && (iter < maxIterations)))
204// {
205// gsInfo << "Iteration number " << iter + 1 << "...";
206
207// gsMatrix<T> rhs = stokesRhs;
208// gsMatrix<T> newSol = m_solution;
209
210// for (index_t s = 0; s < getAssembler()->getTarDim(); ++s)
211// rhs.middleRows(s * uDofs, uDofs).noalias() += invTimeStep * getAssembler()->getVelocityMassMatrix() * m_solution.middleRows(s * uDofs, uDofs);
212
213// m_clock.restart();
214// newSol = m_solver.solve(rhs);
215// m_solveT += m_clock.stop();
216
217// relNorm = this->solutionChangeRelNorm(m_solution, newSol);
218// gsInfo << " Solution change relative norm: " << relNorm << "\n";
219
220// m_solution = newSol;
221// iter++;
222// }
223// }
224
225} //namespace gismo
A scalar of vector field defined on a m_parametric geometry.
Definition gsField.h:55
virtual void nextIteration()
Perform next iteration step.
Definition gsINSSolver.hpp:38
The unsteady incompressible Navier-Stokes solver.
Definition gsINSSolver.h:138
virtual void nextIteration()
Perform next iteration step.
Definition gsINSSolver.hpp:95
virtual void initMembers()
Initialize all members.
Definition gsINSSolver.hpp:55
virtual void solveStokes()
Compute the Stokes problem and save the solution into m_solution.
Definition gsINSSolver.hpp:19
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition gsMatrix.h:41
Sparse matrix class, based on gsEigen::SparseMatrix.
Definition gsSparseMatrix.h:139
#define index_t
Definition gsConfig.h:32
#define GISMO_ASSERT(cond, message)
Definition gsDebug.h:89
The G+Smo namespace, containing all definitions for the library.
void gsWriteOutputLine(std::ofstream &file, const std::string line, bool fileOutput, bool dispInTerminal)
Writes an output line into the given file and optionally also into terminal.
Definition gsFlowUtils.h:70
void gsWriteOutput(std::ofstream &file, const std::string output, bool fileOutput, bool dispInTerminal)
Writes an output into the given file and optionally also into terminal.
Definition gsFlowUtils.h:55