Torus opengl does not converge - python

I have a problem with my Torus in Opengl.
Here is my code on Python:
global numc, numt, r
twopi = 2 * math.pi
for i in range(0, numc):
for j in range(0, numt):
k = 1
while k >= 0:
s = (i + k) % numc + 0.5
t = j % numt
x = (1 + r * math.cos(s * twopi / numc)) * math.cos(t * twopi / numt)
y = (1 + r * math.cos(s * twopi / numc)) * math.sin(t * twopi / numt)
z = r * math.sin(s * twopi / numc)
Colors_torus.extend([1.0, 0.0, 0.0])
Torus.extend([x, y, z])
k -= 1
On the screen below on the right side you may observe that the circles do not converge.

Related

Vedo 3D-Gyroid structures STL export

I need to generate a double 3D gyroid structure. For this, I'm using vedo
from matplotlib import pyplot as plt
from scipy.constants import speed_of_light
from vedo import *
import numpy as np
# Paramters
a = 5
length = 100
width = 100
height = 10
pi = np.pi
x, y, z = np.mgrid[:length, :width, :height]
def gen_strut(start, stop):
'''Generate the strut parameter t for the gyroid surface. Create a linear gradient'''
strut_param = np.ones((length, 1))
strut_param = strut_param * np.linspace(start, stop, width)
t = np.repeat(strut_param[:, :, np.newaxis], height, axis=2)
return t
plt = Plotter(shape=(1, 1), interactive=False, axes=3)
scale=0.5
cox = cos(scale * pi * x / a)
siy = sin(scale * pi * y / a)
coy = cos(scale * pi * y / a)
siz = sin(scale * pi * z / a)
coz = cos(scale * pi * z / a)
six = sin(scale * pi * x / a)
U1 = ((six ** 2) * (coy ** 2) +
(siy ** 2) * (coz ** 2) +
(siz ** 2) * (cox ** 2) +
(2 * six * coy * siy * coz) +
(2 * six * coy * siz * cox) +
(2 * cox * siy * siz * coz)) - (gen_strut(0, 1.3) ** 2)
threshold = 0
iso1 = Volume(U1).isosurface(threshold).c('silver').alpha(1)
cube = TessellatedBox(n=(int(length-1), int(width-1), int(height-1)), spacing=(1, 1, 1))
iso_cut = cube.cutWithMesh(iso1).c('silver').alpha(1)
# Combine the two meshes into a single mesh
plt.at(0).show([cube, iso1], "Double Gyroid 1", resetcam=False)
plt.interactive().close()
The result looks quite good, but now I'm struggling with exporting the volume. Although vedo has over 300 examples, I did not find anything in the documentation to export this as a watertight volume for 3D-Printing. How can I achieve this?
I assume you mean that you want to extract a watertight mesh as an STL (?).
This is a non trivial problem because it is only well defined on a subset of the mesh regions where the in/out is not ambiguous, in those cases fill_holes() seems to do a decent job..
Other cases should be dealt "manually". Eg, you can access the boundaries with mesh.boundaries() and try to snap the vertices to a closest common vertex. This script is not a solution, but I hope can give some ideas on how to proceed.
from vedo import *
# Paramters
a = 5
length = 100
width = 100
height = 10
def gen_strut(start, stop):
strut_param = np.ones((length, 1))
strut_param = strut_param * np.linspace(start, stop, width)
t = np.repeat(strut_param[:, :, np.newaxis], height, axis=2)
return t
scale=0.5
pi = np.pi
x, y, z = np.mgrid[:length, :width, :height]
cox = cos(scale * pi * x / a)
siy = sin(scale * pi * y / a)
coy = cos(scale * pi * y / a)
siz = sin(scale * pi * z / a)
coz = cos(scale * pi * z / a)
six = sin(scale * pi * x / a)
U1 = ((six ** 2) * (coy ** 2) +
(siy ** 2) * (coz ** 2) +
(siz ** 2) * (cox ** 2) +
(2 * six * coy * siy * coz) +
(2 * six * coy * siz * cox) +
(2 * cox * siy * siz * coz)) - (gen_strut(0, 1.3) ** 2)
iso = Volume(U1).isosurface(0).c('silver').backcolor("p5").lw(1).flat()
cube = TessellatedBox(n=(length-1, width-1, height-1)).c('red5').alpha(1)
cube.triangulate().compute_normals()
cube.cut_with_mesh(iso).compute_normals()
print(iso.boundaries(return_point_ids=True))
print(cube.boundaries(return_point_ids=True))
print(iso.boundaries().join().lines())
show(iso, cube).close()
merge(iso, cube).clean().backcolor("p5").show().close()
iso.clone().fill_holes(15).backcolor("p5").show().close()

Is it possible to create a graph using MatPlotLib with data from a scientific model?

