-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFOinternalFieldMinMax
More file actions
130 lines (108 loc) · 4.49 KB
/
FOinternalFieldMinMax
File metadata and controls
130 lines (108 loc) · 4.49 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
/*--------------------------------*- 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 min/max values and their positions
// in the internal field.
//
// Input arguments:
// - path to the snapshot (time folder)
// - field
//
internalFieldMinMax
{
type coded;
libs (utilityFunctionObjects);
region region0;
name internalFieldMinMax;
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()
);
// Access only the internal field
const scalarField& internalField = field.internalField();
// Initialize variables to store max and min values and their positions
scalar maxVal = -VGREAT;
scalar minVal = VGREAT;
label maxPos = -1;
label minPos = -1;
// Iterate over the internal field to find max and min values and their positions
forAll(internalField, fieldI)
{
if (internalField[fieldI] > maxVal)
{
maxVal = internalField[fieldI];
maxPos = fieldI;
}
if (internalField[fieldI] < minVal)
{
minVal = internalField[fieldI];
minPos = fieldI;
}
}
// Convert cell labels to positions
const vector& maxPosition = mesh().C()[maxPos];
const vector& minPosition = mesh().C()[minPos];
// Info output
string snapshotID = snapshotPath.substr(snapshotPath.rfind('/', snapshotPath.rfind('/') - 1) + 1);
Info << "Minimum: " << snapshotID << ", location = " << minPosition << ", " << fieldList[fieldI] << " = " << minVal << endl;
Info << "Maximum: " << snapshotID << ", location = " << maxPosition << ", " << fieldList[fieldI] << " = " << maxVal << endl;
}
}
#};
}
// ************************************************************************* //