CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

How to write a vector for each timestep

Register Blogs Community New Posts Updated Threads Search

Like Tree13Likes
  • 1 Post By niaz
  • 1 Post By niaz
  • 8 Post By bigphil
  • 3 Post By bigphil

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   July 20, 2012, 09:48
Question How to write a vector for each timestep
  #1
Member
 
Stefano
Join Date: Jul 2009
Posts: 36
Rep Power: 17
Whyman is on a distinguished road
Hi foamers!

I have a question. I would like to write a dimensioned vector (the center of mass of the liquid) for each timestep that I save.

How can I write the command line?

Thank you
Stefano
Whyman is offline   Reply With Quote

Old   July 20, 2012, 10:04
Default
  #2
Senior Member
 
niaz's Avatar
 
A_R
Join Date: Jun 2009
Posts: 122
Rep Power: 17
niaz is on a distinguished road
Dear Stefano
your case is vague. you said that you have saved it. now, you want to write it?
how you can saved it, without writing?
niaz is offline   Reply With Quote

Old   July 20, 2012, 10:12
Default
  #3
Member
 
Stefano
Join Date: Jul 2009
Posts: 36
Rep Power: 17
Whyman is on a distinguished road
Dear Niaz,

I would like to save on a file the value of the centre of mass (CoM) of a liquid contained in an oscillating tank. The case is unsteady, so i would like to save the vector every timestep, in order to check the time evolution.

Up to now i can write the vector only on the terminal. Now i would like to save it on a file.

What command line have I to write on the source code?
Whyman is offline   Reply With Quote

Old   July 20, 2012, 10:32
Default
  #4
Senior Member
 
niaz's Avatar
 
A_R
Join Date: Jun 2009
Posts: 122
Rep Power: 17
niaz is on a distinguished road
Dear stefano
just write your consul by command like this
icoFoam > stefano.txt
then use grep command to separate your line from others.
mm.abdollahzadeh likes this.
niaz is offline   Reply With Quote

Old   July 20, 2012, 10:42
Default
  #5
Member
 
Stefano
Join Date: Jul 2009
Posts: 36
Rep Power: 17
Whyman is on a distinguished road
Niaz,

I already do this, but I would like to avoid all these passages and directly write on file by using the source code.

Isn't there any other way to directly write a dimensioned vector on file, for each timestep?


Stefano
Whyman is offline   Reply With Quote

Old   July 20, 2012, 11:04
Default
  #6
Senior Member
 
niaz's Avatar
 
A_R
Join Date: Jun 2009
Posts: 122
Rep Power: 17
niaz is on a distinguished road
you should use pstream to do that
you can look fieldminmax as sample to create your functionobject.
it is a bit hard but the sample may help you
mm.abdollahzadeh likes this.
niaz is offline   Reply With Quote

Old   July 20, 2012, 13:00
Default
  #7
Super Moderator
 
bigphil's Avatar
 
Philip Cardiff
Join Date: Mar 2009
Location: Dublin, Ireland
Posts: 1,097
Rep Power: 34
bigphil will become famous soon enoughbigphil will become famous soon enough
Hi Stefano,

If you would like to save some data to a file during your simulation, you could do something like this:

Put this at the top of your file under "#include "fvCFD.H":
Code:
#include "OFstream.H
Then put this before the time loop:
Code:
OFstream myFile("myData.txt");
Then put this inside the time loop where ever you want to do your calculation:
Code:
vector someVector(1,2,3);

myFile << "data for time " << runTime.value() << " is " << someVector << endl;
Best regards,
Philip

By the way, the above code is for serial simulations. For parallel simulations, it must be edited so as processors do not write to the same file.

Last edited by bigphil; July 20, 2012 at 13:04. Reason: Mention parallel stuff
bigphil is offline   Reply With Quote

Old   April 20, 2015, 10:34
Post
  #8
Member
 
