|
[Sponsors] |
July 14, 2015, 02:11 |
Writing data to a file
|
#1 |
Member
Join Date: May 2014
Posts: 31
Rep Power: 12 |
Hello,
I modified a solver to calculate a scalar coefficient which I need to compare with some experimental data. This coefficient is not a scalar field, a simple scalar for each time value. My problem is how shall I write this scalar to some text file along with time value? Some thing like: (time scalar value) (0 0 ) (0.1 0.125 ) (0.2 0.137 ) etc? Or some alternative form is also fine. All I need to write is coefficient and time. Can some one please help? |
|
July 14, 2015, 10:05 |
|
#2 |
Senior Member
Hassan Kassem
Join Date: May 2010
Location: Germany
Posts: 242
Rep Power: 17 |
I believe this question was answered before in this forum, here is a short answer
Code:
#include "OFstream.H" fileName name = ("yorFileName"); OFstream OS(name); if (Pstream::master()) { if(OS.opened()) { OS << runTime.value() << tab << scalarValue << endl; } } |
|
July 15, 2015, 04:32 |
|
#3 |
Senior Member
Mehdi Asghari
Join Date: Feb 2010
Posts: 127
Rep Power: 16 |
Hello Hassan and Kcn ;
Should we use OFstream or IOdictionary? In which part of OpenFoam code, users should add above your code lines? Thanks for any answer. |
|
July 15, 2015, 05:19 |
|
#4 | |
Senior Member
Hassan Kassem
Join Date: May 2010
Location: Germany
Posts: 242
Rep Power: 17 |
Quote:
You use this code anywhere based on your code layout and the limitations of OFstreams. Generally for solvers, as per the original question, you can declare the OFstream before the time loop then use the OFstream object anywhere in your code in the same way as using Info or cout. |
||
July 18, 2015, 04:53 |
|
#5 | |
Senior Member
Mehdi Asghari
Join Date: Feb 2010
Posts: 127
Rep Power: 16 |
Quote:
Really, I have a problem which I want to know if I have to use OFstream or IOdictionary. Suppose, There is an inlet x-axis velocity as boundary condition in my problem. I'm going to save the flow field for four different inlet velocity values. These four inlet velocities have been specified according to the following expression in a vector: A={1, 2, 3.5, 4.2}. Therefore, I defined boundary condition as follows: Code:
inlet { type codedFixedValue; value $internalField; redirectType ramp; code #{ # How to read A[i] from main C++ program ( stated at next code)? Should I use IOdictionary or OFstream? (*this)==vector(A(i),0,0); #}; } Code:
#start of Main program vector<double> A(4); A[0]=1;A[1]=2;A[2]=3.5;A[3]=4.2; for (int i = 0; i < 4; ++i) { # How to run OF program into a loop with A[i] in this line? # How to save steady state flow in the separated folder for every inlet velocity in this line? } #end of main program Thanks for any kind attention &answer. |
||
July 18, 2015, 07:23 |
|
#6 |
Senior Member
Hassan Kassem
Join Date: May 2010
Location: Germany
Posts: 242
Rep Power: 17 |
Hello Mehdi,
I haven't the full answer for your questions but I would like to highlight few points;
I hope that my comments will be useful for your case and kindly notice that these are just general recommendations you shouldn't consider them as the only solution or a black and white guidelines. For example you will find that sometimes is essential to use the standard for-loop and OF source code has plenty of such cases. |
|
July 19, 2015, 05:29 |
|
#7 |
Senior Member
Mehdi Asghari
Join Date: Feb 2010
Posts: 127
Rep Power: 16 |
Hello Hassan,
First of all, I should describe that in my case , every member of vector(A) is corresponding with a inlet velocity that is in X-axis direction (Ux). Suppose, I need to gain flow field for t=10 s. I don't need transient results up to 10s and I'd like to compare flow fields corresponding every inlet velocity with together in final time (t=10s). There are two ways for this object explained as follows: 1-Running OF by run command in Ubuntu for every Ux with considering fix value for inlet boundary condition. Then comparing results and plotting graphs for every Ux (Vector A). 2- Altogether, Writing a code to run OF within a loop calling OF 2.3.1 and calling every Ux=A(i) and saving 10s results for every Ux in a separated folder. In this case, calling OF within loop is an important issue. In my belief, this way is faster than first option. I selected second option. Obviously, for option 2, Wmake command have to be executed after code writing until code be executable. How can I do this case ? What are general guidelines for wring a code according to the way (option)2 ? Thanks, |
|
July 19, 2015, 05:46 |
|
#8 |
Senior Member
Hassan Kassem
Join Date: May 2010
Location: Germany
Posts: 242
Rep Power: 17 |
As I understand, you want to run you solver with four different inlet conditions for Ux and stop and save the results after 10s. Additionally you want to run these for cases automatically.
I would suggest in that case to write bash script (like Allrun in OF) to loop over the different cases and you can change the BC using sed command. Also you can have a look on pyFoam maybe it could do the job for you. |
|
July 19, 2015, 12:21 |
|
#9 | |
Senior Member
Mehdi Asghari
Join Date: Feb 2010
Posts: 127
Rep Power: 16 |
Quote:
I don't know great things about bash scripting. For example, how can I transfer vector/matrices values between inlet boundary & basch script? For example, I defined inlet boundary in blockMeshdict as follows: Code:
inlet { type codedFixedValue; value $internalField; redirectType ramp; code #{ # How to read A[i] from main bash scripting code presented as follows? (*this)==vector(A(i),0,0); #}; } Code:
# Is this expression correct here?---> A={1, 2, 3.5, 4.2} # How create folders here for every Ux? for i =1 to 4 do runApplication $(getApplication) done Also, I tried to use PyFoam. However, after installing PyFoam, I had some troubles with a simple tutorial that I post it at following post: http://www.cfd-online.com/Forums/ope...-problems.html Again, I asked my questions with red color at above codes. Many thanks for your attentions. |
||
July 19, 2015, 12:46 |
|
#10 |
Senior Member
Hassan Kassem
Join Date: May 2010
Location: Germany
Posts: 242
Rep Power: 17 |
I really cannot help with pyFoam because I don't use it but I read many times on this forum how it could help solving such problems. I think even basic python script could solve your problem.
For bash script, it should be straight forward to create and copy folder because it works exactly like working from the terminal. Your for loop syntax is right, for more options see; http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html Also, you will need to modify the velocity BC from your bash script which could be done using sed see; http://stackoverflow.com/questions/5...ork-it-empties I hope these hints could be useful because I have limited experience with bash scripting in general. |
|
July 20, 2015, 16:41 |
|
#11 |
Senior Member
Mehdi Asghari
Join Date: Feb 2010
Posts: 127
Rep Power: 16 |
Hello Hassan,
Thank you very much for your attentions & advises during this discussion. Although, I have many problems with reading and writing files/data/vectors into OF and vise versa operation yet that I will probably have to post them in next treads and I welcome you to take part in the my next discussion soon. Sincerely. M.Asghari |
|
April 17, 2016, 10:02 |
|
#12 | |
New Member
Aaron
Join Date: Apr 2016
Posts: 24
Rep Power: 10 |
Quote:
I use OF2.3.1 version, why I use this expression or only use OFstream.H head file #include "OFstream.H" show this errors? can you provide some suggestion? /usr/include/c++/4.8/bits/codecvt.h: In function ‘int main(int, char**)’: /usr/include/c++/4.8/bits/codecvt.h:41:15: error: expected ‘=’ before ‘__attribute__’ namespace std _GLIBCXX_VISIBILITY(default) ^ /usr/include/c++/4.8/bits/codecvt.h:41:15: error: expected identifier before ‘__attribute__’ /usr/include/c++/4.8/bits/codecvt.h:41:15: error: expected ‘;’ before ‘__attribute__’ /usr/include/c++/4.8/bits/codecvt.h:41:15: error: expected primary-expression before ‘__attribute__’ /usr/include/c++/4.8/bits/codecvt.h:41:15: error: expected ‘;’ before ‘__attribute__’ Best Reards, Aaron |
||
April 17, 2016, 10:48 |
|
#13 | |
Senior Member
Hassan Kassem
Join Date: May 2010
Location: Germany
Posts: 242
Rep Power: 17 |
Hello Aaron,
I don't fully understand your question. Could you please include the code which gives this error? I'm sure that this problem isn't related to OF version because I used this class many times under diffrent versions. Bw, Hassan Kassem Quote:
|
||
March 21, 2021, 21:50 |
How can I avoid overwritting its old time value ?
|
#14 | |
New Member
王子阳
Join Date: Aug 2019
Posts: 29
Rep Power: 6 |
Quote:
How can i solve this problem? best wishes ziyang |
||
November 19, 2022, 14:26 |
|
#15 |
Member
Shah Akib Sarwar
Join Date: Mar 2021
Posts: 41
Rep Power: 5 |
||
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
how to calculate mass flow rate on patches and summation of that during the run? | immortality | OpenFOAM Post-Processing | 104 | February 16, 2021 08:46 |
CFD by anderson, chp 10.... supersonic flow over flat plate | varunjain89 | Main CFD Forum | 18 | May 11, 2018 07:31 |
[Other] Adding solvers from DensityBasedTurbo to foam-extend 3.0 | Seroga | OpenFOAM Community Contributions | 9 | June 12, 2015 17:18 |
centOS 5.6 : paraFoam not working | yossi | OpenFOAM Installation | 2 | October 9, 2013 01:41 |
Version 15 on Mac OS X | gschaider | OpenFOAM Installation | 113 | December 2, 2009 10:23 |