To preface, I am completely new to Python and MatPlotLib, and I am working on a school project to calculate the trajectory of a satellite around the earth. I was wondering if it was possible to grab the data from every point the model calculates and put it inside the graph, plotting time on the x-axis and height on the y-axis. I tried to directly plot the two values, but it doesn't seem to appear on the graph. Is it possible to put every value from the calculations in the graph, or will I need to have a direct formula for the line to plot it in a graph.
The air resistance also changes for certain heights, so it makes it harder to make a direct formula. Here's the code, I am aware it is terribly formatted but it being my first project all I need from it is to get a t,h-graph. Thanks in advance for the help!
from typing import Any, Union
import matplotlib.pyplot as plt
import numpy as np
data = []
t = 0
dt = 10
A = 10
Cw = 2.7
h = 300000
h0 = h
G = 6.67384 * 10 ** -11
M = 5.972 * 10 ** 24
m = 209.4 * A ** (3 / 2)
r = 6371000 + h
v: Union[float, Any] = (G * M / r) ** .5
vx = v
vy = 0
Px = 0
Py = r
while 0 < h < h0 + 100000:
# for _ in range(2592000):
t = t + dt
r = (Px ** 2 + Py ** 2) ** .5
Fg = G * M * m / r ** 2
Fgx = -Fg * Px / r
Fgy = -Fg * Py / r
h = r - 6371000
if h > 600000:
z = 1.607 * 10 ** -11 * 0.991169 ** (h / 1000)
else:
if h > 139000:
z = 3.848 * 10 ** -8 * 0.978294 ** (h / 1000)
else:
z = 1.225 * 0.863697 ** (h / 1000)
v = (vx ** 2 + vy ** 2) ** .5
Fwl = 0.5 * z * Cw * A * v ** 2
Fwlx = -Fwl * vx / v
Fwly = -Fwl * vy / v
ax = (Fgx + Fwlx) / m
ay = (Fgy + Fwly) / m
vx = vx + ax * dt
vy = vy + ay * dt
Px = Px + vx * dt
Py = Py + vy * dt
data += [[h, t]]
print(h)
print(t)
fig, ax = plt.subplots()
ax.plot(t, h)
plt.show()
As suggested by Michael Szczesny in the comment, you should indent data += [[h, t]] within the while loop.
Then I suggest you to plot with:
ax.plot(*zip(*data))
in order to separate h for t and get h(t) curve.
An other suggestion is to flip the order in which you store h and t in data:
data += [[h, t]]
in this way you will get a plot of the height with respect to time (and not the inverse).
Complete Code
from typing import Any, Union
import matplotlib.pyplot as plt
data = []
t = 0
dt = 10
A = 10
Cw = 2.7
h = 300000
h0 = h
G = 6.67384 * 10 ** -11
M = 5.972 * 10 ** 24
m = 209.4 * A ** (3 / 2)
r = 6371000 + h
v: Union[float, Any] = (G * M / r) ** .5
vx = v
vy = 0
Px = 0
Py = r
while 0 < h < h0 + 100000:
# for _ in range(2592000):
t = t + dt
r = (Px ** 2 + Py ** 2) ** .5
Fg = G * M * m / r ** 2
Fgx = -Fg * Px / r
Fgy = -Fg * Py / r
h = r - 6371000
if h > 600000:
z = 1.607 * 10 ** -11 * 0.991169 ** (h / 1000)
else:
if h > 139000:
z = 3.848 * 10 ** -8 * 0.978294 ** (h / 1000)
else:
z = 1.225 * 0.863697 ** (h / 1000)
v = (vx ** 2 + vy ** 2) ** .5
Fwl = 0.5 * z * Cw * A * v ** 2
Fwlx = -Fwl * vx / v
Fwly = -Fwl * vy / v
ax = (Fgx + Fwlx) / m
ay = (Fgy + Fwly) / m
vx = vx + ax * dt
vy = vy + ay * dt
Px = Px + vx * dt
Py = Py + vy * dt
data += [[h, t]]
fig, ax = plt.subplots()
ax.plot(*zip(*data))
plt.show()

How do you iterate and update a numpy array to solve a system of linear equations of form Ax=B?

