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

Create a makefile with allocatable memory in fortran

  1. Jul 2, 2013 #1
    I have many subroutines which I want to compile individually using makefile. But the memory allocation is not specified in ".INC" file but as separate subroutine depending on the user input. The "gfortran" compiler is showing the following error:

    -Syntax error in ALLOCATE statement.

    This is not due to missing bracket or something on my carelessness in typing as the program runs fine with a single file.

    Please help me create a makefile with dynamic memory allocation.

    Thank you
  2. jcsd
  3. Jul 3, 2013 #2
    please post your code
  4. Jul 3, 2013 #3
    The folder contains all subroutines. The following is my makefile:

    SRC = ./
    EXE = ./
    CFLAGS = -c

    DEFINEOBJ={list of object files to be created}
    uvlm: $(DEFINEOBJ) $(SRC) file.INC
    gfortran -o $(EXE)uvlm $(DEFINEOBJ)
    rm uvlm $(DEFINEOBJ)

    uvlm.o: $(SRC)uvlm.f $(SRC)file.INC
    gfortran $(CFLAGS) $(SRC)uvlm.f

    //then list of object files compiled


    file.INC contains list of variables with allocatable memory. Separate subroutine is written to allocate memory.

    When I compile I get -Syntax error in ALLOCATE statement-.
    Hope the problem is clear.

    Thanks for your time.
  5. Jul 4, 2013 #4
    Nope, the problem is not clear...

    I want to see the fortran source where the ALLOCATE statement is
    I want to see copy/paste of the terminal when you compile and the error message
  6. Jul 5, 2013 #5
    c ====================
    c Allocation of memory
    c ====================

    subroutine memory_alloc

    allocate (x(MM,4),y(MM,4),z(MM,4))
    allocate (x_c(MM,4),y_c(MM,4),z_c(MM,4))
    allocate (x_v(MM,1),y_v(MM,1),z_v(MM,1),norm_x(MM,3))
    allocate (xc_ics(MM,4),yc_ics(MM,4),zc_ics(MM,4))
    allocate (xv_ics(MM,4),yv_ics(MM,4),zv_ics(MM,4))
    allocate (w_x(MM,4),w_y(MM,4),w_z(MM,4))
    allocate (wx_v(MM,1),wy_v(MN,1),wz_v(MM,1))
    allocate (xc_i(MM,4),yc_i(MM,4),zc_i(MM,4))
    allocate (u_bcs(MM,3),v_bcs(MM,3),w_bcs(MM,3))
    allocate (u_ics(MM,3),v_ics(MM,3),w_ics(MM,3),result_vel(MM,3))
    allocate (wx_ics(MM,4),wy_ics(MM,4),wz_ics(MM,4),sum_wake(MM,3))

    end subroutine memory_alloc
    MM defined in init.f.
    This is my .INC file
    implicit none
    real*8, allocatable, dimension(:,:,:,:)::wi_wke_ics_t
    real*8, allocatable, dimension(:,:)::x,y,z,w_x,w_y,w_z
    real*8, allocatable, dimension(:,:)::x_c,y_c,z_c,wx_v,wy_v,wz_v
    real*8, allocatable, dimension(:,:)::x_v,y_v,z_v
    real*8, allocatable, dimension(:,:)::norm_x
    real*8, allocatable, dimension(:,:)::xc_ics,yc_ics,zc_ics
    real*8, allocatable, dimension(:,:)::xv_ics,yv_ics,zv_ics
    real*8, allocatable, dimension(:,:)::temp1,temp2,temp3,temp4,ic

  7. Jul 5, 2013 #6
    gfortran -c ./memory_alloc.f

    allocate (x(MM,4),y(MM,4),z(MM,4))

    Error: Syntax error in ALLOCATE statement at (1)

  8. Jul 6, 2013 #7
    I guess you insist in not posting everything or attaching all files...dude, you are killing me, here, by spoon feeding the sources!...the thing is that it is very important to see the entire contents of every file and where each is being called from.

    For example, I have no idea if "subroutine memory_alloc" has accesss to all the variables is allocating...is subroutine memory_alloc contained within a module or within another procedure where such variables have been declared? If not, well, that's your problem...you are trying to allocate arrays that you never declared as far as memory_alloc is concerned.

    ...or who know what else! I have no access to all the sources or a chance to compile them myself.
  9. Jul 6, 2013 #8
    Hi Mr.gsal,

    I am sorry the post is unclear. I was thinking of not making the post very long with unnecessary information. I have declared the same variables for which the subroutine "memory.f" allocates the memory. I am not sure if my question was valid. Sorry to have bothered you so much. I will figure out and post the makefile. I really thank you for all your replies.


    Attached Files:

  10. Jul 7, 2013 #9

    I presume that you changed the extensions of the attached files from .f/.INC to .txt only before uploading them, correct? And that in actuality they do have to corresponding .f and .INC extensions as they appear in the make file.

    By the way, just to make sure, you are aware of this much, right? that Linux is case sensitive and that "INC" is not the same as "inc".

    I see that you have a bunch of files to compile...and I think I see what you are trying to do.

    What you are trying to do seems to be a mix of two different approaches, so, it is not going to work...you are mixing pre and post Fortran 90 approaches.

    The traditional (pre Fortran 90) approach uses include files and common blocks; basically, you type all your variable declarations in one file (as in vortex.INC) but you also need to include a common block in the same file. You then include this *.INC file into each other *.f source file that needs to have access to those variables; you do this via the 'include vortex.INC' fortran statement...something like what you are trying to do with the "use vortex" statement in memory_alloc.txt ...but this is where you are mixing stuff...

    ...vortex.INC, as it is, is just an include file because is not a proper (complete) fortran procedure or module and it is not compilable by itself...it is a bunch of lines meant to be included, i.e., as if typed inside other procedures...this is what the 'include' fortran statment does.

    ...but let's forget about such approach

    The new approach is to use modules.

    So, you need to turn your program into a proper Fortran 90 program.

    To start, I recommend changing the extension of all your files to f90. I think your using of extensions *.f is telling gfortran to compile using f77 syntax and Fortran 77 does not have the allocatable statement...that is why you are getting a syntax error.

    Then, you need to replace all those 'c' at the beginning of comment lines with '!'

    Then, you need to fix all continuation lines by eliminating the mark from column 6 and adding an '&' at the end of the previous line, instead.

    Then, you need to turn your vortex file into a proper module.

    Then, in every other source of yours, you do "use vortex"...as you have attempted.

    That is it, for now...let's see what kind of shape you end up after these changes.
  11. Jul 8, 2013 #10

    Thanks for your help. I did exactly as you said-mixing FORTRAN77 with F90. Now I am able to compile the files after I changed the .inc file to a module and "use vortex" option-thanks. I need to link object files and create an executable file. I guess the files are compiled even without renaming and still the compiler accepts "$" sign for line continuation.

    I am pretty novice in these programming as you could have figured out. Thanks again for your help!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook