Writing/output of simple data into an ASCII-File
Dear All,
The backgrund is the coupling of an external program to OpenFoam. The coupling works with a data file which is read and written alternately from the two programs. The input from the file to OpenFoam and the calling of the external porogramm works, but not the output of data from OpenFoam. I calculate data in a self-defined boundary condition of the solver "chtMultiRegionSimpleFoam" and want to write out this data into an ASCII-File, but it doesn`t work. I searched for a long time in the forum and in the documentation and don`t find any hint for the exact syntax. Maybe anybody can give me an advice ? Firstly, I define an Iodictionary: IOdictionary OutputtoFile ( IOobject ( "output-file", db().time().constant(), db(), IOobject::NO_READ, IOobject::AUTO_WRITE, true ) ); Normally the command OutputtoFile.write() should work, but it doesn`t. Secondly, I found two hints, that there is a bug in Version 1.7 (which I use): http://www.cfd-online.com/Forums/ope...herfields.html and http://www.openfoam.com/mantisbt/view.php?id=34 The authors, that it should work like this: "OutputtoFile.Foam::regIOobject::write()" And really, with this command, I find a file which is named "output-file" in the directory "constant" !!! It includes the formal file header which can be found in all data files. But I am not able to get the syntax for writing out any data. I define for example a scalar field with 5 variables and calculate 5 values which I want to have in the data file: "scalarField area(number_);", where number = 5. Now, I want to write out the scalar field "area". I tried several syntax variants, but always there is an error during the translation. Here some trials and the errors after the arrow: OutputtoFile.Foam::regIOobject::write(area); è derivedFvPatchFields/detchemHeatFluxTemperature/detchemHeatFluxTemperatureFvPatchScalarField.C:332 :61: error: no matching function for call to âFoam::IOdictionary::write(Foam::scalarField&)â /home/mahe/OpenFOAM/OpenFOAM-1.7.1/src/OpenFOAM/lnInclude/regIOobject.H:216:26: note: candidate is: virtual bool Foam::regIOobject::write() const OutputtoFile.Foam::regIOobject::write("area"); è derivedFvPatchFields/detchemHeatFluxTemperature/detchemHeatFluxTemperatureFvPatchScalarField.C:332 :63: error: no matching function for call to âFoam::IOdictionary::write(const char [22])â /home/mahe/OpenFOAM/OpenFOAM-1.7.1/src/OpenFOAM/lnInclude/regIOobject.H:216:26: note: candidate is: virtual bool Foam::regIOobject::write() const OutputtoFile.Foam::regIOobject::write(area&); è derivedFvPatchFields/detchemHeatFluxTemperature/detchemHeatFluxTemperatureFvPatchScalarField.C:332 :62: error: expected primary-expression before â)â token It seems also not possible to write out one single number or constant ... I don`t believe that it is so complicated in OpenFoam to write out some simple numbers to somewhere ??? I hope, there are other users concerned, who solved the problem meanwhile or look for a discussion. I would be very grateful for every helpful advice. Greetings Matthi |
Quote:
I believe that using the IOdictionary to write the solution like you have works for OpenFOAM's data structures like 'volumeScalarField's or 'surfaceScalarField' s. I have not tried it for 'scalarField's though. If the data you want is not a standard OpenFOAM data structure I would just go with a standard C++ file write. |
The object registry has its own built-in format for writing every object. If you want to take control of the output, don't use an IOdictionary. Use an OFstream.
Code:
fileName outputFile(runTime.path()/runTime.timeName()); |
Dear Adhiraj
Thanks for you fast reply. If I want to have the file in the "constant" or the "system"-directory is not the point. Meanwhile I tried several approaches and found a small hint concerning the command ".set". ========================= The actual status is the following: ========================= DEFINITION OF SCALAR FIELD ---------------------------- "scalarField T_mean(number_);", where number = 5. DEFINITION OF IODICTIONARY ---------------------------- IOdictionary OutputtoFile ( IOobject ( "output-file", db().time().constant(), db(), IOobject::NO_READ, IOobject::AUTO_WRITE, true ) ); Then I calculate the 5 values of the scalar "T_mean". SET AND OUTPUT ----------------- OutputtoFile.set("T_mean",T_mean); OutputtoFile.Foam::regIOobject::write(); Great! The output in the File "output-file" looks like: T_mean 5 ( 300.01791 300.03583 300.05374 300.07166 300.0895 ); The values are mean temperatures of defined grid regions of a single boundary. This is one big step further !!! The format is not very suitable to read it in with fortran (with another code), because the number of values is variable. But I will search for a solution where alle values are in one column. The code lines above are now positioned in the routine where the boundary conditions are defined. This is BEFORE the time step. Better would be to calculate the mean Temperatures I want to have AFTER the time step. But I don`t know how to get access to the boundary somewhere at the end of the solver to handle the output of the values AFTER the solving. Thanks and Greetings Matthi |
Hello David,
thanks for the advice. I will try this approach and see If I understand the syntax ... Also Thanks and Greetings for you Matthi |
Quote:
thanks for the advice. I will try this approach and see If I understand the syntax ... Also Thanks and Greetings for you Matthi |
Dear Adhiraj, Dear Marupio,
thanks for your help. It works now. I used the approach of marupio. First the translation failed. The solution was to include additionally the line (#include "OFstream.H") in the routine. Now the test code is: DEFINITION OF SCALAR FIELD ---------------------------- scalarField T_mean(5); Then, calculation of the 5 values of the scalar "T_mean". OUTPUT OF SCALAR DATA -------------------------- fileName outputFile("TEST.txt"); OFstream os(db().time().system()/outputFile); os << "This is the first line in the file.\n"; os << T_mean.size(); os << endl; forAll(T_mean, i) { os << T_mean[i]; os << endl; } THE DATA IN THE FILE "TEST.txt" IN THE FOLDER "system" IS ----------------------------------------------------------- This is the first line in the file. 5 300.01791 300.03583 300.05374 300.07166 300.0895 This is a great basis for my further development. Again Thanks a lot and many greetings Matthi |
Hi
I would like to output a simple array data from a .H file to a text file. And also, I would like to read this data set from another .H file. However, I couldn't do it. === 1 === It is the array: Code:
label array[100]; The array is filled with a group of arbitrary values. === 3 === I had tried this. Code:
std::ofstream output; When I compiled it with wmake, the following error popped up: Code:
error: aggregate ‘std::ofstream output’ has incomplete type and cannot be defined. I have found some other forum pages/web-stes which consider similar issues in a slightly different context. Therefore, I somehow couldn't adapt the given answers to my case. I appreciate any help. Many thanks in advance. |
The solution to my case is to change the location of output code from .H file to .C file.
Code:
#include "IFstream.H" |
Hi HakikiCanakkaleli,
Maybe it is not suitable to post here, but I think you can help me, since the topic is quite similar... My problem is like this: I want to use sampleDict to get the velocity on a line. Since there are too many lines I need to plot, therefore it is challenging to input the start point and end point by hands. I got the coordinates of all the points in .txt file (x,y,z). Do you know how to read these data in sampleDict? I think we need a loop to use all these points, right? I really appreciate you can give some hints! Thank you! Best regards, Ye |
Quote:
|
Hi there,
I wrote and compiled the following but it is not writing any file: Code:
const fvMesh& mesh = this->owner().mesh(); I am anyway not sure about the path I should specify given that this variables should be printed continuously, as there will be particles hitting patches all the time. Any suggestions would be very much appreciated! Thanks a lot Quote:
|
Nevermind... I solved my own problem once again...
|
Quote:
|
Quote:
Well, there is a nice cloudFunctionObject that allows you to do that: patchPostProcessing. The set up of this function should be inside the cloudFunctions section in the reacting/kinematic/colliding/CloudProperties dictionary and it is similar to this one: Code:
patchPostProcessing Regards |
Thanks Alex.
Actually, this has nothing to do with the simulations I'm running now. But I was curious to know the solution. ;-) |
Dear Foamers,
Greetings !! Using the information in this thread, i was able to write a file containing a variable of type labelListList successfully. Thanks for sharing your knowledge. Now I would like to read this file and store it in a variable of same type (labelListList). Can someone suggest me a way to do this ?? |
Reading/writing OpenFoam data within a loop structure
Hello All,
I need to modify the value of a vector used into the some files (dictionaries/fvOptions) at Openfoam into a loop striucture, too. An algorithm with loop structure can be suggested for this purpose such as follows: 1- Initialize Vector V 2- Into the loop structure: i) Modifying some files/dictionaries based on Vector (V) by reading vector V. ii) Running/Executing OpenFoam application with new modified files/dictionaries within OF iii) Writing/modifying some output parameters (for example by probe) and saving them at another accessory ASCII file/program iv) Writing/modifying V vector based on accessory ASCII file/Program gained at previous section 3- Out-processing As you consider, vector V transfers between accessory ASCII file and main OpenFoam2.3.1 program. Does anyone know how to do this operation by cplusplus in Ubuntu/Linux? Thanks a lot for any advise. M.Asghari |
Quote:
filename outputfile("OutputtoFile.txt") . . . . . . scalarField fluxOut(2) OFstream os(db().time().system()/outputFile); os<<fluxOut.size(); os<<endl; forAll(fluxOut,i) { os<<fluxOut[i]; os<<endl; } OutputtoFile.txt.set(fluxOut); OutputtoFile.txt.Foam::regIOobject::write(); and got the following error calcMassFlow.C:214:5: note: in expansion of macro ‘forAll’ forAll(fluxOut,i) ^ In file included from rhoCentralFoam.C:267:0: calcMassFlow.C:216:20: error: invalid types ‘Foam::scalar {aka double}[Foam::label {aka int}]’ for array subscript os<<fluxOut[i]; ^ calcMassFlow.C:219:5: error: ‘OutputtoFile’ was not declared in this scope OutputtoFile.txt.set(fluxOut); |
Quote:
I have wrote down for same piece of code for flux calculation filename outputfile("OutputtoFile.txt") . . . . . . scalarField fluxOut(2) OFstream os(db().time().system()/outputFile); os<<fluxOut.size(); os<<endl; forAll(fluxOut,i) { os<<fluxOut[i]; os<<endl; } OutputtoFile.txt.set(fluxOut); OutputtoFile.txt.Foam::regIOobject::write(); and got the following error calcMassFlow.C:214:5: note: in expansion of macro ‘forAll’ forAll(fluxOut,i) ^ In file included from rhoCentralFoam.C:267:0: calcMassFlow.C:216:20: error: invalid types ‘Foam::scalar {aka double}[Foam::label {aka int}]’ for array subscript os<<fluxOut[i]; ^ calcMassFlow.C:219:5: error: ‘OutputtoFile’ was not declared in this scope OutputtoFile.txt.set(fluxOut); |
All times are GMT -4. The time now is 18:43. |