How to Handle Line Truncation in Fortran?

  • Context: Fortran 
  • Thread starter Thread starter LucasCampos
  • Start date Start date
  • Tags Tags
    Fortran Line
Click For Summary
SUMMARY

The discussion addresses the issue of line truncation in Fortran programs when using gfortran with the -ffixed-line-length-n flag. Users report errors stemming from lines exceeding 72 characters, particularly at the beginning of the code. A solution is provided through the use of line continuation with an ampersand (&) for Fortran 90 and later versions, allowing for longer lines without truncation. This method ensures that code remains readable and compilable without errors related to line length.

PREREQUISITES
  • Understanding of Fortran programming language, specifically Fortran 90 or later.
  • Familiarity with gfortran compiler options, particularly -ffixed-line-length-n.
  • Knowledge of line continuation syntax in Fortran using ampersands (&).
  • Basic concepts of subroutines and program structure in Fortran.
NEXT STEPS
  • Research the use of gfortran compiler flags and their implications on code execution.
  • Learn about Fortran 90 line continuation techniques and best practices.
  • Explore debugging techniques for Fortran programs to handle compilation errors effectively.
  • Investigate performance optimization in Fortran, focusing on code readability and maintainability.
USEFUL FOR

This discussion is beneficial for Fortran developers, particularly those working with legacy code or transitioning to Fortran 90 and later versions. It is also useful for educators teaching Fortran programming and anyone involved in optimizing Fortran code for better performance and readability.

LucasCampos
Messages
17
Reaction score
0
I've made a program that uses more than 72 characters per line. When I try to compile it on gfortran, using -ffixed-line-length-n, it returns several errors, all of them derived from a line truncated at the very beginning. How can I compile it or untruncate the line?

The code is as follows

Code:
Program Rotation
Implicit None 
Double Precision dt,ve,mass,radius,height,pmass,vx,vy,area,rad,de,weight,g,t,tmax,ymax,rangmax,inertia,pweight,tet,ar,torque,vymid,vxmid,dy,dx,ax,ay,rang,w
Integer nshow,counter
CALL Opener
CALL Inserter (dt,ve,de,tet,w,mass,radius,height,pmass,nshow)
CALL Calc (ve,vx,vy,mass,radius,area,rad,de,weight,g,t,tmax,ymax,rangmax,inertia,pmass,pweight,counter)
CALL Torq (pweight,tet,ar,inertia,torque)
CALL Velocitymed (vx,vy,ay,ax,t,dt,vymid,vxmid)
CALL Drags (vx,vy,vymid,vxmid,dy,dx,area,ax,ay,weight,mass)
CALL Euler (radius,mass,area,weight,vxmid,vymid,vy,vx,dy,dx,ay,ax,t,dt,height,rang,rangmax,tmax,ymax,ar,w,tet,inertia,torque,pweight,counter,nshow)
CALL print_table (vx,vy,t,ax,ay,dx,dy,height,rang,tet,w,ar)
End

Subroutine inserter (dt,ve,de,tet,w,mass,radius,height,pmass,nshow)
Implicit None 
Double Precision dt,ve,de,tet,w,mass,radius,height,pmass
Integer nshow
Write (*,*) 'Insert time step(s)'
Read (*,*) dt
Write (*,*) 'Insert initial height(m)'
Read (*,*) height
Write (*,*) 'Insert initial translational speed (m/s)'
Read (*,*)ve
Write (*,*) "Insert inital translational speed's angle to the ground (degrees)"
Read (*,*)de
Write (*,*) 'Insert initial angular space (rad)'
Read (*,*) tet
Write (*,*) 'Insert initial angular speed (rad/s)'
Read (*,*) w
Write (*,*) "Insert disk's mass(kg)"
Read (*,*)mass
Write (*,*) "Insert point's mass(kg)"
Read(*,*) pmass
Write (*,*) 'Insert radius(m)'
Read (*,*)radius
Write (*,*) 'Insert number os steps between output'
Read (*,*)nshow
End

Subroutine Calc (ve,vx,vy,mass,radius,area,rad,de,weight,g,t,tmax,ymax,rangmax,inertia,pmass,pweight,counter)
Implicit None
Double Precision ve,vx,vy,mass,radius,area,rad,de,weight,g,t,tmax,ymax,rangmax,inertia,pmass,pweight
Integer counter
g=-9.8
t=0.0D0
rad=de*0.0174532925
vx=ve*(cos(rad))
vy=ve*(sin(rad))
area=3.141592654*(radius**2)
weight=mass*g
pweight=pmass*g
counter = 0
ymax = -5000
tmax = -5000
rangmax = -5000
inertia=(pmass+mass)*(radius**2)*0.66
END

Subroutine Torq (pweight,tet,ar,inertia,torque)
Implicit None
Double Precision pweight,tet,ar,inertia,torque
torque=pweight*cos(tet)
ar=torque/inertia
End

Subroutine Velocitymed (vx,vy,ay,ax,t,dt,vymid,vxmid)
Implicit None
Double Precision vx,vy,ay,ax,t,dt,vymid,vxmid
IF (t .EQ. 0.0D0) vymid = vy
IF (t .EQ. 0.0D0) vxmid = vx
IF (t .NE. 0.0D0) vymid = vy+(ay*(dt/2))
IF (t .NE. 0.0D0) vxmid = vx+(ax*(dt/2))
END

