From: Mike on
Hi

I'd like to use module to pass data between procedures.
In the following, subroutine FCN is an argument of subroutine CDGRD
which calculates gradient (IMSL). I have comment those statements
related to IMSL.

module shared_data
implicit none
save
real,dimension(2) :: y
end module shared_data

module A
contains
!C
SUBROUTINE FCN (N, X, F)
use shared_data
INTEGER N
REAL X(N), F
!C
F = y(1)*X(1) +y(2)*x(1)- 3.*X(2)
!C
RETURN
END SUBROUTINE FCN
end module A

!module B
!contains
!subroutine mm(XC,XSCALE,GC,N)
!use A
!INTEGER I, N, NOUT
!real :: EPSFCN
!REAL,dimension(N) :: GC, XC, XSCALE
!!C Set function noise.
!EPSFCN = 0.

!CALL CDGRD (FCN, N, XC, XSCALE, EPSFCN, GC)

!CALL UMACH (2, NOUT)
!WRITE (NOUT,99999) (GC(I),I=1,N)
!99999 FORMAT (' The gradient is', 2F8.2, /)

!END subroutine mm
!end module B

program main
!use B
use shared_data
!integer,parameter :: N=2
!C Initialization.
!REAL,dimension(N) :: XC=(/1.,2./), XSCALE=(/1.,1./);
!The gradient
!REAL,dimension(N) :: GC
real,dimension(2) :: y

y=(/-2.,-1./);
!do i=1,2
! xc(1)=(i-1)*0.5;
!do j=1,2
! xc(2)=(j-1)*1.;
!call mm(XC,XSCALE,GC,N)
!enddo;enddo
end

after compiled by CVF6.6c:
Error: The attributes of this name conflict with those made accessible
by a USE statement. [Y]
y=(/-2.,-1./);
^
Error: The shapes of the array expressions do not conform. [Y]

I know I have declared twice: one in main program and the other in
module (module shared_data).
How to pass y array to FCN? please note that FCN can have only three
arguments.

thank you in advance.

Mike
From: Arjen Markus on
On 22 apr, 10:22, Mike <Sulfate...(a)gmail.com> wrote:
> Hi
>
>   I'd like to use module to pass data between procedures.
> In the following, subroutine FCN is an argument of subroutine CDGRD
> which calculates gradient (IMSL).  I have comment those statements
> related to IMSL.
>
> module shared_data
> implicit none
> save
> real,dimension(2) :: y
> end module shared_data
>
> module A
> contains
> !C
> SUBROUTINE FCN (N, X, F)
> use shared_data
> INTEGER N
> REAL X(N), F
> !C
> F = y(1)*X(1) +y(2)*x(1)- 3.*X(2)
> !C
> RETURN
> END SUBROUTINE FCN
> end     module A
>
> !module B
> !contains
> !subroutine mm(XC,XSCALE,GC,N)
> !use A
> !INTEGER I, N, NOUT
> !real :: EPSFCN
> !REAL,dimension(N) :: GC, XC, XSCALE
> !!C Set function noise.
> !EPSFCN = 0.
>
> !CALL CDGRD (FCN, N, XC, XSCALE, EPSFCN, GC)
>
> !CALL UMACH (2, NOUT)
> !WRITE (NOUT,99999) (GC(I),I=1,N)
> !99999 FORMAT (' The gradient is', 2F8.2, /)
>
> !END subroutine mm
> !end module B
>
> program main
> !use B
> use shared_data
> !integer,parameter :: N=2
> !C Initialization.
> !REAL,dimension(N) :: XC=(/1.,2./), XSCALE=(/1.,1./);
> !The gradient
> !REAL,dimension(N) :: GC
> real,dimension(2) :: y
>
> y=(/-2.,-1./);
> !do i=1,2
> !       xc(1)=(i-1)*0.5;
> !do j=1,2
> !       xc(2)=(j-1)*1.;
> !call mm(XC,XSCALE,GC,N)
> !enddo;enddo
> end
>
> after compiled by CVF6.6c:
> Error: The attributes of this name conflict with those made accessible
> by a USE statement.   [Y]
> y=(/-2.,-1./);
> ^
> Error: The shapes of the array expressions do not conform.   [Y]
>
> I know I have declared twice: one in main program and the other in
> module (module shared_data).
> How to pass y array to FCN? please note that FCN can have only three
> arguments.
>
> thank you in advance.
>
> Mike

You should leave out the declaration of Y in the main program.
Because you are using SHARED_DATA you already have access to a
variable Y.

Regards,

