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

Add to a list in IOdictionary

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By stosse

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   February 6, 2015, 04:50
Default Add to a list in IOdictionary
  #1
New Member
 
Sindre Tosse
Join Date: Sep 2014
Posts: 6
Rep Power: 11
stosse is on a distinguished road
Hi fellow foamers,
I am trying to figure out how to add an entry to a list in an IOdictionary.

Specifically, I am trying to add a dictionary to the "actions" list in an allready existing topoSetDict (see example below), possibly also modifying an existing entry. How would I do this? I know how to add to and modify a dictionary, but I can't figure out how to do the same with a list.

before:
Code:
actions
(
    {
        name    foo;
        etc.
    }
);
after:
Code:
actions
(
    {
        name    foo;
        etc.
    }
    {
        name    bar;
        etc.
    }
);
Regards,
Sindre
stosse is offline   Reply With Quote

Old   February 6, 2015, 12:02
Default
  #2
New Member
 
Ramon
Join Date: Feb 2014
Location: Eindhoven
Posts: 25
Rep Power: 12
RjwV is on a distinguished road
Hello Sindre,

I am not sure how to do what you requested (and perhaps I misunderstood your request), but is it not possible to make your dict like this?

Code:
names
(
    foo
    bar
);

foo
{
    action_foo;
};

bar
{
    action_bar;
};
This is not so difficult to program into OpenFOAM.

Kind regards,
Ramon
RjwV is offline   Reply With Quote

Old   February 9, 2015, 02:49
Default
  #3
New Member
 
Sindre Tosse
Join Date: Sep 2014
Posts: 6
Rep Power: 11
stosse is on a distinguished road
Hi Ramon,
No, the formatting of the dictionary is set.
I figured out how to modify entries in a list of dictionaries. The below example adds a field "test" which is true if the name of the dict is "baffleFaces" and false otherwise. However, when the dictionary is written, all formatting (e.g. line breaks and whitespace) disappears, so the topoSetDict becomes very ugly
Code:
IOdictionary topoSetDict
        (
            IOobject
            (
                "topoSetDict",
                runTime.system(),
                runTime,
                IOobject::READ_IF_PRESENT,
                IOobject::AUTO_WRITE
            )
    );
   
    PtrList<dictionary> actions(topoSetDict.lookup("actions"));
    
    const word bfname = "baffleFaces";
    for(int i; i<actions.size(); i++){
        const word name = actions[i].lookup("name");
        
        if (name.compare(bfname)==0){
            actions[i].add("test", "true", true);
        }
        else{
            actions[i].add("test", "false", true);
        }
    }

    topoSetDict.add("actions", actions, true); //true flag merges dictionaries
    
    topoSetDict.regIOobject::write();
sk11 likes this.
stosse is offline   Reply With Quote

Old   March 17, 2021, 05:11
Default write list of dictionaries as one keyword per line
  #4
New Member
 
Sarath
Join Date: Mar 2017
Location: Spain
Posts: 22
Rep Power: 9
sk11 is on a distinguished road
I could do this using creating a list of dictionaries and adding them by append().

for example :

Code:
List<dictionary> actions;
dictionary actionDict;
actionDict.add("name",foo);
... 

actions.append(actionDict);


topoSetSict.add("actions",actions,true);
topoSetDict.regIOobject::write();
But with this code I have the same problem where IODictionary writes lists to file without newlines which is not userfreindly(OF version 8.0). This is perfect for list of scalars or vectors but not for list of dictionaries like actions in topoSetDict. It should be written as subdictionaries, one keyword per line.
Is there a way to control how lists of dictionaries (or entries) are written to the file?

for example, cellSet dict files generated by topoSet contains a list of labels with one label per line.
sk11 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
[swak4Foam] difficulties installing swak4foam newbie29 OpenFOAM Community Contributions 120 October 21, 2022 04:01
Help for the small implementation in turbulence model shipman OpenFOAM Programming & Development 25 March 19, 2014 10:08
[swak4Foam] funkySetFields compilation error tayo OpenFOAM Community Contributions 39 December 3, 2012 05:18
CFX problem in ubuntu (linux) Vigneshramaero CFX 0 July 13, 2012 10:22
Read inside a class tonyuprm OpenFOAM 12 July 14, 2010 02:35


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