Code Monkey home page Code Monkey logo

Comments (5)

mattwthompson avatar mattwthompson commented on June 25, 2024

Thanks for finding this - I can reproduce it and it's totally valid

That part of the JSON blob is parsed here, where something is going awry when re-instantiating the object.

It's not the raw JSON parsing, which finds the right value, but something in .parse_raw is not iterating over the keys and mapping them onto fields like I'd expect

In [17]: json.loads(i["Electrostatics"].json())["scale_14"]
Out[17]: 0.8333333333

In [18]: SMIRNOFFElectrostaticsCollection.parse_raw(i["Electrostatics"].json())
Out[18]: Handler 'Electrostatics' with expression 'coul', 11 mapping keys, and 11 potentials

In [19]: SMIRNOFFElectrostaticsCollection.parse_raw(i["Electrostatics"].json()).scale_14
Out[19]: 0.5

Interchange has a custom loader, but this particular collection does not. It falls back to this function, which ... maybe isn't parsing numbers at all? I think this line is indented over one too far

The quickest fix for this behavior would be to set the default value to the traditional value of round(5 / 6, 10) - notice how the class definition doesn't override the default provided by its base class(es).

In [25]: SMIRNOFFElectrostaticsCollection().scale_14
Out[25]: 0.5

Obviously a more robust fix would go onto the base class's JSON parsing function. To be honest, the JSON roundtripping of Interchanges (and their constituent components) is neither well-tested nor broadly-used yet, so there are probably more than a few of these bugs out there. One day I'll add a bunch of tests that just round-trip everything with JSON from a variety of different states ....

Anyway, happy to review PRs for any combination of these ideas if you wanted to take a stab at them and if my suggestions are coherent. You're under no obligation to, of course, as this is all behavior that a user should expect to work.

from openff-interchange.

mattwthompson avatar mattwthompson commented on June 25, 2024

Here's the sort of test I would like added en masse one day1: using only slight extensions of your snippet:

from openff.toolkit import Molecule, ForceField, Quantity
from openff.interchange import Interchange
from openff.interchange.drivers import get_gromacs_energies

def test_issue_908():
    m = Molecule.from_smiles("CCC")
    m.generate_conformers()
    t = m.to_topology()
    t.box_vectors = Quantity([4, 4, 4], "nanometer")
    
    ff = ForceField("openff-2.1.0.offxml")
    
    i = ff.create_interchange(t)
    
    with open("test.json", "w") as f:
        f.write(i.json())
    
    j = Interchange.parse_file("test.json")
    
    get_gromacs_energies(i).compare(get_gromacs_energies(j))
    # FAILED test.py::test_issue_908 - openff.interchange.exceptions.EnergyError: {'Electrostatics': <Quantity(-0.29432869, 'kilojoule / mole')>}

I think there are some basic tests for JSON round-tripping, just not one that looks at this particular comparison

Footnotes

  1. Just with a lot of different chemistries, force fields, engines, serialization formats, periodicity on or off, virtual sites included or excluded, the list goes on ...

from openff-interchange.

mattwthompson avatar mattwthompson commented on June 25, 2024

@pbuslaev try the main branch now - not sure when the next release will be

from openff-interchange.

pbuslaev avatar pbuslaev commented on June 25, 2024

@mattwthompson Thanks a lot! Looks super cool.

Related question, I would like to use Interchange combination functionality. Do you want me to add test cases for it?

from openff-interchange.

mattwthompson avatar mattwthompson commented on June 25, 2024

Absolutely!

The only detail you should be aware of is that I'm planning on Interchange.__add__ going away and it being replaced by some sort of Interchange.combine. The business logic should be similar, but having it be a concrete method allows for arguments to be passed in and for the possibility that something could be returned (i.e. some sort of atom mapping between input and final states).

from openff-interchange.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.