SSCHAcode / python-sscha

The python implementation of the Stochastic Self-Consistent Harmonic Approximation (SSCHA).
GNU General Public License v3.0
55 stars 21 forks source link

Error running SSCHA with symmetries #119

Closed FlemkMeserath closed 1 year ago

FlemkMeserath commented 1 year ago

Dear developers,

I was running the SSCHA with python3.9 for one of my systems following the example for PbTe. Whenever executing the command minimizer.run() i get the following warning at the first step of the minimization:

/home/fbelli/.conda/envs/SSCHA/lib/python3.9/site-packages/cellconstructor/symmetries.py:1415: ComplexWarning: Casting complex values to real discards the imaginary part
  dyn_matrix[3 * na : 3* na + 3, 3*nb: 3 * nb + 3] = Methods.convert_matrix_cart_cryst(fc, self.structure.unit_cell, True)
/home/fbelli/.conda/envs/SSCHA/lib/python3.9/site-packages/cellconstructor/symmetries.py:776: ComplexWarning: Casting complex values to real discards the imaginary part
  fcq[:,:,:] = final_fc

After this warning the minimization ends. I have noticed that if I change the flag minimizer.neglect_symmetries to True, the warning disappears and everything continues as normal. But then the minimization is performed without symmetries.

Is there a way to fix this?

mesonepigreco commented 1 year ago

The warning is normal, it occurs because it convert the dynamical matrix at Gamma (which is defined as a real number) from the results it gets from the quantum-espresso FORTRAN symmetry subroutine (which works with complex values).

Does the minimization crash with some other error? Is it the last version of the code?

FlemkMeserath commented 1 year ago

I forgot to mention that as soon as the minimization is interrupted I get the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[3], line 27
     17 #io_func = sscha.Utilities.IOInfo()
     18 #io_func.SetupSaving("frequencies.dat", save_each_step=True) # The file that will contain the frequencies is frequencies.dat
     19 
   (...)
     24 #    sys.stdout = f # Change the standard output to the file we created.
     25     # Lest start the minimization 
     26 minimizer.init()
---> 27 minimizer.run()
     28 minimizer.finalize()
     29 #    sys.stdout = original_stdout # Reset the standard output to its original value

File ~/.conda/envs/SSCHA/lib/python3.9/site-packages/sscha/SchaMinimizer.py:1163, in SSCHA_Minimizer.run(self, verbose, custom_function_pre, custom_function_post, custom_function_gradient)
   1161 # Perform the minimization step
   1162 t1 = time.time()
-> 1163 self.minimization_step(custom_function_gradient)
   1164 t2 = time.time()
   1165 if verbose >=1:

File ~/.conda/envs/SSCHA/lib/python3.9/site-packages/sscha/SchaMinimizer.py:383, in SSCHA_Minimizer.minimization_step(self, custom_function_gradient)
    381     CC.symmetries.CustomASR(dyn_grad[0,:,:])
    382 else:
--> 383     qe_sym.SymmetrizeFCQ(dyn_grad, np.array(self.dyn.q_stars), asr = "custom")
    384     #qe_sym.SymmetrizeFCQ(err, np.array(self.dyn.q_stars), asr = "custom")
    385 
    386 # Just divide the error by the square root the number of symmetries
    387 err /= np.sqrt(qe_sym.QE_nsym * np.prod(self.ensemble.supercell))

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (3,) + inhomogeneous part.
FlemkMeserath commented 1 year ago

I have downloaded the version I'm using from the following two links: github.com/SSCHAcode/CellConstructor](https://github.com/SSCHAcode/CellConstructor github.com/SSCHAcode/python-sscha](https://github.com/SSCHAcode/python-sscha

However I have noticed that if instead try to install the sscha through pip I get a different version. So I am not entirely sure of which version I am using.

FlemkMeserath commented 1 year ago

I have checked the versions and I am using the CellConstructor 1.2.2 and the SSCHA 1.2.1.

Sorry for the flurry of messages.

diegomartinez2 commented 1 year ago

It looks like you are trying to fill a sequence of numbers into a single number slot in q_stars. Maybe your dimensions are wrong defined? (check if the array you have have different length or sequences). I will known more if I see your code for the minimization. You say you use the example for PbTe. Check if you are using a list instead of an array somewhere.

mesonepigreco commented 1 year ago

I think this error has been fixed in one of the latest branch (like #111 ). However, the branch cannot yet be deployed due to other bugs in the parallelized version of the gradient calculation. Maybe I can cherry-pick the commit with the fix. This needs to be fixed as soon as possible so I'm now pinning the issue.

mesonepigreco commented 1 year ago

This is the commit with the fix: https://github.com/SSCHAcode/python-sscha/pull/118/commits/579ab81630ab4ab79812dcd0c90cba89069b7716

579ab81630ab4ab79812dcd0c90cba89069b7716

mesonepigreco commented 1 year ago

It cannot be cherry picked as it uses inside the commit the new timing syntax of the #111 branch. I have the feeling that this error is caused by an update of numpy, anyway, it is a bug of the code and it needs to be fixed as soon as possible.

diegomartinez2 commented 1 year ago

Lorenzo, in 'ShaMinimizer.py' of main there is this discrepancy in the code: line 383 --> qe_sym.SymmetrizeFCQ(dyn_grad, np.array(self.dyn.q_stars), asr = "custom") line 982 --> qe_sym.SymmetrizeFCQ(fcq, self.dyn.q_stars, asr = "custom")

mesonepigreco commented 1 year ago

Yes, the correct one is WITHOUT the np.array (as you can see from the commit

579ab81

mesonepigreco commented 1 year ago

I committed the fix, let us wait for the testsuite to see if it works

mesonepigreco commented 1 year ago

Ok, the testsuite passed https://github.com/SSCHAcode/python-sscha/actions/runs/4251737035 Therefore the bug has been fixed. @FlemkMeserath You can install the latest version in the master branch and it should fix the problem