SIGSEGV: Segmentation fault - invalid memory reference - reference

I'm a beginner in Fortran. I have to make a program with a function and an array but it doesn't work. I tried to use debugger :
-g -fcheck-bounds -fbacktrace -fimplicit-none etc
but nothing is wrong.
Here is my function :
FUNCTION tabspot (i, j, OFFSHORE, hauteurhoulemin, hauteurhoulemax)
IMPLICIT NONE
!---------------------------------------
! Déclaration des arguments
!---------------------------------------
INTEGER, INTENT(INOUT) :: i, j
CHARACTER(LEN=5), INTENT(OUT) :: OFFSHORE
INTEGER, INTENT(OUT):: hauteurhoulemin
INTEGER, INTENT(OUT):: hauteurhoulemax
!---------------------------------------
! Déclaration du type de la fonction
!---------------------------------------
CHARACTER(LEN=14), DIMENSION(4) :: tabspot
!---------------------------------------
! Déclaration des varibles locales
!---------------------------------------
CHARACTER(LEN=14) :: box
tabspot(1) = 'Cotedesbasques'
tabspot(2) = 'Mundaka'
tabspot(3) = 'Parlementia'
tabspot(4) = 'Hendaye'
DO i=1,4
SELECT CASE (i)
CASE(1)
OFFSHORE = 'EST'
hauteurhoulemin = 30
hauteurhoulemax = 150
CASE(2)
OFFSHORE = 'SUD'
hauteurhoulemin = 90
hauteurhoulemax = 400
CASE(3)
OFFSHORE = 'EST'
hauteurhoulemin = 120
hauteurhoulemax = 500
CASE(4)
OFFSHORE = 'SUD'
hauteurhoulemin = 60
hauteurhoulemax = 200
END SELECT
DO j=i+1,5
IF (((vent1%direction == OFFSHORE) .AND. (hauteurhoule>=hauteurhoulemin)&
.AND. (hauteurhoule<=hauteurhoulemax)) .OR. &
((vent1%force == 0) .AND. (hauteurhoule>=hauteurhoulemin)&
.AND. (hauteurhoule<=hauteurhoulemax)))THEN
box=tabspot(i)
tabspot(i)=tabspot(j)
tabspot(j)=box
END IF
END DO
END DO
END FUNCTION
Thank you in advance for your answer.
Martin

Related

I want to create a vba userform in excel for my personal calculations. But I'm having trouble using it

Help me I need to create a Userform for my personal calculations. But I ran into a lot of problems. Because I've never written a program before.
When I enter a value 0 or enter a value other than 0 and delete it in the text field PriceCoinBuy, BuyCoin , PriceCoinSell , SellCoin , Vat one of the fields I will get Msg : Run-time error '6'; overflow.
But when I put a number other than 0 in the BuyCoin field, I get Msg : Run-time error '11'; Division by zero.
I've been searching for a solution for 2 days but can't find it in please help.
I have 5 textboxes for input.
Sub SumAll()
Dim A, B, C, D, E, F, G, H, I, J, K, V As Double
A = Val(Order.PriceCoinBuy.Value)
B = Val(Order.BuyCoin.Value)
C = Val(Order.PriceCoinSell.Value)
D = Val(Order.SellCoin.Value)
V = Val(Order.Vat.Value)
'-------------- Math --------------
E = CDbl(B) / A
F = CDbl(E) * (V / 100)
G = CDbl(E) - F
H = CDbl(G) * A
I = CDbl(D) * C
J = CDbl(I) * (V / 100)
K = CDbl(I) - J
'---------------- Show -------------
Order.GetCoin.Text = Format(E, "##,##0.000000")
Order.AfterVatBuy.Text = Format(F, "##,##0.0000")
Order.CoinBalance.Text = Format(G, "##,##0.000000")
Order.ToMoney.Text = Format(H, "##,##0.00")
Order.GetMoney.Text = Format(I, "##,##0.00")
Order.AfterVatSell.Text = Format(J, "##,##0.00")
Order.MoneyBalance.Text = Format(K, "##,##0.00")
End Sub

Why is this loop stopping after the first pass?

I want my function to loop over a set number of times before it gives me an answer. It's stopping after the first loop. I want it to go through the loop "PerInq" times before giving me the answer. I have a series of math problems that must done consecutively to get the proper answer. Below is my code:
Function IntFinder(PVLoan, APR, PMT_Freq, Term, PerInq)
n = PMT_Freq * Term
R = APR / PMT_Freq
PMT1 = Pmt(R, n, -PVLoan)
Intrest = PVLoan * R
Prin = PMT1 - Intrest
Bal = PVLoan - Prin
BalSum = 0
For counter = 1 To counter = PerInq
Intrest = R * Bal
Prin = PMT1 - Intrest
Bal = Bal - Prin
counter = counter + 1
Next counter
IntFinder = Intrest
End Function

