Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to store large rank-4 array to file?

  1. May 1, 2012 #1
    I am writing a code to calculate and to operations with a large set of matrix elements, indexed by four numbers.

    I am also writing these subroutines within a much larger code structure, so I do not have total freedom to modify the objects I am using.

    I am trying something like this, which compiles but crashes. I am not sure if it would do what I want though, even if it didn't crash.

    What is the best way to write operations like this? How can I most efficiently store the arrays and recall them?


    inquire(iolength=recl) overlap
    open(10,file='OVERLAPS.OUT',action='WRITE',form='UNFORMATTED',access='DIRECT',recl=recl)
    do ikloc=1,nkptnrloc
    ik=mpi_grid_map(nkpt,dim_k,loc=ikloc)
    write(10,rec=ik) overlap(:,:,:,ikloc)
    enddo
    close(10)

    inquire(iolength=recl) overlap
    open(10,file='OVERLAPS.OUT',action='WRITE',form='UNFORMATTED',access='DIRECT',recl=recl)
    do ikloc=1,nkptnrloc
    ik=mpi_grid_map(nkpt,dim_k,loc=ikloc)
    write(10,rec=ik) overlap(:,:,:,ikloc)
    enddo
    close(10)
     
  2. jcsd
  3. May 2, 2012 #2

    Mark44

    Staff: Mentor

    Deleted extra copy of code.
    Others on this site are more versatile with fortran, as the last time I wrote any code of this type was about 20 years ago.

    In any case, I don't believe you have given us enough information to be able to help. The problem you describe may or may not be occurring in the lines of code you show, and might be a result of something in the code that you don't show.

    From your description your program is syntactically correct (it compiles successfully) but a logical error is causing problems at run time. Simply saying that the program "crashed" gives us no clue at all about why the crash occurred. Any additional information that the program produces at that time would be helpful for us to determine the cause.

    If you have a debugger to use, that could be used to narrow down the cause of the crash. If you are unfamiliar with using debuggers, the old-fashioned way to find problems is to liberally sprinkle WRITE statements throughout the code to display the values of variables when the code is running.

    <rant>
    I mean no disrespect, but your coding style is pretty much identical to the coding style used by countless Fortran programmers. Here are some of the hallmarks of bad Fortran code.

    1. Incomprehensible variable names - ikloc, nkptnrloc, ik.
      I have no idea what these are supposed to represent, which makes it impossible to understand what their roles might be or the values they might have. I can guess that ikloc represents the location of something - ik? - but that's only a guess. Possibly nkptnrloc represents the location of something, but I can't even guess what an nkptnr might be.
      At one point many years ago, Fortran programmers were constrained to using short variable names due to the lack of memory. Although those days are long gone, all too many Fortran programmers continue following this style.
    2. Nonuse of whitespace - The statement below has not a single space character in it.
      Code (Text):
      open(10,file='OVERLAPS.OUT',action='WRITE',form='UNFORMATTED',access='DIRECT',recl=recl)
       
      In general, it seems to me that Fortran programmers are unfamiliar with the location and purpose of the space bar. Whitespace added to program code makes it easier for us humans to read and understand the code. The lack of whitespace and the gobbledegook variable names puts the code pretty firmly into the "write-only" category, IMO.
    Other traits that I often see in others' Fortran code are no indentation or random indentation of program control structures, and lack of comments.

    Although the compiler couldn't care less about the names of variables, use of whitespace, indentation, and comments, we poor humans have a more difficult time understanding what the code is trying to do when little or no thought is given to these attributes of good programming style.
    </rant>
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook