Repository navigation
Expand file tree
/
Copy pathOpenFOAM_programming_command.C
More file actions
381 lines (248 loc) · 14.1 KB
/
Copy pathOpenFOAM_programming_command.C
File metadata and controls
381 lines (248 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
scalarTransportFoam
Description
Solves the steady or transient transport equation for a passive scalar.
\*---------------------------------------------------------------------------*/
#include "fvCFD.H"
#include "fvModels.H"
#include "fvConstraints.H"
#include "simpleControl.H"
#include "wallShearStress.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
#include "setRootCaseLists.H"
#include "createTime.H"
#include "createMesh.H"
simpleControl simple(mesh);
#include "createFields_OpenFOAM_programming_command.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Info<< "\nCalculating scalar transport\n" << endl;
//#include "CourantNo.H"
while (simple.loop(runTime))
{
Info<< "Time = " << runTime.userTimeName() << nl << endl;
volScalarField wallShearStressMag = mag(wallShearStress);
// 1) define temporary variable --- tarfi variable temporary
tmp<volVectorField> u_star
(new volVectorField
(
IOobject
(
"u_star_result",
mesh.time().timeName(),
mesh,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh,
dimensionedVector
(
"zero",
dimensionSet(0,1,-1,0,0,0,0),
vector::zero
)
)
);
// volVectorField& u_star_ref = u_star.ref();
// 2) Create reference to boundary of u_star
volVectorField::Boundary& u_star_Bf =
u_star.ref().boundaryFieldRef();
// /* BLOCK A
// compute and update u_star_Bf which automatically update u_star too since u_star_Bf is referred to u_star
forAll(u_star_Bf, patchi) // loop allover patches mesh ID
{
//label patchi = iter.key();
// 3) mesh face normal vector on a boundary field
const vectorField& Sfp = mesh.Sf().boundaryField()[patchi];
// 4) magnitude of mesh face normal vector on a boundary field
const scalarField& magSfp = mesh.magSf().boundaryField()[patchi];
Info << "Sfp: " << Sfp << endl;
Info << "magSfp: " << magSfp << endl;
//const vectorField& Sfp = mesh.Sf().boundaryField()[patchi];
//const scalarField& sqr_mag_wallShearStress = sqr(mag(wallShearStress.boundaryField()[patchi]));
//Info << "sqr_mag_wallShearStress: " << sqr_mag_wallShearStress << endl;
//auto wallShearStress_Mag = mag(wallShearStress.boundaryField()[patchi]);
//const scalarField& wallShearStress_Mag = mag(wallShearStress.boundaryField()[patchi]); Error in run segmentationfault since if it is defined as reference
//it will be deallocated after first time calling noting that mag(wallShearStress.boundaryField()[patchi]) return a temporary object
// 5) define magnitude of vector field
const scalarField wallShearStress_Mag = mag(wallShearStress.boundaryField()[patchi]);
Info << " patchi: " << patchi << endl;
Info << "wallShearStress_Mag: " << mag(wallShearStress.boundaryField()[patchi]) << endl;
//u_star_Bf[patchi] = (-Sfp/magSfp);
u_star_Bf[patchi] = wallShearStress.boundaryField()[patchi];
// 5) find the id number of a patch with its name
label patchID_ground = mesh.boundaryMesh().findPatchID("ground");
Info << "mesh.boundaryMesh().whichPatchID(\"ground\"): " << patchID_ground << endl;
forAll(u_star_Bf[patchi], facei)
{
// 9) find a variable vector
Info << "patchi, componentIndex: "
<< " (" << patchi << "," << facei << ") "
<< "wallShearStress.boundaryField()[patchi][facei]: "
<< wallShearStress.boundaryField()[patchi][facei] << endl;
// 10) find a variable x component
Info << "patchi, componentIndex: "
<< " (" << patchi << "," << facei << ") "
<< "wallShearStress.boundaryField()[patchi][facei].x(): "
<< wallShearStress.boundaryField()[patchi][facei].x() << endl;
// 11) find a mesh boundary name, size of boundar patches and start number of boundary patches and end number of boundary patches
Info << "mesh.boundary()[patchi].name(): " << mesh.boundary()[patchi].name()
<< " mesh.boundary()[patchi].Cf().size() :" << mesh.boundary()[patchi].Cf().size()
<< " mesh.boundary()[patchi].start(): " << mesh.boundary()[patchi].start()
<< " mesh.boundary()[patchi].start() + mesh.boundary()[patchi].size(): "<< mesh.boundary()[patchi].start() + mesh.boundary()[patchi].size() << endl;
//Info << " wallShearStress.boundaryField()[patchi]: "<< wallShearStress.boundaryField()[patchi] << endl;
// in dorost nist : Info << " wallShearStress.boundary()[patchi].start() + wallShearStress.boundary()[patchi].size(): "<< wallShearStress.boundaryField()[patchi].start() + wallShearStress.boundaryField()[patchi].size() << endl;
// 6) find the id number of a patch that includes the input face id
//label patch_id= mesh.boundaryMesh().whichPatch(faceid); return -1 if faceid is not involved in boundary patches faces
label patch_name_ground_face_164654 = mesh.boundaryMesh().whichPatch(164654);
label patch_name_ground = mesh.boundaryMesh().whichPatch(facei);
Info << "mesh.boundaryMesh().whichPatch(facei): " << patch_name_ground << endl;
Info << "mesh.boundaryMesh().whichPatch(164654): " << patch_name_ground_face_164654 << endl;
//Info << "wallShearStress.boundaryField().name(): " << wallShearStress.boundaryField().name() <<
// " with "
// << wallShearStress.boundaryField().size() << " faces. Starts at total face "
// << wallShearStress.boundaryField().start() << endl; // -- Error
double chert_pert_2;
//std::cin >> chert_pert_2;
}
Info << "patchi: " << patchi << "u_star_Bf: " << u_star_Bf[patchi] << endl;
double chert_pert_2;
//std::cin >> chert_pert_2;
}
//End BLOCK A */
/* BLOCK B
const polyBoundaryMesh& boundaryMesh = mesh.boundaryMesh();
label patch_index_ground;
forAll(mesh.boundary(), patchI)
{
//const polyPatch& currentPatch = mesh.boundary()[patchI];
const word& patchName = mesh.boundary()[patchI].name();
if (patchName == "ground")
{
patch_index_ground = patchI;
Info << "patchName = " << patchName << " patch_index_ground: " << patch_index_ground << nl
<< endl;
// index face avale patchI ra neshan midahad albate in index face general hast ahr patch local
// face index ham darad
// 7) start id global for faces patchI
const label& faceStart = mesh.boundaryMesh()[patchI].start();
Info << "mesh.boundaryMesh()[patchI].start(): " << faceStart << nl << endl;
// 8) start id local (inside mesh) for faces patchI
const label localFaceStart = mesh.boundaryMesh()[patchI].whichFace(faceStart);
Info << "mesh.boundaryMesh()[patchI].whichFace(faceStart): " << localFaceStart << nl << endl;
label size_face_in_patch = mesh.boundary()[patchI].size();
Info << "mesh.boundary()[patchI].size(): " << size_face_in_patch << nl << endl;
double chert_pert;
std::cin >> chert_pert;
}
}
end BLOCK B */
/* Start : -------- block test part method 2 --- it is not perfect but can be used in future code
// obtain patchID or index certain name of patches
label patchID_ground = mesh.boundaryMesh().findPatchID("ground");
Info << "patchID_ground: "<< patchID_ground << endl;
const Foam::fvPatch& mesh_boundary_patch_ground = mesh.boundary()[patchID_ground];
Info << "mesh_boundary_patch_ground: "<< mesh_boundary_patch_ground.name() << endl;
int chert_pert1;
std::cin >> chert_pert1;
vector u_star_vector_i;
volVectorField& u_star_nonconst = const_cast<volVectorField&>(u_star);
// Loop over all faces of boundary patch
forAll(mesh.boundary()[patchID_ground], facei)
{
const label& bCell = boundaryMesh[patchID_ground].faceCells()[facei]; // Boundary cell index
const label& face = boundaryMesh[patchID_ground].start() + facei; // Face index
scalar wallShearStress_Mag = mag(wallShearStress.boundaryField()[patchID_ground][facei]);
Info << "bCell: " << bCell << " face: " << face << endl;
Info << "wallShearStress_Mag: " << wallShearStress_Mag << endl;
vector u_star_vector_i = sqr(wallShearStress_Mag) * (wallShearStress.boundaryField()[patchID_ground][facei]/wallShearStress_Mag);
Info << "u_star_vector_i: " << u_star_vector_i << endl;
}
forAll(u_star.boundaryField()[patchID_ground], facei)
{
scalar wallShearStress_Mag = mag(wallShearStress.boundaryField()[patchID_ground][facei]);
vector u_star_vector_i = sqr(wallShearStress_Mag) * (wallShearStress.boundaryField()[patchID_ground][facei]/wallShearStress_Mag);
std::cout << "typeid(u_star.boundaryField()[patchID_ground][facei]).name(): " << typeid(u_star.boundaryField()[patchID_ground][facei]).name() << std::endl;
std::cout << "typeid(u_star_vector_i).name(): " << typeid(u_star_vector_i).name() << std::endl;
std::cout << "vector(10,20,50): " << typeid(vector(10,20,50)).name() << std::endl;
std::cout << "variable u_star.boundaryField()[patchID_ground][facei]: " << "\n";
//std::cout << "u_star.boundaryField()[patchID_ground][facei]: " << u_star.boundaryField()[patchID_ground][facei];
//std::cout << "u_star.boundaryField()[patchID_ground][facei].ref(): " << u_star.boundaryField()[patchID_ground][facei].ref();
if (std::is_reference<decltype(u_star.boundaryField()[patchID_ground][facei])>::value)
{
std::cout << "Variable is a reference" << "\n";
}
else if (std::is_pointer<decltype(u_star.boundaryField()[patchID_ground][facei])>::value)
{
std::cout << "Variable is a pointer" << "\n";
}
else
{
std::cout << "Variable is neither a reference nor a pointer" << "\n";
}
std::cout << "variable u_star_vector_i: " << "\n";
if (std::is_reference<decltype(u_star_vector_i)>::value)
{
std::cout << "Variable is a reference" << "\n";
}
else if (std::is_pointer<decltype(u_star_vector_i)>::value)
{
std::cout << "Variable is a pointer" << "\n";
}
else
{
std::cout << "Variable is neither a reference nor a pointer" << "\n";
}
//u_star.boundaryField()[patchID_ground][facei] = vector(10,20,50); //u_star_vector_i; // vector::zero; //vector(10,20,50); //u_star_vector_i;
//Foam::Vector<double>& vector_ref = u_star.boundaryField()[patchID_ground][facei];
//vector_ref = Foam::Vector<double>(10, 20, 50); // This modifies the vector object directly
//u_star.boundaryField()[patchID_ground][facei] = wallShearStress.boundaryField()[patchID_ground][facei];
//const vector u_star_value(1, 2, 3);
//u_star.boundaryField()[patchID_ground][facei] = u_star_value;
//u_star.boundaryField()[patchID_ground][facei] = Foam::Vector<double>(10, 20, 50);
//u_star.boundaryField()[patchID_ground][facei].x() = 10;
//u_star.boundaryField()[patchID_ground][facei].y() = 20;
//u_star.boundaryField()[patchID_ground][facei].z() = 50;
//u_star_nonconst.boundaryField()[patchID_ground][facei] = vector(10,20,50);
//tmp_volVectorField_1_ref.boundaryField()[patchID_ground][facei] = vector(3,3,3); --------------Error
// tmp_volVectorField_1.boundaryField()[patchID_ground][facei] = vector(3,3,3);
u_star.correctBoundaryConditions();
Info << "u_star.boundaryField()[patchID_ground][facei]: " << u_star.boundaryField()[patchID_ground][facei] << endl;
Info << "u_star_nonconst.boundaryField()[patchID_ground][facei]: " << u_star_nonconst.boundaryField()[patchID_ground][facei] << endl;
Info << "mp_volVectorField_1.ref().boundaryField()[patchID_ground][facei]: " << tmp_volVectorField_1.ref().boundaryField()[patchID_ground][facei] << endl;
//Info << "tmp_volVectorField : " << tmp_volVectorField << endl;
//Info << "tmp_volScalarField : " << tmp_volScalarField << endl;
//vector myVector = vector(0, 0, 0); // Create a vector and initialize it with (0, 0, 0)
//vector& myVectorRef = myVector.ref(); // Create a reference to myVector
//u_star.boundaryField()[patchID_ground][facei].write(vector::zero); // myVector; //vector::zero; //vector(10,20,50); //u_star_vector_i;
//u_star.boundaryField()[patchID_ground][facei] (vector::zero);
}
End : -------- block test part method 2 --- it is not perfect but can be used in future code */
//u_star.write(); // Error
u_star.ref().write();
runTime.write();
}
Info<< "End\n" << endl;
return 0;
}
// ************************************************************************* //