Type Mismatch Error And Bisection inside Integration inside iterative loop

I am trying to convert a Mathcad calculation to excel spreadsheet. I am trying to find a variable called kw. I am getting type mismatch error in the line:
INTEG = INTEG + ...
Would someone know why I am getting this error ? I have already spent a lots of time on it but couldn't find the reason. As this work is really important to me, I would like to thank in advance any help/suggestion on it.#
Option Explicit
Dim wp As Double, alpha As Double, w As Double, gama As Double, d As Double
Dim kw As Double, n As Integer, sExp As String, g As Double
'Calculating kw
Sub CalculateKw()
Dim l As Integer
alpha = ThisWorkbook.Sheets("Current-Wave Calculation").Range("H49").Value
gama = ThisWorkbook.Sheets("Current-Wave Calculation").Range("H34").Value
d = ThisWorkbook.Sheets("Environmental Data Input").Range("H4").Value
wp = ThisWorkbook.Sheets("Current-Wave Calculation").Range("H47").Value
kw = 100
For l = 1 To 99
If Err.Number = 0 Then
kw = INTEG(0, kw, 5000)
Else
kw = kw - 1
End If
Next l
ThisWorkbook.Sheets("Sheet1").Range("B1").Value = kw
End Sub
'DEFINITE INTEGRAL from 0 to kw*wp to solve JONSWAP Equation
Function INTEG(n, kw, lBit As Long)
Dim SpectralWidthParameter As Double, dMin As Double, dMax As Double
Dim dW As Double, lW As Long, AAA As String
g = 9.80665
alpha = ThisWorkbook.Sheets("Current-Wave Calculation").Range("H49").Value
gama = ThisWorkbook.Sheets("Current-Wave Calculation").Range("H34").Value
d = ThisWorkbook.Sheets("Environmental Data Input").Range("H4").Value
wp = ThisWorkbook.Sheets("Current-Wave Calculation").Range("H47").Value
dMin = 0
dMax = kw * wp
If w <= wp Then
SpectralWidthParameter = 0.07
Else
SpectralWidthParameter = 0.09
End If
sExp = "(w ^ n) * (((w / _
(Application.WorksheetFunction.Sinh(WaveNumber(0,20,w,d) * d))) ^ 2)*
(alpha * (g ^ 2) * (w ^ (-5)) * (EXP((-5 / 4) * ((w / wp) ^ (-4)))) *
(gama ^ (EXP(-0.5 * (((w - wp) / (SpectralWidthParameter * wp)) ^ 2))))))"
sExp = Replace(sExp, "EXP", "AAA")
dW = (dMax - dMin) / lBit
For lW = 1 To lBit
*INTEG = INTEG + Evaluate(Replace(Replace(sExp, "w", dMin), "AAA", _
"EXP")) * dW + 0.5 * dW * Abs(Evaluate(Replace(Replace(sExp, "w", dMin _
+dW), "AAA", "EXP")) - Evaluate(Replace(Replace(sExp, "w", dMin), _
"AAA", "EXP")))*
dMin = dMin + dW
Next lW
End Function
'BISECTION METHOD TO CALCULATE Wave number k
Function WaveNumber(a, b, w, d)
Dim klow As Double, khigh As Double, kmid As Double, i As Integer
Dim a As Integer, b As Integer
klow = a
khigh = b
kmid = (klow + khigh) / 2
For i = 1 To 100
If SolveFunction(klow, w, d) * SolveFunction(kmid, w, d) < 0 Then
khigh = kmid
kmid = (klow + khigh) / 2
Else
klow = kmid
kmid = (klow + khigh) / 2
End If
Next i
WaveNumber = kmid
End Function
'HELPER FUNCTION(Wave Dispersion Equation)FOR BISECTION METHOD
Function SolveFunction(k, w, d)
SolveFunction = k * Application.WorksheetFunction.Tanh(k * d) - (w ^ 2) _
/ 9.80665
End Function
INTEG = INTEG + Evaluate(....)
"Type mismatch" because your Evaluate expression returned an error. Adding a number to an error variant generates that runtime error.
Basically you shouldn't insert Application.WorksheetFunction.Sinh(...) in the evaluated expression, but just Sinh(...).
Moreover I think that you can re-write your code without using Evaluate, this will make debugging your code easier.

Processing special characters

Let's say I receive the following string in Lua mÜ⌠⌠í∩and would like to apply it to my current processing code, which is the following
function inTable(tbl, item)
for key, value in pairs(tbl) do
if value == item then return true end
end
return false
end
function processstring(instr)
finmsg = ""
achar = {131,132,133,134,142,143,145,146,160,166,181,182,183,198,199,224}
echar = {130,137,138,144,228}
ichar = {139,140,141,161,173,179,244}
ochar = {147,148,149,153,162,167,229,233,234,248}
uchar = {129,150,151,154,163}
nchar = {164,165,227,252}
outmsg = string.upper(instr)
for c in outmsg:gmatch"." do
bc = string.byte(c)
if(bc <= 47 or (bc>=58 and bc<=64) or (bc>=91 and bc<=96) or bc >=123)then
elseif (bc == 52) then finmsg = finmsg.."A"
elseif (bc == 51) then finmsg = finmsg.."E"
elseif (bc == 49) then finmsg = finmsg.."I"
elseif (bc == 48) then finmsg = finmsg.."O"
elseif (inTable(achar, bc)==true) then finmsg = finmsg.."A"
elseif (inTable(echar, bc)==true) then finmsg = finmsg.."E"
elseif (inTable(ichar, bc)==true) then finmsg = finmsg.."I"
elseif (inTable(ochar, bc)==true) then finmsg = finmsg.."O"
elseif (inTable(uchar, bc)==true) then finmsg = finmsg.."U"
elseif (inTable(nchar, bc)==true) then finmsg = finmsg.."N"
else
finmsg = finmsg..c
end
end
return finmsg
end
function checkword (instr)
specword = [[]]
wordlist = {"FIN", "FFI", "PHIN", "PHEN", "FIN", "PHIN", "IFFUM", "MUF", "MEUFEEN", "FEN","FEEN"}
for i, v in ipairs (wordlist) do
if (string.match(processstring(instr), v) ~= nil)then
return 1
end
end
--if (string.match(instr,specword) ~= nil)then
-- return 1
--end
end
print (checkword("mÜ⌠⌠í∩"))
As of now, I have found no way to proof strings like that. Not even by using string.byte() to reduce it to ASCII have I been able to reliably work with exoctic characters like those. Even more weird is that if I do a print(bc) on processstring I get the folowing output
160 226 140 160 195 173 226 136 169
Now, that's 9 ASCII codes for a 6 letter word, how can this be? I built the code referencing http://www.asciitable.com/, is it wrong? How can I approach this processing?
local subst = {
U = "üûùÜú",
N = "ñÑπⁿ∩",
O = "ôöòÖóºσΘΩ°",
I = "ïîìí¡│",
F = "⌠",
A = "âäàåÄÅæÆáª╡╢╖╞╟α",
E = "éëèÉΣ",
}
local subst_utf8 = {}
for base_letter, list_of_letters in pairs(subst) do
for utf8letter in list_of_letters:gmatch'[%z\1-\x7F\xC0-\xFF][\x80-\xBF]*' do
subst_utf8[utf8letter] = base_letter
end
end
function processstring(instr)
return (instr:upper():gsub('[%z\1-\x7F\xC0-\xFF][\x80-\xBF]*', subst_utf8))
end
print(processstring("mÜ⌠⌠í∩")) --> MUFFIN

OpenMP block gives false results

