FrontISTR  6.0.0
Large-scale structural analysis program with finit element method
hecmw_solver_contact.f90
Go to the documentation of this file.
1 !-------------------------------------------------------------------------------
2 ! Copyright (c) 2019 FrontISTR Commons
3 ! This software is released under the MIT License, see LICENSE.txt
4 !-------------------------------------------------------------------------------
8 
10 
11  use hecmw_util
18  use m_hecmw_comm_f
19  use hecmw_ebc_defer
21 
22  implicit none
23 
24  private
25  public :: solve_lineq_contact_init
26  public :: solve_lineq_contact
27 
28 contains
29 
31  subroutine solve_lineq_contact_init(hecMESH,hecMAT,is_sym)
32  type (hecmwst_local_mesh) :: hecmesh
33  type (hecmwst_matrix) :: hecmat
34  logical :: is_sym
35  integer(kind=kint) :: contact_elim, solver_type, loglevel, myrank
36  logical :: writelog
37 
38  contact_elim = hecmw_mat_get_contact_elim(hecmat)
39  solver_type = hecmw_mat_get_solver_type(hecmat)
40  loglevel = max(0, hecmw_mat_get_loglevel(hecmat)) ! LOGLEVEL is -1 when not given
41  myrank = hecmw_comm_get_rank()
42  writelog = (myrank==0 .and. loglevel>0)
43 
44  if( contact_elim==0 )then ! auto
45  if( solver_type==1 )then ! iterative
46  contact_elim=1
47  else ! direct
48  contact_elim=-1
49  endif
50  call hecmw_mat_set_contact_elim(hecmat,contact_elim)
51  endif
52 
53  ! without a Lagrange multiplier anywhere (ALAGRANGE, or SLAGRANGE before any activation)
54  ! there is no saddle point part to keep, so the single-process limitation of the
55  ! no-elimination DIRECTmf path does not apply: the elimination path solves the plain
56  ! distributed matrix as is. The stored CONTACT_ELIM is not changed, so the limitation
57  ! still guards the solves after multipliers appear.
58  if( contact_elim/=1 .and. solver_type==7 .and. hecmw_comm_get_size() > 1 )then
59  if( .not. any_lagrange(hecmesh, hecmat) ) contact_elim = 1
60  endif
61 
62  if( contact_elim==1 )then
63  if( writelog ) write(*,*) 'solve contact with elimination'
64  call solve_lineq_contact_elim_init(hecmat,is_sym)
65  else
66  if( writelog ) write(*,*) 'solve contact without elimination'
67  if( solver_type==1 )then
68  write(*,*) 'ERROR: iterative solver without elimination not available in contact analysis'
70  elseif( solver_type==2 )then
71  call solve_lineq_serial_lag_hecmw_init(hecmat,is_sym)
72  else if( solver_type==3 )then
73  call solve_lineq_mkl_contact_init(hecmesh,is_sym)
74  elseif( solver_type==5 ) then
76  elseif( solver_type==7 ) then
77  if( hecmw_comm_get_size() > 1) then
78  write(*,*) 'ERROR: !SOLVER,METHOD=DIRECTmf not available in parallel contact analysis',&
79  ' without elimination; specify CONTACT_ELIM=1 or use MUMPS or DIRECTmkl instead'
81  endif
82  call solve_lineq_mf_contact_init(is_sym)
83  else
84  write(*,*) 'ERROR: specified solver not available in contact analysis without elimination;',&
85  ' please use MUMPS or DIRECTmkl instead'
87  endif
88  endif
89  end subroutine solve_lineq_contact_init
90 
91 
93  subroutine solve_lineq_contact(hecMESH,hecMAT,hecEBC,istat,rf,is_contact_active)
94 
95  type (hecmwst_local_mesh) :: hecmesh
96  type (hecmwst_matrix) :: hecmat
97  type (hecmwst_ebc) :: hecebc
98  integer(kind=kint), intent(out) :: istat
99  real(kind=kreal), optional :: rf
100  logical :: is_contact_active
101 
102  real(kind=kreal) :: factor
103  real(kind=kreal) :: t1, t2, resid
104  integer(kind=kint) :: ndof
105  integer(kind=kint) :: contact_elim, solver_type
106  logical :: conmat_added
107 
108  contact_elim = hecmw_mat_get_contact_elim(hecmat)
109  solver_type = hecmw_mat_get_solver_type(hecmat)
110 
111  ! the same bypass as the init: no multiplier anywhere means no saddle point part
112  if( contact_elim/=1 .and. solver_type==7 .and. hecmw_comm_get_size() > 1 )then
113  if( .not. any_lagrange(hecmesh, hecmat) ) contact_elim = 1
114  endif
115 
116  factor = 1.0d0
117  if( present(rf) )factor = rf
118 
119  t1 = hecmw_wtime()
120 
121  istat = 0
122  conmat_added = .false.
123  ! the direct-solver branches below report failure through istat only and never
124  ! touch these flags; clear them so a stale verdict from an earlier solve is not
125  ! mistaken for this one's. The elimination branch overwrites them with the real result.
126  call hecmw_mat_set_flag_converged(hecmat, 1)
127  call hecmw_mat_set_flag_diverged(hecmat, 0)
128  if( contact_elim==1 )then
129  call solve_lineq_contact_elim(hecmesh,hecmat,hecebc,istat,is_contact_active)
130  else
131  if( solver_type==1 )then
132  write(*,*) 'ERROR: iterative solver without elimination not available in contact analysis'
134  elseif( solver_type==2 )then
135  if( hecmw_comm_get_size() > 1) then
136  write(*,*) 'ERROR: !SOLVER,METHOD=DIRECT not available in parallel contact analysis;',&
137  ' please use MUMPS or DIRECTmkl instead'
139  else
140  call add_conmat_to_hecmat(hecmat)
141  conmat_added = .true.
142  call solve_lineq_serial_lag_hecmw(hecmesh,hecmat,hecebc)
143  endif
144  elseif( solver_type==3 )then
145  if( hecmw_comm_get_size() == 1 )then
146  call add_conmat_to_hecmat(hecmat)
147  conmat_added = .true.
148  endif
149  call solve_lineq_mkl_contact(hecmesh,hecmat,hecebc,istat)
150  elseif( solver_type==5 ) then
151  call solve_lineq_mumps_contact(hecmesh,hecmat,hecebc,istat)
152  elseif( solver_type==7 ) then
153  if( hecmw_comm_get_size() > 1) then
154  write(*,*) 'ERROR: !SOLVER,METHOD=DIRECTmf not available in parallel contact analysis',&
155  ' without elimination; specify CONTACT_ELIM=1 or use MUMPS or DIRECTmkl instead'
157  else
158  call add_conmat_to_hecmat(hecmat)
159  conmat_added = .true.
160  call solve_lineq_mf_contact(hecmesh,hecmat,hecebc,istat)
161  endif
162  else
163  write(*,*) 'ERROR: specified solver not available in contact analysis without elimination;',&
164  ' please use MUMPS or DIRECTmkl instead'
166  endif
167  endif
168 
169  ndof = hecmat%NDOF
170  call hecmw_update_r(hecmesh,hecmat%X,hecmesh%n_node, ndof)
171 
172  ! the final residual of every direct solver is printed here, as hecmw_solve does on the
173  ! other paths; the solvers print at most the history of their own refinement
174  if( contact_elim/=1 .and. hecmw_mat_get_iterlog(hecmat) > 0 )then
175  ! hecMAT%conmat keeps its values after it is added to hecMAT, so it is left out then
176  resid = rel_resid_lagrange(hecmesh,hecmat,.not. conmat_added)
177  if( hecmw_comm_get_rank()==0 ) write(*,"(a,1pe12.5)") '### Relative residual =', resid
178  endif
179 
180  t2 = hecmw_wtime()
181  if (hecmw_mat_get_timelog(hecmat) .ge. 1) then
182  if ( hecmw_comm_get_rank() ==0) write(*,*) ' solve time :', t2 - t1
183  endif
184 
185  hecmat%X=factor*hecmat%X
186 
187  end subroutine solve_lineq_contact
188 
189 
191  logical function any_lagrange(hecMESH, hecMAT)
192  type (hecmwst_local_mesh) :: hecmesh
193  type (hecmwst_matrix) :: hecmat
194  integer(kind=kint) :: n
195 
196  n = hecmat%lagmat%num_lagrange
197  call hecmw_allreduce_i1(hecmesh, n, hecmw_max)
198  any_lagrange = n > 0
199  end function any_lagrange
200 
201 
202  subroutine add_conmat_to_hecmat(hecMAT)
203  type (hecmwst_matrix), intent(inout) :: hecmat
204 
205 
206  integer(kind=kint) :: ndof,ndof2,i
207 
208  ndof = hecmat%NDOF
209  ndof2 = ndof*ndof
210 
211  do i=1,hecmat%NP*ndof + hecmat%lagmat%num_lagrange
212  hecmat%B(i) = hecmat%B(i) + hecmat%conmat%B(i)
213  enddo
214 
215  do i=1,hecmat%NP*ndof2
216  hecmat%D(i) = hecmat%D(i) + hecmat%conmat%D(i)
217  enddo
218 
219  do i=1,hecmat%NPL*ndof2
220  hecmat%AL(i) = hecmat%AL(i) + hecmat%conmat%AL(i)
221  enddo
222 
223  do i=1,hecmat%NPU*ndof2
224  hecmat%AU(i) = hecmat%AU(i) + hecmat%conmat%AU(i)
225  enddo
226  end subroutine add_conmat_to_hecmat
227 
228 
231  function rel_resid_lagrange(hecMESH, hecMAT, with_conmat) result(resid)
232  type (hecmwst_local_mesh), intent(in) :: hecmesh
233  type (hecmwst_matrix), intent(in) :: hecmat
234  logical, intent(in) :: with_conmat
235  real(kind=kreal) :: resid
236 
237  integer(kind=kint) :: ndof, ndof2, nndof, npndof, i, idof, jdof, j, k, l, irow
238  real(kind=kreal), allocatable :: y(:), yc(:), bc(:)
239  real(kind=kreal), pointer :: blag(:)
240  real(kind=kreal) :: bi, rl, sums(2)
241 
242  ndof = hecmat%NDOF
243  ndof2 = ndof*ndof
244  nndof = hecmat%N*ndof
245  npndof = hecmat%NP*ndof
246 
247  allocate(y(npndof), yc(npndof), bc(npndof))
248  call hecmw_matvec(hecmesh, hecmat, hecmat%X, y)
249 
250  ! the contact part and the multiplier columns also have entries on the external rows,
251  ! which are this rank's share of the rows of the owner rank
252  yc(:) = 0.0d0
253  bc(:) = 0.0d0
254  if( with_conmat )then
255  do i = 1, hecmat%NP
256  do idof = 1, ndof
257  irow = ndof*(i-1)+idof
258  bc(irow) = hecmat%conmat%B(irow)
259  do jdof = 1, ndof
260  yc(irow) = yc(irow) + hecmat%conmat%D(ndof2*(i-1)+ndof*(idof-1)+jdof)*hecmat%X(ndof*(i-1)+jdof)
261  enddo
262  do k = hecmat%conmat%indexL(i-1)+1, hecmat%conmat%indexL(i)
263  j = hecmat%conmat%itemL(k)
264  do jdof = 1, ndof
265  yc(irow) = yc(irow) + hecmat%conmat%AL(ndof2*(k-1)+ndof*(idof-1)+jdof)*hecmat%X(ndof*(j-1)+jdof)
266  enddo
267  enddo
268  do k = hecmat%conmat%indexU(i-1)+1, hecmat%conmat%indexU(i)
269  j = hecmat%conmat%itemU(k)
270  do jdof = 1, ndof
271  yc(irow) = yc(irow) + hecmat%conmat%AU(ndof2*(k-1)+ndof*(idof-1)+jdof)*hecmat%X(ndof*(j-1)+jdof)
272  enddo
273  enddo
274  enddo
275  enddo
276  call hecmw_assemble_r(hecmesh, bc, hecmat%NP, ndof)
277  endif
278  if( hecmat%lagmat%num_lagrange > 0 )then
279  do i = 1, hecmat%NP
280  do l = hecmat%lagmat%indexU_lagrange(i-1)+1, hecmat%lagmat%indexU_lagrange(i)
281  j = hecmat%lagmat%itemU_lagrange(l)
282  do idof = 1, ndof
283  irow = ndof*(i-1)+idof
284  yc(irow) = yc(irow) + hecmat%lagmat%AU_lagrange(ndof*(l-1)+idof)*hecmat%X(npndof+j)
285  enddo
286  enddo
287  enddo
288  endif
289  call hecmw_assemble_r(hecmesh, yc, hecmat%NP, ndof)
290 
291  ! near convergence the parts of the right-hand side in hecMAT and conmat nearly cancel, so
292  ! they are summed before the products are subtracted, as in the solver input
293  sums(:) = 0.0d0
294  do i = 1, nndof
295  bi = hecmat%B(i) + bc(i)
296  sums(1) = sums(1) + (bi - (y(i) + yc(i)))**2
297  sums(2) = sums(2) + bi**2
298  enddo
299 
300  ! the solvers take the right-hand side of the multiplier rows from conmat when it is separate
301  if( with_conmat )then
302  blag => hecmat%conmat%B
303  else
304  blag => hecmat%B
305  endif
306  do i = 1, hecmat%lagmat%num_lagrange
307  rl = blag(npndof+i)
308  do l = hecmat%lagmat%indexL_lagrange(i-1)+1, hecmat%lagmat%indexL_lagrange(i)
309  j = hecmat%lagmat%itemL_lagrange(l)
310  do jdof = 1, ndof
311  rl = rl - hecmat%lagmat%AL_lagrange(ndof*(l-1)+jdof)*hecmat%X(ndof*(j-1)+jdof)
312  enddo
313  enddo
314  sums(1) = sums(1) + rl**2
315  sums(2) = sums(2) + blag(npndof+i)**2
316  enddo
317 
318  call hecmw_allreduce_r(hecmesh, sums, 2, hecmw_sum)
319  if( .not. (sums(2) > 0.0d0) ) sums(2) = 1.0d0
320  resid = sqrt(sums(1)/sums(2))
321 
322  deallocate(y, yc, bc)
323  end function rel_resid_lagrange
324 
325 
326 end module m_solve_lineq_contact
Essential boundary conditions kept as per-DOF marks and values so that they can be imposed on the mat...
integer(kind=kint) function, public hecmw_mat_get_solver_type(hecMAT)
subroutine, public hecmw_mat_set_contact_elim(hecMAT, contact_elim)
subroutine, public hecmw_mat_set_flag_diverged(hecMAT, flag_diverged)
subroutine, public hecmw_mat_set_flag_converged(hecMAT, flag_converged)
integer(kind=kint) function, public hecmw_mat_get_iterlog(hecMAT)
integer(kind=kint) function, public hecmw_mat_get_timelog(hecMAT)
integer(kind=kint) function, public hecmw_mat_get_loglevel(hecMAT)
integer(kind=kint) function, public hecmw_mat_get_contact_elim(hecMAT)
subroutine, public hecmw_matvec(hecMESH, hecMAT, X, Y, COMMtime)
I/O and Utility.
Definition: hecmw_util_f.F90:7
integer(kind=kint), parameter hecmw_sum
subroutine hecmw_abort(comm, code)
integer(kind=kint) function hecmw_comm_get_size()
integer(kind=kint) function hecmw_comm_get_comm()
integer(kind=kint), parameter hecmw_max
integer(kind=4), parameter kreal
integer(kind=kint) function hecmw_comm_get_rank()
real(kind=kreal) function hecmw_wtime()
subroutine hecmw_assemble_r(hecMESH, val, n, m)
subroutine hecmw_update_r(hecMESH, val, n, m)
subroutine hecmw_allreduce_i1(hecMESH, s, ntag)
subroutine hecmw_allreduce_r(hecMESH, val, n, ntag)
This module provides interface of iteratie linear equation solver for contact problems using Lagrange...
subroutine, public solve_lineq_contact_elim_init(hecMAT, is_sym)
subroutine, public solve_lineq_contact_elim(hecMESH, hecMAT, hecEBC, istat, is_contact_active)
This module provides functions to solve sparse system of \linear equitions in the case of contact ana...
subroutine, public solve_lineq_contact(hecMESH, hecMAT, hecEBC, istat, rf, is_contact_active)
This subroutine.
subroutine, public solve_lineq_contact_init(hecMESH, hecMAT, is_sym)
This subroutine.
subroutine solve_lineq_serial_lag_hecmw(hecMESH, hecMAT, hecEBC)
subroutine solve_lineq_serial_lag_hecmw_init(hecMAT, is_sym)
This module provides linear equation solver interface of DIRECTmf for contact problems using Lagrange...
subroutine, public solve_lineq_mf_contact(hecMESH, hecMAT, hecEBC, istat)
subroutine, public solve_lineq_mf_contact_init(is_sym)
This module provides functions to solve sparse system of \linear equitions using intel MKL direct spa...
subroutine, public solve_lineq_mkl_contact_init(hecMESH, is_sym)
subroutine, public solve_lineq_mkl_contact(hecMESH, hecMAT, hecEBC, istat)
This subroutine executes the MKL solver.
This module provides linear equation solver interface of MUMPS for contact problems using Lagrange mu...
subroutine, public solve_lineq_mumps_contact(hecMESH, hecMAT, hecEBC, istat)
subroutine, public solve_lineq_mumps_contact_init(is_sym)