Arjen
From: A. Belli on
Mike schrieb:
> Hi
>
> I'd like to use module to pass data between procedures.
> In the following, subroutine FCN is an argument of subroutine CDGRD
> which calculates gradient (IMSL). I have comment those statements
> related to IMSL.
>
> module shared_data
> implicit none
> save
> real,dimension(2) :: y
> end module shared_data
>
> module A
> contains
> !C
> SUBROUTINE FCN (N, X, F)
> use shared_data
> INTEGER N
> REAL X(N), F
> !C
> F = y(1)*X(1) +y(2)*x(1)- 3.*X(2)
> !C
> RETURN
> END SUBROUTINE FCN
> end module A
>
> !module B
> !contains
> !subroutine mm(XC,XSCALE,GC,N)
> !use A
> !INTEGER I, N, NOUT
> !real :: EPSFCN
> !REAL,dimension(N) :: GC, XC, XSCALE
> !!C Set function noise.
> !EPSFCN = 0.
>
> !CALL CDGRD (FCN, N, XC, XSCALE, EPSFCN, GC)
>
> !CALL UMACH (2, NOUT)
> !WRITE (NOUT,99999) (GC(I),I=1,N)
> !99999 FORMAT (' The gradient is', 2F8.2, /)
>
> !END subroutine mm
> !end module B
>
> program main
> !use B
> use shared_data
> !integer,parameter :: N=2
> !C Initialization.
> !REAL,dimension(N) :: XC=(/1.,2./), XSCALE=(/1.,1./);
> !The gradient
> !REAL,dimension(N) :: GC
> real,dimension(2) :: y
>
> y=(/-2.,-1./);
> !do i=1,2
> ! xc(1)=(i-1)*0.5;
> !do j=1,2
> ! xc(2)=(j-1)*1.;
> !call mm(XC,XSCALE,GC,N)
> !enddo;enddo
> end
>
> after compiled by CVF6.6c:
> Error: The attributes of this name conflict with those made accessible
> by a USE statement. [Y]
> y=(/-2.,-1./);
> ^
> Error: The shapes of the array expressions do not conform. [Y]
>
> I know I have declared twice: one in main program and the other in
> module (module shared_data).
> How to pass y array to FCN? please note that FCN can have only three
> arguments.
>
> thank you in advance.
>
> Mike

Man this code is hard to read. But as far as I can see, you define y two
times. One time in the module and secondly in the main program.
Try:
use shared_data, only: y
and delete the second real,dimension(2) :: y

greetings
From: Les on

"Mike" <SulfateIon(a)gmail.com> wrote in message
news:9f2fdad5-682c-4275-9214-29a57fada3eb(a)t12g2000prg.googlegroups.com...

<snip>

> program main
> !use B
> use shared_data
> !integer,parameter :: N=2
> !C Initialization.
> !REAL,dimension(N) :: XC=(/1.,2./), XSCALE=(/1.,1./);
> !The gradient
> !REAL,dimension(N) :: GC
> real,dimension(2) :: y
You already have "y" available via the shared_data module
Remove this line.

<snip>

> How to pass y array to FCN? please note that FCN can have only three
> arguments.
>

It is already "passed" ie made available via the shared_data module.
You assign values to, (ie initialise), "y" in your program and those values
are available so that *anything* that needs the array "y" just needs a "use
shared_data" statement. Of course anything that uses the module and assigns
new values to "y" overwrites the original values.

Les

PS Look at the RESHAPE function


From: Mike on
On Apr 22, 5:03 pm, "Les" <l.neil...(a)nospam.acecad.co.uk> wrote:
> "Mike" <Sulfate...(a)gmail.com> wrote in message
>
> news:9f2fdad5-682c-4275-9214-29a57fada3eb(a)t12g2000prg.googlegroups.com...
>
> <snip>
>
> > program main
> > !use B
> > use shared_data
> > !integer,parameter :: N=2
> > !C Initialization.
> > !REAL,dimension(N) :: XC=(/1.,2./), XSCALE=(/1.,1./);
> > !The gradient
> > !REAL,dimension(N) :: GC
> > real,dimension(2) :: y
>
> You already have "y" available via the shared_data module
> Remove this line.
>
> <snip>
>
> > How to pass y array to FCN? please note that FCN can have only three
> > arguments.
>
> It is already "passed" ie made available via the shared_data module.
> You assign values to, (ie initialise), "y" in your program and those values
> are available so that *anything* that needs the array "y" just needs a "use
> shared_data" statement. Of course anything that uses the module and assigns
> new values to "y" overwrites the original values.
>
> Les
>
> PS    Look at the RESHAPE function

thank you for your explanation.
So I need to check every time, if I use a module to pass the data
between procedures. It's quite troublesome.
I have read some Fortran book which saids that common is obselete.
But I think if I use named common to pass these data between
procedures (I mean FCN), then I don't have to worry about if these
data are declared twice. Am I right?

Mike