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

Writing/Reading Field<Type> to/from Dictionary

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 19, 2012, 07:51
Default Writing/Reading Field<Type> to/from Dictionary
  #1
ngj
Senior Member
 
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,902
Rep Power: 37
ngj will become famous soon enoughngj will become famous soon enough
Dear all,

I have a small problem with IO of Field<Type> to and from dictionaries. I have, say, a vectorField named k of size 50 and a dictionary named dict.

I add the field k to dict in a preprocessing program by

dict.add("waveNumber", k, true);

In the dictionary, I get the following:

waveNumber 50 ( (<vector0>) ... (<vector49>) );

However, because it does not look like

waveNumber nonuniform List<vector> 50 ( (<vector0>) ... (<vector49>) );

I cannot read the field into another program in the constructor with the command

k_("waveNumber", dict, 50 )

Adding "nonuniform List<vector>" by hand solves the problem.

My question is, how I can write the field in the correct way from the very start?

Thanks a lot for your help,

Niels
ngj is offline   Reply With Quote

Old   January 19, 2012, 11:29
Default
  #2
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 22
marupio is on a distinguished road
I'm on the windows side doing post-processing this morning, so I can't look through the code in too much depth. But I can tell you, there is only one function that writes "nonuniform" to the Ostream, and that's Field::writeEntry. I guess using dictionary.add doesn't lead to this function.

Maybe that will help you a little...
__________________
~~~
Follow me on twitter @DavidGaden
marupio is offline   Reply With Quote

Old   January 19, 2012, 11:47
Default
  #3
ngj
Senior Member
 
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,902
Rep Power: 37
ngj will become famous soon enoughngj will become famous soon enough
Hi David

Thanks for your answer. I followed that lead as well, as I had seen e.g. boundary conditions writing with nonuniform, but I found myself in a dead end.

I also tried doing a brute force:

Code:
dict.add("waveNumber nonuniform List<vector>", k, 50);
but the compiler literally yelled at me, when I gave it to her - and all of the spaces are removed from the string anyway.

/ Niels
ngj is offline   Reply With Quote

Old   January 19, 2012, 12:13
Default
  #4
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 22
marupio is on a distinguished road
This problem was too tempting... so I switched to Linux...

When you use dictionary::add, it first converts your Field into a primitive entry, which writes it directly with operator<< instead of writeEntry, thus bypassing the "nonuniform" stuff.

I think the simplest way to get it to use writeEntry may be to make it into a "DimensionedField". Can you do that? DimensionedField uses writeEntry. You'd need a mesh and a dimensionSet. Your output would have the dimensionSet added as another keyword "dimensions".

The next easiest would be to create a custom derived "writeableField<Type>", which inherits Field, and does everything Field does, except override operator<< to use writeEntry.

-Dave
__________________
~~~
Follow me on twitter @DavidGaden
marupio is offline   Reply With Quote

Old   January 19, 2012, 13:00
Default
  #5
ngj
Senior Member
 
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,902
Rep Power: 37
ngj will become famous soon enoughngj will become famous soon enough
Thanks!

I will try either of your suggestions tomorrow morning.

/ Niels
ngj is offline   Reply With Quote

Old   January 20, 2012, 13:45
Default
  #6
ngj
Senior Member
 
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,902
Rep Power: 37
ngj will become famous soon enoughngj will become famous soon enough
Hi

Just to give a small update. I was not quite successful, however, I have made a ad-hoc solution, which only needs a tiny amount of manual interference. Instead of using

add("amp", amplitude, true)

where amplitude is a scalarField, I constructed a string as follows:

Code:
std::stringstream samp;

samp << "nonuniform List<scalar> " << amplitude.size() << "(";
forAll(amplitude, index)
{
    samp << amplitude[index] << " ";
}
samp << ")";
and then the write command looks like this:

Code:
dict_.add("amplitude", samp.str(), true);
It does, unfortunately leave " around the string in the dictionary, but it is easy to handle those in a small bash-script.

The solution is not as elegant as the one David suggested, however, it is a solution, which will definitely crash, if one forgets to use the bash-script, whereas the method first reported continues with a faulty field!

All the best,

Niels
ngj is offline   Reply With Quote

Old   January 23, 2012, 08:36
Default
  #7
ngj
Senior Member
 
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,902
Rep Power: 37
ngj will become famous soon enoughngj will become famous soon enough
And to put a final closure to the issue:

I added the following piece of code into the pre-processing program

Code:
std::string cmd("sed -e 's/\"nonuniform/nonuniform/' -e 's/)\";/);/' constant/waveProperties > constant/waveProperties.irregular; mv constant/waveProperties.irregular constant/waveProperties");

std::system( cmd.c_str() );
It works, so I am happy

Kind regards,

Niels
ngj is offline   Reply With Quote

Old   February 14, 2012, 08:47
Default
  #8
ngj
Senior Member
 
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,902
Rep Power: 37
ngj will become famous soon enoughngj will become famous soon enough
A small update on the above problem:

The method outlined above only works for short strings! In OF there is a hard-coded limit for the concatenation of strings, where the string is not allowed to exceed 1024 characters. This restriction seems weird as OF hardly uses strings, so why have such a limit?

Kind regards,

Niels
ngj is offline   Reply With Quote

Reply


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
writing subDict in a dictionary ubaid OpenFOAM Programming & Development 3 October 25, 2014 18:17
Doubt in term representation in the fvSchemes dictionary titio OpenFOAM Running, Solving & CFD 0 July 17, 2009 14:21
Reading from User Defined Dictionary File brosemu OpenFOAM Running, Solving & CFD 2 March 30, 2009 16:25
Problem with rhoSimpleFoam matteo_gautero OpenFOAM Running, Solving & CFD 0 February 28, 2008 07:51
FoamX error aachenBomb case Ervin Adorean (Adorean) OpenFOAM Pre-Processing 13 March 7, 2005 04:50


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