MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
Function.F90
Go to the documentation of this file.
1 
3 
4 
5 
6 
11 
12 
13 Subroutine formfunction(snes,X,F,user,ierr)
15 Implicit None
16 #include <finclude/petscsys.h>
17 #include <finclude/petscvec.h>
18 #include <finclude/petscvec.h90>
19 #include <finclude/petscdmda.h>
20 #include <finclude/petscdmda.h90>
21 #include <finclude/petscis.h>
22 #include <finclude/petscsnes.h>
23 #include <finclude/petscsnes.h90>
24 
25 ! Input/output variables:
26  snes snes
27  vec x,f
28  petscerrorcode ierr
29  type(userctx) user
30  dm da
31 
32 ! Declarations for use with local arrays:
33  petscscalar,pointer :: lx_v(:,:),lf_v(:,:)
34  vec localx
35 
36 ! Scatter ghost points to local vector, using the 2-step process
37 ! DMGlobalToLocalBegin(), DMGlobalToLocalEnd().
38 ! By placing code between these two statements, computations can
39 ! be done while messages are in transition.
40  call snesgetdm(snes,da,ierr)
41  call dmgetlocalvector(da,localx,ierr)
42  call dmglobaltolocalbegin(da,x,insert_values, &
43  & localx,ierr)
44  call dmglobaltolocalend(da,x,insert_values,localx,ierr)
45 
46 
47  !call VecGetArrayF90(localX,lx_v,ierr) !only for DOF=1
48  !call VecGetArrayF90(F,lf_v,ierr) !only for DOF=1
49  call dmdavecgetarrayf90(da,localx,lx_v,ierr)
50  call dmdavecgetarrayf90(da,f,lf_v,ierr)
51 
52 ! Compute function over the locally owned part of the grid
53  call formfunctionlocal(lx_v,lf_v,user,ierr)
54 
55 ! Restore vectors
56  !call VecRestoreArrayF90(localX,lx_v,ierr) !only for DOF=1
57  !call VecRestoreArrayF90(F,lf_v,ierr) !only for DOF=1
58  call dmdavecrestorearrayf90(da,localx,lx_v,ierr)
59  call dmdavecrestorearrayf90(da,f,lf_v,ierr)
60 
61 
62 ! Insert values into global vector
63 
64  call dmrestorelocalvector(da,localx,ierr)
65 
66  return
67 End Subroutine formfunction
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
82 
83 
84 Subroutine formfunctionlocal(rhop,f,user,ierr)
85 !PETSc modules
86  Use petscmanagement
87 !DFT modules
88  Use mod_dft_fmt
89  Use mod_dft_chain
90  Use mod_dft_disp_wda
91  Use parameters, Only: pi ,muhs,muhc,mudisp
92  Use basic_variables, Only: ncomp,nc,np ,nphas,xi,dense,parame,ensemble_flag !nphas nur fur fugacity call
93  Use eos_variables, Only:dhs,rho ,phas,eta_start,x,mseg,eta !letze 4 nur zum Test for aufruf eos_phi!!
94  Use mod_dft, Only: fa,zp,dzp,free,pbulk, fa_disp, ab_disp !letzen beiden nur fur elmars version
95  Use vle_var, Only: rhob
96  Use dft_fcn_module, Only: chempot_res,chempot_total
97  Use global_x, Only: ngrid, ngp
98 
99 
100 Implicit None
101 
102 ! Input/output variables:
103  type(userctx) user
104  petscscalar rhop(ncomp,user%gxs:user%gxe)
105  petscscalar f(ncomp,user%xs:user%xe)
106  petscerrorcode ierr
107 
108 ! Local variables:
109  petscint i
110  petscint k
111 
112  !FMT
113  REAL,dimension(user%gxs:user%gxe) :: n0,n1,n2,n3,nv1,nv2 !ngp muss groesser als fa+fa/2 sein!!
114  REAL,dimension(user%gxs:user%gxe) :: phi_dn0,phi_dn1,phi_dn2,phi_dn3,phi_dnv1,phi_dnv2
115  REAL :: f_fmt,temp,zs
116  REAL,dimension(user%xs:user%xe,ncomp) :: dF_drho_FMT
117 
118  !Chain
119  REAL,dimension(user%gxs:user%gxe,ncomp) :: rhobar,lambda
120  REAL :: f_ch
121  REAL,dimension(user%xs:user%xe,ncomp) :: dF_drho_CHAIN
122 
123  !DISP VAR
124  REAL,dimension(user%gxs:user%gxe,ncomp) :: rhop_wd,my_disp,df_disp_drk
125  REAL,dimension(user%gxs:user%gxe) :: f_disp
126  REAL, dimension(ncomp) :: dF_drho_disp
127  !for Elmars Version
128  REAL, DIMENSION(user%gxs:user%gxe,ncomp):: rhoi_disp,mydisp,dadisp_dr
129  REAL, DIMENSION(user%gxs:user%gxe) :: adisp,rho_disp,rhop_sum
130  REAL :: dF_drho_att(ncomp)
131  INTEGER :: fa_psi_max,WDA_var
132 
133 
134  !polar
135  REAL :: fres_polar,fdd,fqq,fdq,mu_polar(nc)
136  REAL :: fdd_rk, fqq_rk, fdq_rk, z3_rk
137  INTEGER :: ik
138  !association
139  REAL :: f_assoc
140  REAL :: mu_assoc(nc)
141  REAL :: lnphi(np,nc), fres
142 
143  REAL :: f_tot, delta_f
144  REAL :: Vext(ncomp)
145 
146 
147  Do k = 1,ncomp
148  Do i= user%xs,user%xe
149  If( rhop(k,i) < epsilon(dhs) ) rhop(k,i) = epsilon(dhs)
150  End Do
151  End Do
152 
153  DO i = user%gxs,user%gxe
154  rhop_sum(i) = sum( rhop(1:ncomp,i) )
155  END DO
156 
157 
158 
159  !calculate weighted densities
160  call fmt_weighted_densities(rhop,n0,n1,n2,n3,nv1,nv2,phi_dn0,phi_dn1,phi_dn2,phi_dn3,phi_dnv1,phi_dnv2,user)
161 
162  !calculate averaged density rhobar and lambda (both needed for chain term)
163  call chain_aux(rhop,rhobar,lambda,user)
164 
165 
166  !weighted densities for Dispersion
167 ! call DISP_Weighted_Densities(rhop, rhop_wd, user)
168 ! call DISP_mu(rhop_wd,f_disp,my_disp,df_disp_drk,user)
169 
170  !Elmars Version
171  fa_psi_max = maxval(fa_disp(1:ncomp))
172  call rhoi_disp_wd ( ngrid, fa_disp, fa_psi_max, ab_disp, rhop, rhoi_disp,user )
173  CALL a_disp_pcsaft ( ngrid, fa_disp, fa_psi_max, rhoi_disp, rho_disp, adisp, mydisp, dadisp_dr,user )
174 
175 
176  free = 0.0
177 
178  DO i = user%xs,user%xe
179 
180  !FMT
181  call fmt_dfdrho(i,fa,user,phi_dn0,phi_dn1,phi_dn2,phi_dn3,phi_dnv1,phi_dnv2,df_drho_fmt)
182  zs = 1.0 - n3(i)
183  temp = log(zs)
184  f_fmt = - n0(i)*temp + n1(i)*n2(i)/zs - nv1(i)*nv2(i)/zs & !see for example eq. 30 in "Fundamental measure theory for hard-sphere mixtures revisited: the White bear version (Roth)
185  + (n2(i)**3 -3.0*n2(i)*nv2(i)*nv2(i)) *(n3(i)+zs*zs*temp) &
186  /36.0/pi/zs/zs/n3(i)**2
187 
188  !Chain
189  call chain_dfdrho(i,rhop,lambda,rhobar,df_drho_chain,f_ch,user)
190 
191  !Dispersion
192  wda_var = 1
193  call df_disp_drho_wda( i, wda_var, fa_disp, ab_disp, rhop, rhop_sum, &
194  rhoi_disp, rho_disp, adisp, mydisp, dadisp_dr, df_drho_att,user )
195 
196  if(wda_var == 1) adisp(i) = rho_disp(i) * adisp(i)
197  if(wda_var == 2) adisp(i) = rhop_sum(i) * adisp(i)
198 
199 
200  !polar as LDA
201  fres_polar = 0.0
202  mu_polar(:) = 0.0
203  dense(1) = pi / 6.0 * sum( rhop(1:ncomp,i) * mseg(1:ncomp) * dhs(1:ncomp)**3 )
204  xi(1,1:ncomp) = rhop(1:ncomp,i) / sum( rhop(1:ncomp,i) )
205  ensemble_flag = 'tv'
206  IF ( sum( parame(1:ncomp,6) ) > 1.e-10 .OR. sum( parame(1:ncomp,7) ) > 1.e-10 ) THEN
207  eta = dense(1)
208  rho = sum(rhop(1:ncomp,i))
209  x(1:ncomp) = xi(1,1:ncomp)
210  call f_polar ( fdd, fqq, fdq )
211  fres_polar = ( fdd + fqq + fdq ) * sum( rhop(1:ncomp,i) )
212  DO ik = 1, ncomp
213  z3_rk = pi/6.0 * mseg(ik) * dhs(ik)**3
214  call phi_polar ( ik, z3_rk, fdd_rk, fqq_rk, fdq_rk )
215  mu_polar(ik) = fdd_rk + fqq_rk + fdq_rk
216  END DO
217  END IF
218 
219  !association as LDA
220  f_assoc = 0.0
221  mu_assoc(:) = 0.0
222  IF ( sum( nint(parame(1:ncomp,12)) ) > 0) THEN
223  call only_one_term_eos_numerical ( 'hb_term ', 'TPT1_Chap' )
224  call fugacity ( lnphi )
225  call restore_previous_eos_numerical
226  f_assoc = fres * sum( rhop(1:ncomp,i) )
227  mu_assoc(1:ncomp) = lnphi(1,1:ncomp)
228  END IF
229 
230  f_tot = f_fmt + f_ch + adisp(i) + fres_polar + f_assoc &
231  + sum( rhop(1:ncomp,i)*( log(rhop(1:ncomp,i)/rhob(1,1:ncomp))-1.0 ) )
232  delta_f = f_tot - ( sum(rhop(1:ncomp,i)*chempot_res(1:ncomp)) - pbulk) ! all quantities .../(kT)
233  free = free + delta_f*dzp
234 
235 
236 
237  Do k=1,ncomp
238  f(k,i) = -(rhob(1,k) * exp(chempot_res(k)-df_drho_fmt(i,k)-df_drho_chain(i,k)-df_drho_att(k) &
239  -mu_polar(k) - mu_assoc(k)) - rhop(k,i))
240  End Do
241 
242  END DO
243 
244 
245 
246 End Subroutine formfunctionlocal
247 
248 
249 
250 
251 
252 
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW This module contains constant...
Definition: modules.f90:6
subroutine formfunctionlocal(rhop, f, user, ierr)
This subroutine performs the local evaluation of the residual function. It calls the subroutines whic...
Definition: Function.F90:85
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW This module contains paramete...
Definition: modules.f90:120
This module contains variables associated with the PETSc solver.
Definition: mod_PETSc.F90:24
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW This module contains the vari...
Definition: modules.f90:327
subroutine formfunction(snes, X, F, user, ierr)
The subroutine FormFunction is a wrapper function which takes care of handling the global PETSc data ...
Definition: Function.F90:14
In this module, the application context is defined.
Definition: mod_PETSc.F90:7
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW This module contains paramete...
Definition: modules.f90:29