Как сохранить значение (из каждой отдельной итерации) члена производного типа? - PullRequest
0 голосов
/ 24 апреля 2019

Я не опытный программист на фортране, поэтому мне нужна помощь по поводу моего простого кода. Мой код:

module derived_type

implicit none

type :: iter_type

  integer :: calc_tmp
  integer :: n_iter

  contains

    procedure :: calc_iter     => calc_iter_process
    procedure :: take_calc_tmp => take_data_calc_tmp
    procedure :: take_n_iter   => take_data_n_iter

end type iter_type

private :: calc_iter_process
private :: take_data_calc_tmp
private :: take_data_n_iter

contains

function calc_iter_process( this, indx_00 ) result( err_tmp )

  class( iter_type )    :: this
  integer, intent( in ) :: indx_00
  logical               :: err_tmp

  err_tmp = .false.

  this%n_iter = 0

  this%calc_tmp = 1

  do while( this%calc_tmp < indx_00 )

    this%n_iter = this%n_iter + 1

    if ( this%n_iter > 50 ) then

      write(*,*) "error - maximal number of iterations !!!"
      err_tmp = .true.
      exit

    end if

    this%calc_tmp = this%calc_tmp + 1

  end do

end function calc_iter_process

function take_data_calc_tmp( this ) result( data_tmp )

  class( iter_type ) :: this
  integer            :: data_tmp

  data_tmp = this%calc_tmp

end function take_data_calc_tmp

function take_data_n_iter( this ) result( data_tmp )

  class( iter_type ) :: this
  integer            :: data_tmp

  data_tmp = this%n_iter

end function take_data_n_iter

end module derived_type

program iteration_values

use, non_intrinsic :: derived_type

implicit none

integer, parameter :: number_00 = 32
logical            :: global_err

type( iter_type ) :: iter_object

global_err = iter_object%calc_iter( number_00 )

if ( global_err ) stop "error - global !!!"

end program iteration_values

Мне нужно найти способ для модификации кода, который может дать мне возможность сохранять или сохранять значение «calc_tmp» в каждой отдельной итерации. Когда я думаю об этом, я не могу себе представить, как выделить или освободить некоторый массив, который должен иметь размерность, равную или превышающую «n_iter». Есть ли способ сделать это?

1 Ответ

0 голосов
/ 28 апреля 2019

Я бы порекомендовал использовать атрибуты allocatable и move_alloc. Вот пример программы. move_alloc - это Fortran 2003. В этом примере я увеличиваю размер массива при каждом превышении его размера.

program temp

implicit none

integer, dimension(:), allocatable :: tempval, calc_tmp_history
integer :: i, j, calc_tmp, totalSize

totalSize = 0
allocate(calc_tmp_history(2))
do i = 1,4
    calc_tmp = 2*i
    if (i > size(calc_tmp_history)) then
        call move_alloc(calc_tmp_history,tempval)
        allocate(calc_tmp_history(2*i))
        do j = 1,i
            calc_tmp_history(j) = tempval(j)
        end do
    end if
    calc_tmp_history(i) = calc_tmp
    totalSize = totalSize + 1
end do

do i = 1,totalSize
    print *, calc_tmp_history(i)
end do
end program

Выходные данные:

2
4
6
8
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...