update new files
[unres.git] / source / maxlik / src_MD_T_maxlik-NEWCORR-PMF-PDB / pdbstatread.F
1       subroutine pdbstatread(*)
2       include "DIMENSIONS"
3       include "DIMENSIONS.ZSCOPT"
4 #ifdef MPI
5       include 'mpif.h'
6       include 'COMMON.MPI'
7       include 'COMMON.MPISET'
8       integer ierror
9       double precision bufin(maxpoint),bufout(maxpoint)
10       integer ibufin(maxpoint),ibufout(maxpoint)
11       double precision sumweight_pdb_(0:ntyp,0:ntyp)
12 #endif
13       include "COMMON.PDBSTAT"
14       include "COMMON.IOUNITS"
15       include "COMMON.WEIGHTS"
16       include "COMMON.TORSION"
17       include "COMMON.GEO"
18       double precision sumweight_pdb(0:ntyp,0:ntyp)
19       integer rescode
20       logical lprn /.false./
21       write (iout,*) "Calling PDBSTATREAD"
22       write (iout,*) "deg2rad",deg2rad
23
24 #ifdef MPI
25       write (iout,*) "Master",Master
26       if (me.eq.Master) then
27 #endif
28       do i=0,ntortyp-1
29         do j=0,ntortyp-1
30           npdbtor(i,j)=0
31           sumweight_pdb(i,j)=0.0d0
32         enddo
33       enddo
34       ii=0
35       do i=1,maxpoint
36         read (icsa_seed,'(4f10.5,4i5,3x,a3,1x,a3)',end=13,err=13) 
37      &    zz_pdb(1,ii+1),zz_pdb(2,ii+1),zz_pdb(3,ii+1),resol(ii+1),
38      &    ires1,ires2,iat1,iat2,res1,res2
39         if (zz_pdb(1,ii+1).gt.deg2rad*70.and.
40      &      zz_pdb(2,ii+1).gt.deg2rad*70) then
41           ii=ii+1
42           i1=itype2loc(rescode(1,res1,0))
43           i2=itype2loc(rescode(1,res2,0))
44           ipdbtyp(1,ii)=i1
45           ipdbtyp(2,ii)=i2
46           npdbtor(i1,i2)=npdbtor(i1,i2)+1 
47           sumweight_pdb(i1,i2)=sumweight_pdb(i1,i2)+1.0d0/resol(ii)**2
48           if (lprn) write(iout,'(i7,3f10.5,2i5,f10.5)')
49      &       ii,zz_pdb(1,ii),zz_pdb(2,ii),zz_pdb(3,ii),
50      &       ipdbtyp(1,ii),ipdbtyp(2,ii),resol(ii)
51         endif
52       enddo
53    13 npoint_pdb=ii
54       write (iout,*)npoint_pdb," data points found in file"
55       write(iout,*) 
56      & "Numbers of points for the pairs of types and sumweight"
57       do i=0,ntortyp-1
58         do j=0,ntortyp-1
59           write (2,*) i,j,npdbtor(i,j),sumweight_pdb(i,j)
60         enddo
61       enddo
62       do i=1,npoint_pdb
63         i1 = ipdbtyp(1,i)
64         i2 = ipdbtyp(2,i)
65         resol(i)=resol(i)/sumweight_pdb(i1,i2)
66       enddo
67 c Check normalization of the resol array
68       sumweight_pdb=0.0d0
69       do i=1,npoint_pdb
70         i1 = ipdbtyp(1,i)
71         i2 = ipdbtyp(2,i)
72         sumweight_pdb(i1,i2)=sumweight_pdb(i1,i2)+resol(i)
73       enddo
74       write (iout,*) "The sumweight_pdb array after normalization"
75       do i1=0,ntortyp-1
76         do i2=0,ntortyp-1
77           write (2,*) i1,i2,sumweight_pdb(i1,i2)
78           if (dabs(sumweight_pdb(i1,i2)-1.0d0).gt.1.0d-7) then
79             write (iout,*) "!!!! CHECKSUM ERROR IN sumweight_pdb",
80      &        i1,i2,sumweight_pdb(i1,i2)
81             call MPI_Abort(MPI_COMM_WORLD,ierror)
82             stop
83           endif
84         enddo
85       enddo
86 #ifdef MPI
87       npoint_pdb_all=npoint_pdb
88       call work_partition_pmf(npoint_pdb_all,.true.)
89       write (iout,*) "After scatter"
90       write (iout,*) "scount_pmf",(scount_pmf(i),i=0,nprocs-1)
91       call flush(iout)
92       endif
93       call MPI_Bcast(npdbtor(1,1),1,MPI_INTEGER,Master,WHAM_COMM,ierror)
94       call MPI_Scatter(scount_pmf,1,MPI_INTEGER,npoint_pdb,1,
95      &     MPI_INTEGER,Master,WHAM_COMM,ierror)
96       write (iout,*) "After scatter scount npoint_pdb",npoint_pdb
97       call flush(iout)
98 c Distribute angles
99       do j=1,3
100         if (me.eq.Master) then
101           do i=1,npoint_pdb_all
102             bufin(i)=zz_pdb(j,i)
103           enddo
104         endif
105         call MPI_Scatterv(bufin(1),scount_pmf,idispl_pmf,
106      &    MPI_DOUBLE_PRECISION,
107      &    bufout(1),npoint_pdb,MPI_DOUBLE_PRECISION,Master,
108      &    WHAM_COMM,ierror)
109         do i=1,npoint_pdb
110           zz_pdb(j,i)=bufout(i)
111         enddo 
112       enddo
113       write (iout,*) "ZZ_pdb distributed"
114       call flush(iout)
115 C Distribute angle types
116       do j=1,2
117         if (me.eq.Master) then
118           do i=1,npoint_pdb_all
119             ibufin(i)=ipdbtyp(j,i)
120           enddo
121         endif
122         call MPI_Scatterv(ibufin(1),scount_pmf,idispl_pmf,
123      &    MPI_INTEGER,
124      &    ibufout(1),npoint_pdb,MPI_INTEGER,Master,
125      &    WHAM_COMM,ierror)
126         do i=1,npoint_pdb
127           ipdbtyp(j,i)=ibufout(i)
128         enddo 
129       enddo
130       write (iout,*) "ipdbtyp distributed"
131       call flush(iout)
132 c Distribute resolutions
133       if (me.eq.Master) then
134         do i=1,npoint_pdb_all
135           bufin(i)=resol(i)
136         enddo
137       endif
138       call MPI_Scatterv(bufin(1),scount_pmf,idispl_pmf,
139      &  MPI_DOUBLE_PRECISION,
140      &  resol(1),npoint_pdb,MPI_DOUBLE_PRECISION,Master,
141      &  WHAM_COMM,ierror)
142 c Check normalization of the resol array
143       sumweight_pdb_=0.0d0
144       do i=1,npoint_pdb
145         i1 = ipdbtyp(1,i)
146         i2 = ipdbtyp(2,i)
147         sumweight_pdb_(i1,i2)=sumweight_pdb_(i1,i2)+resol(i)
148       enddo
149       write (iout,*) "Before REDUCE"
150       call MPI_Reduce(sumweight_pdb_,sumweight_pdb,(ntyp+1)*(ntortyp+1),
151      &  MPI_DOUBLE_PRECISION,MPI_SUM,Master,Comm1,ierror)
152       write (iout,*) "After REDUCE"
153
154       if (me.eq.Master) then 
155
156       write (iout,*) 
157      &  "The distributed sumweight_pdb array after normalization"
158       do i1=0,ntortyp-1
159         do i2=0,ntortyp-1
160           write (2,*) i1,i2,sumweight_pdb(i1,i2)
161           if (dabs(sumweight_pdb(i1,i2)-1.0d0).gt.1.0d-7) then
162             write (iout,*) "!!!! CHECKSUM ERROR IN sumweight_pdb",
163      &        i1,i2,sumweight_pdb(i1,i2)
164             call MPI_Abort(MPI_COMM_WORLD,ierror)
165             stop
166           endif
167         enddo
168       enddo
169
170       endif
171
172       if (lprn) then
173         write (iout,*) "Points asigned to the procesor"
174         do i=1,npoint_pdb
175           write(iout,'(i7,3f10.5,2i5,1pe10.2)')
176      &      i,zz_pdb(1,i),zz_pdb(2,i),zz_pdb(3,i),
177      &      ipdbtyp(1,i),ipdbtyp(2,i),resol(i)
178         enddo
179       endif 
180       n_calka = ((160-70)/incr_theta-1)**2*(360/incr_gam-1)
181       write (iout,*) "n_calka",n_calka
182       call work_partition_pmf(n_calka,.true.)
183       istart_calka=indstart_pmf(me)
184       iend_calka=indend_pmf(me)
185       write (iout,*) "Processor",me," n_calka",n_calka,
186      & " istart_calka",istart_calka," iend_calka",iend_calka
187       call flush(iout)
188 #endif
189       return
190       end