[ase-users] Modification in optimizer.py

Ask Hjorth Larsen asklarsen at gmail.com
Sat Jan 16 22:24:04 CET 2021


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>:

> 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
>
>
> On 15 Jan 2021, at 18:30, Gaël Donval via ase-users <
> 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
>
>
>
>
> The ASE source code is available (for download AND contributions) on
> gitlab.com:
>
>   https://gitlab.com/ase/ase
>
> All the information necessary to contribute code is given there:
>
>   https://wiki.fysik.dtu.dk/ase/development/contribute.html
>
> 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 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
> https://listserv.fysik.dtu.dk/mailman/listinfo/ase-users
>
>
> _______________________________________________
> ase-users mailing list
> ase-users at listserv.fysik.dtu.dk
> https://listserv.fysik.dtu.dk/mailman/listinfo/ase-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listserv.fysik.dtu.dk/pipermail/ase-users/attachments/20210116/a20ff88a/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/20210116/a20ff88a/attachment-0001.jpg>


More information about the ase-users mailing list