Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace rapidjson to nlohmann json #58

Merged
merged 127 commits into from
Jan 16, 2019

Conversation

RiccardoRossi
Copy link
Member

Hello,
this branch removes rapidjson as support library for the KratosParameters and replaces it to the "nlohmann" json library.

This is more or less a complete rewrite of the kratos_parameters, which however maintains the same API and should (hopefully) not affect at any user. It passes all of the existing unit tests.

i believe that the new code base is sensibly cleaner than the old version and the nlohmann library is more modern than rapidjson, apart for being contained within a single header.
The most important advantage is that json objects are stored in a std::map instead in a list as happend with rapidjson, making unordered insertion/search much faster.

A nice feature is also that the new lib should also allow serializing/reading to a binary format

WARNING: the current version has not been tested on windows, so before accepting the pull request someone should try that out.

@loumalouomega
Copy link
Member

Just one thing, if JSON is faster as IO there is any plan in the future to switch the mdpa in plain text to mdpa.json? @RiccardoRossi @pooyan-dadvand

@RiccardoRossi
Copy link
Member Author

RiccardoRossi commented Feb 22, 2017 via email

@pooyan-dadvand
Copy link
Member

I will check it in windows. But would be tomorrow.

@josep-m-carbonell
Copy link
Member

I think two important things must be solved before replacing the json library:

1.- In this library, are list iterators available? And can them be exported to python to avoid asking for the size of the json lists and to avoid loops with range ?

2.- Does it allows to ask for the json keys ? We need this functionality to read the variables in the new materials processes in json.

Regards

@RiccardoRossi
Copy link
Member Author

RiccardoRossi commented Feb 22, 2017 via email

@josep-m-carbonell
Copy link
Member

josep-m-carbonell commented Feb 22, 2017

Yes, I think something is missing. In the py file you mention, you have the example:

This is how a loop is done now when we have a list in json:

    my_list = subparams["list_value"]
    for i in range(my_list.size()):
        if my_list[i].IsBool()
        ...

and this is how we would like to do it:

    my_list = subparams["list_value"]

    for item in my_list:
        if item.IsBool():
        ...

And also the important feature of getting the keys and variables from a json. As is done in the read_materials_process.py, which imports the native json from python, i.e.:

    #TODO: change to use the KratosParameters once dictionary iterators are exported
    import json

    #read variables 
    for key, value in mat["Variables"].items():
        var = self._GetItemFromModule(key)
        prop.SetValue( var, value)

    #read table
    for key, table in mat["Tables"].items():
        table_name = key

        input_var = self._GetItemFromModule(table["input_variable"])
        output_var = self._GetItemFromModule(table["output_variable"])

        new_table = PiecewiseLinearTable()
        for i in range(len(table["data"])):
            new_table.AddRow(table["data"][i][0], table["data"][i][1])
        prop.SetTable(input_var,output_var,new_table)

I think we also would like to do it without importing json from python.. or can we import it and use it?

@RiccardoRossi
Copy link
Member Author

hello,
i added the iterators to the new branch,
you can find an example of usage at the very end of the test_kratos_parameters.py,
(the interface is as the one you described)

however before changing the materials input i would wait for this branch to be approved, since this feature is not current available in the master branch

Copy link
Member

@pooyan-dadvand pooyan-dadvand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have checked it in my Visual Studio and it doesn't compile due to the fact that my VS build version (14.023107.0) is older than minimum version supported by nlohmann json (14.0.25123.0). The updated version won't work with boost.
So we should postponed these changes.

@RiccardoRossi
Copy link
Member Author

RiccardoRossi commented Feb 23, 2017

@pooyan-dadvand

i agree. This is a blocker. Writing an issue for the upstream library

@roigcarlo roigcarlo deleted the replace-rapidjson-to-nlohmann-json branch May 8, 2017 07:39
@roigcarlo roigcarlo restored the replace-rapidjson-to-nlohmann-json branch May 8, 2017 07:39
@loumalouomega
Copy link
Member