I would appreciate your point of view where I might did wrong using OpenMP.
I parallelized this code pretty strait forward - yet even with single thread (i.e., call omp_set_num_threads(1)) I get wrong results.
I have checked with Intel Inspector, and I do not have a race condition, yet the Inspector tool indicated as a warning that a thread might approach other thread stack (I have this warning in other code I have, and it runs well with OpenMP). I do not think this is the problem.
SUBROUTINE GR(NUMBER_D, RAD_D, RAD_CC, SPECT)
use TERM,only: DENSITY, TEMPERATURE, VISCOSITY, WATER_DENSITY, &
PRESSURE, D_HOR, D_VER, D_TEMP, QQQ, UMU
use SATUR,only: FF, A1, A2, AAA, BBB, SAT
use DELTA,only: DDM, DT
use CONST,only: PI, G
IMPLICIT NONE
INTEGER,INTENT(IN) :: NUMBER_D
DOUBLE PRECISION,INTENT(IN) :: RAD_CC(NUMBER_D), SPECT(NUMBER_D)
DOUBLE PRECISION,INTENT(INOUT) :: RAD_D(NUMBER_D)
DOUBLE PRECISION :: R3, DR3, C2, C0, P, Q, RAD_CR, SAT_CR, C4, A, &
C, D, CC, DD, CC2, DD2, RAD_ST, DRAA, DRA, DM, X1
INTEGER :: I
DDM = 0.0D0
!$OMP PARALLEL DO DEFAULT(SHARED) &
!$OMP PRIVATE(I,R3,DR3,C2,C0,P,Q,SAT,SAT_CR,C4,A) &
!$OMP PRIVATE (C,D,CC,DD,CC2,DD2,RAD_ST,DRAA,DRA,DM,RAD_CR,X1) &
!$OMP REDUCTION (+:DDM)
DO I=1,NUMBER_D
R3 = RAD_CC(I)**3
DR3 = RAD_D(I)**3-R3
IF(DR3.LT.1.0D-100) DR3 = 1.0D-100
C2 = -DSQRT(3.0D0*BBB*R3/AAA)
C0 = -R3
P = -0.3333333333D0*C2**2
Q = C0+0.074074074D0*C2**3
CALL CUBIC(P, Q, RAD_CR)
RAD_CR = RAD_CR - 0.3333333333D0*C2
SAT_CR = DEXP(AAA/RAD_CR-BBB*R3/(RAD_CR**3-R3))-1.0D0
DRA = DT*(SAT+1.0D0-DEXP(AAA/RAD_DROP(I)-BBB*R3/DR3))/ &
(FF*RAD_D(I))
IF(SAT.LT.SAT_CR) THEN
IF(DABS(SAT).LT.1.0D-10) THEN
P = -BBB*R3/AAA
Q = -R3
CALL CUBIC(P, Q, RAD_ST)
GO TO 22
END IF
C4 = DLOG(SAT+1.0D0)
A = -AAA/C4
C = (BBB-C4)*R3/C4
D = -A*R3
P = A*C-4.0D0*D
Q = -(A**2*D+C**2)
CALL CUBIC(P, Q, X1)
CC = DSQRT(A**2+4.D0*X1)
DD = DSQRT(X1**2-4.D0*D)
CC2 = 0.5D0*(A-CC)
IF(SAT.LT.0.0D0) THEN
DD2 = 0.5D0*(X1-DD)
RAD_ST = 0.5D0*(-CC2+DSQRT(CC2**2-4.0D0*DD2))
ELSE
DD2 = 0.5D0*(X1+DD)
RAD_ST = 0.5D0*(-CC2-DSQRT(CC2**2-4.0D0*DD2))
END IF
22 CONTINUE
DRAA = RAD_ST-RAD_D(I)
IF(ABS(DRAA).LT.ABS(DRA)) THEN
DRA = DRAA
DM = 1.3333333333333333D0*PI*WATER_DENSITY* &
(RAD_ST**3-RAD_D(I)**3)
ELSE
DM = 4.0D0*PI*WATER_DENSITY*RAD_D(I)**2*DRA
END IF
DDM = DDM+SPECT(I)*DM
RAD_D(I) = RAD_D(I) + DRA
ELSE
DM = 4.0D0*PI*WATER_DENSITY*RAD_D(I)**2*DRA
DDM = DDM+SPECT(I)*DM
RAD_D(I) = RAD_D(I) + DRA
END IF
END DO
!$OMP END PARALLEL DO
RETURN
END SUBROUTINE GR
SUBROUTINE CUBIC(P, Q, X)
IMPLICIT NONE
DOUBLE PRECISION,INTENT(IN) :: P, Q
DOUBLE PRECISION,INTENT(OUT) :: X
DOUBLE PRECISION :: DIS, PP, COSALFA,ALFA, QQ, U, V
DIS = (P/3.D0)**3+(0.5D0*Q)**2
IF(DIS.LT.0.0D0) THEN
PP = -P/3.0D0
COSALFA = -0.5D0*Q/DSQRT(PP**3)
ALFA = DACOS(COSALFA)
X = 2.0D0*DSQRT(PP)*DCOS(ALFA/3.0D0)
RETURN
ELSE
QQ = DSQRT(DIS)
U = -0.5D0*Q+QQ
V = -0.5D0*Q-QQ
IF(U.GE.0.0D0) THEN
U = U**0.333333333333333D0
ELSE
U = -(-U)**0.333333333333333D0
END IF
IF(V.GE.0.0D0) THEN
V = V**0.333333333333333D0
ELSE
V = -(-V)**0.333333333333333D0
END IF
X = U+V
END IF
RETURN
END SUBROUTINE CUBIC

Resources