GCC Code Coverage Report


Directory: ./
File: Solver/CVGraphInterface.cpp
Date: 2024-04-14 07:32:34
Exec Total Coverage
Lines: 0 166 0.0%
Branches: 0 212 0.0%

Line Branch Exec Source
1 // ______ ______ _ _ _____ ______
2 // | ____| ____| | (_)/ ____| | ____|
3 // | |__ | |__ | | _| (___ ___| |__
4 // | __| | __| | | | |\___ \ / __| __|
5 // | | | |____| |____| |____) | (__| |____
6 // |_| |______|______|_|_____/ \___|______|
7 // Finite Elements for Life Sciences and Engineering
8 //
9 // License: LGL2.1 License
10 // FELiScE default license: LICENSE in root folder
11 //
12 // Main authors: J. Foulon, S. Smaldone
13 //
14
15 // System includes
16
17 // External includes
18
19 // Project includes
20 #include "Solver/CVGraphInterface.hpp"
21
22
23 namespace felisce
24 {
25 CVGraphInterface::CVGraphInterface()
26 {
27 }
28
29 /***********************************************************************************/
30 /***********************************************************************************/
31
32 CVGraphInterface::~CVGraphInterface()
33 {
34 if (m_listVariable)
35 delete [] m_listVariable;
36 if (m_listOfNodeInInterface)
37 delete [] m_listOfNodeInInterface;
38 if (m_TotSolToImpose)
39 delete [] m_TotSolToImpose;
40 if (m_StoredSolution)
41 delete [] m_StoredSolution;
42 if (m_SolutionToImpose)
43 delete [] m_SolutionToImpose;
44 if (m_ComputedVariableToImpose)
45 delete [] m_ComputedVariableToImpose;
46 if (m_listOfDofInInterface)
47 delete [] m_listOfDofInInterface;
48 if (m_listOfValuesInInterface)
49 delete [] m_listOfValuesInInterface;
50 if (m_listOfDofForVariable)
51 delete [] m_listOfDofForVariable;
52 }
53
54 /***********************************************************************************/
55 /***********************************************************************************/
56
57 void CVGraphInterface::initialize(ListVariable* listVariable, const std::string& fileName)
58 {
59 m_listVariable = listVariable;
60 //Read interface file.
61 FILE *pFile;
62 pFile = fopen(fileName.c_str(),"r");
63 if (pFile==nullptr) {
64 FEL_WARNING("Impossible to read interface file: "+fileName+".");
65 } else {
66 //Read supportDof in interface.
67 if (! fscanf(pFile, "%d", &m_totalNumberOfNode) )
68 FEL_ERROR("totalNumberOfNode in interface not scanned in file.");
69 if (!m_totalNumberOfNode) {
70 FEL_ERROR("Your interface is empty.\n");
71 }
72
73 if (m_listOfNodeInInterface == nullptr)
74 m_listOfNodeInInterface = new felInt[m_totalNumberOfNode];
75
76 felInt id = 0;
77 for (felInt indc = 0; indc<m_totalNumberOfNode; indc++) {
78 if (! fscanf(pFile, "%d", &id) )
79 FEL_ERROR("id of interface supportDof not scanned in file.");
80 m_listOfNodeInInterface[indc] = id - 1;
81 }
82
83 // We count how many dof a single node is supporting.
84 // For instance for NS in 3D we have 3+1 -> m_numberOfDofBySupport = 4
85 // What about the case of P2-P1? Some of the node will support only the velocity -> 3
86 // and some others also the pressure -> 4
87 for (unsigned int iVar = 0; iVar < m_listVariable->listVariable().size(); iVar++) {
88 m_numberOfDofBySupport += m_listVariable->listVariable()[iVar].numComponent();
89 }
90
91 // Number of total dof at the interface
92 m_TotNumDofOnInterface = m_numberOfDofBySupport*m_totalNumberOfNode;
93 // Arrays allocation
94 if (m_listOfValuesInInterface == nullptr)
95 m_listOfValuesInInterface = new double[m_TotNumDofOnInterface];
96 if (m_StoredSolution == nullptr)
97 m_StoredSolution = new double[m_TotNumDofOnInterface];
98 // Arrays initialization (to zero)
99 for(felInt il=0; il<m_TotNumDofOnInterface; il++) {
100 m_listOfValuesInInterface[il]=0;
101 m_StoredSolution[il]=0;
102 }
103 }
104 fclose(pFile);
105 }
106
107 /***********************************************************************************/
108 /***********************************************************************************/
109
110 void CVGraphInterface::identifyIdDof(Dof& dof)
111 {
112 // Alocation of vectors
113 // This vector will contain all the id of the dofs, component wise.
114 if (m_listOfDofInInterface == nullptr)
115 m_listOfDofInInterface = new felInt[m_numberOfDofBySupport*m_totalNumberOfNode];
116 std::vector<felInt> idDofBySupport;
117 felInt cptComponent = 0;
118
119 // Each variable dofs start in a different position
120 felInt indiceStart[m_listVariable->listVariable().size()];
121 indiceStart[0] = 0;
122 for (unsigned int iVar = 1; iVar < m_listVariable->listVariable().size(); iVar++) {
123 indiceStart[iVar] = indiceStart[iVar-1] + (m_totalNumberOfNode-1)*m_listVariable->listVariable()[iVar-1].numComponent(); //why -1?
124 }
125
126 // for each node (or supportDof)
127 for (felInt isd = 0; isd<m_totalNumberOfNode; isd++) {
128 // Get the list of dofs (var1comp1,var1comp2,var1comp3,..,var2Comp1,..)
129 dof.supportDofToDof(m_listOfNodeInInterface[isd],idDofBySupport);
130 for (unsigned int iVar = 0; iVar < m_listVariable->listVariable().size(); iVar++) {
131 for (std::size_t iComp = 0; iComp < m_listVariable->listVariable()[iVar].numComponent(); iComp++) {
132 m_listOfDofInInterface[indiceStart[iVar]+m_listVariable->listVariable()[iVar].numComponent()*isd+cptComponent] = idDofBySupport[cptComponent];
133 cptComponent++;
134 }
135 }
136 idDofBySupport.clear();
137 cptComponent = 0;
138 }
139 }
140
141 /***********************************************************************************/
142 /***********************************************************************************/
143
144 felInt* CVGraphInterface::listOfDofInInterfaceForVariable(int iVariable)
145 {
146 if (m_listOfDofForVariable == nullptr) {
147 identifyIdDofForVariable();
148 }
149 return m_listOfDofForVariable[iVariable];
150 }
151
152 /***********************************************************************************/
153 /***********************************************************************************/
154
155 void CVGraphInterface::identifyIdDofForVariable()
156 {
157 if (m_listOfDofForVariable == nullptr) {
158 m_listOfDofForVariable = new felInt*[m_listVariable->listVariable().size()];
159 for (unsigned int iVar = 1; iVar < m_listVariable->listVariable().size(); iVar++)
160 m_listOfDofForVariable[iVar] = nullptr;
161 }
162
163 int indiceStart[m_listVariable->listVariable().size()];
164 indiceStart[0] = 0;
165 for (unsigned int iVar = 1; iVar < m_listVariable->listVariable().size(); iVar++) {
166 indiceStart[iVar] = indiceStart[iVar-1] + m_totalNumberOfNode*m_listVariable->listVariable()[iVar-1].numComponent();
167 }
168
169 for (unsigned int iVar = 1; iVar < m_listVariable->listVariable().size(); iVar++) {
170
171 felInt VarNumComp = m_listVariable->listVariable()[iVar].numComponent();
172 m_SizeListOfDofForVariable = VarNumComp*m_totalNumberOfNode;
173
174 if (m_listOfDofForVariable[iVar] == nullptr)
175 m_listOfDofForVariable[iVar] = new felInt[m_SizeListOfDofForVariable];
176
177 for (felInt iv = 0; iv < m_SizeListOfDofForVariable; iv++) {
178 m_listOfDofForVariable[iVar][iv] = m_listOfDofInInterface[iv+indiceStart[iVar]];
179 }
180 }
181 }
182
183 /***********************************************************************************/
184 /***********************************************************************************/
185
186 void CVGraphInterface::ImposeSolution(double* ImpValue)
187 {
188 felInt VarNumComp = m_listVariable->listVariable()[0].numComponent();
189 m_SizeListOfDofForVariable = VarNumComp*m_totalNumberOfNode;
190 if (m_TotSolToImpose == nullptr) {
191 m_TotSolToImpose = new double[m_SizeListOfDofForVariable];
192 }
193 m_TotSolToImpose=ImpValue;
194 }
195
196 /***********************************************************************************/
197 /***********************************************************************************/
198
199 void CVGraphInterface::StoreSolution(double* StoringValue)
200 {
201 m_StoredSolution=StoringValue;
202 }
203
204 /***********************************************************************************/
205 /***********************************************************************************/
206
207 void CVGraphInterface::ImposeSolutionAndComputedVariable(double* ImpValue, int idVariable)
208 {
209 felInt VarNumComp = m_listVariable->listVariable()[idVariable].numComponent();
210 m_SizeListOfDofForVariable = VarNumComp*m_totalNumberOfNode;
211
212 // Vector with the solution at interface
213 if (m_SolutionToImpose == nullptr)
214 m_SolutionToImpose = new double[m_SizeListOfDofForVariable];
215 //Vector with the computed variable at interface
216 if (m_ComputedVariableToImpose == nullptr)
217 m_ComputedVariableToImpose = new double[m_SizeListOfDofForVariable];
218
219 for(int is=0; is<m_SizeListOfDofForVariable; is++) {
220 m_SolutionToImpose[is]=ImpValue[is];
221 }
222
223 for(int ik=0; ik<m_SizeListOfDofForVariable; ik++) {
224 m_ComputedVariableToImpose[ik]=ImpValue[m_SizeListOfDofForVariable+ik];
225 }
226 }
227
228 /***********************************************************************************/
229 /***********************************************************************************/
230
231 void CVGraphInterface::ExtractSolution(PetscVector& VecOfSol, double* valueDofInterface)
232 {
233 VecOfSol.getValues( m_numberOfDofBySupport * m_totalNumberOfNode,
234 m_listOfDofInInterface, valueDofInterface);
235 m_listOfValuesInInterface = valueDofInterface;
236 }
237
238 /***********************************************************************************/
239 /***********************************************************************************/
240
241 void CVGraphInterface::ExtractSolution( PetscVector& VecOfSol, double* valueDofInterface, int idVariable)
242 {
243 int indiceStart[m_listVariable->listVariable().size()];
244 indiceStart[0] = 0;
245 for (unsigned int iVar = 1; iVar < m_listVariable->listVariable().size(); iVar++) {
246 indiceStart[iVar] = indiceStart[iVar-1] +
247 m_totalNumberOfNode*m_listVariable->listVariable()[iVar-1].numComponent();
248 }
249
250 m_SizeListOfDofForVariable = m_listVariable->listVariable()[idVariable].numComponent()*m_totalNumberOfNode;
251 felInt listOfDofForVariable[m_SizeListOfDofForVariable];
252
253 for (felInt iv = 0; iv < m_SizeListOfDofForVariable; iv++) {
254 listOfDofForVariable[iv] = m_listOfDofInInterface[iv+indiceStart[idVariable]];
255 }
256
257 VecOfSol.getValues(m_SizeListOfDofForVariable,listOfDofForVariable,valueDofInterface);
258 m_listOfValuesInInterface = valueDofInterface;
259 }
260
261 /***********************************************************************************/
262 /***********************************************************************************/
263
264 void CVGraphInterface::ExtractSolutionAndComputedVariable( PetscVector& VecOfSol, std::vector<double>& VecComputedVar,
265 double* valueDofInterface, int idVariable)
266 {
267 felInt VarNumComp = m_listVariable->listVariable()[idVariable].numComponent();
268 m_SizeListOfDofForVariable = VarNumComp*m_totalNumberOfNode;
269 double VariableSolution[m_SizeListOfDofForVariable];
270 felInt listOfDofForVariable[m_SizeListOfDofForVariable];
271 std::vector<double> ComputedVariable(m_SizeListOfDofForVariable,0);
272
273 //----take only the the solution with IdVaraible VecOfSol
274 int indiceStart[m_listVariable->listVariable().size()];
275 indiceStart[0] = 0;
276 for (unsigned int iVar = 1; iVar < m_listVariable->listVariable().size(); iVar++) {
277 indiceStart[iVar] = indiceStart[iVar-1] +
278 m_totalNumberOfNode*m_listVariable->listVariable()[iVar-1].numComponent();
279 }
280 for (felInt iz = 0; iz < m_SizeListOfDofForVariable; iz++) {
281 listOfDofForVariable[iz] = m_listOfDofInInterface[iz+indiceStart[idVariable]];
282 }
283 VecOfSol.getValues(m_SizeListOfDofForVariable,listOfDofForVariable,VariableSolution);
284 m_listOfValuesInInterface=VariableSolution;
285
286 //----------put the computed varaible in the sencond part of the std::vector------
287 for(int ia=0; ia<m_SizeListOfDofForVariable; ia++) {
288 ComputedVariable[ia] = VecComputedVar[listOfDofForVariable[ia]];
289 }
290
291 for(int ib=0; ib<m_SizeListOfDofForVariable; ib++) {
292 valueDofInterface[ib] = VariableSolution[ib];
293 }
294 for(int ic=0; ic<m_SizeListOfDofForVariable; ic++) {
295 valueDofInterface[m_SizeListOfDofForVariable + ic] = ComputedVariable[ic];
296 }
297 }
298
299 /***********************************************************************************/
300 /***********************************************************************************/
301
302 void CVGraphInterface::print(int verbose, std::ostream& c) const
303 {
304 verbose = 1;
305 if (verbose) {
306 c << "Welcome in the interface.\n";
307 if (m_listOfNodeInInterface) {
308 c << "Your list of nodes on the interface ( Total number of nodes = "<<m_totalNumberOfNode<< " ): ";
309 for (felInt ie = 0; ie < m_totalNumberOfNode; ie++) {
310 c << m_listOfNodeInInterface[ie]<< " ";
311 }
312 c << std::endl;
313 }
314
315 int sizeVar = 0;
316 felInt indiceStart[m_listVariable->listVariable().size()];
317 indiceStart[0] = 0;
318 for (unsigned int iVar = 1; iVar < m_listVariable->listVariable().size(); iVar++) {
319 indiceStart[iVar] = indiceStart[iVar-1] +
320 m_totalNumberOfNode*m_listVariable->listVariable()[iVar-1].numComponent();
321 }
322
323 if (m_listOfDofInInterface) {
324 for (unsigned int iVar = 0; iVar < m_listVariable->listVariable().size(); iVar++) {
325 sizeVar = m_totalNumberOfNode*m_listVariable->listVariable()[iVar].numComponent();
326 c << "Dof in interface for " << m_listVariable->listVariable()[iVar].name() << ": \n";
327 for (int il = 0; il < sizeVar; il++) {
328 c << m_listOfDofInInterface[il+indiceStart[iVar]] << " ";
329 }
330 c << std::endl;
331 c<<"With values: "<<std::endl;
332 for (int im = 0; im < sizeVar; im++) {
333 c << m_listOfValuesInInterface[im+indiceStart[iVar]] << " ";
334 }
335 c << std::endl;
336 }
337 }
338 }
339 }
340 }
341