Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed tools/getFromFoam/getFromFoam.py
Empty file.
104 changes: 104 additions & 0 deletions tools/linAlg4Foam/OpenFoamFunctionObjects/FOL2norm
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*--------------------------------*- 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;
}
}

#};
}

// ************************************************************************* //

127 changes: 127 additions & 0 deletions tools/linAlg4Foam/OpenFoamFunctionObjects/FOfieldMinMax
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*--------------------------------*- 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 field.
//
// Input arguments:
// - path to the snapshot (time folder)
// - field
//

fieldMinMax
{
type coded;
libs (utilityFunctionObjects);
region region0;

name fieldMinMax;

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()
);

// 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 field to find max and min values and their positions
forAll(field, fieldI)
{
if (field[fieldI] > maxVal)
{
maxVal = field[fieldI];
maxPos = fieldI;
}
if (field[fieldI] < minVal)
{
minVal = field[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;
}
}

#};
}

// ************************************************************************* //

130 changes: 130 additions & 0 deletions tools/linAlg4Foam/OpenFoamFunctionObjects/FOinternalFieldMinMax
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*--------------------------------*- 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;
}
}

#};
}

// ************************************************************************* //

Loading