source: src/main/aggr.F90 @ ea465d3

Revision ea465d3, 10.5 KB checked in by Oleg Batrashev <ogbash@…>, 2 years ago (diff)

Move cdat and cdat setup under Coarse Preconditioner.

  • Property mode set to 100644
Line 
1! DOUG - Domain decomposition On Unstructured Grids
2! Copyright (C) 1998-2006 Faculty of Computer Science, University of Tartu and
3! Department of Mathematics, University of Bath
4!
5! This library is free software; you can redistribute it and/or
6! modify it under the terms of the GNU Lesser General Public
7! License as published by the Free Software Foundation; either
8! version 2.1 of the License, or (at your option) any later version.
9!
10! This library is distributed in the hope that it will be useful,
11! but WITHOUT ANY WARRANTY; without even the implied warranty of
12! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13! Lesser General Public License for more details.
14!
15! You should have received a copy of the GNU Lesser General Public
16! License along with this library; if not, write to the Free Software
17! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18! or contact the authors (University of Tartu, Faculty of Computer Science, Chair
19! of Distributed Systems, Liivi 2, 50409 Tartu, Estonia, http://dougdevel.org,
20! mailto:info(at)dougdevel.org)
21
22!> Main program for running DOUG with input files in assembled form.
23!> \section aggregation_running Running the aggregation-based DOUG code: (example)
24!>   <tt>mpirun -np 3 doug_aggr -f doug.ctl</tt>
25!>     where \c doug.ctl may contain the following fields
26!>   \subsection example Input-file example:
27!> \code
28!> solver 2
29!> solve_maxiters 300
30!> method 1
31!> coarse_method 1
32!> levels  2
33!> overlap -1
34!> smoothers 0
35!> input_type 2
36!> symmstruct T
37!> symmnumeric T
38!> # ###################
39!> # aggregate level 1:
40!> radius1 5
41!> strong1 0.67e0
42!> minasize1 2
43!> #maxasize1 19
44!> # aggregate level 2:
45!> radius2 35
46!> strong2 0.67e0
47!> minasize2 2
48!> #maxasize2 96
49!> # ###################
50!> matrix_type 1
51!> number_of_blocks 1
52!> initial_guess 2
53!> start_vec_file ./NOT.DEFINED.start_vec_file
54!> start_vec_type 2
55!> solve_tolerance 1.0e-12
56!> solution_format 2
57!> solution_file ./solution.file
58!> #debug -5
59!> debug 0
60!> verbose 0
61!> plotting 1
62!> assembled_mtx_file Hetero32.txt
63!> \endcode
64program main_aggr
65
66  use doug
67  use Distribution_mod
68  use Partitioning_mod
69  use Preconditioner_mod
70  use FinePreconditioner_complete_mod
71  use Mesh_class
72  use Mesh_plot_mod
73  use SpMtx_mods
74  use Vect_mod
75  use DenseMtx_mod
76  use solvers_mod
77  use Aggregate_mod
78  use Aggregate_utils_mod
79  use CoarseMtx_mod
80  use CoarseAllgathers
81  use RobustCoarseMtx_mod
82  use pcgRobust_mod
83
84  implicit none
85
86#include<doug_config.h>
87
88#ifdef D_COMPLEX
89#define float complex
90#else
91#define float real
92#endif
93
94  type(SpMtx)    :: AC  !< coarse matrix
95  type(SpMtx)    :: Restrict !< Restriction matrix (for operation)
96  type(SumOfInversedSubMtx) :: B_RCS !< B matrix for the Robust Coarse Spaces
97  !type(SpMtx)    :: Rest_cmb !< Restriction matrix (for coarse matrix build)
98
99  float(kind=rk), dimension(:), pointer :: xl !< local solution vector
100  float(kind=rk), dimension(:), pointer :: x  !< global solution on master
101  float(kind=rk), dimension(:), pointer :: sol, rhs  !< for testing solver
102  real(kind=rk), pointer :: rhs_1(:), g(:)
103
104  ! Partitioning
105  integer               :: nparts !< number of partitons to partition a mesh
106  integer, dimension(6) :: part_opts = (/0,0,0,0,0,0/)
107
108  type(ConvInf) :: resStat
109
110  real(kind=rk) :: t,t1
111  integer :: i,j,k,it,ol
112  float(kind=rk), dimension(:), pointer :: r, y
113  character :: str
114  character(len=40) :: frm
115  float(kind=rk) :: cond_num,nrm
116  integer,allocatable :: nodes(:), inds(:)
117
118  type(Distribution) :: D !< mesh and matrix distribution
119  type(Partitionings) :: P !< fine and coarse aggregates
120  type(FinePreconditioner) :: FP
121  type(CoarsePreconditioner) :: CP
122  type(RobustPreconditionMtx) :: C
123  type(CoarseSpace) :: CS
124  integer,pointer :: aggrnum(:)
125  integer :: nagr
126  ! Parallel coarse level
127  !type(CoarseData) :: CP%cdat --
128
129  ! Init DOUG
130  call DOUG_Init()
131
132  ! Master participates in calculations as well
133  nparts = numprocs
134
135  D = Distribution_NewInit(sctls%input_type,nparts,part_opts)
136
137  if (sctls%levels>1.or.(numprocs==1.and.sctls%levels==1)) then !todo remove
138    P = Partitionings_New()
139    call Partitionings_CreateFine(P,D)
140    ! profile info
141    if(pstream/=0) then
142      write(pstream, "(I0,':fine aggregates:',I0)") myrank, P%fAggr%inner%nagr
143    end if
144
145    !if (sctls%plotting>=2) then
146    !   call SpMtx_writeLogicalValues(A, D%A%strong, 'strong.txt')
147    !end if
148
149    call Mesh_printInfo(D%mesh)
150   
151    if (numprocs==1.and.sctls%plotting==2.and.D%mesh%nell>0) then
152      call Mesh_pl2D_plotAggregate(P%fAggr%inner,D%mesh,&
153                      D%A%strong_rowstart,D%A%strong_colnrs,&
154                      mctls%assembled_mtx_file, &
155                                 INIT_CONT_END=D_PLPLOT_INIT)
156                                 !D_PLPLOT_END)
157    endif
158   
159    ! Testing coarse matrix and aggregation through it:
160    if (numprocs>1) then
161      allocate(aggrnum(D%mesh%nlf))
162      nagr = P%fAggr%inner%nagr
163      aggrnum = 0
164      aggrnum(1:D%mesh%ninner) = P%fAggr%inner%num
165      call setup_aggr_cdat(CP%cdat, CP%cdat_vec, nagr, D%mesh%ninner,aggrnum,D%mesh)
166     
167      call SpMtx_find_strong(A=D%A,alpha=P%strong_conn1,A_ghost=D%A_ghost)
168      call SpMtx_exchange_strong(D%A,D%A_ghost,D%mesh)
169      call SpMtx_symm_strong(D%A,D%A_ghost,.false.)
170      call SpMtx_unscale(D%A)
171
172      call IntRestBuild(D%A,P%fAggr%inner,Restrict,D%A_ghost)
173      CS = CoarseSpace_Init(Restrict)
174      call CoarseData_Copy(CP%cdat,CP%cdat_vec)
175      call CoarseSpace_Expand(CS,Restrict,D%mesh,CP%cdat)
176      call CoarseMtxBuild(D%A,CP%cdat%LAC,Restrict,D%mesh%ninner,D%A_ghost)
177      call KeepGivenRowIndeces(Restrict, (/(i,i=1,P%fAggr%inner%nagr)/))
178
179      if (sctls%verbose>3.and.CP%cdat%LAC%nnz<400) then
180        write(stream,*)'Restrict (local) is:=================='
181        call SpMtx_printRaw(A=Restrict)
182        write(stream,*)'A coarse (local) is:=================='
183        call SpMtx_printRaw(A=CP%cdat%LAC)
184      endif
185
186    else
187      ! non-parallel
188      call IntRestBuild(D%A,P%fAggr%inner,Restrict)
189
190      if (sctls%coarse_method<=1) then ! if not specified or ==1
191         call CoarseMtxBuild(D%A,AC,Restrict,D%mesh%ninner)
192
193      else if (sctls%coarse_method==2) then
194         ! use the Robust Coarse Spaces algorithm
195         B_RCS = CoarseProjectionMtxsBuild(D%A,Restrict,P%fAggr%inner%nagr)
196         allocate(rhs_1(D%A%nrows))
197         allocate(g(D%A%ncols))
198
199         rhs_1 = 1.0
200         call pcg_forRCS(B_RCS,rhs_1,g)
201
202         if(sctls%verbose>5) then
203            write (stream, *) "Solution g = ", g
204         end if
205
206         call RobustRestrictMtxBuild(B_RCS,g,Restrict)
207         call CoarseMtxBuild(D%A,AC,Restrict,D%mesh%ninner)
208
209      else
210         write(stream,'(A," ",I2)') 'Wrong coarse method', sctls%coarse_method
211         call DOUG_abort('Error in aggr', -1)
212      endif
213           
214      if (sctls%verbose>3.and.AC%nnz<400) then
215        write(stream,*)'A coarse is:=================='
216        call SpMtx_printRaw(A=AC)
217      endif
218    endif
219   
220    ! coarse aggregates
221    if (numprocs==1) then
222      call Partitionings_CreateCoarse(P,D,AC)
223      !call Aggrs_readFile_coarse(P%cAggr, "aggregates.txt")
224
225      ! profile info
226      if(pstream/=0) then
227        write(pstream, "(I0,':coarse aggregates:',I0)") myrank, P%cAggr%inner%nagr
228      end if
229
230      if (sctls%plotting==2) then
231         call Aggr_writeFile(P%fAggr%inner, 'aggr2.txt', P%cAggr%inner)
232      end if
233      if (sctls%plotting==2.and.D%mesh%nell>0) then
234        !print *,'press Key<Enter>'
235        !read *,str
236        call Mesh_pl2D_plotAggregate(P%fAggr%inner,D%mesh,&
237                        D%A%strong_rowstart,D%A%strong_colnrs,&
238                        mctls%assembled_mtx_file, &
239                        caggrnum=P%cAggr%inner%num, &
240                      INIT_CONT_END=D_PLPLOT_END)!, &
241                      !INIT_CONT_END=D_PLPLOT_CONT)!, &
242                                  ! D_PLPLOT_END)
243      endif
244      write(stream,*)'# coarse aggregates:',P%cAggr%inner%nagr
245
246    endif
247  endif
248
249  ! overlap for subdomains
250  if (sctls%overlap<0) then ! autom. overlap from smoothing
251    ol = max(sctls%smoothers,0)
252  else
253    ol = sctls%overlap
254  endif
255
256  FP = FinePreconditioner_New(D)
257  if (numprocs==1) then
258    call FinePreconditioner_InitAggrs(FP, D, P, ol)
259    call FinePreconditioner_complete_Init(FP)
260    call AggrInfo_Destroy(P%cAggr)
261    call AggrInfo_Destroy(P%fAggr)
262  else
263    call FinePreconditioner_InitFull(FP, D, ol)
264    call FinePreconditioner_complete_Init(FP)
265    ! call Aggrs_writeFile(M, P%fAggr, CP%cdat, "aggregates.txt")
266    if (sctls%levels>1) call AggrInfo_Destroy(P%fAggr)
267  end if
268
269  ! Testing UMFPACK:
270  allocate(sol(D%A%nrows))
271  allocate(rhs(D%A%ncols))
272
273  ! Solve the system
274  allocate(xl(D%mesh%nlf))
275  xl = 0.0_rk
276
277  select case(sctls%solver)
278  case (DCTL_SOLVE_CG)
279     ! Conjugate gradient
280     call cg(D%A, D%rhs, xl, D%mesh, solinf=resStat, resvects_=.true.)
281     !call cg(A, b, xl, M, solinf=resStat)
282  case (DCTL_SOLVE_PCG)
283     ! Preconditioned conjugate gradient
284     t1 = MPI_WTIME()
285     if (sctls%levels==2) then
286       write(stream,*)'calling pcg_weigs with coarse matrix'
287       call pcg_weigs(A=D%A,b=D%rhs,x=xl,Msh=D%mesh,finePrec=FP,coarsePrec=CP,it=it,cond_num=cond_num, &
288            A_interf_=D%A_ghost, &
289            CoarseMtx_=AC,Restrict=Restrict, &
290            refactor_=.true.)
291     else
292       write(stream,*)'calling pcg_weigs'
293       call pcg_weigs(D%A, D%rhs, xl, D%mesh,FP,CP,it,cond_num, &
294            A_interf_=D%A_ghost, refactor_=.true.)
295     endif
296     t=MPI_WTIME()-t1
297     write(stream,*) 'time spent in pcg():',t
298     t1=total_setup_time()
299     write(stream,*) '    ...of which setup:',t1
300     write(stream,*) '       ...of which factorisation:', &
301        total_factorisation_time()
302     write(stream,*) 'solve time without setup:',t-t1
303  case default
304     call DOUG_abort('[DOUG main] : Wrong solution method specified', -1)
305  end select
306
307  if (numprocs>1) then
308    ! Assemble result on master
309    if (ismaster()) then
310       print *, "freedoms", D%mesh%ngf
311      allocate(x(D%mesh%ngf)); x = 0.0_rk
312    end if
313    call Vect_Gather(xl, x, D%mesh)
314    if (ismaster().and.sctls%verbose>2.and.(size(x) <= 100)) &
315      call Vect_Print(x, 'sol > ')
316    if (ismaster()) then
317       call WriteSolutionToFile(x)
318    end if
319
320  else
321    if (sctls%verbose>2.and.size(xl)<=100) then
322      call Vect_Print(xl, 'sol > ')
323    endif
324    call WriteSolutionToFile(xl)
325  endif
326
327
328  ! Destroy objects
329  call Mesh_Destroy(D%mesh)
330  call SpMtx_Destroy(D%A)
331  call ConvInf_Destroy(resStat)
332  if (associated(xl)) deallocate(xl)
333  if (associated(x)) deallocate(x)
334  if (associated(sol)) deallocate(sol)
335
336  call DOUG_Finalize()
337
338
339end program main_aggr
Note: See TracBrowser for help on using the repository browser.