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

Loss of precision with += #101

Open
ilfreddy opened this issue Jun 22, 2023 · 11 comments
Open

Loss of precision with += #101

ilfreddy opened this issue Jun 22, 2023 · 11 comments
Assignees

Comments

@ilfreddy
Copy link
Contributor

ilfreddy commented Jun 22, 2023

It looks like the implementation of the += operator for function_trees leads to precision loss.

The following code (taken from ReMRChem) gives two very different results for rho and n which ideally should be identical.

    def setup(self):
        rho = vp.FunctionTree(self.mra)
        rho.setZero()
        rholist = []
        for i in range(0, len(self.Psi)):
            dens = self.Psi[i].overlap_density(self.Psi[i], self.prec)
            print("density i = ", i)
            print(dens)
            rho += dens.real
            rholist.append(dens.real)
        #rho.crop(self.prec)
        n = rholist[0] + rholist[1]
        self.potential = (4.0*np.pi) * self.poisson(n).crop(self.prec)

Commit of ReMRChem showing the issue: 2b6052d94ea4f68715f840b62f9cb7

@ilfreddy
Copy link
Contributor Author

ilfreddy commented Jun 22, 2023

Suggestions:

  1. reimplement __iadd__ to mirror __add__
  2. keep __add__ written with pybind11, remove __iadd__ from the pybind11 binding set, re-implement it in pure Python to use __add__ and then dynamically attach it to the FunctionTree class.

@robertodr
Copy link
Contributor

Number 2 is incorrect: you want to keep __add__ written with pybind11, remove __iadd__ from the pybind11 binding set, re-implement it in pure Python to use __add__ and then dynamically attach it to the FunctionTree class.

@ilfreddy
Copy link
Contributor Author

Updated your suggestion above

@bjorgve
Copy link
Member

bjorgve commented Jun 22, 2023

Hmm, I like the dynamically add solution. I guess we need to do something like this then:
https://pybind11.readthedocs.io/en/stable/classes.html?highlight=dynamically%20add#dynamic-attributes

Where would you suggest putting the python code @robertodr ?

A third alternative would be to write:

while (refine_grid(*out, *inp)); as Stig mentions in Zulip. That would be correcting the current implementation.

@robertodr
Copy link
Contributor

It's true that Stig's suggestion (probably) solves the issue, though using two different implementations for the same things it's not good.1

As to how to implement dynamic attributes in practice. You'd have to add a Python file under src/vampyr, say _function_tree.py. The drawback of this approach is that you need to repeat the same code for D=1,2,3 🤦🏻

Footnotes

  1. And I realize that I should have spotted this earlier during code review!

@bjorgve
Copy link
Member

bjorgve commented Jun 25, 2023

@robertodr can you write an example of how to use it for a method in f_tree method in 1D?

I can do it for the other methods and dims once I have the blueprint

I tried to do it with a "hello_world" kind of example, but didn't get it to work properly.

@robertodr
Copy link
Contributor

On vacation until 5th July 🥥🌴

@robertodr
Copy link
Contributor

There is a fix for addition on master now. All other overloaded operators should be double-checked (and in case re-implemented) before closing this issue.

@robertodr
Copy link
Contributor

@ilfreddy can you check if this is still an issue?

@robertodr
Copy link
Contributor

@ilfreddy can you check if this is still an issue? If it isn't, please close.

@ilfreddy
Copy link
Contributor Author

ilfreddy commented Dec 7, 2023

I can try, but then I need to compile from scratch. Not sure I'm able to 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants