Loading...
Searching...
No Matches
POD_example_00004.cpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2023 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
39#include <array>
40#if BITPIT_ENABLE_MPI
41#include <mpi.h>
42#endif
43
44#include "pod.hpp"
45
46using namespace bitpit;
47
48/*
49 * Print the L2 norm of each field of a PODField object.
50 *
51 * \param[in] field, PODField object.
52 * \param[in] pod, POD object defined on the same mesh.
53 * \param[in] field_name, string of the field name to display.
54 */
55void printL2norm (pod::PODField &field, POD &pod, std::string field_name)
56{
57 std::vector<double> vecL2 = pod.fieldsl2norm(field);
58 std::vector<std::string> scalarNames = pod.getScalarNames();
59 std::vector<std::array<std::string,3>> vectorNames= pod.getVectorNames();
60 int N = scalarNames.size();
61 for (int i=0; i<N; i++) {
62 std::cout << "L2 norm of " << field_name << " " << scalarNames[i] << " is "<< vecL2[i] << std::endl;
63 }
64 int M = vectorNames.size();
65 for (int i=N; i<N+M; i++) {
66 std::cout << "L2 norm of " << field_name << " " << vectorNames[i-N][0].substr(0,vectorNames[i-N][0].size()-2) << " is "<< vecL2[i] << std::endl;
67 }
68}
69
70/*
71 * Print a matrix.
72 *
73 * \param[in] mat, matrix of size M rows times N columns.
74 */
75void printMat (std::vector < std::vector<double>> mat)
76{
77 std::cout << "mat = " << std::endl;
78 size_t M = mat.size();
79 size_t N = mat[0].size();
80 for (size_t i=0; i<M; i++) {
81 for (size_t j=0; j<N; j++) {
82 if (j == 0) {
83 std::cout << "[ "<< mat[i][j] ;
84 }
85 else if (j==(N-1)) {
86 std::cout << " , " << mat[i][j] << " ]" << std::endl;
87 }
88 else {
89 std::cout << " , " << mat[i][j] ;
90 }
91 if (N==1) {
92 std::cout << " ]" << std::endl;
93 }
94 }
95 }
96}
97
101void run()
102{
104 POD pod;
105
107 for (int i=0; i<6; i++) {
108 pod.addSnapshot("./data", "test_set2."+std::to_string(i));
109 }
110
112 pod.setMeshType(POD::MeshType::VOLOCTREE);
113 pod.setStaticMesh(true);
114 pod.setErrorMode(POD::ErrorMode::SINGLE);
115 pod.setWriteMode(POD::WriteMode::DEBUG);
116 pod.setMemoryMode(POD::MemoryMode::MEMORY_NORMAL);
117 pod.setEnergyLevel(99.00);
118 pod.setUseMean(false);
119 pod.setDirectory("pod");
120 pod.setName("pod.test.solver");
121
123 pod.evalDecomposition();
124
125 /* Remark: the reconstruction of the first mode is equal to the first mode
126 * only if the mean is not used in the POD algorithm
127 * if the mean is used, the mean has to be subtracted to the reconstruction
128 * to get the first mode */
129
131 std::cout << "the number of modes is = " << pod.getModeCount() << std::endl;
132 std::size_t N_modes = pod.getModeCount();
133 std::vector<std::string> names = pod.getScalarNames();
134 std::vector<std::array<std::string,3>> namev = pod.getVectorNames();
135 std::size_t N_sfields = names.size();
136 std::size_t N_vfiedls = namev.size();
137 std::size_t N_fields = N_sfields+N_vfiedls;
138 /* set up of the coefficient matrix
139 * each column contains the coefficient of a specific mode
140 * each row contains the coefficient of a specific field, either scalar or vector.*/
141 std::vector < std::vector<double>> recostructionCoeffs;
142 recostructionCoeffs.clear();
143 recostructionCoeffs.resize(N_fields, std::vector<double>(N_modes, 0.0));
144 for (std::size_t i = 0; i < N_fields; i++) {
145 recostructionCoeffs[i][0] = 1;
146 }
147 pod::PODField mode1_recon;
148 pod.reconstructFields(recostructionCoeffs, mode1_recon);
149 printL2norm(mode1_recon, pod, "mode 1");
150 std::vector < std::vector<double>> test_mat = pod.projectField(mode1_recon);
151 std::cout << "the coefficient matrix of the projection on the first mode is " << std::endl;
152 printMat(test_mat);
153
154 /* <Write the first mode as VTK file */
155 pod.write(mode1_recon, "mode1_recon");
156 pod.write(0, "mode1");
157}
158
163int main(int argc, char *argv[])
164{
165#if BITPIT_ENABLE_MPI
166 MPI_Init(&argc,&argv);
167#endif
168
170 try {
171 run();
172 } catch (const std::exception &exception) {
173 log::cout() << exception.what();
174 exit(1);
175 }
176
177#if BITPIT_ENABLE_MPI
178 MPI_Finalize();
179#endif
180
181}
The POD (Proper Orthogonal Decomposition) class provides an interface for defining POD object.
Definition pod.hpp:41
std::size_t getModeCount()
Definition pod.cpp:315
std::vector< std::array< std::string, 3 > > getVectorNames()
Definition pod.cpp:738
std::vector< std::string > getScalarNames()
Definition pod.cpp:728
void setMeshType(MeshType type)
Definition pod.cpp:390
void setStaticMesh(bool flag)
Definition pod.cpp:483
std::vector< double > fieldsl2norm(pod::PODField &snap)
Definition pod.cpp:2577
void setMemoryMode(MemoryMode mode)
Definition pod.cpp:505
void write(const pod::PODField &snap, std::string file_name) const
Definition pod.cpp:3080
void setDirectory(const std::string &directory)
Definition pod.cpp:180
void reconstructFields(pod::PODField &field, pod::PODField &recon)
Definition pod.cpp:1631
void setUseMean(bool flag)
Definition pod.cpp:493
std::vector< std::vector< double > > projectField(pod::PODField &field)
Definition pod.cpp:1764
void setWriteMode(WriteMode mode)
Definition pod.cpp:576
void setEnergyLevel(double energy)
Definition pod.cpp:326
void setName(const std::string &name)
Definition pod.cpp:160
void setErrorMode(ErrorMode mode)
Definition pod.cpp:635
void evalDecomposition()
Definition pod.cpp:873
void addSnapshot(const std::string &directory, const std::string &name)
Definition pod.cpp:208
Logger & cout(log::Level defaultSeverity, log::Visibility defaultVisibility)
Definition logger.cpp:1705
The PODfield structure is used to store the fields inside POD classes.
--- layout: doxygen_footer ---