@RiccardoRossi the branch is still opened

@philbucher
Copy link
Member

now that boost python has been replaced maybe you want to give this another try?

@RiccardoRossi
Copy link
Member Author

RiccardoRossi commented May 18, 2018 via email

@loumalouomega
Copy link
Member

Which is the difference respect the current version?

@loumalouomega loumalouomega reopened this May 18, 2018
@RiccardoRossi
Copy link
Member Author

RiccardoRossi commented May 18, 2018 via email

@loumalouomega
Copy link
Member

This is ready @RiccardoRossi and @pooyan-dadvand

@@ -228,6 +228,14 @@ Parameters::Parameters(Parameters const& rOther)
/***********************************************************************************/
/***********************************************************************************/

Parameters::Parameters(Parameters&& rOther) : Parameters()
{
swap(rOther);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is custom then maybe a different name would be better to not confuse it with the one in the standard ...?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation already has a swap method

void swap(Parameters& rOther) noexcept

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is on master

@pooyan-dadvand
Copy link
Member

@philbucher does it work with Intel compiler? We should be very cautious with compiler compatibility of new libraries...

@philbucher
Copy link
Member

I tested it with Intel and it seems to work 👍
I ran multiple tests in release and fulldebug without problems

@philbucher
Copy link
Member

is this happening?

then we could also implement the long-awaited #2241

@loumalouomega
Copy link
Member

I will solve the conflict, but this is ready to merge

@loumalouomega
Copy link
Member

Conflict solved

pybind11::list items(Parameters const& self)
{
pybind11::list t;
for(Parameters::const_iterator it=self.begin(); it!=self.end(); ++it)
for(auto it=self.begin(); it!=self.end(); ++it)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does auto determine the correct type here? => iterator or const_iterator?

With iterators I am usually a bit unsure

@roigcarlo do you know?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The self is const, so the auto can be only a const iterator.. In any can be changed

Suggested change
for(auto it=self.begin(); it!=self.end(); ++it)
for(Parameters::const_iterator it=self.begin(); it!=self.end(); ++it)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted @philbucher


// External includes
#include "json/json_fwd.hpp" // Import forward declaration nlohmann json library
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you explain briefly what exactly you did with this?
I know it is needed to pass Travis, but it would be good to know in case someone wants to update the library

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Th forward declaration is only a declaration of the classes (just the name), which means that you can work only with references and pointers, so the compiler can manage the memory for that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -0,0 +1,79 @@
#ifndef NLOHMANN_JSON_FWD_HPP
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the original file that you downloaded or did you modify it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the one in the json repository + the declaration of the iterators (in the original just the json class is forward declared)

Copy link
Member

@philbucher philbucher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx for the effort

I meant that we can also add it in the future, since this is the eternal PR already and now it needs again to be reviewed

If you want you could revert and we can add it in the future

* This version walks and validates the entire json tree below the point at which the function is called
* @param rDefaultParameters Parameters of reference which we use to check
*/
void RecursivelyValidateAndAssignDefaults(const Parameters& rDefaultParameters);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method should only accept a const object, why would it need to modify the input?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not change the current behavior right now

@pooyan-dadvand
Copy link
Member

To be discussed in the next week @KratosMultiphysics/technical-committee meeting.

@loumalouomega
Copy link
Member

loumalouomega commented Jan 11, 2019 via email

@pooyan-dadvand
Copy link
Member

Special thanks to @RiccardoRossi and @loumalouomega for their effort on this! 👍

@loumalouomega
Copy link
Member

I am so happy, finally

@loumalouomega
Copy link
Member

I am so happy, finally

Finally

imagen

@loumalouomega loumalouomega merged commit e2e88f4 into master Jan 16, 2019
@loumalouomega loumalouomega deleted the replace-rapidjson-to-nlohmann-json branch January 16, 2019 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants