3 ! Solve a diffusion problem using an object-oriented approach
5 ! Author: Arjen Markus (comp.lang.fortran)
6 ! This version: pault@gcc.gnu.org
9 ! (i) This could be turned into a more sophisticated program
10 ! using the techniques described in the chapter on
11 ! mathematical abstractions.
12 ! (That would allow the selection of the time integration
13 ! method in a transparent way)
15 ! (ii) The target procedures for process_p and source_p are
16 ! different to the typebound procedures for dynamic types
17 ! because the passed argument is not type(base_pde_object).
19 ! (iii) Two solutions are calculated, one with the procedure
20 ! pointers and the other with typebound procedures. The sums
21 ! of the solutions are compared.
23 ! (iv) The source is a delta function in the middle of the
24 ! mesh, whilst the process is quartic in the local value,
25 ! when it is positive.
28 ! Module to define the basic objects
30 module base_pde_objects
32 type, abstract :: base_pde_object
34 procedure(process_p), pointer, pass :: process_p
35 procedure(source_p), pointer, pass :: source_p
37 procedure(process), deferred :: process
38 procedure(source), deferred :: source
39 procedure :: initialise
42 procedure(real_times_obj), pass(obj), deferred :: real_times_obj
43 procedure(obj_plus_obj), deferred :: obj_plus_obj
44 procedure(obj_assign_obj), deferred :: obj_assign_obj
45 generic :: operator(*) => real_times_obj
46 generic :: operator(+) => obj_plus_obj
47 generic :: assignment(=) => obj_assign_obj
50 function process_p (obj)
51 import base_pde_object
52 class(base_pde_object), intent(in) :: obj
53 class(base_pde_object), allocatable :: process_p
54 end function process_p
57 function source_p (obj, time)
58 import base_pde_object
59 class(base_pde_object), intent(in) :: obj
60 real, intent(in) :: time
61 class(base_pde_object), allocatable :: source_p
65 function process (obj)
66 import base_pde_object
67 class(base_pde_object), intent(in) :: obj
68 class(base_pde_object), allocatable :: process
72 function source (obj, time)
73 import base_pde_object
74 class(base_pde_object), intent(in) :: obj
75 real, intent(in) :: time
76 class(base_pde_object), allocatable :: source
80 function real_times_obj (factor, obj) result(newobj)
81 import base_pde_object
82 real, intent(in) :: factor
83 class(base_pde_object), intent(in) :: obj
84 class(base_pde_object), allocatable :: newobj
85 end function real_times_obj
88 function obj_plus_obj (obj1, obj2) result(newobj)
89 import base_pde_object
90 class(base_pde_object), intent(in) :: obj1
91 class(base_pde_object), intent(in) :: obj2
92 class(base_pde_object), allocatable :: newobj
93 end function obj_plus_obj
96 subroutine obj_assign_obj (obj1, obj2)
97 import base_pde_object
98 class(base_pde_object), intent(inout) :: obj1
99 class(base_pde_object), intent(in) :: obj2
100 end subroutine obj_assign_obj
104 ! Print the concentration field
105 subroutine print (obj)
106 class(base_pde_object) :: obj
110 ! Initialise the concentration field using a specific function
111 subroutine initialise (obj, funcxy)
112 class(base_pde_object) :: obj
114 real function funcxy (coords)
115 real, dimension(:), intent(in) :: coords
119 end subroutine initialise
121 ! Determine the divergence
122 function nabla2 (obj)
123 class(base_pde_object), intent(in) :: obj
124 class(base_pde_object), allocatable :: nabla2
127 end module base_pde_objects
128 ! cartesian_2d_objects --
129 ! PDE object on a 2D cartesian grid
131 module cartesian_2d_objects
134 type, extends(base_pde_object) :: cartesian_2d_object
135 real, dimension(:,:), allocatable :: c
139 procedure :: process => process_cart2d
140 procedure :: source => source_cart2d
141 procedure :: initialise => initialise_cart2d
142 procedure :: nabla2 => nabla2_cart2d
143 procedure :: print => print_cart2d
144 procedure, pass(obj) :: real_times_obj => real_times_cart2d
145 procedure :: obj_plus_obj => obj_plus_cart2d
146 procedure :: obj_assign_obj => obj_assign_cart2d
147 end type cartesian_2d_object
148 interface grid_definition
149 module procedure grid_definition_cart2d
152 function process_cart2d (obj)
153 class(cartesian_2d_object), intent(in) :: obj
154 class(base_pde_object), allocatable :: process_cart2d
155 allocate (process_cart2d,source = obj)
156 select type (process_cart2d)
157 type is (cartesian_2d_object)
158 process_cart2d%c = -sign (obj%c, 1.0)*obj%c** 4
162 end function process_cart2d
163 function process_cart2d_p (obj)
164 class(base_pde_object), intent(in) :: obj
165 class(base_pde_object), allocatable :: process_cart2d_p
166 allocate (process_cart2d_p,source = obj)
167 select type (process_cart2d_p)
168 type is (cartesian_2d_object)
170 type is (cartesian_2d_object)
171 process_cart2d_p%c = -sign (obj%c, 1.0)*obj%c** 4
176 end function process_cart2d_p
177 function source_cart2d (obj, time)
178 class(cartesian_2d_object), intent(in) :: obj
179 real, intent(in) :: time
180 class(base_pde_object), allocatable :: source_cart2d
184 allocate (source_cart2d, source = obj)
185 select type (source_cart2d)
186 type is (cartesian_2d_object)
187 if (allocated (source_cart2d%c)) deallocate (source_cart2d%c)
188 allocate (source_cart2d%c(m, n))
189 source_cart2d%c = 0.0
190 if (time .lt. 5.0) source_cart2d%c(m/2, n/2) = 0.1
194 end function source_cart2d
196 function source_cart2d_p (obj, time)
197 class(base_pde_object), intent(in) :: obj
198 real, intent(in) :: time
199 class(base_pde_object), allocatable :: source_cart2d_p
202 type is (cartesian_2d_object)
208 allocate (source_cart2d_p,source = obj)
209 select type (source_cart2d_p)
210 type is (cartesian_2d_object)
211 if (allocated (source_cart2d_p%c)) deallocate (source_cart2d_p%c)
212 allocate (source_cart2d_p%c(m,n))
213 source_cart2d_p%c = 0.0
214 if (time .lt. 5.0) source_cart2d_p%c(m/2, n/2) = 0.1
218 end function source_cart2d_p
221 ! Initialises the grid
223 subroutine grid_definition_cart2d (obj, sizes, dims)
224 class(base_pde_object), allocatable :: obj
225 real, dimension(:) :: sizes
226 integer, dimension(:) :: dims
227 allocate( cartesian_2d_object :: obj )
229 type is (cartesian_2d_object)
230 allocate (obj%c(dims(1), dims(2)))
232 obj%dx = sizes(1)/dims(1)
233 obj%dy = sizes(2)/dims(2)
237 end subroutine grid_definition_cart2d
239 ! Print the concentration field to the screen
241 subroutine print_cart2d (obj)
242 class(cartesian_2d_object) :: obj
243 character(len=20) :: format
244 write( format, '(a,i0,a)' ) '(', size(obj%c,1), 'f6.3)'
245 write( *, format ) obj%c
246 end subroutine print_cart2d
247 ! initialise_cart2d --
248 ! Initialise the concentration field using a specific function
250 subroutine initialise_cart2d (obj, funcxy)
251 class(cartesian_2d_object) :: obj
253 real function funcxy (coords)
254 real, dimension(:), intent(in) :: coords
258 real, dimension(2) :: x
260 do j = 2,size (obj%c, 2)-1
261 x(2) = obj%dy * (j-1)
262 do i = 2,size (obj%c, 1)-1
263 x(1) = obj%dx * (i-1)
264 obj%c(i,j) = funcxy (x)
267 end subroutine initialise_cart2d
269 ! Determine the divergence
270 function nabla2_cart2d (obj)
271 class(cartesian_2d_object), intent(in) :: obj
272 class(base_pde_object), allocatable :: nabla2_cart2d
279 allocate (cartesian_2d_object :: nabla2_cart2d)
280 select type (nabla2_cart2d)
281 type is (cartesian_2d_object)
282 allocate (nabla2_cart2d%c(m,n))
283 nabla2_cart2d%c = 0.0
284 nabla2_cart2d%c(2:m-1,2:n-1) = &
285 -(2.0 * obj%c(2:m-1,2:n-1) - obj%c(1:m-2,2:n-1) - obj%c(3:m,2:n-1)) / dx**2 &
286 -(2.0 * obj%c(2:m-1,2:n-1) - obj%c(2:m-1,1:n-2) - obj%c(2:m-1,3:n)) / dy**2
290 end function nabla2_cart2d
291 function real_times_cart2d (factor, obj) result(newobj)
292 real, intent(in) :: factor
293 class(cartesian_2d_object), intent(in) :: obj
294 class(base_pde_object), allocatable :: newobj
298 allocate (cartesian_2d_object :: newobj)
300 type is (cartesian_2d_object)
301 allocate (newobj%c(m,n))
302 newobj%c = factor * obj%c
306 end function real_times_cart2d
307 function obj_plus_cart2d (obj1, obj2) result( newobj )
308 class(cartesian_2d_object), intent(in) :: obj1
309 class(base_pde_object), intent(in) :: obj2
310 class(base_pde_object), allocatable :: newobj
314 allocate (cartesian_2d_object :: newobj)
316 type is (cartesian_2d_object)
317 allocate (newobj%c(m,n))
319 type is (cartesian_2d_object)
320 newobj%c = obj1%c + obj2%c
327 end function obj_plus_cart2d
328 subroutine obj_assign_cart2d (obj1, obj2)
329 class(cartesian_2d_object), intent(inout) :: obj1
330 class(base_pde_object), intent(in) :: obj2
332 type is (cartesian_2d_object)
337 end subroutine obj_assign_cart2d
338 end module cartesian_2d_objects
339 ! define_pde_objects --
340 ! Module to bring all the PDE object types together
342 module define_pde_objects
344 use cartesian_2d_objects
346 interface grid_definition
347 module procedure grid_definition_general
350 subroutine grid_definition_general (obj, type, sizes, dims)
351 class(base_pde_object), allocatable :: obj
352 character(len=*) :: type
353 real, dimension(:) :: sizes
354 integer, dimension(:) :: dims
356 case ("cartesian 2d")
357 call grid_definition (obj, sizes, dims)
359 write(*,*) 'Unknown grid type: ', trim (type)
362 end subroutine grid_definition_general
363 end module define_pde_objects
365 ! Module holding the routines specific to the PDE that
371 real function patch (coords)
372 real, dimension(:), intent(in) :: coords
373 if (sum ((coords-[50.0,50.0])**2) < 40.0) then
379 end module pde_specific
381 ! Small test program to demonstrate the usage
383 program test_pde_solver
384 use define_pde_objects
387 class(base_pde_object), allocatable :: solution, deriv
389 real :: time, dtime, diff, chksum(2)
391 call simulation1 ! Use proc pointers for source and process define_pde_objects
392 select type (solution)
393 type is (cartesian_2d_object)
394 deallocate (solution%c)
397 type is (cartesian_2d_object)
400 deallocate (solution, deriv)
402 call simulation2 ! Use typebound procedures for source and process
403 if (chksum(1) .ne. chksum(2)) call abort
404 if ((chksum(1) - 0.881868720)**2 > 1e-4) call abort
406 subroutine simulation1
410 call grid_definition (solution, "cartesian 2d", [100.0, 100.0], [16, 16])
411 call grid_definition (deriv, "cartesian 2d", [100.0, 100.0], [16, 16])
413 ! Initialise the concentration field
415 call solution%initialise (patch)
417 ! Set the procedure pointers
419 solution%source_p => source_cart2d_p
420 solution%process_p => process_cart2d_p
422 ! Perform the integration - explicit method
428 ! Give the diffusion coefficient correct dimensions.
429 select type (solution)
430 type is (cartesian_2d_object)
431 diff = diff * solution%dx * solution%dy / dtime
434 ! write(*,*) 'Time: ', time, diff
435 ! call solution%print
437 deriv = solution%nabla2 ()
438 solution = solution + diff * dtime * deriv + solution%source_p (time) + solution%process_p ()
439 ! if ( mod(i, 25) == 0 ) then
440 ! write(*,*)'Time: ', time
441 ! call solution%print
445 ! write(*,*) 'End result 1: '
446 ! call solution%print
447 select type (solution)
448 type is (cartesian_2d_object)
449 chksum(1) = sum (solution%c)
452 subroutine simulation2
456 call grid_definition (solution, "cartesian 2d", [100.0, 100.0], [16, 16])
457 call grid_definition (deriv, "cartesian 2d", [100.0, 100.0], [16, 16])
459 ! Initialise the concentration field
461 call solution%initialise (patch)
463 ! Set the procedure pointers
465 solution%source_p => source_cart2d_p
466 solution%process_p => process_cart2d_p
468 ! Perform the integration - explicit method
474 ! Give the diffusion coefficient correct dimensions.
475 select type (solution)
476 type is (cartesian_2d_object)
477 diff = diff * solution%dx * solution%dy / dtime
480 ! write(*,*) 'Time: ', time, diff
481 ! call solution%print
483 deriv = solution%nabla2 ()
484 solution = solution + diff * dtime * deriv + solution%source (time) + solution%process ()
485 ! if ( mod(i, 25) == 0 ) then
486 ! write(*,*)'Time: ', time
487 ! call solution%print
491 ! write(*,*) 'End result 2: '
492 ! call solution%print
493 select type (solution)
494 type is (cartesian_2d_object)
495 chksum(2) = sum (solution%c)
498 end program test_pde_solver
499 ! { dg-final { cleanup-modules "pde_specific define_pde_objects cartesian_2d_objects base_pde_objects" } }