MHD Shock Waves - python-2.7

I need to solve these six equations (https://en.wikipedia.org/wiki/Shocks_and_discontinuities_(magnetohydrodynamics)) for MHD shock waves condition using scipy.fsolve. There are 6 unknown variables whose values need to be stored in the array z. While I am getting correct answers for some cases, in most cases the values are wrong. They vary according to the initial guess, what would be the correct initial guess so that it works for all cases.
Note- z[0] and z[1] denote pressure and density and can't be negative. So a correct answer will give positive values.
from scipy.optimize import fsolve, root
from scipy.constants import mu_0 as w
print("Assume the shock propogates in the x direction")
p1=float(input("Upstream pressure-"))
rho1=float(input("Upstream density-"))
vx1=float(input("Upstream velocity along x axis-"))
vy1=float(input("Upstream velocity along y axis-"))
bx1=float(input("Upstream magnetic field along x axis-"))
by1=float(input("Upstream magnetic field along y axis-"))
def eqn(x): #jump conditions
f1=(x[1]*x[2])-(rho1*vx1)
f2=x[0]+(x[1]*x[2]*x[2])+(x[5]*x[5]*0.5/w)-p1-(rho1*vx1*vx1)-by1*by1*0.5/w
f3=(x[1]*x[2]*x[3]-x[4]*x[5]/w)-rho1*vx1*vy1+bx1*by1/w
f4= x[4]-bx1
f5=x[2]*x[5]-x[4]*x[3]-vx1*by1+vy1*bx1
f6=x[1]*x[2]*(0.5*(x[2]**2+x[3]**2) +2.5*x[0]/x[1]) +x[2]*(x[5]**2)/w -x[4]*x[5]*x[3]/w - rho1*vx1*(0.5*(vx1**2+vy1**2) +2.5*p1/rho1) -(vx1*by1**2)/w + bx1*by1*vy1/w
return(f1,f2,f3,f4,f5,f6)
y=[2*p1,4*rho1,0.5,0.5,bx1*2,by1*2] #initial guess value
z=fsolve(eqn,y)
print '\n','Upstream Pressure- ',p1,'\t'*3,'Downstream Pressure- ',z[0]
print 'Upstream Density- ',rho1,'\t'*4,'Downstream Density- ',z[1]
print 'Upstream velocity along x axis- ',vx1,'\t'*2,'Downstream velocity along x axis- ',z[2]
print 'Upstream velocity along y axis- ',vy1,'\t'*2,'Downstream velocity along y axis- ',z[3]
print 'Upstream magnetic field along x axis- ',bx1,'\t','Downstream magnetic field along x axis- ',z[4]
print 'Upstream magnetic field along y axis- ',by1,'\t','Downstream magnetic field along y axis- ',z[5]

Related

Python regridding a 2D rotated mesh grid

I have a 2D rotated rectangular grid with longitude and latitude values with dimension [405, 555] and I can't understand how to regrid it, I want a rectangular grid with the axis "parallel" to Parallels and Meridians.
I tried to use scipy interpolation functions as: griddata or RegularGridInterpolator, but I always have problem with the old grid dimension because they are 2D and rotated, the values are not repeated and I don't know how to solve it.
Sorry I can't post my original code and data because they are proprietary and I don't know how to create a MWE.
I tried this:
import scipy.interpolate.ndgriddata as ndgriddata
import numpy as np
x = np.linspace(35.0, 42.0, 405) # my new longitude
y = np.linspace(36.0, 48.0, 555) # my new latitude
X, Y = np.meshgrid(x, y)
# grid_lon: old 2D array [405, 555] for the longitude
# grid_lat: old 2D array [405, 555] for the latitude
# data: old 2D array [405, 555] for the data
test = ndgriddata.griddata((grid_lon, grid_lat), data, (X, Y), method="linear")
but, of course I obtain the error:
ValueError: invalid shape for input data points
I know like this is complicated to answer it but if someone have an idea, please let me know.
Thanks,
Ciccio
I had just to flatten the old coordinates and the data.
ndgriddata.griddata((grid_lon.flatten(), grid_lat.flatten()),
data.flatten(), (X, Y), method="linear")

Fitting a Gaussian, getting a straight line. Python 2.7

As my title suggests, I'm trying to fit a Gaussian to some data and I'm just getting a straight line. I've been looking at these other discussion Gaussian fit for Python and Fitting a gaussian to a curve in Python which seem to suggest basically the same thing. I can make the code in those discussions work fine for the data they provide, but it won't do it for my data.
My code looks like this:
import pylab as plb
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy import asarray as ar,exp
y = y - y[0] # to make it go to zero on both sides
x = range(len(y))
max_y = max(y)
n = len(y)
mean = sum(x*y)/n
sigma = np.sqrt(sum(y*(x-mean)**2)/n)
# Someone on a previous post seemed to think this needed to have the sqrt.
# Tried it without as well, made no difference.
def gaus(x,a,x0,sigma):
return a*exp(-(x-x0)**2/(2*sigma**2))
popt,pcov = curve_fit(gaus,x,y,p0=[max_y,mean,sigma])
# It was suggested in one of the other posts I looked at to make the
# first element of p0 be the maximum value of y.
# I also tried it as 1, but that did not work either
plt.plot(x,y,'b:',label='data')
plt.plot(x,gaus(x,*popt),'r:',label='fit')
plt.legend()
plt.title('Fig. 3 - Fit for Time Constant')
plt.xlabel('Time (s)')
plt.ylabel('Voltage (V)')
plt.show()
The data I am trying to fit is as follows:
y = array([ 6.95301373e+12, 9.62971320e+12, 1.32501876e+13,
1.81150568e+13, 2.46111132e+13, 3.32321345e+13,
4.45978682e+13, 5.94819771e+13, 7.88394616e+13,
1.03837779e+14, 1.35888594e+14, 1.76677210e+14,
2.28196006e+14, 2.92781632e+14, 3.73133045e+14,
4.72340762e+14, 5.93892782e+14, 7.41632194e+14,
9.19750269e+14, 1.13278296e+15, 1.38551838e+15,
1.68291212e+15, 2.02996957e+15, 2.43161742e+15,
2.89259207e+15, 3.41725793e+15, 4.00937676e+15,
4.67187762e+15, 5.40667931e+15, 6.21440313e+15,
7.09421973e+15, 8.04366842e+15, 9.05855930e+15,
1.01328502e+16, 1.12585509e+16, 1.24257598e+16,
1.36226443e+16, 1.48356404e+16, 1.60496345e+16,
1.72482199e+16, 1.84140400e+16, 1.95291969e+16,
2.05757166e+16, 2.15360187e+16, 2.23933053e+16,
2.31320228e+16, 2.37385276e+16, 2.42009864e+16,
2.45114362e+16, 2.46427484e+16, 2.45114362e+16,
2.42009864e+16, 2.37385276e+16, 2.31320228e+16,
2.23933053e+16, 2.15360187e+16, 2.05757166e+16,
1.95291969e+16, 1.84140400e+16, 1.72482199e+16,
1.60496345e+16, 1.48356404e+16, 1.36226443e+16,
1.24257598e+16, 1.12585509e+16, 1.01328502e+16,
9.05855930e+15, 8.04366842e+15, 7.09421973e+15,
6.21440313e+15, 5.40667931e+15, 4.67187762e+15,
4.00937676e+15, 3.41725793e+15, 2.89259207e+15,
2.43161742e+15, 2.02996957e+15, 1.68291212e+15,
1.38551838e+15, 1.13278296e+15, 9.19750269e+14,
7.41632194e+14, 5.93892782e+14, 4.72340762e+14,
3.73133045e+14, 2.92781632e+14, 2.28196006e+14,
1.76677210e+14, 1.35888594e+14, 1.03837779e+14,
7.88394616e+13, 5.94819771e+13, 4.45978682e+13,
3.32321345e+13, 2.46111132e+13, 1.81150568e+13,
1.32501876e+13, 9.62971320e+12, 6.95301373e+12,
4.98705540e+12])
I would show you what it looks like, but apparently I don't have enough reputation points...
Anyone got any idea why it's not fitting properly?
Thanks for your help :)
The importance of the initial guess, p0 in curve_fit's default argument list, cannot be stressed enough.
Notice that the docstring mentions that
[p0] If None, then the initial values will all be 1
So if you do not supply it, it will use an initial guess of 1 for all parameters you're trying to optimize for.
The choice of p0 affects the speed at which the underlying algorithm changes the guess vector p0 (ref. the documentation of least_squares).
When you look at the data that you have, you'll notice that the maximum and the mean, mu_0, of the Gaussian-like dataset y, are
2.4e16 and 49 respectively. With the peak value so large, the algorithm, would need to make drastic changes to its initial guess to reach that large value.
When you supply a good initial guess to the curve fitting algorithm, convergence is more likely to occur.
Using your data, you can supply a good initial guess for the peak_value, the mean and sigma, by writing them like this:
y = np.array([...]) # starting from the original dataset
x = np.arange(len(y))
peak_value = y.max()
mean = x[y.argmax()] # observation of the data shows that the peak is close to the center of the interval of the x-data
sigma = mean - np.where(y > peak_value * np.exp(-.5))[0][0] # when x is sigma in the gaussian model, the function evaluates to a*exp(-.5)
popt,pcov = curve_fit(gaus, x, y, p0=[peak_value, mean, sigma])
print(popt) # prints: [ 2.44402560e+16 4.90000000e+01 1.20588976e+01]
Note that in your code, for the mean you take sum(x*y)/n , which is strange, because this would modulate the gaussian by a polynome of degree 1 (it multiplies a gaussian with a monotonously increasing line of constant slope) before taking the mean. That will offset the mean value of y (in this case to the right). A similar remark can be made for your calculation of sigma.
Final remark: the histogram of y will not resemble a Gaussian, as y is already a Gaussian. The histogram will merely bin (count) values into different categories (answering the question "how many datapoints in y reach a value between [a, b]?").

