Conversation
|
I think this how is done but not sure hopefully it will work: where the round( ,16) has been used to because I am getting the error: and by using round( , 16) is fixed |
jwaldrop107
left a comment
There was a problem hiding this comment.
I know this is still a work in progress, but there are some cleanliness things to deal with.
|
Regarding the suggested change to friendzone/utils/unwrap_inputs.py, you'll need to open a PR in that repository with the recommended changes. |
|
I think I now did the change in the FriendZone repository |
bba231d to
8ced491
Compare
jwaldrop107
left a comment
There was a problem hiding this comment.
I know this is still a work in progress, but the following are some notes on needed changes that I wanted to highlight.
| import pluginplay as pp | ||
| from simde import EnergyNuclearGradientStdVectorD, TotalEnergy, MoleculeFromString | ||
| from berny import Berny, geomlib | ||
| import chemist | ||
| import numpy as np | ||
| import tensorwrapper as tw | ||
|
|
||
|
|
||
| class GeomoptViaPyberny(pp.ModuleBase): | ||
|
|
||
| def __init__(self): | ||
| pp.ModuleBase.__init__(self) | ||
| self.satisfies_property_type(TotalEnergy()) | ||
| self.description("Performs PyBerny optimization") | ||
| self.add_submodule(TotalEnergy(), "Energy") | ||
| self.add_submodule(EnergyNuclearGradientStdVectorD(), "Gradient") | ||
| self.add_submodule(MoleculeFromString(), "StringConv") | ||
|
|
||
| def run_(self, inputs, submods): | ||
| pt = TotalEnergy() | ||
| mol, = pt.unwrap_inputs(inputs) | ||
| molecule = mol.molecule | ||
|
|
||
| # Convert Chemist Chemical System to XYZ | ||
| xyz = "" | ||
| xyz += (str(molecule.size()) + "\n\n") | ||
| for i in range(molecule.size()): | ||
| xyz += (molecule.at(i).name + " " + str(molecule.at(i).x) + " " + | ||
| str(molecule.at(i).y) + " " + str(molecule.at(i).z) + "\n") | ||
|
|
||
| # Loads the geometry string into the Berny optimizer | ||
| # object. | ||
| optimizer = Berny(geomlib.loads(xyz, fmt='xyz')) | ||
|
|
||
| for geom in optimizer: | ||
|
|
||
| # Converts the "Berny" geometry object to Chemical System | ||
| geom2xyz = geom.dumps('xyz') | ||
| print('Berny Geom to XYZ value: \n' + geom2xyz + '\n') | ||
| lines = geom2xyz.split('\n') | ||
| print('Lines of geom2xyz: \n' + str(lines) + '\n') | ||
| mol_string = '\n'.join(lines[2:]) | ||
| print('Lines to string: \n' + mol_string + '\n') | ||
| xyz2chem_mol = submods["StringConv"].run_as( | ||
| MoleculeFromString(), mol_string) | ||
| print('String conversion from xyz to chem sys: \n' + | ||
| str(xyz2chem_mol.nuclei) + '\n') | ||
| geom = chemist.ChemicalSystem(xyz2chem_mol) | ||
| print('Chemical system of xyz2chem_mol: \n' + | ||
| str(geom.molecule.nuclei) + '\n') | ||
| geom_nuclei = geom.molecule.nuclei.as_nuclei() | ||
| geom_points = geom_nuclei.charges.point_set | ||
|
|
||
| # Main optimizer operation | ||
| energy = submods["Energy"].run_as(TotalEnergy(), geom) | ||
| print('Interim energy: \n' + str(energy) + '\n') | ||
| gradients = submods["Gradient"].run_as( | ||
| EnergyNuclearGradientStdVectorD(), geom, | ||
| geom_points.as_point_set()) | ||
| print('Interim gradient: \n' + str(gradients) + '\n') | ||
| optimizer.send((energy, gradients)) | ||
|
|
||
| opt_geom = geom.molecule.nuclei | ||
| print( | ||
| 'Resulting relaxed geometry (assigned to variable opt_geom): \n' + | ||
| str(opt_geom)) | ||
| # Optimized energy is of type "float" | ||
| e = tw.Tensor(energy) | ||
| print(e) | ||
| rv = self.results() | ||
| return pt.wrap_results(rv, e) | ||
|
|
||
|
|
||
| def load_pyberny_modules(mm): | ||
| mm.add_module("PyBerny", GeomoptViaPyberny()) |
There was a problem hiding this comment.
| import pluginplay as pp | |
| from simde import EnergyNuclearGradientStdVectorD, TotalEnergy, MoleculeFromString | |
| from berny import Berny, geomlib | |
| import chemist | |
| import numpy as np | |
| import tensorwrapper as tw | |
| class GeomoptViaPyberny(pp.ModuleBase): | |
| def __init__(self): | |
| pp.ModuleBase.__init__(self) | |
| self.satisfies_property_type(TotalEnergy()) | |
| self.description("Performs PyBerny optimization") | |
| self.add_submodule(TotalEnergy(), "Energy") | |
| self.add_submodule(EnergyNuclearGradientStdVectorD(), "Gradient") | |
| self.add_submodule(MoleculeFromString(), "StringConv") | |
| def run_(self, inputs, submods): | |
| pt = TotalEnergy() | |
| mol, = pt.unwrap_inputs(inputs) | |
| molecule = mol.molecule | |
| # Convert Chemist Chemical System to XYZ | |
| xyz = "" | |
| xyz += (str(molecule.size()) + "\n\n") | |
| for i in range(molecule.size()): | |
| xyz += (molecule.at(i).name + " " + str(molecule.at(i).x) + " " + | |
| str(molecule.at(i).y) + " " + str(molecule.at(i).z) + "\n") | |
| # Loads the geometry string into the Berny optimizer | |
| # object. | |
| optimizer = Berny(geomlib.loads(xyz, fmt='xyz')) | |
| for geom in optimizer: | |
| # Converts the "Berny" geometry object to Chemical System | |
| geom2xyz = geom.dumps('xyz') | |
| print('Berny Geom to XYZ value: \n' + geom2xyz + '\n') | |
| lines = geom2xyz.split('\n') | |
| print('Lines of geom2xyz: \n' + str(lines) + '\n') | |
| mol_string = '\n'.join(lines[2:]) | |
| print('Lines to string: \n' + mol_string + '\n') | |
| xyz2chem_mol = submods["StringConv"].run_as( | |
| MoleculeFromString(), mol_string) | |
| print('String conversion from xyz to chem sys: \n' + | |
| str(xyz2chem_mol.nuclei) + '\n') | |
| geom = chemist.ChemicalSystem(xyz2chem_mol) | |
| print('Chemical system of xyz2chem_mol: \n' + | |
| str(geom.molecule.nuclei) + '\n') | |
| geom_nuclei = geom.molecule.nuclei.as_nuclei() | |
| geom_points = geom_nuclei.charges.point_set | |
| # Main optimizer operation | |
| energy = submods["Energy"].run_as(TotalEnergy(), geom) | |
| print('Interim energy: \n' + str(energy) + '\n') | |
| gradients = submods["Gradient"].run_as( | |
| EnergyNuclearGradientStdVectorD(), geom, | |
| geom_points.as_point_set()) | |
| print('Interim gradient: \n' + str(gradients) + '\n') | |
| optimizer.send((energy, gradients)) | |
| opt_geom = geom.molecule.nuclei | |
| print( | |
| 'Resulting relaxed geometry (assigned to variable opt_geom): \n' + | |
| str(opt_geom)) | |
| # Optimized energy is of type "float" | |
| e = tw.Tensor(energy) | |
| print(e) | |
| rv = self.results() | |
| return pt.wrap_results(rv, e) | |
| def load_pyberny_modules(mm): | |
| mm.add_module("PyBerny", GeomoptViaPyberny()) |
The contents in this file are just copy/paste from elsewhere. Everything but the license header can be deleted.
| @@ -0,0 +1,129 @@ | |||
| #!/usr/bin/env python3 | |||
There was a problem hiding this comment.
I think this whole file can be removed.
| @@ -0,0 +1,137 @@ | |||
| #!/usr/bin/env python3 | |||
There was a problem hiding this comment.
Similar to the other one, I think this whole file can be removed.
| R_xyz[3 * i_atom] = molecule.at(i_atom).x | ||
| R_xyz[3 * i_atom + 1] = molecule.at(i_atom).y | ||
| R_xyz[3 * i_atom + 2] = molecule.at(i_atom).z | ||
| #print(list(R_xyz)) |
There was a problem hiding this comment.
I'm not going to mark all occurrences, but all of these commented out lines from testing/debugging will need to be removed before this is merged.
| def backwardeuler_FIRE(molecule, | ||
| R_xyz, | ||
| h0=0.03, | ||
| alpha=0.1, | ||
| t_max=0.3, | ||
| numbcycles=150, | ||
| error_power=-8, | ||
| time_step_update=5): |
There was a problem hiding this comment.
The contents of this function are really the intended contents of this module, so you don't need to define it as a function here. The parameters of this function (h0, alpha, so on) should be added as input to the module itself.
| if (Rxyz_error < error) and (egy_error | ||
| < error) and (force_error | ||
| < error): |
There was a problem hiding this comment.
| if (Rxyz_error < error) and (egy_error | |
| < error) and (force_error | |
| < error): | |
| coord_conv = (Rxyz_error < error) | |
| energy_conv = (egy_error < error) | |
| force_conv = (force_error < error) | |
| if coord_conv and energy_conv and force_conv : |
Evaluating and storing the booleans makes this easier to read. You can change the names as you see fit, though.
| if k == Ncycle: | ||
| print( | ||
| f"Maximum number of optimization steps achieved: {Ncycle}" | ||
| ) | ||
| break |
There was a problem hiding this comment.
| if k == Ncycle: | |
| print( | |
| f"Maximum number of optimization steps achieved: {Ncycle}" | |
| ) | |
| break |
This break is handled by the loop itself. If you want to record that the max iterations was reached, either track it with a variable or look into else statements for loops in Python.
| Rxyz_error = test['Coordinates error'] | ||
| print(Rxyz_error) | ||
|
|
||
| input('Press enter to continue') |
There was a problem hiding this comment.
| input('Press enter to continue') |
User intervention should be avoided in modules.
| e = tw.Tensor(np.array([0.0])) | ||
| ps = chemist.PointSetD() | ||
| for i in range(numb_atoms): | ||
| ps.push_back( | ||
| chemist.PointD(R_xyz[3 * i], R_xyz[3 * i + 1], | ||
| R_xyz[3 * i + 2])) |
There was a problem hiding this comment.
I know this was written as placeholder for the initial boilerplate, but you should be able to replace the placeholder values now with the actual results.
| print("Energy = " + str(egy)) | ||
| print_pointset(pts) | ||
| #print(self.sys.molecule) | ||
| # print(pts) <-- chemist types missing python string representation |
There was a problem hiding this comment.
Again, remove testing/debug printing when ready to merge.
Is this pull request associated with an issue(s)?
Please list issues associated with this pull request, including closing words
as appropriate.
Description
Describe what this pull request will accomplish.
TODOs
For draft pull requests please include a list of what needs to be done and check
off items as you complete them.