Fortran 90/95 Pointers in Derived Type -
i have derived type pointer array of sec derived type
type vertex real :: x, y, z end type type path type(vertex), dimension(:), pointer :: vertices => null() end type
the intent create vertices array resizable number of vertex points can added array. have created code append vertex pointer.
subroutine path_append_vertex(this, x, y, z) implicit none !------------------------------------------------------------------------------- ! variable declarations. !------------------------------------------------------------------------------- type(path), intent(inout) :: real, intent(in) :: x, y, z !------------------------------------------------------------------------------- ! local variable declarations. !------------------------------------------------------------------------------- integer :: status type(vertex), dimension(:), allocatable :: vertices !------------------------------------------------------------------------------- ! start of executable code !------------------------------------------------------------------------------- if (associated(this%vertices)) ! create temporary array same size current number of vertices. re-create ! contents of old array new array delete old array. allocate(vertices(size(this%vertices)), stat = status) phone call check_status(status) vertices = this%vertices deallocate(this%vertices) ! create new array 1 element. re-create contents of temporary ! array new 1 delete temporary array. allocate(this%vertices(size(vertices) + 1), stat = status) phone call check_status(status) this%vertices(1:size(vertices)) = vertices deallocate(vertices) this%vertices(size(this%vertices))%x = x this%vertices(size(this%vertices))%y = y this%vertices(size(this%vertices))%z = z else allocate(this%vertices(1), stat = status) phone call check_status(status) this%vertices(1)%x = x this%vertices(1)%y = y this%vertices(1)%z = z endif end subroutine
i create few path objects.
type ipch_desc ... type(path) :: chordpath end type subroutine ipch_desc_construct(this, ...) ... type (ipch_desc), intent(inout) :: ... ! must null out vertices array or else point lastly ! integration_path created in memory. not sure why these defaulting ! null this%chordpath%vertices => null() phone call path_append_vertex(this%chordpath, xcart_i(1), xcart_i(2), xcart_i(3)) phone call path_append_vertex(this%chordpath, xcart_f(1), xcart_f(2), xcart_f(3)) ! check value of path vertices. write(*,*) this%chordpath%vertices end subroutine
everything work , , right values each vertex. instance 3 path objects created get
-0.33808113528699218 1.0467574437103653 0.10713720000000000 -0.16057879084545851 0.49717960298733294 0.10713720000000000 -0.33322243268266594 1.0483142707971911 1.42240000000000010e-003 -0.14945358419461796 0.47017940500485894 1.42240000000000010e-003 -0.33656460666251325 1.0472460386853264 -0.10629900000000000 -0.15821659220752302 0.49230280357365630 -0.10629900000000000
when using these path objects latter in code,
subroutine ipch_mc_model_compute(a_ipch, ...) ... type (ipch_desc), intent (inout) :: a_ipch ... ! check value of path vertices again. write(*,*) a_ipch%chordpath%vertices ... end subroutine
only first n-1 values remain correct. same values created above get,
-0.33808113528699218 1.0467574437103653 0.10713720000000000 -0.16057879084545851 0.49717960298733294 0.10713720000000000 -0.33322243268266594 1.0483142707971911 1.42240000000000010e-003 -0.14945358419461796 0.47017940500485894 1.42240000000000010e-003 0.15094203233057696 6.94277920927416864e-310 -0.10629900000000000 1.63041663127611360e-322 3.01884064661153912e-003 6.94277920927179713e-310
regardless of number of path
objects create, nth ends wrong values. causing this?
your code seems correct. simplify bit. why have derived type path contain single variable? straight resize array of type vertex without additional type. also, see no reason utilize pointer; allocatable see sufficient. fortran 2003 provide move_alloc, provide simplification (if available in compiler using) (see insert value changing shape in allocated vector fortran).
module vertex_stuff type vertex real :: x, y, z end type contains subroutine path_append_vertex(this, x, y, z) implicit none !------------------------------------------------------------------------------- ! variable declarations. !------------------------------------------------------------------------------- type(vertex), dimension (:), allocatable, intent(inout) :: real, intent(in) :: x, y, z !------------------------------------------------------------------------------- ! local variable declarations. !------------------------------------------------------------------------------- type(vertex), dimension(:), allocatable :: tmp_vertices !------------------------------------------------------------------------------- ! start of executable code !------------------------------------------------------------------------------- if (allocated(this)) ! create temporary array same size current number of vertices. re-create ! contents of old array new array delete old array. allocate(tmp_vertices(size(this))) tmp_vertices = deallocate(this) ! create new array 1 element. re-create contents of temporary ! array new 1 delete temporary array. allocate(this(size(tmp_vertices) + 1)) this(1:size(tmp_vertices)) = tmp_vertices deallocate(tmp_vertices) this(size(this))%x = x this(size(this))%y = y this(size(this))%z = z else allocate(this(1)) this(1)%x = x this(1)%y = y this(1)%z = z endif end subroutine subroutine output_vertices (this) implicit none type(vertex), dimension (:), intent(in) :: integer :: write (*, '(// "current vertices:" )' ) i=1, size(this) write (*, '( 3f5.2 )' ) (i) % x, (i) % y, (i) % z end end subroutine output_vertices end module vertex_stuff programme vertices utilize vertex_stuff implicit none type (vertex), dimension (:), allocatable :: phone call path_append_vertex(this, 1.0, 1.1, 1.2) phone call output_vertices (this) phone call path_append_vertex(this, 2.0, 2.1, 2.2) phone call output_vertices (this) phone call path_append_vertex(this, 3.0, 3.1, 3.2) phone call output_vertices (this) phone call path_append_vertex(this, 4.0, 4.1, 4.2) phone call output_vertices (this) phone call path_append_vertex(this, 5.0, 5.1, 5.2) phone call output_vertices (this) end programme vertices
pointers fortran derived-types
No comments:
Post a Comment