Running CP2K calc with ASE

949 views
Skip to first unread message

Aniruddha Dive

unread,
Jul 27, 2020, 8:37:23 PM7/27/20
to cp2k
Hi, 

I am trying to utilize ASE's "Global optimization - Basin Hopping methods" with CP2K. I am not sure how exactly to incorporate CP2K calculators in the ASE python script. Please find attached the python script I am utilizing currently. I am trying to read coordinates from a xyz file and then run a CP2K geometry optimization. The final optimized structure would then be input to the Basin Hopping algorithm.

However, I am stuck on the first step where I am not able to read the xyz file and perform a geometry optimization. My CP2K shell executable is working correctly.

Thanks,
Aniruddha M Dive
bh_cp2k.py

Maxime Van den Bossche

unread,
Aug 4, 2020, 1:16:11 AM8/4/20
to cp2k
Dear Aniruddha,

Since you haven't provided a minimal example, nor the error message you're getting
from ASE or CP2K, I haven't looked into debugging the (bulky) example you sent.

But I don't think you can do the geometry optimization in this way. ASE uses the cp2k_shell binary,
which seems to only perform single-point calculations but not e.g. geometry optimizations.

The idea behind cp2k_shell is that the 'driver' (in this case ASE) is the one moving the atoms
around, and what CP2K does is e.g. calculating the energy and the gradients for a given geometry.

Here is a minimal example of a geometry optimization, and you can work your way up
from there:

from ase.build import molecule
from ase.calculators.cp2k import CP2K
from ase.optimize import BFGS

atoms
= molecule('H2O')
atoms
.center(vacuum=2.0)

calc
= CP2K(command='cp2k_shell.sopt')
atoms
.set_calculator(calc)

dyn
= BFGS(atoms)
dyn
.run(fmax=0.05)

Best regards,
Maxime

Hasan Al-Mahayni

unread,
Aug 4, 2020, 1:12:17 PM8/4/20
to cp...@googlegroups.com
Hello,

I had similar problems starting CP2K with ASE earlier this summer. I will attach an example of geometric optimization for a slab, I hope it helps. You need to copy paste a large portion of your cp2k input file in your python script, because ASE does not provide all the input parameters you will need to perform geo_opt. Please find an example attached below.

Cheers,

Hasan.

--
You received this message because you are subscribed to the Google Groups "cp2k" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cp2k+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cp2k/df050a2d-1161-4e2c-98c9-0463adcb4c3do%40googlegroups.com.
example.txt

Aniruddha Dive

unread,
Aug 5, 2020, 12:18:11 PM8/5/20
to cp2k
Thanks Maxime and Hasan. I am going to try out the options you suggested.

Best,
Aniruddha M Dive

Aniruddha Dive

unread,
Aug 13, 2020, 4:20:17 PM8/13/20
to cp2k
Thanks Hasan,

I was able to get the Cp2K working. However I face another issue. The simulation runs well for few steps and after that I cannot see my output file being updated. The simulation shows running however I am not able to see any output being updated in the cp2k output file as well as the slurm output file. I have attached my job submission script, python script as well as the out files for reference. Could this be a memory issue? 

Best,
Aniruddha M Dive


On Tuesday, August 4, 2020 at 10:12:17 AM UTC-7, Hasan Al-Mahayni wrote:
Hello,

I had similar problems starting CP2K with ASE earlier this summer. I will attach an example of geometric optimization for a slab, I hope it helps. You need to copy paste a large portion of your cp2k input file in your python script, because ASE does not provide all the input parameters you will need to perform geo_opt. Please find an example attached below.

Cheers,

Hasan.

On Tue, Aug 4, 2020 at 1:16 AM Maxime Van den Bossche <maxime.cp....@gmail.com> wrote:
Dear Aniruddha,

Since you haven't provided a minimal example, nor the error message you're getting
from ASE or CP2K, I haven't looked into debugging the (bulky) example you sent.

But I don't think you can do the geometry optimization in this way. ASE uses the cp2k_shell binary,
which seems to only perform single-point calculations but not e.g. geometry optimizations.

The idea behind cp2k_shell is that the 'driver' (in this case ASE) is the one moving the atoms
around, and what CP2K does is e.g. calculating the energy and the gradients for a given geometry.

