CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (http://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   How to write a vector for each timestep (http://www.cfd-online.com/Forums/openfoam-programming-development/105004-how-write-vector-each-timestep.html)

Whyman July 20, 2012 08:48

How to write a vector for each timestep
 
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

niaz July 20, 2012 09:04

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?

Whyman July 20, 2012 09:12

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?

niaz July 20, 2012 09:32

Dear stefano
just write your consul by command like this
icoFoam > stefano.txt
then use grep command to separate your line from others.

Whyman July 20, 2012 09:42

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

niaz July 20, 2012 10:04

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

bigphil July 20, 2012 12:00

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.

13msmemusman April 20, 2015 09:34

1 Attachment(s)
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.

meth November 5, 2015 22:09

Quote:

Originally Posted by bigphil (Post 372637)
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

bigphil November 12, 2015 13:33

Quote:

Originally Posted by meth (Post 572115)
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

meth November 15, 2015 19:49

Thank you very much Philip.


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