GeoDjango: Determine the area of a polygon

In my model I have a polygon field defined via
polygon = models.PolygonField(srid=4326, geography=True, null=True, blank=True)
When I want to determine the area of the polygon, I call
area_square_degrees = object.polygon.area
But how can I convert the result in square degrees into m2 with GeoDjango?
This answer does not work, since area does not have a method sq_m. Is there any built-in conversion?
You need to transform your data to the correct spatial reference system.
area_square_local_units = object.polygon.transform(srid, clone=False).area
In the UK you might use the British National Grid SRID of 27700 which uses meters.
area_square_meters = object.polygon.transform(27700, clone=False).area
You may or may not want to clone the geometry depending on whether or not you need to do anything else with it in its untransformed state.
Docs are here https://docs.djangoproject.com/en/1.8/ref/contrib/gis/geos/
I have struggled a lot with this, since i could'nt find a clean solution. The trick is you have to use the postgis capabilities (and and thus its only working with postgis..):
from django.contrib.gis.db.models.functions import Area
loc_obj = Location.objects.annotate(area_=Area("poly")).get(pk=??)
# put the primary key of the object
print(loc_obj.area_) # distance object, result should be in meters, but you can change to the unit you want, e.g .mi for miles etc..
The models.py:
class Location(models.Model):
poly = gis_models.PolygonField(srid=4326, geography=True)
It's i think the best way to do it if you have to deal with geographic coordinates instead of projections. It does handle the curve calculation of the earth, and the result is precise even with big distance/area
I needed an application to get the area of poligons around the globe and if I used an invalid country/region projection I got the error OGRException: OGR failure
I ended using an OpenLayers implementation
using the 4326 projection (is the default projection) to avoid concerning about every country/region specific projection.
Here is my code:
import math
def getCoordsM2(coordinates):
d2r = 0.017453292519943295 # Degrees to radiant
area = 0.0
for coord in range(0, len(coordinates)):
point_1 = coordinates[coord]
point_2 = coordinates[(coord + 1) % len(coordinates)]
area += ((point_2[0] - point_1[0]) * d2r) *\
(2 + math.sin(point_1[1] * d2r) + math.sin(point_2[1] * d2r))
area = area * 6378137.0 * 6378137.0 / 2.0
return math.fabs(area)
def getGeometryM2(geometry):
area = 0.0
if geometry.num_coords > 2:
# Outer ring
area += getCoordsM2(geometry.coords[0])
# Inner rings
for counter, coordinates in enumerate(geometry.coords):
if counter > 0:
area -= getCoordsM2(coordinates)
return area
Simply pass your geometry to getGeometryM2 function and you are done!
I use this function in my GeoDjango model as a property.
Hope it helps!
If Its earths surface area that you are talking about, 1 square degree has 12,365.1613 square km. So multiple your square degree and multiply by 10^6 to convert to meters.

