This article shows how to draw a geometry from given line elements. We provide three examples:
Note that we use spherical coordinate (r,theta,phi), but plot on cartesian coordinate (x,y,z).
0. Main Program
For each line element, the main program composes of:
- Calling functions
- Initialization (we use the following initialization for every example)
init <- c(1., 2.001, pi/2., 0., 0.0001, 10, 5, 10) # M, r > 2M, theta, phi, distance, n_grid, r_grid, phi_grid
view_angle <- c(45.,0.) # theta, phi in degree
- Constructing coordinates from the line element in spherical coordinate
- Transforming from spherical to cartesian coordinate
- Visualization
4. Codes
4.1. Circle.R
Circle<-function(init){
r <- init[2]
theta <- init[3]
#phi <- init[4]
distance <- init[5]
#n_grid <- init[6]
delta_phi <- Delta_Phi_Circle(r,theta,distance)
n_grid <- as.integer((2*pi/delta_phi) + 1)
length <- n_grid+1
sphere_coor <- array(data = 0,dim = c(length,3))
for (i in 1:length){
sphere_coor[i,1] <- r
sphere_coor[i,2] <- theta
sphere_coor[i,3] <- (i-1.) * delta_phi
}
sphere_coor
}
4.2. Delta_Phi_Circle.R
Delta_Phi_Circle<-function(r,theta,distance){
delta_phi <- abs(distance / ( r*sin(theta) ))
delta_phi
}
4.3. Sphere_2_XYZ.R
Sphere_2_XYZ<-function(sphere_coor){
length <- dim(sphere_coor)[1]
xyz <- array(data = 0,dim = c(length,3))
for(i in 1:length){
r <- sphere_coor[i,1]
theta <- sphere_coor[i,2]
phi <- sphere_coor[i,3]
xyz[i,1] <- r*sin(theta)*cos(phi)
xyz[i,2] <- r*sin(theta)*sin(phi)
xyz[i,3] <- r*cos(theta)
}
xyz
}
4.4. Sphere.R
Sphere<-function(init){
r <- init[2]
#theta <- init[3]
#phi <- init[4]
distance <- init[5]
n_grid <- init[6]
phi_grid <- init[8]
delta_theta <- pi/n_grid
length_theta <- n_grid+1
sphere_coor <- vector(mode = "list",length = length_theta)
delta_phi <- 2*pi/phi_grid
n_grid_phi <- phi_grid
length_phi <- n_grid_phi+1
for(i in 1:length_theta){
theta <- (i-1.) * delta_theta
sphere_coor[[i]] <- array(data = 0,dim = c(length_phi,3))
for (j in 1:length_phi){
sphere_coor[[i]][j,1] <- r
sphere_coor[[i]][j,2] <- theta
sphere_coor[[i]][j,3] <- (j-1.) * delta_phi
}
}
sphere_coor
}
4.5. Show3D.R
Show3D<-function(xyz_coor,view_angle){
length <- length(xyz_coor)
total <- 0
for(i in 1:length){
total <- total + dim(xyz_coor[[i]])[1]
}
x <- array(data = 0,dim = c(total))
y <- array(data = 0,dim = c(total))
z <- array(data = 0,dim = c(total))
k <- 1
for(i in 1:length){
ll <- dim(xyz_coor[[i]])[1]
for(j in 1:ll){
x[k] <- xyz_coor[[i]][j,1]
y[k] <- xyz_coor[[i]][j,2]
z[k] <- xyz_coor[[i]][j,3]
k <- k+1
}
}
library(plot3D)
lines3D(x = x,y = y,z = z,theta=view_angle[1], phi=view_angle[2])
}
4.6. WormHole.R
WormHole<-function(init){
m <- init[1]
r <- init[2]
#theta <- init[3]
#phi <- init[4]
distance <- init[5]
n_grid <- init[6]
r_grid <- init[7]
phi_grid <- init[8]
delta_theta <- (pi/4)/n_grid
length_theta <- n_grid+1
sphere_coor <- vector(mode = "list",length = length_theta)
delta_phi <- 2*pi/phi_grid
n_grid_phi <- phi_grid
length_phi <- n_grid_phi+1
for(i in 1:length_theta){
theta <- (i-1.) * delta_theta
sphere_coor[[i]] <- vector(mode = "list",length = length_phi)
for (j in 1:length_phi){
phi <- (j-1.) * delta_phi
sphere_coor[[i]][[j]] <- array(data = 0,dim = c(r_grid,3))
for(k in 1:r_grid){
sphere_coor[[i]][[j]][k,1] <- r + Delta_R_WormHole(r,theta,distance,m)
sphere_coor[[i]][[j]][k,2] <- theta
sphere_coor[[i]][[j]][k,3] <- phi
r <- sphere_coor[[i]][[j]][k,1]
}
}
}
sphere_coor
}
4.7. Delta_R_WormHole.R
Delta_R_WormHole<-function(r,theta,distance,m){
delta_r = sqrt(abs(distance**2 / ( 1- (2*m/r) )))
delta_r
}
4.8. Show3D_WormHole.R
Show3D_WormHole<-function(xyz_coor, view_angle){
length <- length(xyz_coor)
total <- 0
for(i in 1:length){
ll <- length(xyz_coor[[i]])
for(j in 1:ll){
total <- total + dim(xyz_coor[[i]][[j]])[1]
}
}
x <- array(data = 0,dim = c(total))
y <- array(data = 0,dim = c(total))
z <- array(data = 0,dim = c(total))
k <- 1
k <- 1
for(i in 1:length){
ll <- length(xyz_coor[[i]])
for(j in 1:ll){
lll <- dim(xyz_coor[[i]][[j]])[1]
for(p in 1:lll){
x[k] <- xyz_coor[[i]][[j]][p,1]
y[k] <- xyz_coor[[i]][[j]][p,2]
z[k] <- xyz_coor[[i]][[j]][p,3]
k <- k+1
}
}
}
library(plot3D)
lines3D(x = x,y = y,z = z,theta=view_angle[1], phi=view_angle[2])
}
4.9. Main.R
source("Circle.R")
source("Delta_Phi_Circle.R")
source("Sphere_2_XYZ.R")
source("Sphere.R")
source("Show3D.R")
source("WormHole.R")
source("Delta_R_WormHole.R")
source("Show3D_WormHole.R")
################################################################################
init <- c(1., 2.001, pi/2., 0., 0.0001, 10, 5, 10) # M, r > 2M, theta, phi, distance, n_grid, r_grid, phi_grid
view_angle <- c(45.,0.) # theta, phi in degree
################################################################################
print("Hello World!")
if(init[2]>2*init[1]){
sphere_coor <- Circle(init)
xyz_coor <- Sphere_2_XYZ(sphere_coor)
plot(x = xyz_coor[,1],y = xyz_coor[,2], type="l")
readline()
sphere_coor <- Sphere(init)
length <- length(sphere_coor)
xyz_coor <- vector(mode = "list", length = (length-2))
for(i in 1:(length-2)){
xyz_coor[[i]] <- Sphere_2_XYZ(sphere_coor[[i+1]])
}
Show3D(xyz_coor, view_angle)
readline()
sphere_coor <- WormHole(init)
length <- length(sphere_coor)
xyz_coor <- vector(mode = "list", length = length)
for(i in 1:length){
ll <- length(sphere_coor[[i]])
xyz_coor[[i]] <- vector(mode = "list",length = ll)
for(j in 1:ll)
xyz_coor[[i]][[j]] <- Sphere_2_XYZ(sphere_coor[[i]][[j]])
}
Show3D_WormHole(xyz_coor, view_angle)
} else {
print("Error: r>2M")
}
print("End program.")