Create and copy multi-dimensional arraysΒΆ

This example focuses on creating multi-dimensional arrays, using complex type and copying IDS structures.

See also

API documentation for ids_validate()

subroutine copying_and_validating_ids()
    use ids_routines
    implicit none

    ! create empty gyrokinetics_local
    ! NOTE: gyrokinetics_local is an alpha IDS
    type(ids_gyrokinetics_local) :: gyrokinetics_local
    type(ids_gyrokinetics_local) :: gyrokinetics_local_copy
    character(:), allocatable :: err_msg
    integer :: status

    ! set mandatory field
    gyrokinetics_local%ids_properties%homogeneous_time = IDS_TIME_MODE_HOMOGENEOUS
    allocate(gyrokinetics_local%time(3))
    gyrokinetics_local%time = (/ 1.0, 2.0, 3.0 /)

    ! some IDS fields contain multi-dimensional arrays
    allocate(gyrokinetics_local%non_linear%fields_zonal_2d%phi_potential_perturbed_norm(3,3))
    gyrokinetics_local%non_linear%fields_zonal_2d%phi_potential_perturbed_norm = &
        reshape([cmplx(1.0, 1.0), cmplx(2.0, 2.0), cmplx(3.0, 3.0), &
                 cmplx(4.0, 4.0), cmplx(5.0, 5.0), cmplx(6.0, 6.0), &
                 cmplx(7.0, 7.0), cmplx(8.0, 8.0), cmplx(9.0, 9.0)], &
                 [3, 3])

    write(*,*) 'Filled 2D array (gyrokinetics_local/non_linear/fields_zonal_2d/phi_potential_perturbed_norm):'
    write(*,'(3(f0.1,"+",f0.1,"i",TR1))') gyrokinetics_local%non_linear%fields_zonal_2d%phi_potential_perturbed_norm

    ! some fields have coordinates consistency. <isd>.validate() method checks for this consistency.
    ! example of field of this type is gyrokinetics_local/non_linear/fields_zonal_2d/phi_potential_perturbed_norm
    ! it's first dimension size must be equal to non_linear/radial_wavevector_norm size and second dimension size equal to non_linear/time_norm
    
    call ids_validate(gyrokinetics_local, status, err_msg)
    if (status /= 0) then
        write (*,*) 'IDS validation failed (intentionally), error message: ', err_msg
    end if

    ! to fix this
    allocate(gyrokinetics_local%non_linear%radial_wavevector_norm(3))
    allocate(gyrokinetics_local%non_linear%time_norm(3))
    gyrokinetics_local%non_linear%radial_wavevector_norm = (/ 1.0, 2.0, 3.0 /)
    gyrokinetics_local%non_linear%time_norm              = (/ 1.0, 2.0, 3.0 /)

    call ids_validate(gyrokinetics_local, status, err_msg)
    if (status /= 0) then
        write (*,*) 'IDS validation failed , error message: ', err_msg
    else
        write (*,*) 'IDS validation passed successfully'
    end if

    ! IDSs can be copied using ids_copy subroutine
    ! gyrokinetics_local/linear.wavevector(i1)/eigenmode(i2)/fields.phi_potential_perturbed_norm has two dimensions and stores complex numbers

    allocate(gyrokinetics_local%linear%wavevector(1))
    allocate(gyrokinetics_local%linear%wavevector(1)%eigenmode(1))
    allocate(gyrokinetics_local%linear%wavevector(1)%eigenmode(1)%fields%phi_potential_perturbed_norm(3,3))
    gyrokinetics_local%linear%wavevector(1)%eigenmode(1)%fields%phi_potential_perturbed_norm = &
        reshape([cmplx(1.0, 1.0), cmplx(2.0, 2.0), cmplx(3.0, 3.0), &
                 cmplx(4.0, 4.0), cmplx(5.0, 5.0), cmplx(6.0, 6.0), &
                 cmplx(7.0, 7.0), cmplx(8.0, 8.0), cmplx(9.0, 9.0)], &
                 [3, 3])

    ! right way to copy IDS
    call ids_copy(gyrokinetics_local, gyrokinetics_local_copy)


    gyrokinetics_local_copy%linear%wavevector(1)%eigenmode(1)%fields%phi_potential_perturbed_norm = &
        reshape([cmplx(11.0, 11.0), cmplx(12.0, 12.0), cmplx(13.0, 13.0), &
                 cmplx(14.0, 14.0), cmplx(15.0, 15.0), cmplx(16.0, 16.0), &
                 cmplx(17.0, 17.0), cmplx(18.0, 18.0), cmplx(19.0, 19.0)], &
                 [3, 3])
    gyrokinetics_local%linear%wavevector(1)%eigenmode(1)%fields%phi_potential_perturbed_norm(1,1) = cmplx(21,37)

    write(*,*) 'Original value:'
    write(*,'(3(F0.1,"+",F0.1,"i",TR1))') &
     gyrokinetics_local%linear%wavevector(1)%eigenmode(1)%fields%phi_potential_perturbed_norm

    write(*,*) 'Copied value:'
    write(*,'(3(F0.1,"+",F0.1,"i",TR1))') &
     gyrokinetics_local_copy%linear%wavevector(1)%eigenmode(1)%fields%phi_potential_perturbed_norm

    call ids_deallocate(gyrokinetics_local)
    call ids_deallocate(gyrokinetics_local_copy)
    
end subroutine copying_and_validating_ids