How to plot a scatter diagram using rpy2 in python?

I have a dataset like below in dictionary format,
data={'a': [10, 11,12,5,4,3,1], 'b': [7, 18,5,11,9,2,0]}
How we can make a scatter plot in python using rpy2? where x axis is the months and y axis are the mutiples of 5? we need to plot the graph with the above values where a and b are the data points
Months should be based on the length of each key i.e for the above data we have 7 months since we have 7 data points
This is a pretty involved data structure, and it's not completely clear what you're looking to do in terms of plotting. Here are a few hints, but it'd be easiest to help you if you would post the code you've tried but hasn't worked.
The R plot function takes two vectors corresponding to the x-axis values (months, here), and y-axis values (frequencies?). You'll want to go through your graph_data dictionary and calculate the y-axis values you want to plot for each month, and then make a corresponding vector for x containing the month numbers. For example:
x = [1,2,3,4]
y = [0.7, 0.9, 0.2, 0.4]
To do the plotting from rpy2, you'll need to convert the lists to vectors like so:
from rpy2 import robjects
x_vector = robjects.IntVector(x)
y_vector = robjects.FloatVector(y)
Then do the plotting:
robjects.r.plot(x_vector, y_vector, xlab="month", ylab="freq", main="")

SetViewBox moving the paper

I am using the setViewBox() function in Raphael 2. The width and height is multiplied by a value like (1.2, 1.3 ...). This changes the magnification/ zooming properly but the x and y which I have given as 0,0 makes the paper display its contents after some offset. If i modify the x and y to some positive value after the rendering( using firebug!!) then the top left of the paper moves back and above to its right position. I want to know how will the value be calculated. I have no idea about how the x,y affect the viewbox. If anybody can give me any pointers for this it will be a real help.
I have tried giving the difference between the width/ height divided by 2. Also I must mention that I am not rendering an image but various raphael shapes e.g. rects, paths text etc. in my paper.
Looking forward to some help!
Kavita
this is an example showing how to calculate the setViewBox values, I included jquery (to get my SVG cocntainer X and Y : $("#"+map_name).offset().left and $("#"+map_name).offset().top) and after that I calculated how much zoom I need :
var original_width = 777;
var original_height = 667;
var zoom_width = map_width*100/original_width/100;
var zoom_height = map_height*100/original_height/100;
if(zoom_width<=zoom_height)
zoom = zoom_width;
else
zoom = zoom_height;
rsr.setViewBox($("#"+map_name).offset().left, $("#"+map_name).offset().top, (map_width/zoom), (map_height/zoom));
did you put the center of your scaling to 0,0 like:
element.scale(1.2,1.2,0,0);
this can scale your element without moving the coordinates of the top left corner.