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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving matrices with parameters (Adt=1time=1000)

Status
Not open for further replies.

mjerdem

Programmer
Nov 7, 2012
5
US
Hi,

I am trying to write my matrix in a file using some parameters in its name (DxmatAsp=0.1Nx=5Nz=3) . Like number of rows,columns, etc ...
But when I run the code only some part is written in the name, i.e, aspect ratio.

Here is my code

program trialwrite
implicit none


integer :: i,j,k
integer :: Nx, Nz
real(8), dimension:),:),allocatable :: Dx
real(8)::asp
CHARACTER (LEN=100):: Dxmat,Nxc,Nzc, Aspect
Nx=3
Nz=5
Asp=0.1
allocate (Dx(Nz,Nx))

Dx(1,:)=(/3.00, -2.00, -1.00/)
Dx(2,:)=(/4.00, -1.00, 0.00/)
Dx(3,:)=(/4.00, -1.00, 0.00/)
Dx(4,:)=(/4.00, -1.00, 0.00/)
Dx(5,:)=(/4.00, -1.00, 0.00/)

WRITE(Aspect,'(f4.2)')asp
WRITE(Nxc,'(i1)')Nx
WRITE(Nzc,'(i1)')Nz

Dxmat='Dx'//'Asp='//Aspect//'Nx='//Nxc//'Nz='//Nzc
OPEN(10,FILE=Dxmat)
DO i=1,Nz
WRITE(10,*) (Dx(i,j),j=1,Nx)
ENDDO
CLOSE(10)

end program trialwrite

The output file from this is DxmatAsp=0.1 the rest is not there. I would appreciate if someone point my mistake.

Thank you.
 
I guess I could test it and figure it out, but I let you do the investigating, instead...here are some hints.

I think you need to get selective as to how much of your "character" variables you use, before concatenating them; in other words, you may have to use indices...I am afraid that you may be concatenating 100-character long values with A LOT of empty space in between.

Before you try to actually name a file with such a string, take a step back and simply write the "resulting" file name to standard output to see what it looks like.

By the way, depending on your platform, there may a limit of how long a file name can be.

 
As Salgerman says, you are trying to concatenate a number of 100-character variables into a single 100-character variable, which will of course not work. All the characters that exceed the 100 characters of Dxmat will be truncated. Wonder why you do not get a runtimerrror for this.

But instead of working with indices I would recommend you use the intrinsic function len_trim(), which gives the number of characters in a string not counting the trailing blanks.

So the piece of code might look like:

Code:
iLen1 = len_trim (Aspect)
iLen2 = len_trim (Nxc)
iLen3 = len_trim (Nzc)
Dxmat = 'DxAsp=' // Aspect(1 : iLen1) // 'Nx=' // Nxc(1 : iLen2) // 'Nz=' // Nzc(1 : ilen3)

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Thank you that solved my problem. And I used the idea to create new folders. However, now I am trying to move files (or directly save them) into the new folder. Again thank you for your help.

program trialwrite
implicit none


integer :: i,j,k
integer :: Nx, Nz
integer :: iLen1, iLen2, iLen3
real(8), dimension:),:),allocatable :: Dx
real(8)::asp
CHARACTER (LEN=40):: Dxmat,Nxc,Nzc, Aspect
character(LEN=300):: folder, path, makedirectory*100, Destin

Nx=3
Nz=5
Asp=0.1
allocate (Dx(Nz,Nx))
WRITE(Aspect,'(f4.2)')asp
WRITE(Nxc,'(i1)')Nx
WRITE(Nzc,'(i1)')Nz
iLen1=len_trim(Aspect)
iLen2=len_trim(Nxc)
iLen3=len_trim(Nzc)


path = '/home/er/Desktop/Den/'
folder = 'Asp='//Aspect(1:iLen1)//'Nx='//Nxc(1:iLen2)//'Nz='//Nzc(1:iLen3)

makedirectory = 'mkdir ' // trim(path) // trim(folder)

call system(makedirectory)

Destin='/home/er/Desktop/Den/Asp='//Aspect(1:iLen1)//'Nx='//Nxc(1:iLen2)//'Nz='//Nzc(1:iLen3)

Dx(1,:)=(/3.00, -2.00, -1.00/)
Dx(2,:)=(/4.00, -1.00, 0.00/)
Dx(3,:)=(/4.00, -1.00, 0.00/)
Dx(4,:)=(/4.00, -1.00, 0.00/)
Dx(5,:)=(/4.00, -1.00, 0.00/)

Dxmat='DxAsp='//Aspect(1:iLen1)//'Nx='//Nxc(1:iLen2)//'Nz='//Nzc(1:iLen3)

OPEN(10,FILE=Dxmat//'.dat',FORM='formatted')
DO i=1,Nz
WRITE(10,*) (Dx(i,j),j=1,Nx)
ENDDO

CLOSE(10)

end program trialwrite


I keep the characters long because in my real code I will have many variables.
 
If I were you, I would think twice if I would want to do extensive filemanagement from inside my program. Or if I really want one directory for one file. Seems like some overkill to me. Assigning different names to files is enough to distinguish them, you usually do not need to have seperate directories to seperate them further.

Usually you work with existing directories, and then the task just a matter of creating and opening files which can easily be done by the open-statement. Just include your full path's name into the filename and you are set.

Code:
cName = 'D:\My_Data\Important_Project\Input\GarbageIn.txt'
open (unit = 20, file = cName)
cName = 'D:\My_Data\Important_Project\Output\GarbageOut.txt'
open (unit = 21, file = cName)

This code will open two different files in two different directories. This will work, provided this directories allready exist when the program executes.

However, handling directories, like createing and deleting them, is possible from inside your fortran code but how this can be done is a question of the operating system and the compiler you use. If you are in a windows environment there are quite a few routines like CreateDirectory or RemoveDirectory and much more that allow you to do this. In my Compaq Visual Fortran this would look like

Code:
...
use dfwin
...
character*100 cDirectory
logical lrslt
...
...
cDirectory = 'd:\MyNewDirectory'
lrslt = CreateDirectory (cDirectory, NULL)
...

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top