Introduction
User subroutine for the definition of a user module. The following example illustrates the creation of user arrays called rwork
and iwork
.
Arrays declaration
The first step is to declare these two arrays at the very beginning of the module. The first one iwork
is an allocatable array of one dimension whereas rwork
is a pointer to a bidimensionnal array.
integer, dimension(:), allocatable :: iwork
double precision, dimension(:,:), pointer :: rwork => null()
Arrays allocation
The following code allocates rwork
and iwork
if they are not already allocated or associated (for rwork
).
subroutine init_user_module(ncel, ncelet)
integer, intent(in) :: ncel, ncelet
integer :: err = 0
if (.not.allocated(iwork)) then
allocate(iwork(ncelet), stat=err)
endif
if (err .eq. 0 .and. .not.associated(rwork)) then
allocate(rwork(3, ncelet), stat=err)
endif
if (err /= 0) then
write (*, *) "Error allocating array."
endif
return
end subroutine init_user_module
void csexit(const int *status)
Definition: cs_base_fortran.c:240
Access to arrays in C
It is possible to access the rwork
array in the C part of the code. This can be done by using the get_user_module_rwork
subroutine in the following example:
function get_user_module_rwork() result(r) &
bind(c, name='get_user_module_rwork')
use, intrinsic :: iso_c_binding
implicit none
type(c_ptr) :: r
if (associated(rwork)) then
r = c_loc(rwork(1,1))
else
r = c_null_ptr
endif
end function get_user_module_rwork
Freeing arrays
Finally, the finalize_user_module
subroutine is used to free iwork
and rwork
.
subroutine finalize_user_module
if (allocated(iwork)) then
deallocate(iwork)
endif
if (associated(rwork)) then
deallocate(rwork)
endif
end subroutine finalize_user_module