Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28662 Discussions

Calling a File (a matrix with unknown size) inside of the UEL and COMMON variables

Ashkan_K_
Beginner
1,036 Views

Hi everybody,

I'm almost new in FORTRAN and UEL. I have two questions:

1- Inside of the UEL I have to open and read a file which contain a matrix with unknown size. Without defining the size I can't open and read the file. What should I do to over come this?

2- I'm trying to define some of the variables as COMMON variables (for some reasons I can't use module). I have a variable (let's say 'A') which has been defined as allocatable variable. Some how I couldn't define it as COMMON variable. What should I do?

Thanks in advance,

Best Regards,

0 Kudos
15 Replies
mecej4
Honored Contributor III
1,036 Views

What is UEL? Any links to information about it on the Web?

You cannot have allocatable variables in COMMON. See section 5.5.2 of the Fortran 2003 standard, which says:

"(R558) A common-block-object shall not be a dummy argument, an allocatable variable, a
21 derived-type object with an ultimate component that is allocatable, an automatic object, a
22 function name, an entry name, a variable with the BIND attribute, or a result name."

0 Kudos
Ashkan_K_
Beginner
1,037 Views

Hi Mecej4,

Thanks for your response. It was helpful as usual.

UEL means "user defined element" in ABAQUS. It's a subroutine to define new elements. What do you think about the first question? Is there anyway?

Best Regards,

 

0 Kudos
andrew_4619
Honored Contributor III
1,037 Views

Is it a 1D array? If not you are stuck.

You can read the file twice the first time element by element to count them. Allocate an array of the correct size and then REWIND the file and read it again.

 

0 Kudos
mecej4
Honored Contributor III
1,037 Views

Ashkan K. wrote:
What do you think about the first question? Is there anyway?

It would help if you wrote a description of the file containing the matrix. What is its structure/format?

Suppose the file contains 36 real numbers. How can you determine whether those are to be put into a 4 X 9, 6 X 6, or 12 X 3 matrix?

How was that file produced? Is it possible to prepend the size of the matrix to that file before the matrix is written out?

0 Kudos
Anthony_Richards
New Contributor I
1,037 Views

Why can you not use a Module containing the allocatable array?

As Mecej4 queried, without extra information, the string of values in your data file could have come from
an array with a very large number of possible dimension combinations, depending on the total number of values.

You therefore need help from whomever wrote the values into the data file.

0 Kudos
Ashkan_K_
Beginner
1,037 Views

Hi every body,

Thanks for your useful comments.

Due to some limitations in my work I can't use module unfortunately.

The matrix that I want to read from the file is n*2 matrix. I know It has two column but about the rows I have no idea. It can be changed from one problem to another. I have to make the UEL very general and not problem type dependent.

app4619,

How can I read elements o count them? I'm new in Fortran and it would be great if you can give me some ideas.

I'm looking forward to hearing from you all guys.

Thanks in advance.

Best Regards,

0 Kudos
andrew_4619
Honored Contributor III
1,037 Views

What type of file is it text or binary? If text what is the format and how many elements per line? Is the array the only contents? 

0 Kudos
Anthony_Richards
New Contributor I
1,037 Views

Do you even know what type(s) of variables are in the file? Character, integer, floating point...?

If it's a text file,opening it in notepad will let you see the structure.

Otherwise, you appear to be tasked with coming up with a program that will not only read in N*2 variables but which has to
find the value of N but along the way has also to recognise the file format and structure etc., which is a bit much for homework....IMHO

0 Kudos
mecej4
Honored Contributor III
1,037 Views

Here is one way to process an input file with two columns per line and an unknown number of lines:

  1. Read the file in a DO loop, incrementing a line counter after each successful read and branching out of the loop on with an END= clause in the read statement. You can read each line into the same pair of variables, say X and Y.
  2. Now that you have the line count n, allocate the array to hold the n X 2 matrix; suppose we call the array C(:,2).
  3. Rewind the file and read the file as before, but read line no. i into C(i,1) and C(i,2).
  4. Close the file

 

0 Kudos
Ashkan_K_
Beginner
1,037 Views

Thanks again everybody.

app4619, Thanks for your response.

It's a text file (.txt) and array is the only content. array members are in real*8 format. Text file has two lines. First line is for first column of array and the second line is for second column of the array. the is created by another software that I don't have much control on it.

Anthony, Thanks for your consideration.

Mecej4, I appreciate it. I think it should solve my problem. I will check it out and let you know. Because I'm a new user of Fortran, would you please let me know how can I put an END to a DO loop after an unsuccessful read?

EVERY BODY,

If you think of any other way that might solve my problem, please let me know.

I appreciate all of your considerations, responses and kindness.

Best Regards,

0 Kudos
mecej4
Honored Contributor III
1,037 Views

Ashkan K. wrote:
Text file has two lines.
In that case, the suggestions in #10 will not work. We repeatedly asked you to describe the structure of the file, but you did not respond with that information until #11.

Some of the comments in a recent thread, https://software.intel.com/en-us/forums/topic/518286, may help.

0 Kudos
Ashkan_K_
Beginner
1,037 Views

Hi mecej4,

Fortunately I could change some features in software which is producing the text file. now I have a n*1 array and all of the array members are in just one line inside of the text file. I'm uploading the one of the text files here which maybe gives you a better idea. To be honest, because I'm new in FORTRAN, I don't know what do you mean by file structure. I'm so sorry for inconvenience.

377074

0 Kudos
Anthony_Richards
New Contributor I
1,037 Views

FORCE.TXT contains 30 comma-separated floating-point numbers. Unfortunatey, the first number takes up 20 places, then a comma, then the rest take up 21 places, then a comma (no comma after the last number).

Probably

   program READFORCE
    implicit none
    ! Variables
    INTEGER(4), PARAMETER :: NMAX = 1000
    INTEGER(4) I,N
    REAL(8) ARRAY(NMAX)
    ! Body of READFORCE
    print *, 'How long is the data-set?'
    READ(5,*) N
    OPEN(UNIT=10,FILE='C:\TMP\FORCE.TXT',FORM='FORMATTED',STATUS='UNKNOWN')
    IF(N.GT.NMAX) N=NMAX
    READ (10,*, ERR=999,END=9999) (ARRAY(I),I=1,N) 
9999 PAUSE
999  CONTINUE
     
    end program READFORCE

will do what you want.

0 Kudos
John_Campbell
New Contributor II
1,037 Views

I found 31 values, but no recognised end of record characters, which makes me think there is still something missing.

Count of active characters in file force.txt
             1 lines identified if text file
           622 characters read from file force.txt
 Number of <lf>  characters =           0                    0
 Number of <cr>  characters =           0                    0
 Number of <ht>  characters =           0
 Number of ctrl  characters =           0                    0
 Number of alfa  characters =         622                  622
 Number of other characters =           0                    0
 Number of lines identified =           1
 Maximum line length found  =         622
  
 count of characters used
            32                   30
 ,          44                   30
 -          45                    3
 .          46                   31
 0          48                  352
 1          49                   22
 2          50                   23
 3          51                   18
 4          52                   14
 5          53                   20
 6          54                   13
 7          55                   19
 8          56                   26
 9          57                   18
 e         101                    3

A single record of at least 31 numbers is not a good data structure.

This problem lacked some interface file design before force.txt was created.

It would probably require stream input, using an approach as mecej4 has described, although the expected file size should be obtainable from other sources.

John

0 Kudos
mecej4
Honored Contributor III
1,037 Views

Ashkan K. wrote:
I don't know what do you mean by file structure.

This question is not related to Fortran, but to basic aspects of EDP (electronic data processing).

What does the file contain? How many entities, of what types, and in what order? Is it a text file or Is it a Fortran Unformatted File? if the former, how are fields separated and what is the record separator (CR, LF, or CR-LF?). Are numeric fields integer, real or complex? Do character fields contain embedded special characters?

Without information regarding file structure, trying to read the file contents is as pleasant and worthwhile as sifting through the contents of a full garbage dumpster in a large hospital complex.

It was a poor design choice to write a variable count of number fields into a single-line file. Although it is possible to process such a file, one should ask the question, "can we design the file format in such a way that when we want to read the file later, it can be done easily and with no errors?"

0 Kudos
Reply