So I am looking to solve a system of equations in python 3.7 with numpy. However, I need to solve the system of equations at the end of each iteration. During the iterations, it will solve some equations that will make up the contents of A and B to find x in the form of Ax=B. Upon solving for x I need to save these values to then solve the underlying equations for the following iteration to be reimplemented in A and B.
I have tried a more linear approach to solving the problem but it is not good for my end goal of solving the equation attached in the image. What I have done so far has also been attached below:
i = 0
while (y[i] >= 0 ): #Object is above water
t[i+1] = t[i] + dt
vx[i+1] = vx[i] + dt * ax[i] #Update the velocities
vy[i+1] = vy[i] + dt * ay[i]
v_ax[i+1] = (vx[i]*np.sin(phi/180*np.pi)) - (vy[i]*np.cos(phi/180*np.pi))
v_nor[i+1] = (vx[i]*np.cos(phi/180*np.pi)) + (vy[i]*np.sin(phi/180*np.pi))
F_wnor[i+1] = (Cd_a * A_da * rho_air * (v_nor[i] - v_wind*np.sin(phi/180*np.pi)) * abs(v_nor[i] - v_wind*np.sin(phi/180*np.pi)))/2
F_wax[i+1] = (Cd_a * A_da * rho_air * (v_ax[i] - v_wind*np.sin(phi/180*np.pi)) * abs(v_ax[i] - v_wind*np.sin(phi/180*np.pi)))/2
F_wx[i+1] = (-F_wax[i] * np.sin(phi/180*np.pi)) - (F_wnor[i] * np.cos(phi/180*np.pi))
F_wy[i+1] = (F_wax[i] * np.cos(phi/180*np.pi)) - (F_wnor[i] * np.sin(phi/180*np.pi))
ax[i+1] = F_wx[i]/M
ay[i+1] = (F_wx[i]/M) - g
y[i+1] = (y[i]+dt*vy[i])
x[i+1] = (x[i]+dt*vx[i])
i = i + 1
j = i
#under water velocities
# if y(t)>0: M*z'' = M.g - Fb + Fd + Fm
while (y[j] <= 0 and y[j] > -10):
if (abs(y[j]/r)< 2):
theta_degree = 2 * np.arccos(1 - (abs(y[j])/r))
theta = theta_degree/180*np.pi
m = ((rho_water * r**2)/2) * (((2*(np.pi)**3*(1-np.cos(theta))) / ( 3 * (2*np.pi-theta)**2)) \
+ (np.pi * (1-np.cos(theta)*1/3)) + (np.sin(theta)) - (theta))
dm_dz = ((rho_water * r)/np.sin(theta/2)) * (((2 * (np.pi)**3 / 3) * ((np.sin(theta) / (2*np.pi - theta)**2) \
+ (2 * (1-np.cos(theta)) / (2*np.pi - theta )**3))) + (np.pi * np.sin(theta) / 3) + np.cos(theta) - 1)
A_i = (r**2)/2 * (theta - np.sin(theta))
F_m[j] = - m * ay[j] - dm_dz * np.max(vy)*vy[j]
F_uwater[j] = (M * g) - (rho_water * A_i * g) - (Cd_y * rho_water * r * vy[j] * abs(vy[j]))
else:
m = np.pi * rho_water * r**2
dm_dz = 0
A_i = np.pi * r**2
F_m[j] = - m * ay[j] - dm_dz * vy[j]**2
F_uwater[j] = (M * g) - (rho_water * A_i * g) - (Cd_y * rho_water * r * vy[j] * abs(vy[j]))
print("Fully submerged")
t[j+1] = t[j] + dt
vx[j+1] = vx[j] + dt * ax[j] #Update the velocities
vy[j+1] = vy[j] + dt * ay[j]
ax[j+1] = F_wx[j]/M
ay[j+1] = (F_uwater[j] + F_m[j]/M)
y[j+1] = (y[j]+dt*vy[j])
x[j+1] = (x[j]+dt*vx[j])
print(y[j])
j = j + 1
I do not know how to go about this and help for getting started would be greatly appreciated!.
The problem I am trying to solve can be seen more clearly in the picture attached. System of equations I am trying to solve

Trouble with Runge-Kutta Method for Coulomb's Law

