Fortran to Matlab GOTO

5 views (last 30 days)
Carlos
Carlos on 16 May 2014
Answered: Yao Li on 16 May 2014
Hi everyone, I want to translate a Fortran Code to Matlab and I'm stuck in this part of the code specifically with the goto statement. Here is the part of the code I can't understand how to translate it.
DO 11 J=2,N1
Q10=Q1
V10=V1
IF(DU(J).EQ.0) GO TO 15
IF(DB(J).EQ.0) GO TO 515
QPU=0.
QPB=0.
V2=0.5*(V(J)+V(J-1)+Q(J)-Q(J-1))/(1.+0.5*ET*DX)
Q2=Q(J-1)+V2-V(J-1)
V1=V2
Q1=Q(J)-V1+V(J)
DU3=DU(J)-V2*DT
DB3=DB(J)+V2*DT
IF(DU3.LE.0.) GO TO 50
IF(DB3.LE.0.) GO TO 550
GO TO 20
15 QPU=Q(J-1)-Q(J)-V(J)-V(J-1)
IF(QPU.GT.0.) GO TO 30
V2=0.
Q2=Q(J-1)-V(J-1)
V1=0.
Q1=Q(J)+V(J)
GO TO 111
30 QPU=0.
V2=0.5*(V(J)+V(J-1)+Q(J)-Q(J-1))/(1.+0.5*ET*DX)
Q2=Q(J-1)+V2-V(J-1)
V1=V2
Q1=Q(J)-V1+V(J)
DU3=-D2U(J)-V2*DT
DB3=DB(J)+V2*DT
IF(DU3.GE.0.) THEN
DU(J)=DU3
D2U(J)=0.
IF(DB3.LT.0.) GO TO 550
ELSE
DU(J)=0.
D2U(J)=ABS(DU3)
ENDIF
DB(J)=DB3
D2B(J)=0.
GO TO 111
20 DU(J)=DU3
DB(J)=DB3
D2U(J)=0.
D2B(J)=0.
GO TO 111
50 DU(J)=0.
D2U(J)=ABS(DU3)
DB(J)=DB3
D2B(J)=0.
GO TO 111
515 QPB=Q(J-1)-Q(J)-V(J)-V(J-1)
IF(QPB.LT.0.) GO TO 530
V2=0.
Q2=Q(J-1)-V(J-1)
V1=0.
Q1=Q(J)+V(J)
GO TO 111
530 QPB=0.
V2=0.5*(V(J)+V(J-1)+Q(J)-Q(J-1))/(1.+0.5*ET*DX)
Q2=Q(J-1)+V2-V(J-1)
V1=V2
Q1=Q(J)-V1+V(J)
DB3=-D2B(J)+V2*DT
DU3=DU(J)-V2*DT
IF(DB3.GE.0.) THEN
DB(J)=DB3
D2B(J)=0.
IF(DU3.LT.0.) GO TO 50
ELSE
DB(J)=0.
D2B(J)=ABS(DB3)
ENDIF
DU(J)=DU3
D2U(J)=0.
GO TO 111
550 DB(J)=0.
D2B(J)=ABS(DB3)
DU(J)=DU3
D2U(J)=0.
111 Q(J-1)=Q10+Q2-Q(J-1)
V(J-1)=V10+V2-V(J-1)
U(J)=U(J)+V2*DT
UPU(J)=U(J)-D2U(J)
UPB(J)=U(J)+D2B(J)
11 CONTINUE
Thank you very much,
Carlos

Answers (2)

dpb
dpb on 16 May 2014
First branch; similar for the rest altho may be more rework and need some other refactorization than just simple IF...ELSE...ENDIF branching
DO 11 J=2,N1 DO 11 j=2,n1
Q10=Q1 Q10=Q1
V10=V1 V10=V1
IF(DU(J).EQ.0) GO TO 15 IF(DU(J)<>0) then
... ...
15 CONTINUE ENDIF
QPU=Q(J-1)-Q(J)-V(J)-V(J-1) QPU=Q(J-1)-Q(J)-V(J)-V(J-1) % target line 15
... ...
Probably the simpler is one of either
a) rewrite the functional requirements in Matlab syntax rather than try to transliterate, or
b) turn the existing Fortran into a mex file and call from Matlab.

Yao Li
Yao Li on 16 May 2014
There are many options:
  1. Create a function for each destination of "goto". ie. Create a fuction for 111
  2. Copy and paste the corresponding portion to where it is called
  3. Create a classdef and include all the functions in the classdef script

Categories

Find more on Fortran with MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!