Muhammad Usman
Join Date: Feb 2014
Posts: 91
Rep Power: 0
13msmemusman is on a distinguished road
Sir i tried to do as you said. but the problem is that it shows all sphares at a time. i want to watch center of mass moving at runtime. but the method you specified gives all locations of center of mass at a time.
i am attaching the image with this thread.
Attached Images
File Type: jpg Screenshot from 2015-04-20 18:31:19.jpg (50.1 KB, 38 views)
13msmemusman is offline   Reply With Quote

Old   November 5, 2015, 22:09
Default
  #9
Member
 
methma Rajamuni
Join Date: Jul 2015
Location: Victoria, Australia
Posts: 40
Rep Power: 11
meth is on a distinguished road
Quote:
Originally Posted by bigphil View Post
Hi Stefano,

If you would like to save some data to a file during your simulation, you could do something like this:

Put this at the top of your file under "#include "fvCFD.H":
Code:
#include "OFstream.H
Then put this before the time loop:
Code:
OFstream myFile("myData.txt");
Then put this inside the time loop where ever you want to do your calculation:
Code:
vector someVector(1,2,3);

myFile << "data for time " << runTime.value() << " is " << someVector << endl;
Best regards,
Philip

By the way, the above code is for serial simulations. For parallel simulations, it must be edited so as processors do not write to the same file.

Hi Philip,

Your comment helped me a lot

I edited my solver to write a vector into a file in each time step without considering that I am going to run it parallel. And every time I run the program the vector has been repeated 8 time. Now I know it's because I have run using 8 cores.

Do you know how to fix it? I am going to look into the forces.C file to see how they have overcome this when they write the forces data into a dat file.

Thanks,

Methma
meth is offline   Reply With Quote

Old   November 12, 2015, 13:33
Default
  #10
Super Moderator
 
bigphil's Avatar
 
Philip Cardiff
Join Date: Mar 2009
Location: Dublin, Ireland
Posts: 1,097
Rep Power: 34
bigphil will become famous soon enoughbigphil will become famous soon enough
Quote:
Originally Posted by meth View Post
Hi Philip,

Your comment helped me a lot

I edited my solver to write a vector into a file in each time step without considering that I am going to run it parallel. And every time I run the program the vector has been repeated 8 time. Now I know it's because I have run using 8 cores.

Do you know how to fix it? I am going to look into the forces.C file to see how they have overcome this when they write the forces data into a dat file.

Thanks,

Methma
Hi Methma,

For parallel, the information should be sent to one processor and it should be the only processor to write to the file.

Here is an example:

At the start of the program, just the master processor should open the file:
Code:
OFstream* myOutFilePtr = NULL;

if (Pstream::master())
{
    // Open the file
    myOutFilePtr = new OFstream("myFile.txt");
}
The, you can write to the file like this:
Code:
// Data on the current processor
scalar myLocalData = 2;

// Sync data to the master (in this, I will get the max)
reduce(myLocalData, maxOp<scalar>());

// Now myLocalData contains the global max i.e. the max on all processors

// We will now get the master to write to the file
if (Pstream::master())
{
    OFstream& myOutFile = *myOutFilePtr;

    myOutFile
        << "The data is " << myLocalData << ends;
}
Best regards,
Philip
ssss, meth and randolph like this.
bigphil is offline   Reply With Quote

Old   November 15, 2015, 19:49
Default
  #11
Member
 
methma Rajamuni
Join Date: Jul 2015
Location: Victoria, Australia
Posts: 40
Rep Power: 11
meth is on a distinguished road
Thank you very much Philip.
meth is offline   Reply With Quote

Reply

Tags
vector


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 Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
mesh file for flow over a circular cylinder Ardalan Main CFD Forum 7 December 15, 2020 14:06
Restart 2-way FSI with different timestep? Lance CFX 10 April 17, 2013 01:37
Timestep and Pressure Correction Relationship in SIMPLE rks171 Main CFD Forum 23 May 4, 2012 02:04
Phase locked average in run time panara OpenFOAM 2 February 20, 2008 15:37
write a xy data file every timestep pieter brodeoux FLUENT 1 December 5, 2007 18:30


All times are GMT -4. The time now is 05:00.