From: Ragu on
! reading complex data using implied do loops
program cread
use iso_fortran_env, only: output_unit
implicit none
integer, parameter :: sp_k = kind(1.0) ! Default Type of Real
integer, parameter :: dp_k = selected_real_kind(2*precision
(1.0_sp_k))
complex (kind = dp_k), dimension(4) :: cdata
real(kind = dp_k), dimension(4) :: redata, imdata
integer :: ii

continue

! Part 1. Code for reading both real and imaginary in an implied loop
open(unit = 11, file = 'data.inp', form = 'formatted', status = 'old')
read(11,'(2E16.6,16X)') (cdata(ii), ii = 1,4)
close(11)
write(output_unit,'(3A16)') 'REAL PART', 'IMAG PART', 'AMPLITUDE'
write(output_unit,'(2E16.6)') (cdata(ii), ii = 1, 4)

! Part 2. Code to read only the imaginary part of complex
open(unit = 11, file = 'data.inp', form = 'formatted', status = 'old')
read(11,'(16X,E16.6,16X)') (imdata(ii), ii = 1,4)
close(11)
redata = 0.0_dp_k
cdata = cmplx(0.0_dp_k,imdata,kind = dp_k)
write(output_unit,'(3A16)') 'REAL PART', 'IMAG PART', 'AMPLITUDE'
write(output_unit,'(2E16.6)') (cdata(ii), ii = 1, 4)

end program cread

data file 'data.inp'
0.100034E+01 -0.382548E-04 0.100034E+01
0.100054E+01 -0.671324E-04 0.100054E+01
0.100077E+01 -0.102652E-03 0.100077E+01
0.100106E+01 -0.144950E-03 0.100106E+01

I am trying to upgrade my code to accomodate complex values.
Previously I dealt only with reals and this is my first day using
complex. My current code is structured and uses a lot of implied do
loops to read data from both formatted and unformatted data. I am
playing with a test code. I am trying to understand some basic methods
for reading complex values using implied do loops. I am not getting a
much help from my fortran references.

I have two questions:
1. How do I print the ABS(cdata) in the same implied do loop ? Or
would you advise me to just print real(cdata(ii)), imag(cdata(ii)), abs
(cdata(ii)) in a normal do loop?
2. Is it possible to avoid the use of a temporary variable in Part 2
using implied do loop ? My actual arrays are huge in size.

I am hoping that the answers to the above will help me address most of
the special cases that might arise. Thanks.
From: robin on
"Ragu" <ssragunath(a)gmail.com> wrote in message news:51d995ce-48a3-48da-86e9-e84a7fb7843b(a)o3g2000vbo.googlegroups.com...
|! reading complex data using implied do loops
| program cread
| use iso_fortran_env, only: output_unit
| implicit none
| integer, parameter :: sp_k = kind(1.0) ! Default Type of Real
| integer, parameter :: dp_k = selected_real_kind(2*precision
| (1.0_sp_k))
| complex (kind = dp_k), dimension(4) :: cdata
| real(kind = dp_k), dimension(4) :: redata, imdata
| integer :: ii
|
| continue
|
| ! Part 1. Code for reading both real and imaginary in an implied loop
| open(unit = 11, file = 'data.inp', form = 'formatted', status = 'old')
| read(11,'(2E16.6,16X)') (cdata(ii), ii = 1,4)
| close(11)
| write(output_unit,'(3A16)') 'REAL PART', 'IMAG PART', 'AMPLITUDE'
| write(output_unit,'(2E16.6)') (cdata(ii), ii = 1, 4)
|
| ! Part 2. Code to read only the imaginary part of complex
| open(unit = 11, file = 'data.inp', form = 'formatted', status = 'old')
| read(11,'(16X,E16.6,16X)') (imdata(ii), ii = 1,4)
| close(11)
| redata = 0.0_dp_k
| cdata = cmplx(0.0_dp_k,imdata,kind = dp_k)
| write(output_unit,'(3A16)') 'REAL PART', 'IMAG PART', 'AMPLITUDE'
| write(output_unit,'(2E16.6)') (cdata(ii), ii = 1, 4)
|
| end program cread
|
| data file 'data.inp'
| 0.100034E+01 -0.382548E-04 0.100034E+01
| 0.100054E+01 -0.671324E-04 0.100054E+01
| 0.100077E+01 -0.102652E-03 0.100077E+01
| 0.100106E+01 -0.144950E-03 0.100106E+01
|
| I am trying to upgrade my code to accomodate complex values.
| Previously I dealt only with reals and this is my first day using
| complex. My current code is structured and uses a lot of implied do
| loops to read data from both formatted and unformatted data. I am
| playing with a test code. I am trying to understand some basic methods
| for reading complex values using implied do loops. I am not getting a
| much help from my fortran references.
|
| I have two questions:
| 1. How do I print the ABS(cdata) in the same implied do loop ?

Just put ABS(cdata) in the same implied-DO.

| Or
| would you advise me to just print real(cdata(ii)), imag(cdata(ii)), abs
| (cdata(ii)) in a normal do loop?
| 2. Is it possible to avoid the use of a temporary variable in Part 2
| using implied do loop ?

Yes.

| My actual arrays are huge in size.
|
| I am hoping that the answers to the above will help me address most of
| the special cases that might arise. Thanks.


