简体   繁体   English

如何从 Julia 的 if 语句内部中断 for 循环?

[英]How to break a for loop from inside an if statement in Julia?

I am trying to translate a python modelisation (not very effective) into a Julia code (more useful in this context) but I didn't find how to break a for loop and how to print a value in this context.我正在尝试将 python 模型化(不是很有效)转换为 Julia 代码(在这种情况下更有用),但我没有找到如何打破 for 循环以及如何在这种情况下打印值。

Python code Python代码

for i in np.range(1, nt)
    if t[x == s] == 100:
        s_time = i*dt
        print(s_time)
        break

Julia code I tried :我试过的朱莉娅代码:

for i = 1:nt
    if round.(Int, t[x.==s]) == 100
        s_time = i*dt
        println(s_time)
        break
    end
end

The variables here are not important, but dt is a value that allows me to know the time in [y] in the for loop in function of i , and t[x.==s] == 100 allows me to know when I want to break the loop.这里的变量并不重要,但dt是一个值,它允许我知道i函数中 for 循环中[y]中的时间,而t[x.==s] == 100允许我知道我何时想打破循环。

The Julia code do not returns me any error but do not break and do not print anything. Julia 代码不会返回任何错误,但不会中断也不会打印任何内容。

Full code (for Jupyter Notebook users) :完整代码(适用于 Jupyter Notebook 用户):

# NUMERICS
ti = 30  # surface temperature
td = 1000  # dyke temperature
tel = 30  # right lim temperature
teg = tel  # left lim temperature
x1 = 80  # central position dyke 2
x2 = 110  # central position dyke 1
d1 = 24  # dyke diameter
d2 = 10  # dyke diameter
s = 160  # probe position

yd1 = 0  # dyke 1 intrusion year
yd2 = 15  # dyke 2 intrusion year

d = 40  # rock diffusivity
size = 200  # size of the model
ttot = 30;  # total time in year

nx = 2001  # cells number
dx = size/(nx-1)  # cells size
dt = dx^2/(2.1*d)  # frame condition
nt = round(Int, ttot/dt)  # frame number
x = LinRange(0, size, nx); # size model

# INITIALISATION
indexing_1_l = round(Int, (x1 - d1/2)*nx/size)
indexing_1_r = round(Int, (x1 + d1/2)*nx/size)
indexing_2_l = round(Int, (x2 - d2/2)*nx/size)
indexing_2_r = round(Int, (x2 + d2/2)*nx/size);

t = ti * ones(nx)
t[indexing_1_l:indexing_1_r] .= td;
using Plots
gr() ;

# PLOTTING LOOP
for i = 1:nt 
    
    q = -d*(diff(t)/dx)
    dcdt = -diff(q)/dx
    t[2:end-1] = t[2:end-1] + dcdt*dt
    
    t[1] = teg
    t[end] = tel
    
    if i == round(Int, yd2/dt)
        t[indexing_2_l:indexing_2_r] .= td
    end
    
    # IF STATEMENT THAT DOESN'T WORK
    if round.(Int, t[x.==s]) == 100
        s_time = i*dt
        println("$s_time")
        break
    end

    if mod(i, 1000) == 0
        IJulia.clear_output(true)
        pt = plot(x, t, label = :false, c="red", title=" $(round.(Int, i*dt)) [y] - Dyke intrusion", 
            xlabel="Distance [m]", ylabel="Temperature [°C]", ylims=(0, 1000), size = (800, 600))
        Plots.display(pt)
    end
    
end

This will never return true because an array cannot be equal to a number:这永远不会返回true因为数组不能等于数字:

if round.(Int, t[x.==s]) == 100

Look at this看这个

julia> [100] == 100
false

It's not completely clear what you want it to mean, but perhaps this不完全清楚你想要它的意思,但也许这个

if all(round.(Int, t[x.==s]) .== 100)

or perhaps any ?或者any

you have a syntax error!你有一个语法错误! println(s_time) println(s_time)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM