CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Modifying forces.C to output CSV (https://www.cfd-online.com/Forums/openfoam-programming-development/122646-modifying-forces-c-output-csv.html)

Boloar August 24, 2013 04:57

Modifying forces.C to output CSV
 
Hi all.
I'm attempting to make sense of the forces/moments output I get from OpenFOAM, and it's getting rather tiresome to have to edit my forces output into CSV and perform further calculations for every simulation.

I've (kind of) found where the forces is output to file, in src/postProcessing/functionObjects/forces/forces/forces.C:
Code:

00676        file() << obr_.time().value() << tab
00677            << "("
00678            << sum(force_[0]) << ","
00679            << sum(force_[1]) << ","
00680            << sum(force_[2])
00681            << ") "
00682            << "("
00683            << sum(moment_[0]) << ","
00684            << sum(moment_[1]) << ","
00685            << sum(moment_[2])
00686            << ")"
00687            << endl;

Is there a function that outputs the magnitude of a vector input? And how can I get rid of those parentheses that show up in the output? I ask because I'm not the most competent programmer.

ngj August 24, 2013 06:50

Hi Bolar,

The easiest would probably be to make a small shell script, which does all the modifications for you. Something along these lines (but please test on a copy first, since it is untested):

Code:

#!/bin/bash

## Put the first input argument into variable 'inputFile'
inputFile=$1

## Check whether the first argument is empty
if [ -z "$inputFile" ]
then
    echo "Please provide the path to the forces files as input argument."
    exit
fi

## Count the number of lines in the input file
nLines=`wc -l < $inputFile`

## Strip the first line with the header information (line 2->nLines) and
## then after the piping (sign: |) it strips all of the parentheses. The
## result is outputted in the filename $inputFile.mod, i.e. at the same
## location as the input file with a ".mod" added in the end.
sed -n '2,'$nLines'p' < $inputFile | sed 's/[()]//g' > $inputFile.mod

You can work with this script in a couple of manners, i.e. having a local copy in every case folder, but that is tedious and error prone, so I would recommend something along these lines.

1. First, create the directory (if not already existing):
Code:

mkdir $HOME/bin
2. Copy the above script into a file - and let us call it "processForces.sh" - and save it to
Code:

$HOME/bin/processForces.sh
3. To make it into an executable, execute the following command:
Code:

chmod +x $HOME/bin/processForces.sh
4. You also want to make the script available throughout the system, so you will need to add the path "$HOME/bin" to your environmental variables, so add the following line to $HOME/.bashrc file

Code:

export PATH=$HOME/bin/:$PATH
5. You are now ready to execute the script, so go to any case, and for the first time for testing also include the step "A":

A:
Code:

cp -r forces forcesPlay
and then execute the script:
Code:

processForces.sh forcesPlay/0/forces.dat
but in the future, you can merely do
Code:

processForces.sh forces/0/forces.dat
once you have found that the script works correctly.

I hope that this has helped.

Kind regards

Niels

Boloar August 26, 2013 01:56

Quote:

Originally Posted by ngj (Post 447789)
The easiest would probably be to make a small shell script, which does all the modifications for you ...

Much thanks Niels, I'll try this. :)


All times are GMT -4. The time now is 10:32.