Here is a minimal example of a geometry optimization, and you can work your way up
from there:

from ase.build import molecule
from ase.calculators.cp2k import CP2K
from ase.optimize import BFGS

atoms
= molecule('H2O')
atoms
.center(vacuum=2.0)

calc
= CP2K(command='cp2k_shell.sopt')
atoms
.set_calculator(calc)

dyn
= BFGS(atoms)
dyn
.run(fmax=0.05)

Best regards,
Maxime

--
You received this message because you are subscribed to the Google Groups "cp2k" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cp...@googlegroups.com.
LLZO_base.out
basin_hop.sh
bh_cp2k.py
slurm-5606428.out

Sun Geng

unread,
Aug 17, 2020, 10:44:08 AM8/17/20
to cp2k
Dear Aniruddha,

Sometimes ago, I have encountered a similar problem,
I think the reason is that the python code communicates positions/forces with the mpi cp2k_shell.popt.
There is a problem when MPI code read the pipe: the MPI code will only read truncated data  (such as positions) from the pipe, not all of them.
So CP2K_shell.popt is still waiting for more coordinates and gets stuck.

Best,
Geng

Hasan Al-Mahayni

unread,
Aug 17, 2020, 12:56:01 PM8/17/20
to cp...@googlegroups.com
Hello,

I had a similar problem, however I don't quite understand the submission file as we are in different clusters. For me, i fixed this problem by using cpus-per-task instead of tasks-per-nodes.


Hope this helps,

Hasan Al-Mahayni


To unsubscribe from this group and stop receiving emails from it, send an email to cp2k+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cp2k/84755b1c-38d0-4189-98de-1575732071eco%40googlegroups.com.

Aniruddha Dive

unread,
Aug 17, 2020, 7:42:29 PM8/17/20
to cp2k
Thanks Geng,

I went through your earlier post regarding the same issue. Can you send me the respective cp2k_shell.F file that works for you. I would like to incorporate it and check.

Also can you send me the python script you used as well.

Best Regards,
Aniruddha M Dive

Aniruddha Dive

unread,
Aug 17, 2020, 7:43:50 PM8/17/20
to cp2k
Thanks Hasan,

I tried using cpus-per-task but I am still getting same results. Would you mind sharing your job submission file?

Best Regards,
Aniruddha M Dive


On Monday, August 17, 2020 at 9:56:01 AM UTC-7, Hasan Al-Mahayni wrote:
Hello,

I had a similar problem, however I don't quite understand the submission file as we are in different clusters. For me, i fixed this problem by using cpus-per-task instead of tasks-per-nodes.


Hope this helps,

Hasan Al-Mahayni

Maxime Van den Bossche

unread,
Aug 18, 2020, 11:22:56 AM8/18/20
to cp2k
Dear Aniruddha,

Did you compile/run your cp2k_shell executable with IntelMPI or OpenMPI?
A few years back, I had a similar problem as you (hanging after a certain time),
but only with IntelMPI, whereas OpenMPI was fine (unfortunately I don't
remember the versions).

Best,
Maxime

Aniruddha Dive

unread,
Aug 18, 2020, 11:45:09 AM8/18/20
to cp2k
Hi Maxime,

I have compiled it with IntelMPI. I am working on compiling another version with OpenMPI just to compare if the problem can be resolved.

Thanks,
Aniruddha M Dive

Sun Geng

