soccer jersey forums.silverfrost.com :: View topic - Why this is forbidden ?
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why this is forbidden ?

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
DanRRight



Joined: 10 Mar 2008
Posts: 2866
Location: South Pole, Antarctica

PostPosted: Tue Sep 24, 2024 10:46 pm    Post subject: Why this is forbidden ? Reply with quote

I was shocked to lose heck of time to find that neither FTN95 or gFortran allows this code. I declared TYPEd array (the only reason it is TYPEd is because i want arbitrary allocatable number or elements in this 3D matrix and usual method of allocation does not allow that. Below though I put them of same size) and zeroised its elements at the end

Code:
type lostPart
  real*4, allocatable, Dimension (:,:) :: Particles
end type lostPart

Type (lostPart) ispecies(0:2)

  do icase=0,2
    allocate (ispecies(icase)%Particles(12,10:20) )
  enddo

  ispecies(:)%Particles(:,:)  = 0

end


FTN95 gives the diagnostics
Code:

22537) ispecie(:)%Particles(:,:)  = 0
*** Error 852: The array expression involving the array component
    particles is invalid, as the components to the left of the '%' are
    also array-valued


while gFortran diagnoses and SHOWS it like this ("showing is 100x better than saying", proverb says. It is much more clear and specific as opposed to "general ruling" of FTN95 requiring good knowledge of English language and Fortran rules what 99% of programmers who are mostly not English speaking of course do not know. This simplicity in part is why gFortran for example has 240 variations and versions for all processor types on Linux and FTN95 has not a single one and requires Linux-Windows emulator like Wine)
Code:
  11 |   ispecies(:)%Particles(:,:)  = 0
      |  1
Error: Component to the right of a part reference with nonzero rank must not have the ALLOCATABLE attribute at (1)


The same exact thing but zeroising the array "manually", arranging for it additional DO loop, works with no problem

Code:
type lostPart
  real*4, allocatable, Dimension (:,:) :: Particles
end type lostPart

Type (lostPart) ispecies(0:2)

  do icase=0,2
    allocate (ispecies(icase)%Particles(12,10:20) )
  enddo
  do icase=0,2
    ispecies(icase)%Particles(:,:)  = 0
  enddo
end


What is going on ? Putting aside how compilers diagnose the code (which i think FTN95 should also copy and use) , how come both do not allow zeroising first way?
Back to top
View user's profile Send private message
Kenneth_Smith



Joined: 18 May 2012
Posts: 711
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Wed Sep 25, 2024 10:24 am    Post subject: Reply with quote

I think you a trying to break the Fortran rules. iFort says:

Quote:
error #6159: A component cannot be an array if the encompassing structure is an array. [PARTICLES]


PS I looked into this a little more. Fortran does not allow direct assignments to all elements in the allocatable array across different objects in a derived type (because the arrays might have different sizes). You cannot directly assign values to a derived type array section as you are tyring to do.
Back to top
View user's profile Send private message Visit poster's website
DanRRight



Joined: 10 Mar 2008
Posts: 2866
Location: South Pole, Antarctica

PostPosted: Thu Sep 26, 2024 9:14 am    Post subject: Reply with quote

Ken,
Having different sizes of all arrays (and ideally all components to be allocatable, including for the ispicies too but that failed from start), was exactly the purpose of this derived type. The fact that not allocatable but simply defined ispicies with known sizes defined in declaration somehow prohibited this to work (i did not try to zeroise any "sections", i tried to zeroise everything

Code:
ispecies(:)%Particles(:,:)  = 0

or this where any hints on sections were not mentioned at all
Code:
ispecies%Particles  = 0

but by unknown reason allowed this where i do the same thing
Code:
  do icase=0,2
    ispecies(icase)%Particles(:,:)  = 0
  enddo


looks like an utter absurd to me. It is a joke that i can not write the universal program with all variables declared with not initially set, but with the arbitrary allocatable dimensions and during specific run assign to that dimensions exact numbers and continue run - this is how all programs should be obviously written. These errors above from the same "prohibited this, prohibited that" source.
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2584
Location: Sydney

PostPosted: Fri Sep 27, 2024 9:13 am    Post subject: Reply with quote

Dan,

I can't see what you are doing wrong.
I use this approach with both FTN95 and Gfortran.
I also have "ispecies" as an allocatable derived type structure.

What compiler options are you using ?

I am using ftn95 9.04.0.0 with Checkmate x64 /implicit_none etc and this compiles and runs ?

Code:
type lostPart
  real*4, allocatable :: Particles(:,:)
end type lostPart

Type (lostPart) ispecies(0:4)
integer :: icase

  do icase=0,4
    allocate (ispecies(icase)%Particles(12,10:20) )
  end do
  write (*,*) 'particles allocated'
  do icase=0,2
    ispecies(icase)%Particles(:,:)  = 0
  enddo
  write (*,*) 'particles initialised'
end


I have used this derived type approach for years and is very useful as an in-memory database , where I have shifted data from disk to memory.
This works very well, although I mainly use it to store data, rather than arrays for intensive calculations.

I would enhance your "type lostPart" to include info such as
* the record has been allocated
* the record has been defined
* the size of allocatable components ( which can vary between records)

My derived type code example is
Code:
      TYPE elem_array_record                   ! set in elcomp
         integer*4              :: nd,ns,ilg
         integer*4, allocatable :: lm(:)       ! (nd)      element equation map
         real*8,    allocatable :: sd(:)       ! S(nd,nd) !symmetric stiffness matrix  sd( nt )
         real*8,    allocatable :: xm(:)       ! (nd)      element mass matrix
         real*8,    allocatable :: st(:,:)     ! (ns,nd)   element stress matrix
         real*8,    allocatable :: P(:,:)      ! (nd,ilg)  element load group matrix
         real*8,    allocatable :: tt(:,:)     ! (ns,ilg)  element stress correction matrix
      END TYPE elem_array_record

 type (elem_array_record), allocatable :: elem_array_records(:)        ! (max_elem_records)
...
         allocate ( elem_array_records(max_elem_records), stat=stat )
...
         allocate ( elem_array_records(kel)%st(ns,nd) )

Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2584
Location: Sydney

PostPosted: Fri Sep 27, 2024 9:36 am    Post subject: Reply with quote

enhanced test also works in Plato

Code:
 type lostPart
  integer*4 :: is
  real*4, allocatable :: Particles(:,:)
 end type lostPart

 Type (lostPart), allocatable :: ispecies(:)
 integer :: icase, m

  Allocate ( ispecies(0:4) )
  ispecies%is = -1
  write (*,*) 'ispecies allocated'

  do icase=0,4
    m = 12+icase
    allocate (ispecies(icase)%Particles(m,10:20) )
    ispecies(icase)%is = size (ispecies(icase)%Particles)
  end do
  write (*,*) 'particles allocated'

  do icase=0,4
    ispecies(icase)%Particles(:,:)  = 0
    write (*,*) icase, ispecies(icase)%is, maxval (ispecies(icase)%Particles)
  end do
  write (*,*) 'particles initialised'

end
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 8017
Location: Salford, UK

PostPosted: Sat Sep 28, 2024 10:41 am    Post subject: Reply with quote

I have had a look at this code and FTN95 appears to be conforming to the Standard in this case.
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group