-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFOL2norm
More file actions
104 lines (84 loc) · 3.33 KB
/
FOL2norm
File metadata and controls
104 lines (84 loc) · 3.33 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
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: v2406 |
| \\ / A nd | Website: www.openfoam.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
// This function object calculates the L2 norm of a field.
//
// Input arguments:
// - path to the snapshot (time folder)
// - field
L2norm
{
type coded;
libs (utilityFunctionObjects);
region region0;
name L2norm;
codeWrite
#{
// Read the list of fields in python format (e.g., ['T', 'DT'])
string inputLine;
std::cout << "Enter list of fields: ";
std::getline(std::cin, inputLine);
// Strip square brackets
if (!inputLine.empty() && inputLine.front() == '[') inputLine.erase(0, 1);
if (!inputLine.empty() && inputLine.back() == ']') inputLine.pop_back();
// Split and clean elements
wordList fieldList;
std::stringstream ss(inputLine);
std::string item;
while (std::getline(ss, item, ',')) {
// Remove single quotes and whitespace
item.erase(std::remove(item.begin(), item.end(), '\''), item.end());
item.erase(0, item.find_first_not_of(" \t"));
item.erase(item.find_last_not_of(" \t") + 1);
if (item.find("Region") != std::string::npos) {
item = "../" + item;
}
fieldList.append(word(item));
}
Info << "Parsed fields:" << nl;
forAll(fieldList, i)
{
Info << " - " << fieldList[i] << nl;
}
// Loop for field + snapshot sampling
while (true)
{
// Define strings
string snapshotPath;
// Get snapshot path
std::cout << "Enter snapshot path: ";
if (!std::getline(std::cin, snapshotPath) || snapshotPath.empty())
{
break;
}
// Construct snapshot path
std::string fieldTime = snapshotPath;
forAll(fieldList, fieldI)
{
volScalarField field
(
IOobject
(
fieldList[fieldI],
fieldTime,
mesh(),
IOobject::MUST_READ,
IOobject::NO_WRITE
),
mesh()
);
volScalarField field2 = magSqr(field);
dimensionedScalar L2sum = Foam::fvc::domainIntegrate(field2);
scalar L2norm = sqrt(L2sum.value());
// Info output
string snapshotID = snapshotPath.substr(snapshotPath.rfind('/', snapshotPath.rfind('/') - 1) + 1);
Info << "L2norm: " << snapshotID << ", " << fieldList[fieldI] << " = " << L2norm << endl;
}
}
#};
}
// ************************************************************************* //