[ase-users] Modification in optimizer.py

Offermans Willem willem.offermans at vito.be
Tue Jan 26 13:59:21 CET 2021


Dear Ask and ASE friends,

The code below seems to work:

<snip>

from ase.optimize.optimize import Optimizer as OptimizerOriginal

class Optimizer(OptimizerOriginal):
    def log(self, forces=None):
        if forces is None:
            forces = self.atoms.get_forces()
        fmax = sqrt((forces ** 2).sum(axis=1).max())
        e = self.atoms.get_potential_energy(
            force_consistent=self.force_consistent
        )
        T = time.localtime()
        print('This is MyOptimizer method')
        if self.logfile is not None:
            name = self.__class__.__name__
            if self.nsteps == 0:
                args = (" " * len(name), "Step", "Time", "Energy", "fmax")
                msg = "%s  %4s %8s %15s %12s\n" % args
                self.logfile.write(msg)

                if self.force_consistent:
                    msg = "*Force-consistent energies used in optimization.\n"
                    self.logfile.write(msg)

            ast = {1: "*", 0: ""}[self.force_consistent]
            args = (name, self.nsteps, T[2], T[3], T[4], T[5], e, ast, fmax)
            msg = "%s:  %3d %02d-%02d:%02d:%02d %15.6f%1s %12.4f\n" % args
            self.logfile.write(msg)

            self.logfile.flush()

</snip>

I guess the above snippet is what you had in mind when you wrote: “ … to subclass the optimizer …”.

Thank you for your hint.


Met vriendelijke groeten,
Mit freundlichen Grüßen,
With kind regards,


Willem Offermans
Researcher Electrocatalysis SCT
VITO NV | Boeretang 200 | 2400 Mol
Phone:+32(0)14335263 Mobile:+32(0)492182073 

Willem.Offermans at Vito.be