Subroutine Drags (vx,vy,vymid,vxmid,dy,dx,area,ax,ay,weight,mass)
Implicit None
Double Precision vx,vy,vymid,vxmid,dy,dx,area,ax,ay,weight,mass
IF (vy .LT. 0.0D0) dy=(vymid**2)*area*0.5D0
IF (vy .GT. 0.0D0) dy=-(vymid**2)*area*0.5D0
IF (vx .LT. 0.0D0) dx=(vxmid**2)*area*0.5D0
IF (vx .GT. 0.0D0) dx=-(vxmid**2)*area*0.5D0
ay=(dy+weight)/mass
ax=dx/mass
END

Subroutine Euler (radius,mass,area,weight,vxmid,vymid,vy,vx,dy,dx,ay,ax,t,dt,height,rang,rangmax,tmax,ymax,ar,w,tet,inertia,torque,pweight,counter,nshow)
Implicit None
Double Precision radius,mass,area,weight,vxmid,vymid,vy,vx,dy,dx,ay,ax,t,dt,height,rang,rangmax,tmax,ymax,ar,w,tet,inertia,torque,pweight
Integer counter,nshow
100 IF (mod(counter,nshow) .EQ. 0) Call print_table (vx,vy,t,ax,ay,dx,dy,height,rang,tet,w,ar,radius)
Call Euler_Disk (mass,area,weight,vxmid,vymid,vy,vx,dy,dx,ay,ax,t,dt,height,rang,counter,nshow)
Call Euler_pointr (ar,w,tet,dt,inertia,torque,pweight)
counter = counter + 1
IF (height .GT. ymax) tmax=t
IF (height .GT. ymax) rangmax=rang
IF (height .GT. ymax) ymax=height
IF (height .LT. 0.0D0) Go to 200
t=t+dt
Go to 100
200 WRITE(11,*) tmax,ymax
End

Subroutine Euler_pointr (ar,w,tet,dt,inertia,torque,pweight)
Implicit None
Double Precision ar,w,tet,dt,inertia,torque,pweight
Call Torq (pweight,tet,ar,inertia,torque)
w=w+(ar*dt)
tet=tet+(w*dt)
End

Subroutine Euler_Disk (mass,area,weight,vxmid,vymid,vy,vx,dy,dx,ay,ax,t,dt,height,rang,rangmax,tmax,ymax,counter,nshow)
Double Precision mass,area,weight,vxmid,vymid,vy,vx,dy,dx,ay,ax,t,dt,height,rang,rangmax,tmax,ymax
Integer counter,nshow
CALL Velocitymed (vx,vy,ay,ax,t,dt,vymid,vxmid)
CALL Drags (vx,vy,vymid,vxmid,dy,dx,area,ax,ay,weight,mass)
vy=vy+(ay*dt)
vx=vx+(ax*dt)
height=height+(vymid*dt)
rang=rang+(vxmid*dt)
End

Subroutine print_table (vx,vy,t,ax,ay,dx,dy,height,rang,tet,w,ar,radius)
Implicit None
Double Precision vx,vy,t,ax,ay,dx,dy,height,rang,tet,w,ar,radius
WRITE (1,*) t,vx
WRITE (2,*) t,vy
WRITE (3,*) t,ax
WRITE (4,*) t,ay
WRITE (12,*) t,dx
WRITE (13,*) t,dy
WRITE (14,*) t,height
WRITE (15,*) rang,height
WRITE (16,*) t,rang
WRITE (17,*) t,tet
WRITE (18,*) t,w
WRITE (19,*) t,ar
Write (20,*) t, (height+(radius*sin(tet)))
Write (21,*) (rang+(radius*cos(tet))), (height+(radius*sin(tet)))
Write (22,*) t, (rang+(radius*cos(tet)))
END

Subroutine Opener
OPEN (Unit=1,FILE="vx.dat")
OPEN (Unit=2,FILE="vy.dat")
OPEN (Unit=3,FILE="ax.dat")
OPEN (Unit=4,FILE="ay.dat")
OPEN (Unit=11,FILE="heightmax.dat")
OPEN (Unit=12,FILE="dragx.dat")
OPEN (Unit=13,FILE="dragy.dat")
OPEN (Unit=14,FILE="height.dat")
OPEN (Unit=15,FILE="rangeheight.dat")
OPEN (Unit=16,FILE="range.dat")
OPEN (Unit=17,FILE="angle.dat")
OPEN (Unit=18,FILE="rspeed.dat")
OPEN (Unit=19,FILE="rotacce.dat")
OPEN (Unit=20,FILE="point's height.dat")
OPEN (Unit=21,FILE="Full.dat")
OPEN (Unit=22,File="point's range.dat")
END
 
Technology news on Phys.org
You can just use a line continuation, e.g.

Code:
Program Rotation
Implicit None 
Double Precision dt,ve,mass,radius,height,pmass,vx,vy,area,rad,&
de,weight,g,t,tmax,ymax,rangmax,inertia,pweight,tet,ar,torque,&
vymid,vxmid,dy,dx,ax,ay,rang,w
Integer nshow,counter
CALL Opener
CALL Inserter (dt,ve,de,tet,w,mass,radius,height,pmass,nshow)
CALL Calc (ve,vx,vy,mass,radius,area,rad,de,weight,g,t,tmax,ymax,rangmax,&
inertia,pmass,pweight,counter)

Note the ampersands. As long as your in Fortran 90+, you don't have to worry about special characters on the following line, just a single ampersand will work.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
18
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
6K