|
[Sponsors] |
Writing/Reading Field<Type> to/from Dictionary |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
January 19, 2012, 07:51 |
Writing/Reading Field<Type> to/from Dictionary
|
#1 |
Senior Member
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,902
Rep Power: 37 |
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 |
|
January 19, 2012, 11:29 |
|
#2 |
Senior Member
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 22 |
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 |
|
January 19, 2012, 11:47 |
|
#3 |
Senior Member
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,902
Rep Power: 37 |
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); / Niels |
|
January 19, 2012, 12:13 |
|
#4 |
Senior Member
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 22 |
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 |
|
January 19, 2012, 13:00 |
|
#5 |
Senior Member
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,902
Rep Power: 37 |
Thanks!
I will try either of your suggestions tomorrow morning. / Niels |
|
January 20, 2012, 13:45 |
|
#6 |
Senior Member
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,902
Rep Power: 37 |
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 << ")"; Code:
dict_.add("amplitude", samp.str(), true); 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 |
|
January 23, 2012, 08:36 |
|
#7 |
Senior Member
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,902
Rep Power: 37 |
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() ); Kind regards, Niels |
|
February 14, 2012, 08:47 |
|
#8 |
Senior Member
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,902
Rep Power: 37 |
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 |
|
|
|
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 |