CFD Online Logo CFD Online URL
Home > Forums > OpenFOAM Programming & Development

Modifying forces.C to output CSV

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree1Likes
  • 1 Post By ngj

LinkBack Thread Tools Display Modes
Old   August 24, 2013, 04:57
Default Modifying forces.C to output CSV
Anand Lobo
Join Date: Jun 2013
Posts: 56
Rep Power: 6
Boloar is on a distinguished road
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:
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.
Boloar is offline   Reply With Quote

Old   August 24, 2013, 06:50
Senior Member
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Deltares, Delft, The Netherlands
Posts: 1,758
Rep Power: 29
ngj will become famous soon enoughngj will become famous soon enough
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):


## Put the first input argument into variable 'inputFile'

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

## 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):
mkdir $HOME/bin
2. Copy the above script into a file - and let us call it "" - and save it to
3. To make it into an executable, execute the following command:
chmod +x $HOME/bin/
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

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":

cp -r forces forcesPlay
and then execute the script:
Code: forcesPlay/0/forces.dat
but in the future, you can merely do
Code: forces/0/forces.dat
once you have found that the script works correctly.

I hope that this has helped.

Kind regards

wyldckat likes this.
Please note that I do not use the Friend-feature, so do not be offended, if I do not accept a request.
ngj is offline   Reply With Quote

Old   August 26, 2013, 01:56
Anand Lobo
Join Date: Jun 2013
Posts: 56
Rep Power: 6
Boloar is on a distinguished road
Originally Posted by ngj View Post
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.
Boloar is offline   Reply With Quote


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
outputTime in Swak function immortality OpenFOAM Post-Processing 16 February 14, 2016 17:02
RNG diverged during the analysis the flow over a multi element airfoil, why? s.m OpenFOAM Running, Solving & CFD 0 August 5, 2013 08:39
mixerVesselAMI2D's mass is not balancing sharonyue OpenFOAM Running, Solving & CFD 6 June 10, 2013 09:34
Probe Multiple Points in ParaView and Output as CSV eric.m.tridas OpenFOAM Paraview & paraFoam 4 November 29, 2011 14:30
Output transient file to csv Ben CFX 3 September 23, 2008 08:17

All times are GMT -4. The time now is 20:55.