1!
2! Copyright (C) 1996-2016	The SIESTA group
3!  This file is distributed under the terms of the
4!  GNU General Public License: see COPYING in the top directory
5!  or http://www.gnu.org/copyleft/gpl.txt.
6! See Docs/Contributors.txt for a list of contributors.
7!
8      subroutine iocg( task, naux, cgaux, cgcntr, relaxd, found )
9
10c**************************************************************************
11c Reads/writes auxuliary arrays for conj. grag. continuation
12c Written by E. Artacho. January 1999.
13c**************** INPUT ***************************************************
14c character task*3 : 'read' or 'write'
15c integer   naux   : dimension of cgaux
16c**************** INPUT OR OUTPUT (depending on task) *********************
17c real*8    cgaux(naux)   : auxiliary array for CG
18c real*8    cgcntr(0:20)  : same
19c logical   relaxd        : whether system is relaxed or not.
20c***************** OUTPUT *************************************************
21c logical found : Has DM been found in disk? (Only when task='read')
22c**************************************************************************
23
24      use files,     only : slabel, label_length
25      use precision, only : dp
26      use sys,  only      : die
27
28      implicit          none
29
30      character         task*(*)
31      logical           found, relaxd
32      integer           naux
33      real(dp)          cgaux(naux), cgcntr(0:20)
34
35      external          chkdim, io_assign, io_close
36
37
38c Internal variables and arrays ------------------------------------------
39
40      character(len=label_length+3) :: fname
41      logical   exist1
42      integer   nauxr, i, unit1
43
44c ------------------------------------------------------------------------
45
46c find file name ---------------------------------------------------------
47
48      fname = trim(slabel) // '.CG'
49
50c read it if it is there -------------------------------------------------
51
52      if (task.eq.'read' .or. task.eq.'READ') then
53        inquire (file=fname, exist=exist1)
54
55        if (exist1) then
56          write(6,'(/,a)') 'iocg: Reading CG continuation file'
57          call io_assign(unit1)
58          open( unit1, file=fname,
59     .          form='unformatted', status='unknown' )
60          rewind(unit1)
61          read(unit1) nauxr, relaxd
62          call chkdim( 'iocg', 'cgaux', naux, nauxr, 1 )
63          read(unit1) (cgcntr(i), i = 0, 20)
64          read(unit1) (cgaux(i), i = 1, naux)
65          call io_close(unit1)
66          found = .true.
67        else
68          relaxd = .false.
69          cgcntr(0) = 0
70          cgcntr(1) = 1
71          found = .false.
72        endif
73
74c write it ---------------------------------------------------------------
75
76      elseif (task.eq.'write' .or. task.eq.'WRITE') then
77
78        call io_assign(unit1)
79        open( unit1, file=fname,
80     .        form='unformatted', status='unknown' )
81        rewind(unit1)
82        write(unit1) naux, relaxd
83        write(unit1) (cgcntr(i), i = 0, 20)
84        write(unit1) (cgaux(i), i = 1, naux)
85        call io_close(unit1)
86
87      else
88        call die('iocg: Incorrect task')
89      endif
90
91      return
92      end
93
94