unread,
Aug 18, 2020, 12:05:44 PM8/18/20
to cp2k
Hi Aniruddha M Dive,
Below is the code that I changed a few lines in cp2k_shell.F and ase-cp2k calculator. ( You will see the lines I commented out)
Please note that the code is only tested wit cp2k-v6.1 (I found the most recent cp2k code revised cp2k_shell.F significantly).
The idea is that the python code will write a file " CP2K_POSITIONS" with coordinates instead of writing them into PIPE,
and cp2k_shell will read the positions from the file instead of the PIPE.
Best,
Geng


        IF (para_env%mepos==para_env%source) THEN
           !READ (*,*,iostat=iostat) n_atom2
           !IF (iostat/=0) CPABORT('setpos read n_atom')
           !IF (n_atom2/=SIZE(pos)) THEN
           !   CALL my_assert(.FALSE.,'setpos invalid number of atoms',failure)
           !   DO i=1,n_atom
           !      READ(*,'(a)',iostat=iostat) cmdStr
           !      CALL compress(cmdStr,full=.TRUE.)
           !      CALL uppercase(cmdStr)
           !      IF (cmdStr=='*END') EXIT
           !   END DO
           !   GOTO 10
           !END IF
           !READ (*,*,iostat=iostat) pos
           !IF (iostat/=0) CPABORT('setpos read coord')
           inquire(unit=201,opened=unitalive)
           if (unitalive) CPABORT('UNIT 201 is being used')
           inquire(file="CP2K_POSITIONS",exist=filepresence)
           if (.not. filepresence) CPABORT('FILE CP2K_POSITIONS NOT EXIST')
           open(201,action='READ',file="CP2K_POSITIONS",iostat=iostat,status='OLD',form='FORMATTED',ACCESS='SEQUENTIAL')
           if (iostat/=0) CPABORT('read CP2K_POSITIONS')
           READ(201,*,iostat=iostat) n_atom2
           IF (iostat/=0) CPABORT('setpos read n_atom2')
           IF (n_atom2/=SIZE(pos)) THEN
              CALL my_assert(.FALSE.,'setpos invalid number of atoms',failure)
              DO i=1,n_atom
                 READ(201,'(a)',iostat=iostat) cmdStr
                 CALL compress(cmdStr,full=.TRUE.)
                 CALL uppercase(cmdStr)
                 IF (cmdStr=='*END') EXIT
              END DO
              GOTO 10
           END IF
           READ (201,*,iostat=iostat) pos
           CLOSE(201)
           IF (iostat/=0) CPABORT('setpos read coord')
           pos(:) = pos(:)/pos_fact
           READ(*,'(a)',iostat=iostat) cmdStr
           CALL compress(cmdStr,full=.TRUE.)
           CALL uppercase(cmdStr)
           CALL my_assert(cmdStr=='*END',' missing *END',failure)
        END IF


---changes in ase cp2k calcualtor -----
        if 'positions' in system_changes:
            with open("CP2K_POSITIONS","w") as fp:
                fp.write("%d\n" % (3*n_atoms))
                for pos in self.atoms.get_positions():
                    fp.write('%.18e %.18e %.18e\n' % tuple(pos))
            self._shell.send('SET_POS %d' % self._force_env_id)
            #self._shell.send('%d' % (3 * n_atoms))
            #for pos in self.atoms.get_positions():
            #    self._shell.send('%.18e %.18e %.18e' % tuple(pos))
            self._shell.send('*END')
            max_change = float(self._shell.recv())
            assert max_change >= 0 # sanity check
            self._shell.expect('* READY')
            if os.path.isfile("CP2K_POSITIONS"):
                os.remove("CP2K_POSITIONS")

Aniruddha Dive

unread,
Aug 19, 2020, 5:33:38 PM8/19/20
to cp2k
Thanks a lot!! 

Kane Shenton

unread,
Jan 14, 2021, 10:56:45 AM1/14/21
to cp2k
Hi!

I've just run into the same issue. Thanks, Geng, for shedding some light and providing the hack to fix it! 

It's unclear to me how to adapt your fortran code to newer versions of cp2k since, as you say, there have been lots of changes.

Have you, by any chance, subsequently written a similar hack for newer versions of cp2k (e.g. v8.1)?

Best,
Kane


Sun Geng

unread,
Jan 14, 2021, 2:07:12 PM1/14/21
to cp...@googlegroups.com
Hi Kane,
Sorry, I did not check it out for cp2k-v8.1.
I have been mostly using CP2K-v6.1 with ase.
Maybe someone who is more familiar with code knows which subroutine
does the job reading positions from standard input,
then It will be easier to adopt the code.

Best,
Geng

Kane Shenton <jksh...@gmail.com> 于2021年1月14日周四 上午7:56写道:
> To unsubscribe from this group and stop receiving emails from it, send an email to cp2k+uns...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/cp2k/7d31cb08-bfbd-4a88-a0d3-8990d132c7d4n%40googlegroups.com.

Kane Shenton

unread,
Jan 15, 2021, 8:34:01 AM1/15/21
to cp2k
OK cool, thanks anyway! For now I'll see if I can use OpenMPI instead... 

Cheers,
Kane

Reply all
Reply to author
Forward
0 new messages