Python Endless Looping

I have following code below:

for i in range(N):

    # Dispersed Source
    theta = np.random.uniform(0, np.pi, 1)
    phi = np.random.uniform(0, 2 * np.pi, 1)
    R = np.random.uniform(0, Ro, 1)

    x = R * np.sin(theta) * np.cos(phi)
    y = R * np.sin(theta) * np.sin(phi)  # ?
    z = R * np.cos(theta)

    x_vec, y_vec, z_vec, nu = particle_func(x, y, z)

    particle_trace = go.Scatter3d(
                        x=x_vec,
                        y=y_vec,
                        z=z_vec,
                        mode='lines'
                    )

    data.append(particle_trace)

    for j in range(int(nu)):
        x = x_vec[-1]
        y = y_vec[-1]
        z = z_vec[-1]

        x_vec_1, y_vec_1, z_vec_1, nu1 = particle_func(x, y, z)

        particle_trace_fiss = go.Scatter3d(
            x=x_vec_1,
            y=y_vec_1,
            z=z_vec_1,
            mode='lines'
        )

        data.append(particle_trace_fiss)

The problem is that the for loop needs to keep going as such:

for j in range(int(nu)):
        x = x_vec[-1]
        y = y_vec[-1]
        z = z_vec[-1]

        x_vec_1, y_vec_1, z_vec_1, nu1 = particle_func(x, y, z)

        particle_trace_fiss = go.Scatter3d(
            x=x_vec_1,
            y=y_vec_1,
            z=z_vec_1,
            mode='lines'
        )

        data.append(particle_trace_fiss)

 for k in range(int(nu1)):
        x = x_vec_1[-1]
        y = y_vec_1[-1]
        z = z_vec_1[-1]

        x_vec_2, y_vec_2, z_vec_2, nu2 = particle_func(x, y, z)

        particle_trace_fiss = go.Scatter3d(
            x=x_vec_2,
            y=y_vec_2,
            z=z_vec_2,
            mode='lines'
        )

        data.append(particle_trace_fiss)

 for l in range(int(nu2)):
        x = x_vec_2[-1]
        y = y_vec_2[-1]
        z = z_vec_2[-1]

        x_vec_3, y_vec_3, z_vec_3, nu3 = particle_func(x, y, z)

        particle_trace_fiss = go.Scatter3d(
            x=x_vec_3,
            y=y_vec_3,
            z=z_vec_3,
            mode='lines'
        )

        data.append(particle_trace_fiss)

Because the value of nu is a random number or zero (given from the particle_func function), the number of loops I need could potentially go on forever. That is, I could copy/paste

for k in range(int(nu#)):...
     data.append(particle_trace_fiss)

an infinite number of times and get what I am looking for (where nu# denotes the value of nu provided by the previous loop.

Please see the algorithm below for clarification:

Apologies if I missing clarity in my question, I will be happy to respond to any comments requesting clarification. All help is appreciated in advance, thanks!!

It seems like you just need to wrap your for in a while loop, maybe with an arbitrary cutoff.

This code:

for i in range(N):

    # Dispersed Source
    theta = np.random.uniform(0, np.pi, 1)
    phi = np.random.uniform(0, 2 * np.pi, 1)
    R = np.random.uniform(0, Ro, 1)

    x = R * np.sin(theta) * np.cos(phi)
    y = R * np.sin(theta) * np.sin(phi)  # ?
    z = R * np.cos(theta)

    x_vec, y_vec, z_vec, nu = particle_func(x, y, z)

    particle_trace = go.Scatter3d(
                        x=x_vec,
                        y=y_vec,
                        z=z_vec,
                        mode='lines'
                    )

    data.append(particle_trace)

    for j in range(int(nu)):
        x = x_vec[-1]
        y = y_vec[-1]
        z = z_vec[-1]

        x_vec_1, y_vec_1, z_vec_1, nu1 = particle_func(x, y, z)

        particle_trace_fiss = go.Scatter3d(
            x=x_vec_1,
            y=y_vec_1,
            z=z_vec_1,
            mode='lines'
        )

        data.append(particle_trace_fiss)

Becomes this code:

for i in range(N):

    # Dispersed Source
    theta = np.random.uniform(0, np.pi, 1)
    phi = np.random.uniform(0, 2 * np.pi, 1)
    R = np.random.uniform(0, Ro, 1)

    x = R * np.sin(theta) * np.cos(phi)
    y = R * np.sin(theta) * np.sin(phi)  # ?
    z = R * np.cos(theta)

    arbitrary_limit = 10

    x_vec = [x]
    y_vec = [y]
    z_vec = [z]
    nu_vec = [1]

    nu = nu_vec[-1]    # Updated - keep a list of values of nu

    while int(nu) != 0:

        for j in range(int(nu)):
            x = x_vec[-1]
            y = y_vec[-1]
            z = z_vec[-1]

            x_vec, y_vec, z_vec, nu = particle_func(x, y, z)

            nu_vec.append(nu)

            particle_trace = go.Scatter3d(
                            x=x_vec,
                            y=y_vec,
                            z=z_vec,
                            mode='lines'
                        )

            data.append(particle_trace)

I guess the question is, how are you going to decide when to stop? Is there anything you can do to constrain the value of nu ?

Update:Based on your comment, I removed the arbitrary_limit code. I also added a nu_vec to keep track of all the values of nu, per your explanation in another attempted answer.

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Python Endless Looping

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录