siesta-project / flos

Interfacing SIESTA to Lua scripts using the flook code
https://siesta-project.github.io/flos/index.html
MIT License
6 stars 4 forks source link

Question: Can flos be used to manage the transiesta scf cycle? #10

Closed pfebrer closed 3 years ago

zerothi commented 3 years ago

Yes :)

pfebrer commented 3 years ago

Perfeeeect!

Completely naive question though: I guess the code is not exactly the same, right? I mean if you have a convergence algorithm in lua for the SIESTA scf I can not use it directly without changes for transiesta, right?

zerothi commented 3 years ago

Sure, same script, no changes.

The things that are converged are the same. So you should be able to use the same script. Note however, that the convergence path of siesta vs. transiesta are not the same, so I would highly doubt that they behave the same.

Also, any suggestions/sharing of convergence scripts are highly appreciated ;) I have done some simple things which seems ok, but never anything elaborate which I really think could be useful! :)

pfebrer commented 3 years ago

Great! Could you send me some of your experiments to have them as a reference?

We have problems to converge some interesting systems in transiesta and we want to try to amend it playing with the convergence algorithm :)

zerothi commented 3 years ago

I would recommend you to look at the test lua_si111 it contains a simple way to change between defined mixers, so define the mixers in the fdf, and use Lua to switch between them. :)

Probably something like this is the best.

Alternatively my only (simple) attempts at changing mixing parameters while running is something like this:

-- Create table for holding information
mixing = {iscf=0, dD = {}, dH = {}}
-- define scf_loop function (call in siesta.state == siesta.SCF_LOOP segment)
mixing.scf_loop = function(self, siesta)
   -- Step the iteration count
   local iscf = self.iscf + 1
   self.iscf = iscf

   -- Retrieve the maximum changes
   siesta.receive({'SCF.dD', 'SCF.dH', 'SCF.Mixer.Weight'})

   self.dD[iscf] = siesta.SCF.dD
   self.dH[iscf] = siesta.SCF.dH * siesta.Units.eV

   -- If the relative difference is above 10%, we increase the mixing weight
   -- by 5%, if it is a negative relative difference, we decrease it by 10%
   -- Otherwise, we leave it alone
   if iscf > 1 then
      local d = self[self.variable]
      local r = 1 - (d[iscf-1] - d[iscf]) / d[iscf-1]
      if r > 0.1 then
         siesta.SCF.Mixer.Weight = siesta.SCF.Mixer.Weight * 1.05
      elseif r < 0 then
         siesta.SCF.Mixer.Weight = siesta.SCF.Mixer.Weight * 0.9
      end
      -- Force a maximum of 0.5
      siesta.SCF.Mixer.Weight = m.min(siesta.SCF.Mixer.Weight, 0.5)

      siesta.send({'SCF.Mixer.Weight'})
   end
end

Note the above is an excerpt of a much larger code. But I think it is ok ;)

zerothi commented 3 years ago

I planned on doing some tests by analysing the dD[iscf] but I don't know what to look for, yet ;)

pfebrer commented 3 years ago

Aah ok, so you can change the weights mid scf, but if you wanted to implement a new algorithm you'd have to do it inside siesta?

zerothi commented 3 years ago

Hmm.. Well, you could actually do this in Lua if you want. But this may be rather difficult at the moment since Lua does not per see have MPI and thus the matrices are distributed. And for doing new mixing algorithms you really need reductions with MPI.

There are 2 issues related to this:

  1. You need an MPI backend in Lua, this for instance
  2. Using MPI would arguably force you to require an ffi backend to get MPI working nicely (otherwise you'll have to do lots of small messages since memory is not contiguous (tables in Lua are not contiguous memory). And this is another part of the missing Lua implementation
  3. One could get around this by creating temporary files to retain reduction values, but this is tedious and perhaps also a bit slow.

So while it is quite feasible there is some way to go to make it work.

Which mixing algorithm would you implement?

pfebrer commented 3 years ago

Which mixing algorithm would you implement?

I have no idea, I just want to know what are the possibilities :)

zerothi commented 3 years ago

Ok, there are also not that many other mixing implementations around. So I don't think that is the right path. It is more finetuning parameters. number of history elements, weights etc.