update new files
[unres.git] / source / maxlik / src_FPy / zscorez.F
1       subroutine maxlik_init(nvarr,xrange,comm,mepython)
2 c Optimize the UNRES energy function by minimization of a quartic target
3 c function or by the VMC method.
4       implicit none
5 #ifndef ISNAN
6       external proc_proc
7 #endif
8 #ifdef WINPGI
9 cMS$ATTRIBUTES C ::  proc_proc
10 #endif
11       include "DIMENSIONS"
12       include "DIMENSIONS.ZSCOPT"
13 #ifdef MPI
14       include "mpif.h"
15       integer IERROR,ERRCODE,kolor,key,comm
16       include "COMMON.MPI"
17 #endif
18       include "COMMON.IOUNITS"
19       include "COMMON.OPTIM"
20       include "COMMON.XBOUND"
21       integer nvarr,iparm,mepython
22       double precision rr,x(max_paropt)
23       integer idumm
24       integer i
25       double precision xrange(maxvar,2)
26       integer number_of_variables
27       common /patch/ number_of_variables
28 #ifdef PYTHON
29 Cf2py intent(in) comm
30 Cf2py intent(in) mepython
31 Cf2py intent(out) nvarr
32 Cf2Py intent(out) xrange
33 #endif
34       print *,"Starting..."
35 #ifdef MPI
36 #ifndef PYTHON
37 c      print *,"Initializing MPI..."
38       call MPI_Init( IERROR )
39       ALL_COMM = MPI_COMM_WORLD 
40 #else
41       ALL_COMM = comm
42       print *,"comm",comm
43 #endif
44       print *,"Before MPI_Comm_rank"
45       call MPI_Comm_rank( ALL_COMM, me, IERROR )
46       print *,"After MPI_Comm_rank"
47       call MPI_Comm_size( ALL_COMM, nprocs, IERROR )
48       print *,"Finished initializing MPI... nprocs",nprocs
49       Master = 0
50       Master1 = 1
51 c      print *,"Me",me," Master",master," Ierror",ierror
52 #ifndef PYTHON
53       if (me.eq.Master .and. ierror.gt.0) then
54         write(iout,*) "SEVERE ERROR - Can't initialize MPI."
55         call mpi_finalize(ierror)
56         stop
57       endif
58 #endif
59 #endif
60 #ifndef ISNAN
61 c NaNQ initialization
62       i=-1
63       rr=dacos(100.0d0)
64 #ifdef WINPGI
65       idumm=proc_proc(rr,i)
66 #else
67       call proc_proc(rr,i)
68 #endif
69 #endif
70       call initialize
71 c      print *,"calling openunits"
72       call openunits(mepython)
73 c      print *,"openunits called"
74       call read_general_data(*10)
75       if (me.eq.Master) then
76       write (iout,'(80(1h-)/10x,
77      & "Maximum likelihood optimization of UNRES energy function",
78      & " v. 05/10/16"/80(1h-))')
79 #ifdef PYTHON
80       write (iout,*) "My Python task is",mepython
81       write (*,*) "My rank is",me," My Python task is",mepython
82 #endif
83       call flush(iout)
84       call cinfo
85       endif
86 c      call promienie(*10)
87       if (me.eq.Master)write (iout,*) "Finished READ_GENERAL_DATA"
88       call flush(iout)
89       do iparm=1,nparmset
90         call parmread(iparm,*10)
91       enddo
92       if (me.eq.Master) write (iout,*) "Finished parmread"
93       call flush(iout)
94       call read_optim_parm(*10)
95       call print_general_data(*10)
96       call read_protein_data(*10)
97       if (me.eq.Master) write (iout,*) "Finished READ_PROTEIN_DATA"
98       call flush(iout)
99 #ifdef PYTHON
100       if (mepython.gt.0) call read_database(*10)
101 #else
102       call read_database(*10)
103 #endif
104       if (me.eq.Master) then
105         write (iout,*) "Finished READ_DATABASE"
106         call flush(iout)
107       endif
108 #ifdef MPI 
109 c      write (iout,*) Me,' calling PROC_GROUPS'
110       call proc_groups
111 c      write (iout,*) Me,' calling WORK_PARTITION_MAP'
112 c      call work_partition_map(nvarr)
113 #endif
114       call proc_data(nvarr,x,mepython,*10)
115 #ifdef PYTHON
116       number_of_variables=nvarr
117       do i=1,nvarr
118         xrange(i,1)=x_low(i)
119         xrange(i,2)=x_up(i)
120       enddo
121       if (me.eq.Master) write (iout,*) "xrange from MAXLIK_INIT"
122       do i=1,nvarr
123         if (me.eq.Master) write (iout,*) i,xrange(i,1),xrange(i,2)
124       enddo
125       if (me.eq.Master) 
126      & write (iout,*) "================ maxlik intiialization completed"
127       call flush (iout)
128       if (mepython.eq.0) then
129          write (iout,*) 
130      &   "I am PYTHON master and this is the end of my FORTRAN task."
131         call flush(iout)
132         return
133       endif
134 #endif
135       call read_thermal
136       return
137    10 if (me.eq.Master)write (iout,*) "Error termination of the program"
138       call MPI_Finalize( IERROR )
139       return
140       end
141 c------------------------------------------------------------------------
142       subroutine maxlik_optim(x,xmin,fmin)
143 c Optimize the UNRES energy function by minimization of a quartic target
144 c function or by the VMC method.
145       implicit none
146 #ifndef ISNAN
147       external proc_proc
148 #endif
149 #ifdef WINPGI
150 cMS$ATTRIBUTES C ::  proc_proc
151 #endif
152       include "DIMENSIONS"
153       include "DIMENSIONS.ZSCOPT"
154 #ifdef MPI
155       include "mpif.h"
156       integer IERROR,ERRCODE,kolor,key
157       include "COMMON.MPI"
158 #endif
159       include "COMMON.IOUNITS"
160       include "COMMON.OPTIM"
161       integer nvarr,iparm,i
162       double precision rr,x(max_paropt),xmin(max_paropt),fmin
163       double precision tcpu,t1,t1w,t1_ini,t1w_ini
164       integer number_of_variables
165       common /patch/ number_of_variables
166       nvarr=number_of_variables
167 #ifdef PYTHON
168 Cf2py intent(in) x
169 Cf2py intent(out) xmin
170 Cf2py intent(out) fmin
171       if (me.eq.Master) then
172         write (*,*) "MAXLIK_OPTIM: Variables from PYTHON:",nvarr
173         write (iout,*) "MAXLIK_OPTIM: Variables from PYTHON:",nvarr
174         do i=1,nvarr
175           write (iout,*) i,x(i)
176         enddo 
177       endif
178 #else
179       if (me.eq.Master) then
180         write (*,*) "MAXLIK_OPTIM: Variables from maxlik_init:",nvarr
181         write (iout,*) "MAXLIK_OPTIM: Variables from maxlik_init:",nvarr
182         do i=1,nvarr
183           write (iout,*) i,x(i)
184         enddo 
185       endif
186 #endif
187 #ifdef MPI
188       if (me.eq.Master) then
189         t1w_ini = MPI_WTIME()
190         t1_ini = tcpu()
191 #endif
192         call maxlikopt(nvarr,x,xmin,fmin)
193         write (iout,*) "fmin from MAXLIK_OPTIM:",fmin
194 #ifdef MPI
195         call jebadelko(nvarr)
196       else
197         call jebadelko(nvarr)
198       endif
199       call bilans
200       if (me.eq.Master) then
201         t1w = mpi_wtime() - t1w_ini
202         t1 = tcpu() - t1_ini
203         write (iout,*)
204         write (iout,*) "CPU time",t1," wall clock time",t1w
205       call flush(iout)
206       endif
207 #ifdef PYTHON
208       if (me.eq.Master) 
209      & write (iout,'(30(1h-),"Minimization completed",30(1h-))')
210       call flush(iout)
211 #else
212       call MPI_Finalize( IERROR )
213 #endif
214 #else
215       call bilans
216 #endif
217       return
218       end