> On 16 Jan 2021, at 22:24, Ask Hjorth Larsen <asklarsen at gmail.com> wrote:
> 
> Hi Willem,
> 
> For hacking the logging, the first way that comes to mind is to subclass the optimizer in question and override its log() method.
> 
> Indeed it's also possible to monkeypatch it as well as in your example.  Note there's probably a difference between monkeypatching it onto the class itself versus an instance of the class.  To do it correctly, be sure to play around with a *clean* class that you write from scratch, so things are simple.
> 
> Best regards
> Ask
> 
> Am Sa., 16. Jan. 2021 um 15:35 Uhr schrieb Offermans Willem via ase-users <ase-users at listserv.fysik.dtu.dk <mailto:ase-users at listserv.fysik.dtu.dk>>:
> Hi Gaël and ASE friends,
> 
> You misunderstood my request. Probably I was not clear enough about what I want to achieve.
> 
> My changes to the ASE code will only be minor and very personally. At the moment I don’t have
> anything meaningful to contribute. 
> 
> I’m not completely happy with the logging info. Next to time, I want to have info about the day.
> Probably, I will be the only one in the whole ASE community to value this.
> 
> To not to disturb the ASE release code, I want to overwrite some methods from some classes in
> my python calculation script. An example was given in my previous mail. The question is simple:
> How to this in a way that it works? The example I gave did not work.
> 
> 
> 
> Met vriendelijke groeten,
> Mit freundlichen Grüßen,
> With kind regards,
> 
> 
> Willem Offermans
> Researcher Electrocatalysis SCT
> VITO NV | Boeretang 200 | 2400 Mol
> Phone:+32(0)14335263 Mobile:+32(0)492182073 
> 
> Willem.Offermans at Vito.be <mailto:Willem.Offermans at Vito.be>
> <vito.jpg>
> 
>> On 15 Jan 2021, at 18:30, Gaël Donval via ase-users <ase-users at listserv.fysik.dtu.dk <mailto:ase-users at listserv.fysik.dtu.dk>> wrote:
>> 
>> Hi Willem,
>> 
>> Answer below.
>> 
>> 
>>> 
>>> Dear ASE friends,
>>> 
>>> I would like to make a small change in the logging of the optimize.py
>>> procedure.
>>> 
>>> It is the following file in my case:
>>> 
>>> ~/.conda/envs/ase/lib/python3.9/site-packages/ase/optimize/optimize.py 
>>> 
>>> Under the ``class Optimizer(Dynamics):``, there is a method called log:
>>> 
>>> <snip>
>>>     def log(self, forces=None):
>>>         if forces is None:
>>>             forces = self.atoms.get_forces()
>>>         fmax = sqrt((forces ** 2).sum(axis=1).max())
>>>         e = self.atoms.get_potential_energy(
>>>             force_consistent=self.force_consistent
>>>         )
>>>         T = time.localtime()
>>>         if self.logfile is not None:
>>>             name = self.__class__.__name__
>>>             if self.nsteps == 0:
>>>                 args = (" " * len(name), "Step", "Time", "Energy", "fmax")
>>>                 msg = "%s  %4s %8s %15s %12s\n" % args
>>>                 self.logfile.write(msg)
>>> 
>>>                 if self.force_consistent:
>>>                     msg = "*Force-consistent energies used in optimization.\n"
>>>                     self.logfile.write(msg)
>>> 
>>>             ast = {1: "*", 0: ""}[self.force_consistent]
>>> #            args = (name, self.nsteps, T[3], T[4], T[5], e, ast, fmax)
>>>             args = (name, self.nsteps, T[2], T[3], T[4], T[5], e, ast, fmax)
>>>             print('This is Optimizer.log and T[2] = ' + str(T[2]))
>>> #            msg = "%s:  %3d %02d:%02d:%02d %15.6f%1s %12.4f\n" % args
>>>             msg = "%s:  %3d %02d-%02d:%02d:%02d %15.6f%1s %12.4f\n" % args
>>>             self.logfile.write(msg)
>>> 
>>>             self.logfile.flush()
>>> </snip>
>>> 
>>> I already commented the original code lines and added the updated lines.
>>> 
>>> I do realise that it is not handy to do this change in the original code.
>>> 
>>> I would like to rewrite the code in my personal python calc script, by
>>> defining a new 
>>> method that will overwrite the corresponding method. So I added the following
>>> code
>>> in my python calc script:
>>> 
>>> 
>>> <snip>
>>> def MyLog(self, forces=None):
>>>     print('This is MyLog')
>>>     if forces is None:
>>>         forces = self.atoms.get_forces()
>>>     fmax = sqrt((forces ** 2).sum(axis=1).max())
>>>     e = self.atoms.get_potential_energy(
>>>         force_consistent=self.force_consistent
>>>     )
>>>     T = time.localtime()
>>>     if self.logfile is not None:
>>>         name = self.__class__.__name__
>>>         if self.nsteps == 0:
>>>             args = (" " * len(name), "Step", "Time", "Energy", "fmax")
>>>             msg = "%s  %4s %8s %15s %12s\n" % args
>>>             self.logfile.write(msg)
>>> 
>>>             if self.force_consistent:
>>>                 msg = "*Force-consistent energies used in optimization.\n"
>>>                 self.logfile.write(msg)
>>> 
>>>         ast = {1: "*", 0: ""}[self.force_consistent]
>>>         args = (name, self.nsteps, T[2], T[3], T[4], T[5], e, ast, fmax)
>>>         msg = "%s:  %3d %02d-%02d:%02d:%02d %15.6f%1s %12.4f\n" % args
>>>         self.logfile.write(msg)
>>> 
>>>         self.logfile.flush()
>>> 
>>> 
>>> Optimizer(Dynamics).log = MyLog
>>> 
>>> </snip>
>>> 
>>> The changes are trivial, but important to me. Furthermore, I would like to
>>> make some other small changes as well, but this is for later.
>>> 
>>> However, the applied correction doesn’t seem to work and I’m puzzled on the
>>> why.
>>> 
>>> Can someone help me out?
>>> 
>>> 
>>> Met vriendelijke groeten,
>>> Mit freundlichen Grüßen,
>>> With kind regards,
>>> 
>>> 
>>> Willem Offermans
>>> Researcher Electrocatalysis SCT
>>> VITO NV | Boeretang 200 | 2400 Mol
>>> Phone:+32(0)14335263 Mobile:+32(0)492182073 
>>> 
>>> Willem.Offermans at Vito.be <mailto:Willem.Offermans at Vito.be>
>>> 
>>> 
>> 
>> 
>> The ASE source code is available (for download AND contributions) on gitlab.com <https://eur02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fgitlab.com%2F&data=04%7C01%7C%7C07f495f884a04ef4b37e08d8ba651486%7C9e2777ed82374ab992782c144d6f6da3%7C0%7C0%7C637464290607210002%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=2Bje%2FyalBqvTbqToNl%2FDxszyHOgAZ9MwLDMXDKCcvhg%3D&reserved=0>:
>> 
>>   https://gitlab.com/ase/ase <https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitlab.com%2Fase%2Fase&data=04%7C01%7C%7C07f495f884a04ef4b37e08d8ba651486%7C9e2777ed82374ab992782c144d6f6da3%7C0%7C0%7C637464290607210002%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=09cEvjfMpwmOmKSPehv0nWDmj4sL8CWEiiH5kskfjt8%3D&reserved=0>
>> 
>> All the information necessary to contribute code is given there:
>> 
>>   https://wiki.fysik.dtu.dk/ase/development/contribute.html <https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwiki.fysik.dtu.dk%2Fase%2Fdevelopment%2Fcontribute.html&data=04%7C01%7C%7C07f495f884a04ef4b37e08d8ba651486%7C9e2777ed82374ab992782c144d6f6da3%7C0%7C0%7C637464290607219997%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=hkLm8NrYBkKXiBVx1bjYEh6NkgzpNDv8Gug60tBnzG4%3D&reserved=0>
>> 
>> That being said, the way the information is laid out is a bit dry...
>> 
>> 
>> The gist of it is that you need to open a gitlab account. It is free. 
>> 
>> Once there you can use git directly as it is customary with gitlab. Assuming you
>> are familiar with git, the only important bit is to remember to create a new
>> branch before changing anything. If you are familiar with github or other
>> platforms, you'll have to submit your changes as a pull/merge request. The link
>> I provided is much more thorough.
>> 
>> ALTERNATIVELY, if you are not so familiar with git, and for smaller changes like
>> this, open a gitlab account. Go to https://gitlab.com/ase/ase <https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitlab.com%2Fase%2Fase&data=04%7C01%7C%7C07f495f884a04ef4b37e08d8ba651486%7C9e2777ed82374ab992782c144d6f6da3%7C0%7C0%7C637464290607219997%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=HBC9sQd%2BhKMcofzJAU2GizCNKfPmG1M9pjLBfJlhyDs%3D&reserved=0> and find the file
>> you want to edit. At the top right corner of the source code, there is a Web IDE
>> button. Click on that and edit what you need. Once happy with the result, on the
>> bottom left of the page there will be a "commit" button. Click on that. Provide
>> a good description of the change (and why), select "create a new branch", give a
>> meaningful name to the branch and commit. This will automatically open a merge
>> request that we can more easily review.
>> 
>> Regards,
>> Gaël
>> 
>> _______________________________________________
>> ase-users mailing list
>> ase-users at listserv.fysik.dtu.dk <mailto:ase-users at listserv.fysik.dtu.dk>
>> https://listserv.fysik.dtu.dk/mailman/listinfo/ase-users <https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Flistserv.fysik.dtu.dk%2Fmailman%2Flistinfo%2Fase-users&data=04%7C01%7C%7C07f495f884a04ef4b37e08d8ba651486%7C9e2777ed82374ab992782c144d6f6da3%7C0%7C0%7C637464290607229996%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=hgwODGyvdYARvrSjnQZkoXri36Nz%2FeaiGbu0FzfKojM%3D&reserved=0>
> _______________________________________________
> ase-users mailing list
> ase-users at listserv.fysik.dtu.dk <mailto:ase-users at listserv.fysik.dtu.dk>
> https://listserv.fysik.dtu.dk/mailman/listinfo/ase-users <https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Flistserv.fysik.dtu.dk%2Fmailman%2Flistinfo%2Fase-users&data=04%7C01%7C%7C07f495f884a04ef4b37e08d8ba651486%7C9e2777ed82374ab992782c144d6f6da3%7C0%7C0%7C637464290607229996%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=hgwODGyvdYARvrSjnQZkoXri36Nz%2FeaiGbu0FzfKojM%3D&reserved=0>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listserv.fysik.dtu.dk/pipermail/ase-users/attachments/20210126/0dc074b1/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: vito.jpg
Type: image/jpeg
Size: 15232 bytes
Desc: not available
URL: <http://listserv.fysik.dtu.dk/pipermail/ase-users/attachments/20210126/0dc074b1/attachment-0001.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3953 bytes
Desc: not available
URL: <http://listserv.fysik.dtu.dk/pipermail/ase-users/attachments/20210126/0dc074b1/attachment-0001.p7s>


More information about the ase-users mailing list