Reputation: 33
I have been working on some code to carry out some analysis on data from an astrophysics model I have been using (sph specfically). I am currently having a problem whereby the graphs the code below outputs (ten of them), while they appear to be correct, are not in the correct order, ie. the one calling itself the graph for 30 degrees looks like the one 50 degrees should output, 80 should be 40 and so on, with no apparent pattern. Any help that could be offered to fix this would be much appreciated.
#Set inclincations of observation
id <- c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90)
i <- (id * pi) / 180
#Determine sign of velocity vector in direction of observer, then magnitude of said vector (using mask)
for (i in 1:length(i)) {
GD$vecdir <- (GD$Xvel * sin(i) + GD$Zvel * cos(i))
mask <- (GD$vecdir >= 0)
if (sum(mask) > 0) {
GD$vecmag[mask] <- sqrt((GD$Xvel^2 * sin(i)^2 ) + (GD$Zvel^2 * cos(i)^2))
}
if (sum(mask) < length(mask)) {
GD$vecmag[!mask] <- -(sqrt((GD$Xvel^2 * sin(i)^2 ) + (GD$Zvel^2 * cos(i)^2)))
}
h2 <- hist(GD$vecmag, breaks=720, plot=FALSE)
jpeg(paste("VMLOS", (i-1)*10, ".jpeg", sep = ""), width = 1280, height = 720)
print(plot(h2$mids, h2$counts, type="p", pch=4, lty=3, xlab="Velocity Vector Magnitude", ylab="counts", main=paste("VMLOS", (i-1)*10, sep="")))
print(lines(lowess(h2$mids, h2$counts, f=0.05), lwd=4, col="red"))
dev.off()
}
For context, i is the inclination in the xz plane from which the object at the origin is being observed, in radians, with the conversion from degrees shown in the code, then the vecdir section works out whether the vector magnitude of a set or particles moving away from the origin should be positive or negative with reference to the observer, then the mask section calculates the magnitudes of such. Additionally, the mask part of the code returns the warning:
1: In GD$vecmag[mask] <- sqrt((GD$Xvel^2 * sin(i)^2) + (GD$Zvel^2 * ... :
number of items to replace is not a multiple of replacement length
2: In GD$vecmag[!mask] <- -(sqrt((GD$Xvel^2 * sin(i)^2) + ... :
number of items to replace is not a multiple of replacement length
for each usage (so 20 warnings), which doesn't appear to negatively effect the output of the code, but is irritating, and I have been unable to fix it thus far.
Any help will be gratefully received.
Upvotes: 3
Views: 53
Reputation: 42649
For the warnings, you probably want this (note the indexing on the right-hand expression):
GD$vecmag[mask] <- sqrt((GD$Xvel^2 * sin(i)^2 ) + (GD$Zvel^2 * cos(i)^2))[mask]
# ...
GD$vecmag[!mask] <- -(sqrt((GD$Xvel^2 * sin(i)^2 ) + (GD$Zvel^2 * cos(i)^2)))[!mask]
This will change the results.
In addition, you are not iterating over the correct values. 1:length(i) are not the values in i. Change this:
i <- (id * pi) / 180
#Determine sign of velocity vector in direction of observer, then magnitude of said vector (using mask)
for (i in 1:length(i)) {
to this:
angles <- (id * pi) / 180
#Determine sign of velocity vector in direction of observer, then magnitude of said vector (using mask)
for (i in angles) {
The original code is using sin(1), sin(2), ..., rather than the values in the vector. That will also shuffle the results (and it may not be obvious that the wrong angles are being used).
Upvotes: 2