Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

F90 : allocate derived datatypes produces sytax error 1

Status
Not open for further replies.

emloot

Programmer
Apr 20, 2009
3
DE
Hello!
I have a problem allocating derived datatypes.
My setup reads as followed:
I have a module_gv in which my derived datatype is declared like this:

type, public :: w_field, sequence
double precision :: wx,wy,wz
end type w_field

type(w_field),allocatable,dimension:),:,:) :: w

then in the program main main.F90
i declared:

use module_gv
allocate(w(32,32,32))
1

w is being used in another subroutine sub(w) which is called after allocation

compiling this leaves me with a syntax error which is caused by w as indicated by "1"
I dont know if the error is because i cant allocated derived datatypes or if i need to allocate its components as well (which i tried but left me with just the same errors for all the components of w and w as well).
Can anyone tell me what i did wrong or missed during declaration?
How do i get w allocated?
thanks for your help!

btw: I'm using the gfortran compiler.
 
You must not write comma in the type declaration before the key word sequence!

Look this works:
alloc.f95
Code:
[COLOR=#a020f0]module[/color] module_gv
  [COLOR=#2e8b57][b]type[/b][/color], [COLOR=#2e8b57][b]public[/b][/color] :: w_field 
    [COLOR=#2e8b57][b]sequence[/b][/color]
    [COLOR=#2e8b57][b]double precision[/b][/color] :: wx,wy,wz
  [COLOR=#2e8b57][b]end type[/b][/color] w_field
[COLOR=#a020f0]end module[/color] module_gv



[COLOR=#a020f0]program[/color] alloc
[COLOR=#a020f0]use[/color] module_gv

[COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]

[COLOR=#2e8b57][b]type[/b][/color](w_field),[COLOR=#2e8b57][b]allocatable[/b][/color],[COLOR=#2e8b57][b]dimension[/b][/color](:,:,:) :: w
[COLOR=#2e8b57][b]integer[/b][/color] :: i,j,k

[COLOR=#0000ff]! allocating[/color]
[COLOR=#804040][b]allocate[/b][/color](w([COLOR=#ff00ff]32[/color],[COLOR=#ff00ff]32[/color],[COLOR=#ff00ff]32[/color]))

[COLOR=#0000ff]! assigning values[/color]
[COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]3[/color]
  [COLOR=#804040][b]do[/b][/color] j[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]3[/color]
    [COLOR=#804040][b]do[/b][/color] k[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]3[/color]
      [COLOR=#0000ff]! assigning value[/color]
      w(i,j,k) [COLOR=#804040][b]=[/b][/color] w_field(i, j, k)
      [COLOR=#0000ff]! or this way [/color]
      [COLOR=#0000ff]!w(i,j,k)%wx = i[/color]
      [COLOR=#0000ff]!w(i,j,k)%wy = j[/color]
      [COLOR=#0000ff]!w(i,j,k)%wz = k[/color]
    [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#804040][b]end do[/b][/color]
[COLOR=#804040][b]end do[/b][/color]

[COLOR=#0000ff]! printing[/color]
[COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]3[/color]
  [COLOR=#804040][b]do[/b][/color] j[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]3[/color]
    [COLOR=#804040][b]do[/b][/color] k[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]3[/color]
      [COLOR=#804040][b]print[/b][/color] [COLOR=#804040][b]*[/b][/color], [COLOR=#ff00ff]'w('[/color],i,[COLOR=#ff00ff]','[/color],j,[COLOR=#ff00ff]','[/color],k,[COLOR=#ff00ff]') ='[/color], w(i,j,k)
    [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#804040][b]end do[/b][/color]
[COLOR=#804040][b]end do[/b][/color]

[COLOR=#a020f0]end program[/color] alloc
Output:
Code:
$ g95 alloc.f95 -o alloc

$ alloc
 w( 1 , 1 , 1 ) = 1. 1. 1.
 w( 1 , 1 , 2 ) = 1. 1. 2.
 w( 1 , 1 , 3 ) = 1. 1. 3.
 w( 1 , 2 , 1 ) = 1. 2. 1.
 w( 1 , 2 , 2 ) = 1. 2. 2.
 w( 1 , 2 , 3 ) = 1. 2. 3.
 w( 1 , 3 , 1 ) = 1. 3. 1.
 w( 1 , 3 , 2 ) = 1. 3. 2.
 w( 1 , 3 , 3 ) = 1. 3. 3.
 w( 2 , 1 , 1 ) = 2. 1. 1.
 w( 2 , 1 , 2 ) = 2. 1. 2.
 w( 2 , 1 , 3 ) = 2. 1. 3.
 w( 2 , 2 , 1 ) = 2. 2. 1.
 w( 2 , 2 , 2 ) = 2. 2. 2.
 w( 2 , 2 , 3 ) = 2. 2. 3.
 w( 2 , 3 , 1 ) = 2. 3. 1.
 w( 2 , 3 , 2 ) = 2. 3. 2.
 w( 2 , 3 , 3 ) = 2. 3. 3.
 w( 3 , 1 , 1 ) = 3. 1. 1.
 w( 3 , 1 , 2 ) = 3. 1. 2.
 w( 3 , 1 , 3 ) = 3. 1. 3.
 w( 3 , 2 , 1 ) = 3. 2. 1.
 w( 3 , 2 , 2 ) = 3. 2. 2.
 w( 3 , 2 , 3 ) = 3. 2. 3.
 w( 3 , 3 , 1 ) = 3. 3. 1.
 w( 3 , 3 , 2 ) = 3. 3. 2.
 w( 3 , 3 , 3 ) = 3. 3. 3.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top