From: Ragu on
On Jan 27, 6:58 pm, "robin" <robi...(a)bigpond.com> wrote:
> | 1. How do I print the ABS(cdata) in the same implied do loop ?
>
> Just put ABS(cdata) in the same implied-DO.

If it is not a big thing can you please give me a sample syntax. The
combination that I tried showed up as errors.

>
> | Or
> | would you advise me to just print real(cdata(ii)), imag(cdata(ii)), abs
> | (cdata(ii)) in a normal do loop?
> | 2. Is it possible to avoid the use of a temporary variable in Part 2
> | using implied do loop ?
>
> Yes.
>
Umm. Can you provide some details ? Does it involves transfer and
reshaping that I have totally avoided till date ?
From: m_b_metcalf on
On Jan 28, 12:13 am, Ragu <ssragun...(a)gmail.com> wrote:
> ! reading complex data using implied do loops
> program cread
> use iso_fortran_env, only: output_unit
> implicit none
> integer, parameter :: sp_k = kind(1.0) ! Default Type of Real
> integer, parameter :: dp_k = selected_real_kind(2*precision
> (1.0_sp_k))
> complex (kind = dp_k), dimension(4) :: cdata
> real(kind = dp_k), dimension(4)     :: redata, imdata
> integer :: ii
>
> continue
>
> ! Part 1. Code for reading both real and imaginary in an implied loop
> open(unit = 11, file = 'data.inp', form = 'formatted', status = 'old')
> read(11,'(2E16.6,16X)') (cdata(ii), ii = 1,4)
> close(11)
> write(output_unit,'(3A16)') 'REAL PART', 'IMAG PART', 'AMPLITUDE'
> write(output_unit,'(2E16.6)') (cdata(ii), ii = 1, 4)
>
> ! Part 2. Code to read only the imaginary part of complex
> open(unit = 11, file = 'data.inp', form = 'formatted', status = 'old')
> read(11,'(16X,E16.6,16X)') (imdata(ii), ii = 1,4)
> close(11)
> redata = 0.0_dp_k
> cdata = cmplx(0.0_dp_k,imdata,kind = dp_k)
> write(output_unit,'(3A16)') 'REAL PART', 'IMAG PART', 'AMPLITUDE'
> write(output_unit,'(2E16.6)') (cdata(ii), ii = 1, 4)
>
> end program cread
>
>  data file 'data.inp'
>     0.100034E+01   -0.382548E-04    0.100034E+01
>     0.100054E+01   -0.671324E-04    0.100054E+01
>     0.100077E+01   -0.102652E-03    0.100077E+01
>     0.100106E+01   -0.144950E-03    0.100106E+01
>
> I am trying to upgrade my code to accomodate complex values.
> Previously I dealt only with reals and this is my first day using
> complex. My current code is structured and uses a lot of implied do
> loops to read data from both formatted and unformatted data. I am
> playing with a test code. I am trying to understand some basic methods
> for reading complex values using implied do loops. I am not getting a
> much help from my fortran references.
>
>  I have two questions:
>  1. How do I print the ABS(cdata) in the same implied do loop ? Or
> would you advise me to just print real(cdata(ii)), imag(cdata(ii)), abs
> (cdata(ii)) in a normal do loop?
>  2. Is it possible to avoid the use of a temporary variable in Part 2
> using implied do loop ? My actual arrays are huge in size.
>
> I am hoping that the answers to the above will help me address most of
> the special cases that might arise. Thanks.

Does this solve both your problems?

Regards,

Mike Metcalf


program cread
use iso_fortran_env, only: output_unit
implicit none
integer, parameter :: sp_k = kind(1.0) ! Default Type of Real
integer, parameter :: dp_k = selected_real_kind(2*precision
(1.0_sp_k))
complex (kind = dp_k), dimension(4) :: cdata
real (kind = dp_k), dimension(2*4) :: tdata = 0.0_dp_k
equivalence (tdata, cdata)
integer :: ii

! Part 1. Code for reading both real and imaginary in an implied loop
open(unit = 11, file = 'data.txt', form = 'formatted', status =
'old')
read(11,'(2E16.6,16X)') (cdata(ii), ii = 1,4)
write(output_unit,'(3A16)') 'REAL PART', 'IMAG PART', 'AMPLITUDE'
write(output_unit,'(3E16.6)') (cdata(ii),abs(cdata(ii)), ii = 1, 4)


! Part 2. Code to read only the imaginary part of complex
rewind 11
cdata = 0
read(11,'(16X,E16.6,16X)') (tdata(ii), ii = 2,2*4, 2)
close(11)
write(output_unit,'(3A16)') 'REAL PART', 'IMAG PART', 'AMPLITUDE'
write(output_unit,'(3E16.6)') (cdata(ii),abs(cdata(ii)), ii = 1, 4)


end program cread


From: Ragu on
Thanks for the reply. I get the first part. I should have been more
careful with the backets and look dumb after shown the way.

The second part deals uses equivalence. Something I have read a lot in
this forum but never used it. So having the equivalence of two arrays
of same data type, kind and length shares the same storage unit and
same memory location ? The book talks about storage unit but not
memory.

The arrays in my real code are all allocatable arrays. Don't I need to
allocate for the tdata ? In that case will I not be using extra
memory? Is there any way to avoid such situation?

Thanks. I guess many stayed out of this thread thinking that it is
home work.