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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,7 @@ cython_debug/

# PyPI configuration file
.pypirc

# Windows Zone.Identifier files
*:Zone.Identifier
*.Zone.Identifier
88 changes: 88 additions & 0 deletions examples/td_2D_MSFR/pumpPowerVariation/Allrun
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/bin/bash

# ensure that the script exits immediately if a command within exits with a non-zero status
set -e

# Source OpenFOAM environment functions
# The :? will cause the script to exit if WM_PROJECT_DIR is not set.
. ${WM_PROJECT_DIR:?}/bin/tools/RunFunctions

# Function to set up a single case
set-up-case(){
file_of_concern1="./constant/fluidRegion/phaseProperties"
abs_momentumSource_z="${1}"

# Simple check for existence
if [[ ! -f "${file_of_concern1}" ]]; then
echo "${file_of_concern1} doesn't exist"
exit 1
fi

sed -i "s/momentumSource (0 0 -300000);/momentumSource (0 0 -${abs_momentumSource_z});/" ${file_of_concern1}
return
}

# --- Main Script ---
#define the Data file
DataFile="./Data/data.csv"
# define the directory where different cases are kept
case_space="./case_space"
# define the template directory
# this is the root case upon which all other cases in the ./case_space directory are based
directory_template="./template"

#check if DataFile exists
if [[ ! -f "${DataFile}" ]]; then
echo "Error: ./Data/data.csv doesn't exist or can't be accessed"
exit 1
fi

# checks if the template directory exists
if [[ ! -d ${directory_template} ]]; then
echo "Error: 'template' directory not found in the specified directory."
exit 1
fi

# Create the case space if it doesn't exist
mkdir -p "${case_space}"


# read data row by row
tail -n +2 "${DataFile}" | while IFS=, read -r caseName abs_momentumSource_z

do
echo "-----------------------------------------------------"
echo "Processing Case: ${caseName}"
echo "Momentum Source Z (Absolute): ${abs_momentumSource_z}"
echo "-----------------------------------------------------"
new_case="${case_space}/${caseName}"

if [[ -d "${new_case}" ]]; then
echo "Warning: Case directory '${new_case}' already exists. Skipping copy."
else
cp -r "${directory_template}" "${new_case}"
echo "Directory Created: ${new_case}"
fi

(
cd ${new_case}
# Set up the case
set-up-case "${abs_momentumSource_z}"
echo "Case has been set up."
# Run the simulation
echo "--> Starting simulation with $(getApplication)..."
if $(getApplication)> case-run.log 2>&1 ; then
echo "--> Simulation completed successfully for ${caseName}."
else
echo "*****************************************************" >&2
echo "ERROR: Simulation FAILED for case ${caseName}." >&2
echo "Check the log file for details: ${new_case}/case-run.log" >&2
echo "*****************************************************" >&2
fi
)

done

echo "=========================================="
echo "All cases processed."
echo "=========================================="
112 changes: 112 additions & 0 deletions examples/td_2D_MSFR/pumpPowerVariation/Allrun-parallel
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/bin/bash

# ensure that the script exits immediately if a command within exits with a non-zero status
set -e

# Source OpenFOAM environment functions
# The :? will cause the script to exit if WM_PROJECT_DIR is not set.
. ${WM_PROJECT_DIR:?}/bin/tools/RunFunctions

# Use the first argument as NProcess, or default to the number of available cores.
NProcess=${1:-$(nproc)}

# Function to set up a single case
set-up-case(){
abs_momentumSource_z="${1}"
file_of_concern1="./constant/fluidRegion/phaseProperties"
file_of_concern2="./system/fluidRegion/decomposeParDict"
file_of_concern3="./system/neutroRegion/decomposeParDict"

# Simple check for existence
if [[ ! -f "${file_of_concern1}" ]]; then
echo "Error: ${file_of_concern1} does not exist in the case directory." >&2
exit 1
fi

echo "--> Setting momentum source Z to: -${abs_momentumSource_z}"
sed -i "s/momentumSource (0 0 -300000);/momentumSource (0 0 -${abs_momentumSource_z});/" ${file_of_concern1}

echo "--> Setting numberOfSubdomains to: ${NProcess}"
sed -i "s/numberOfSubdomains 4;/numberOfSubdomains ${NProcess};/" ${file_of_concern2}
sed -i "s/numberOfSubdomains 4;/numberOfSubdomains ${NProcess};/" ${file_of_concern3}
echo "--> Decomposing domain for parallel run..."
decomposePar -allRegions

return 0
}

# --- Main Script ---

# define the Data file
DataFile="./Data/data.csv"
# define the directory where different cases are kept
case_space="./case_space"
# define the template directory
# this is the root case upon which all other cases in the ./case_space directory are based
directory_template="./template"

# Check if required files/directories exist
if [[ ! -f "${DataFile}" ]]; then
echo "Error: Data file '${DataFile}' not found." >&2
exit 1
fi

if [[ ! -d "${directory_template}" ]]; then
echo "Error: Template directory '${directory_template}' not found." >&2
exit 1
fi

# Create the case space if it doesn't exist
mkdir -p "${case_space}"

# --- Main Loop ---
while IFS=, read -r caseName abs_momentumSource_z
do
echo "-----------------------------------------------------"
echo "Processing Case: ${caseName}"
echo "Momentum Source Z (Absolute): ${abs_momentumSource_z}"
echo "-----------------------------------------------------"

