Loading...
Searching...
No Matches
ConvLayer.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ██╗████████╗██╗ ██╗ █████╗ ██████╗ █████╗ ███████╗██╗ ██╗
3 ██║╚══██╔══╝██║ ██║██╔══██╗██╔════╝██╔══██╗ ██╔════╝██║ ██║
4 ██║ ██║ ███████║███████║██║ ███████║█████╗█████╗ ██║ ██║
5 ██║ ██║ ██╔══██║██╔══██║██║ ██╔══██║╚════╝██╔══╝ ╚██╗ ██╔╝
6 ██║ ██║ ██║ ██║██║ ██║╚██████╗██║ ██║ ██║ ╚████╔╝
7 ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═╝ ╚═══╝
8
9 * In real Time Highly Advanced Computational Applications for Finite Volumes
10 * Copyright (C) 2017 by the ITHACA-FV authors
11-------------------------------------------------------------------------------
12
13 License
14 This file is part of ITHACA-FV
15
16 ITHACA-FV is free software: you can redistribute it and/or modify
17 it under the terms of the GNU Lesser General Public License as published by
18 the Free Software Foundation, either version 3 of the License, or
19 (at your option) any later version.
20
21 ITHACA-FV is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU Lesser General Public License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with ITHACA-FV. If not, see <http://www.gnu.org/licenses/>.
28
29\*---------------------------------------------------------------------------*/
30#include "ConvLayer.H"
31
32namespace ITHACAtorch
33{
34template<class Type, template<class> class PatchField, class GeoMesh>
36 PtrList<GeometricField<Type, PatchField, GeoMesh>>& snapshots):
37 _snapshots(snapshots),
38 mesh(snapshots[0].mesh()),
39 convDict(autoPtr<IOdictionary>
40 (
41 new IOdictionary
42 (
43 IOobject
44 (
45 "convDict",
46 mesh.time().system(),
47 mesh,
48 IOobject::MUST_READ,
49 IOobject::NO_WRITE
50 )
51 )
52 ))
53{
54 flt = autoPtr<Filter>(Filter::New(word(convDict().lookup("Filter")),
55 convDict()));
56 domainDivision = Vector<label>(convDict().lookup("domainDivision"));
57 filterSize = Vector<scalar>(convDict().lookup("filterSize"));
58 domainSize = mesh.bounds().max() - mesh.bounds().min();
62}
63
64template<class Type, template<class> class PatchField, class GeoMesh>
66 label Nz)
67{
68 M_Assert(((Nx != 1 && mesh.solutionD()[0] != -1 ) || (Nx == 1 &&
69 mesh.solutionD()[0] == -1)),
70 "The mesh has valid components only along the y and z directions, set Nx = 1");
71 M_Assert(((Ny != 1 && mesh.solutionD()[1] != -1 ) || (Ny == 1 &&
72 mesh.solutionD()[1] == -1)),
73 "The mesh has valid components only along the x and z directions, set Ny = 1");
74 M_Assert(((Nz != 1 && mesh.solutionD()[2] != -1 ) || (Nz == 1 &&
75 mesh.solutionD()[2] == -1)),
76 "The mesh has valid components only along the x and y directions, set Nz = 1");
77
78 for (label i = 0; i < ds.size(); i++)
79 {
80 if (mesh.solutionD()[i] != -1 && domainDivision[i] != 1)
81 {
82 ds[i] = domainSize[i] / (domainDivision[i] - 1);
83 }
84 else
85 {
86 ds[i] = domainSize[i] / 2;
87 }
88 }
89
90 convPoints = List<point>(Nx * Ny * Nz);
91 label index = 0;
92
93 for (label i = 0; i < Nx; i++)
94 {
95 for (label j = 0; j < Ny; j++)
96 {
97 for (label k = 0; k < Nz; k++)
98 {
99 if (i == 0 && Nx == 1)
100 {
101 i = 1;
102 }
103
104 if (j == 0 && Ny == 1)
105 {
106 j = 1;
107 }
108
109 if (k == 0 && Nz == 1)
110 {
111 k = 1;
112 }
113
114 convPoints[index] = mesh.bounds().min() + cmptMultiply((ds * i), vector(1, 0,
115 0)) + cmptMultiply((ds * j), vector(0, 1, 0)) + cmptMultiply((ds * k), vector(0,
116 0, 1));
117 index++;
118 }
119 }
120 }
121
122 isDomainDivisionSet = true;
123}
124
125template<class Type, template<class> class PatchField, class GeoMesh>
127 double dz)
128{
129 M_Assert(isDomainDivisionSet, "You need to set the division before.");
130 filterSize[0] = dx;
131 filterSize[1] = dy;
132 filterSize[2] = dz;
133 cellsInBoxes.resize(convPoints.size());
134
135 for (label i = 0; i < convPoints.size(); i++)
136 {
137 cellSet a(mesh, "set", 0);
138 point mini = convPoints[i] - filterSize / 2;
139 point maxi = convPoints[i] + filterSize / 2;
140 treeBoundBox boxi(mini, maxi);
141 List<treeBoundBox> l;
142 l.append(boxi);
143 boxToCell finding(mesh, l);
144#if OPENFOAM >= 1812
145 finding.verbose(false);
146#endif
147 finding.applyToSet(topoSetSource::ADD, a);
148 cellsInBoxes[i] = a.toc();
149 }
150
151 isFilterSizeSet = true;
152}
153
154// template<class Type, template<class> class PatchField, class GeoMesh>
155// torch::Tensor ConvLayer<Type, PatchField, GeoMesh>::filter()
156// {
157// M_Assert(isDomainDivisionSet &&
158// isFilterSizeSet,
159// "You need to set domainDivision and filterSize before calling the filter funtion.");
160// torch::Tensor output;
161// return output;
162// }
163
164template<>
166{
167 M_Assert(isDomainDivisionSet &&
168 isFilterSizeSet,
169 "You need to set domainDivision and filterSize before calling the filter funtion.");
170 label Nx = domainDivision[0];
171 label Ny = domainDivision[1];
172 label Nz = domainDivision[2];
173 torch::Tensor output = torch::zeros({_snapshots.size(), 1, Nx, Ny, Nz});
174 auto foo_a = output.accessor<float, 5>();
175
176 for (label i = 0; i < _snapshots.size(); i++)
177 {
178 label index = 0;
179
180 for (label j = 0; j < Nx; j++)
181 {
182 for (label k = 0; k < Ny; k++)
183 {
184 for (label l = 0; l < Nz; l++)
185 {
186 for (label p = 0; p < cellsInBoxes[index].size(); p++)
187 {
188 foo_a[i][0][j][k][l] += _snapshots[i][cellsInBoxes[index][p]] *
189 weights[index][p];
190 }
191
192 index++;
193 }
194 }
195 }
196 }
197
198 return output;
199}
200
201
202template<>
204{
205 M_Assert(isDomainDivisionSet &&
206 isFilterSizeSet,
207 "You need to set domainDivision and filterSize before calling the filter funtion.");
208 label Nx = domainDivision[0];
209 label Ny = domainDivision[1];
210 label Nz = domainDivision[2];
211 torch::Tensor output = torch::zeros({_snapshots.size(), 3, Nx, Ny, Nz});
212 auto foo_a = output.accessor<float, 5>();
213
214 for (label i = 0; i < _snapshots.size(); i++)
215 {
216 label index = 0;
217
218 for (label j = 0; j < Nx; j++)
219 {
220 for (label k = 0; k < Ny; k++)
221 {
222 for (label l = 0; l < Nz; l++)
223 {
224 for (label p = 0; p < cellsInBoxes[index].size(); p++)
225 {
226 foo_a[i][0][j][k][l] += _snapshots[i][cellsInBoxes[index][p]][0] *
227 weights[index][p];
228 foo_a[i][1][j][k][l] += _snapshots[i][cellsInBoxes[index][p]][1] *
229 weights[index][p];
230 foo_a[i][2][j][k][l] += _snapshots[i][cellsInBoxes[index][p]][2] *
231 weights[index][p];
232 }
233
234 index++;
235 }
236 }
237 }
238 }
239
240 return output;
241}
242
243
246// template class ConvLayer<scalar, fvsPatchField, surfaceMesh>;
247
248};
Header file of the ConvLayer class.
Foam::fvMesh & mesh
Definition createMesh.H:47
#define M_Assert(Expr, Msg)
torch::Tensor filter()
Vector< label > domainDivision
Definition ConvLayer.H:65
Vector< scalar > filterSize
Definition ConvLayer.H:66
autoPtr< Filter > flt
Definition ConvLayer.H:84
ConvLayer(PtrList< GeometricField< Type, PatchField, GeoMesh > > &snapshots)
Construct using Time as functionObject.
Definition ConvLayer.C:35
void setDomainDivision(label Nx, label Ny, label Nz)
Definition ConvLayer.C:65
List< scalarList > weights
Definition ConvLayer.H:79
const fvMesh & mesh
Definition ConvLayer.H:63
void setFilterSize(double dx, double dy, double dz)
Definition ConvLayer.C:126
List< point > convPoints
Definition ConvLayer.H:70
autoPtr< IOdictionary > convDict
Definition ConvLayer.H:85
Vector< scalar > domainSize
Definition ConvLayer.H:67
List< labelList > cellsInBoxes
Definition ConvLayer.H:78
volScalarField & p
label i
Definition pEqn.H:46