I am trying to simulate a two-dimensional two charge situation using Euler's Method and the Runge-Kutta 4th Order Method. I have gotten relatively expected answers using both methods. But I had never tried using my RK4 method with different initial conditions.
Starting with a positive initial velocity vx0 and a negative initial x-position x0, the code seems to work just fine. But when I flip them so that vx0 is negative and x0 is positive, I get different answers, when they should be symmetric.
I made sure that my Euler method worked for both variations of initial conditions to confirm that it was my RK4 function that was the problem. I struggled initially to apply the RK4 method for two dimensional motion, so it doesn't surprise me that this error has come about. Below is an image that might make the problem a bit clearer.
Here is where I calculate the constants:
# Initial conditions
time[0] = t = t0
vx1[0] = vx = vx0
vy1[0] = vy = vy0
x1[0] = x = x0
y1[0] = y = y0
for i in range(1, n + 1):
# Calculate our constants
k1vx = step * ax(x, y, q1, q2, m)
k1vy = step * ay(x, y, q1, q2, m)
k1x = step * vx
k1y = step * vy
k2vx = step * ax(x + 0.5 * k1x, y + 0.5 * k1y, q1, q2, m)
k2vy = step * ay(x + 0.5 * k1x, y + 0.5 * k1y, q1, q2, m)
k2x = step * (vx + (k1vx / 2))
k2y = step * (vy + (k1vy / 2))
k3vx = step * ax(x + 0.5 * k2x, y + 0.5 * k2y, q1, q2, m)
k3vy = step * ay(x + 0.5 * k2x, y + 0.5 * k2y, q1, q2, m)
k3x = step * (vx + (k2vx / 2))
k3y = step * (vy + (k2vy / 2))
k4vx = step * ax(x + k3x, y + k3y, q1, q2, m)
k4vy = step * ay(x + k3x, y + k3y, q1, q2, m)
k4x = step * (vx + k3vx)
k4y = step * (vy + k3vy)
# Update the values based on our calculated constants
vx1[i] = vx = vx + (k1vx + k2vx + k2vx + k3vx + k3vx + k4vx) / 6
vy1[i] = vy = vy + (k1vx + k2vy + k2vy + k3vy + k3vy + k4vy) / 6
x1[i] = x = x + ((k1x + 2 * k2x + 2 * k3x + k4x) / 6)
y1[i] = y = y + ((k1y + 2 * k2y + 2 * k3y + k4y) / 6)
# Update the time
time[i] = t = t0 + i * step
Here are the functions that I use for ax and ay in the previous code
def accel_rk4_x(x, y, q1, q2, m):
const = (q1 * q2) / (4 * math.pi * 8.854e-12 * m)
return const * (x / ((x ** 2 + y ** 2) ** 1.5))
def accel_rk4_y(x, y, q1, q2, m):
const = (q1 * q2) / (4 * math.pi * 8.854e-12 * m)
return const * (y / ((x ** 2 + y ** 2) ** 1.5))
I appreciate any help with this problem! I might just need a second pair of eyes.

Runge-Kutta 4th order to solve 2nd order ODE system using Python

I'm trying to solve system of two odes numerically by runge-kutta 4th order method.
initial system:
system to solve:
And I have very strange solution graph...
I have:
Correct graph:
I can't find trouble in my runge-kutta. Please, help me.
My code is here:
dt = 0.04
#initial conditions
t.append(0)
zdot.append(0)
z.append(A)
thetadot.append(0)
theta.append(B)
#derrive functions
def zdotdot(z_cur, theta_cur):
return -omega_z * z_cur - epsilon / 2 / m * theta_cur
def thetadotdot(z_cur, theta_cur):
return -omega_theta * theta_cur - epsilon / 2 / I * z_cur
i = 0
while True:
# runge_kutta
k1_zdot = zdotdot(z[i], theta[i])
k1_thetadot = thetadotdot(z[i], theta[i])
k2_zdot = zdotdot(z[i] + dt/2 * k1_zdot, theta[i])
k2_thetadot = thetadotdot(z[i], theta[i] + dt/2 * k1_thetadot)
k3_zdot = zdotdot(z[i] + dt/2 * k2_zdot, theta[i])
k3_thetadot = thetadotdot(z[i], theta[i] + dt/2 * k2_thetadot)
k4_zdot = zdotdot(z[i] + dt * k3_zdot, theta[i])
k4_thetadot = thetadotdot(z[i], theta[i] + dt * k3_thetadot)
zdot.append (zdot[i] + (k1_zdot + 2*k2_zdot + 2*k3_zdot + k4_zdot) * dt / 6)
thetadot.append (thetadot[i] + (k1_thetadot + 2*k2_thetadot + 2*k3_thetadot + k4_thetadot) * dt / 6)
z.append (z[i] + zdot[i + 1] * dt)
theta.append (theta[i] + thetadot[i + 1] * dt)
i += 1
Your state has 4 components, thus you need 4 slopes in each stage. It should be obvious that the slope/update for z can not come from k1_zdot, it has to be k1_z which is to be computed previously as
k1_z = zdot
and in the next stage
k2_z = zdot + dt/2*k1_zdot
etc.
But better is to use a vectorized interface
def derivs(t,u):
z, theta, dz, dtheta = u
ddz = -omega_z * z - epsilon / 2 / m * theta
ddtheta = -omega_theta * theta - epsilon / 2 / I * z
return np.array([dz, dtheta, ddz, ddtheta]);
and then use the standard formulas for RK4
i = 0
while True:
# runge_kutta
k1 = derivs(t[i], u[i])
k2 = derivs(t[i] + dt/2, u[i] + dt/2 * k1)
k3 = derivs(t[i] + dt/2, u[i] + dt/2 * k2)
k4 = derivs(t[i] + dt, u[i] + dt * k3)
u.append (u[i] + (k1 + 2*k2 + 2*k3 + k4) * dt / 6)
i += 1
and later unpack as
z, theta, dz, dtheta = np.asarray(u).T

Categories