new_case="${case_space}/${caseName}"

if [[ -d "${new_case}" ]]; then
echo "Warning: Case directory '${new_case}' already exists. Skipping copy."
else
cp -r "${directory_template}" "${new_case}"
echo "Directory Created: ${new_case}"
fi

# Run the case setup and simulation in a subshell to isolate the 'cd'
(
cd "${new_case}"

# Set up the case files
set-up-case "${abs_momentumSource_z}"
echo "Case has been set up."

# Run the simulation
echo "--> Starting simulation with $(getApplication)..."
if mpirun -np ${NProcess} $(getApplication) -parallel < /dev/null > case-run.log 2>&1; then
echo "--> Simulation completed successfully for ${caseName}."
else
echo "*****************************************************" >&2
echo "ERROR: Simulation FAILED for case ${caseName}." >&2
echo "Check the log file for details: ${new_case}/case-run.log" >&2
echo "*****************************************************" >&2
# With 'set -e', the script would exit here automatically.
# If you remove 'set -e', you might want an 'exit 1' here to stop on failure.
exit 1
fi

# Reconstruct the case if the simulation was successful
echo "--> Reconstructing domain..."
reconstructPar -allRegions

echo "Done with ${caseName}."
)

done < <(tail -n +2 "${DataFile}")

echo "=========================================="
echo "All cases processed."
echo "=========================================="
11 changes: 11 additions & 0 deletions examples/td_2D_MSFR/pumpPowerVariation/Data/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
caseName,abs(momentumSource_z)
1,173450.47041969557
2,71635.91727881669
3,50561.41457225072
4,35248.577511071104
5,233172.6573532825
6,206548.28102443245
7,54081.31966285633
8,129748.00966948374
9,231233.92029560855
10,31569.501316553768
86 changes: 86 additions & 0 deletions examples/td_2D_MSFR/pumpPowerVariation/Data/momentum_value_gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"""
Random Number Generator with CSV Output

This script generates n random numbers between two specified values and saves them
to a CSV file with columns 'caseName' and 'abs(momentumSource_z)'.

COMMAND LINE USAGE:
python script.py <start_val> <end_val> <n> [options]

POSITIONAL ARGUMENTS:
start_val Starting value (float)
end_val Ending value (float)
n Number of values to generate (positive integer)

OPTIONAL ARGUMENTS:
-o, --output CSV_FILENAME Output CSV filename (default: data.csv)
-h, --help Show help message and exit

EXAMPLES:
# Basic usage - generate 10 numbers between 1.0 and 5.0
python script.py 1.0 5.0 10

# Generate 5 numbers between -2.5 and 3.7
python script.py -2.5 3.7 5

# Custom output filename
python script.py 0 1 100 -o my_data.csv

# Using long form of optional argument
python script.py 10 20 50 --output experiment_results.csv

OUTPUT:
Creates a CSV file with two columns:
- caseName: Sequential integers starting from 1
- abs(momentumSource_z): Generated random values
"""

import numpy as np
import pandas as pd
import argparse
import random

def generate_values(start_val, end_val, n):
"""Generate n numbers between start_val and end_val (inclusive)"""
if n <= 0:
raise ValueError("n must be a positive integer")
if n == 1:
return [start_val]
return [random.uniform(min(start_val, end_val), max(start_val, end_val)) for _ in range(n)]

def save_to_csv(values, filename="data.csv"):
"""Save the values to a CSV file"""
# Create a dataframe with the required columns
df = pd.DataFrame({
"caseName": range(1, len(values) + 1),
"abs(momentumSource_z)": values
})

# Save to CSV without index
df.to_csv(filename, index=False)
print(f"Values saved to {filename}")

def main():
# Set up argument parser
parser = argparse.ArgumentParser(description="Generate n numbers between two values and save to CSV")
parser.add_argument("start_val", type=float, help="Starting value")
parser.add_argument("end_val", type=float, help="Ending value")
parser.add_argument("n", type=int, help="Number of values to generate")
parser.add_argument("-o", "--output", default="data.csv", help="Output CSV filename")

# Parse arguments
args = parser.parse_args()

# Generate values
try:
values = generate_values(args.start_val, args.end_val, args.n)
# Save to CSV
save_to_csv(values, args.output)
except ValueError as e:
print(f"Error: {e}")
return 1

return 0

if __name__ == "__main__":
main()
18 changes: 18 additions & 0 deletions examples/td_2D_MSFR/pumpPowerVariation/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

The simulation can be run on a single processor by running *[Allrun](./Allrun)* from the terminal as

```bash
./Allrun
```
The simulation can be run in parallel by running *[Allrun-parallel](./Allrun-parallel)* from the terminal as follows:

```bash
./Allrun-parallel <Nprocessor>
```
If `<Nprocessor>` is not provided, the script will use all the processors in the system— that is, the number of processor equal to the value returned by the `nproc` command. It should be noted that *[Allrun-parallel](./Allrun-parallel)* uses `reconstructPar -allRegions` command to recompose after the simulation, but this command does not recompose all the data, e.g. `alpha.structure` in `fluidRegion` doesn't get recomposed.

New `data.csv` file can be generated by running *[momentum_value_gen.py](./Data/momentum_value_gen.py)* from the terminal as follows:
```bash
python3 ./Data/momentum_value_gen.py <start_value> <end_value> <total_count>
```
It should be noted that the value generated will be random.
Loading