def calcIdealAngles2 (desiredh, chi, phi, UBmatrix, wavelength, stars): "Calculates the twotheta, theta, and omega values for a desired h vector. Uses chi and phi from calcScatteringPlane." #Accepts the desired h vector, chi, phi, the UB matrix, the wavelength, and the stars dictionary desiredhp = np.dot(UBmatrix, desiredh) #Old code (scipy.optimize.fsolve) produced inaccurate results with far-off estimates #solutions = scipy.optimize.fsolve(equations, x0, args=(h1p, h2p, wavelength)) q = calcq (desiredh[0], desiredh[1], desiredh[2], stars) twotheta = 2.0 * np.arcsin(wavelength * q / 4.0 / np.pi) x0 = [0.0] p = NLSP(secondequations, x0, args=(desiredhp, chi, phi, wavelength, twotheta)) r = p.solve('nlp:ralg') omega = r.xf[0] theta = twotheta/2.0 + omega # ------ ALTERNATE SOLUTION FOR THETA ------ #theta = r.xf[1] # ------ SOLVER POTENTIALLY INACCURATE FOR THETA ------ solutions = [twotheta, theta, omega] return np.degrees(solutions) #% 360
def calcIdealAngles2(desiredh, chi, phi, UBmatrix, wavelength, stars): "Calculates the twotheta, theta, and omega values for a desired h vector. Uses chi and phi from calcScatteringPlane." #Accepts the desired h vector, chi, phi, the UB matrix, the wavelength, and the stars dictionary desiredhp = np.dot(UBmatrix, desiredh) #Old code (scipy.optimize.fsolve) produced inaccurate results with far-off estimates #solutions = scipy.optimize.fsolve(equations, x0, args=(h1p, h2p, wavelength)) q = calcq(desiredh[0], desiredh[1], desiredh[2], stars) twotheta = 2.0 * np.arcsin(wavelength * q / 4.0 / np.pi) x0 = [0.0] p = NLSP(secondequations, x0, args=(desiredhp, chi, phi, wavelength, twotheta)) r = p.solve('nlp:ralg') omega = r.xf[0] theta = twotheta / 2.0 + omega # ------ ALTERNATE SOLUTION FOR THETA ------ #theta = r.xf[1] # ------ SOLVER POTENTIALLY INACCURATE FOR THETA ------ solutions = [twotheta, theta, omega] return np.degrees(solutions) #% 360
def calcRefineUB(observations, wavelength): #observations are an array of dictionaries for each observed reflection hvectors = [] Uv = [] for i in range(0, len(observations)): h = [observations[i]['h'], observations[i]['k'], observations[i]['l']] hvectors.append(h) ''' sys.stderr.write('i %3.4f \n'%(observations[i]['h'],)) sys.stderr.write('i %3.4f \n'%(observations[i]['k'],)) sys.stderr.write('i %3.4f \n'%(observations[i]['l'],)) sys.stderr.write('i %3.4f \n'%(observations[i]['twotheta'],)) sys.stderr.write('i %3.4f \n'%(observations[i]['theta'],)) sys.stderr.write('i %3.4f \n'%(observations[i]['chi'],)) sys.stderr.write('i %3.4f \n'%(observations[i]['phi'],)) ''' theta = np.radians(observations[i]['theta']) chi = np.radians(observations[i]['chi']) phi = np.radians(observations[i]['phi']) twotheta = np.radians(observations[i]['twotheta']) omega = theta - twotheta / 2.0 u1p = np.cos(omega) * np.cos(chi) * np.cos(phi) - np.sin( omega) * np.sin(phi) u2p = np.cos(omega) * np.cos(chi) * np.sin(phi) + np.sin( omega) * np.cos(phi) u3p = np.cos(omega) * np.sin(chi) uv1p = 2.0 * np.sin(twotheta / 2) / wavelength * u1p uv2p = 2.0 * np.sin(twotheta / 2) / wavelength * u2p uv3p = 2.0 * np.sin(twotheta / 2) / wavelength * u3p Uv.append(uv1p) Uv.append(uv2p) Uv.append(uv3p) x0 = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] p = NLSP(UBRefinementEquations, x0, args=(hvectors, Uv)) r = p.solve('nlp:ralg') a = r.xf[0] b = r.xf[1] c = r.xf[2] d = r.xf[3] e = r.xf[4] f = r.xf[5] g = r.xf[6] h = r.xf[7] j = r.xf[8] UB = [[a, b, c], [d, e, f], [g, h, j]] return UB
def calcRefineUB(observations, wavelength): #observations are an array of dictionaries for each observed reflection hvectors = [] Uv = [] for i in range(0, len(observations)): h = [observations[i]['h'], observations[i]['k'], observations[i]['l']] hvectors.append(h) ''' sys.stderr.write('i %3.4f \n'%(observations[i]['h'],)) sys.stderr.write('i %3.4f \n'%(observations[i]['k'],)) sys.stderr.write('i %3.4f \n'%(observations[i]['l'],)) sys.stderr.write('i %3.4f \n'%(observations[i]['twotheta'],)) sys.stderr.write('i %3.4f \n'%(observations[i]['theta'],)) sys.stderr.write('i %3.4f \n'%(observations[i]['chi'],)) sys.stderr.write('i %3.4f \n'%(observations[i]['phi'],)) ''' theta = np.radians(observations[i]['theta']) chi = np.radians(observations[i]['chi']) phi = np.radians(observations[i]['phi']) twotheta = np.radians(observations[i]['twotheta']) omega = theta - twotheta/2.0 u1p = np.cos(omega)*np.cos(chi)*np.cos(phi) - np.sin(omega)*np.sin(phi) u2p = np.cos(omega)*np.cos(chi)*np.sin(phi) + np.sin(omega)*np.cos(phi) u3p = np.cos(omega)*np.sin(chi) uv1p = 2.0*np.sin(twotheta/2) / wavelength * u1p uv2p = 2.0*np.sin(twotheta/2) / wavelength * u2p uv3p = 2.0*np.sin(twotheta/2) / wavelength * u3p Uv.append(uv1p) Uv.append(uv2p) Uv.append(uv3p) x0 = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] p = NLSP(UBRefinementEquations, x0, args=(hvectors, Uv)) r = p.solve('nlp:ralg') a = r.xf[0] b = r.xf[1] c = r.xf[2] d = r.xf[3] e = r.xf[4] f = r.xf[5] g = r.xf[6] h = r.xf[7] j = r.xf[8] UB = [[a, b, c], [d, e, f], [g, h, j]] return UB
def test(complexity=0, **kwargs): f = lambda x: (x[0]**3 + x[1]**3 - 9, x[0] - 0.5 * x[1], cos(x[2]) + x[0] - 1.5) #optional: gradient def df(x): df = zeros((3, 3)) df[0, 0] = 3 * x[0]**2 df[0, 1] = 3 * x[1]**2 df[1, 0] = 1 df[1, 1] = -0.5 df[2, 0] = 1 df[2, 2] = -sin(x[2]) return df x0 = [8, 15, 80] #w/o gradient: #p = NLSP(f, x0) p = NLSP(f, x0, df=df, maxFunEvals=1e5, iprint=-1, ftol=1e-6, contol=1e-35) p.lb = [-inf, -inf, 150] p.ub = [inf, inf, 158] # you could try also comment/uncomment nonlinear constraints: p.c = lambda x: (x[2] - 150.8)**2 - 1.5 # optional: gradient p.dc = lambda x: asfarray((0, 0, 2 * (x[2] - 150.8))) # also you could set it via p=NLSP(f, x0, ..., c = c, dc = dc) r = p.solve('nssolve', **kwargs) return r.istop > 0, r, p
def calcIdealAngles3(h, UBmatrix, wavelength, phi, stars): "Calculates the chi and theta for a desired vector h in the fixed phi mode." #Accepts ta desired vector h, the UB matrix, the wavelength, and the fixed phi hp = np.dot(UBmatrix, h) q = calcq(h[0], h[1], h[2], stars) twotheta = 2 * np.arcsin(wavelength * q / 4 / np.pi) x0 = [0.0, 0.0] p0 = NLSP(phiEquations, x0, args=(hp, wavelength, phi, twotheta)) r0 = p0.solve('nlp:ralg') #r0.xf is the final array twotheta = np.degrees(twotheta) chi = np.degrees(r0.xf[0]) # xf[0] = chi omega = np.degrees(r0.xf[1]) # xf[1] = omega theta = twotheta / 2.0 + omega return twotheta, theta, omega, chi
def calcIdealAngles3(h, UBmatrix, wavelength, phi, stars): "Calculates the chi and theta for a desired vector h in the fixed phi mode." # Accepts ta desired vector h, the UB matrix, the wavelength, and the fixed phi hp = N.dot(UBmatrix, h) q = calcq(h[0], h[1], h[2], stars) twotheta = 2 * N.arcsin(wavelength * q / 4 / N.pi) x0 = [0.0, 0.0] p0 = NLSP(phiEquations, x0, args=(hp, wavelength, phi, twotheta)) r0 = p0.solve("nlp:ralg") # r0.xf is the final array twotheta = N.degrees(twotheta) chi = N.degrees(r0.xf[0]) # xf[0] = chi omega = N.degrees(r0.xf[1]) # xf[1] = omega theta = twotheta / 2.0 + omega return twotheta, theta, omega, chi
def calcScatteringPlane(h1, h2, UBmatrix, wavelength, stars): "Calculates the chi and phi for the scattering plane defined by h1 and h2. Used with calcIdealAngles2." # Accepts two scattering plane vectors, h1 and h2, the UB matrix, and the wavelength h1p = N.dot(UBmatrix, h1) h2p = N.dot(UBmatrix, h2) x0 = [0.0, 0.0, 0.0, 0.0] q = calcq(h1[0], h1[1], h1[2], stars) twotheta1 = 2.0 * N.arcsin(wavelength * q / 4.0 / N.pi) q = calcq(h2[0], h2[1], h2[2], stars) twotheta2 = 2.0 * N.arcsin(wavelength * q / 4.0 / N.pi) p0 = NLSP(scatteringEquations, x0, args=(h1p, h2p, wavelength, twotheta1, twotheta2)) r0 = p0.solve("nlp:ralg") chi = N.degrees(r0.xf[0]) # xf is the final array, xf[0] = chi phi = N.degrees(r0.xf[1]) # xf[1] = phi return chi, phi
def calcScatteringPlane(h1, h2, UBmatrix, wavelength, stars): "Calculates the chi and phi for the scattering plane defined by h1 and h2. Used with calcIdealAngles2." #Accepts two scattering plane vectors, h1 and h2, the UB matrix, and the wavelength h1p = N.dot(UBmatrix, h1) h2p = N.dot(UBmatrix, h2) x0 = [0.0, 0.0, 0.0, 0.0] q = calcq(h1[0], h1[1], h1[2], stars) twotheta1 = 2.0 * N.arcsin(wavelength * q / 4.0 / N.pi) q = calcq(h2[0], h2[1], h2[2], stars) twotheta2 = 2.0 * N.arcsin(wavelength * q / 4.0 / N.pi) p0 = NLSP(scatteringEquations, x0, args=(h1p, h2p, wavelength, twotheta1, twotheta2)) r0 = p0.solve('nlp:ralg') chi = N.degrees(r0.xf[0]) #xf is the final array, xf[0] = chi phi = N.degrees(r0.xf[1]) # xf[1] = phi return chi, phi
def test(complexity=0, **kwargs): f = lambda x: (x[0]**3+x[1]**3-9, x[0]-0.5*x[1], cos(x[2])+x[0]-1.5) #optional: gradient def df(x): df = zeros((3,3)) df[0,0] = 3*x[0]**2 df[0,1] = 3*x[1]**2 df[1,0] = 1 df[1,1] = -0.5 df[2,0] = 1 df[2,2] = -sin(x[2]) return df x0 = [8,15, 80] #w/o gradient: #p = NLSP(f, x0) p = NLSP(f, x0, df = df, maxFunEvals = 1e5, iprint = -1, ftol = 1e-6, contol=1e-35) p.lb = [-inf, -inf, 150] p.ub = [inf, inf, 158] # you could try also comment/uncomment nonlinear constraints: p.c = lambda x: (x[2] - 150.8)**2-1.5 # optional: gradient p.dc = lambda x: asfarray((0, 0, 2*(x[2]-150.8))) # also you could set it via p=NLSP(f, x0, ..., c = c, dc = dc) r = p.solve('nssolve', **kwargs) return r.istop>0, r, p
""" Solving system of equations: x**3 + y**3 - 9 = 0 x - 0.5*y = 0 cos(z) + x - 1.5 = 0 """ from FuncDesigner import * from openopt import NLSP x, y, z = oovars(3) equations = (x**3 + y**3 - 9, x - 0.5 * y, cos(z) + x - 1.5) # alternatively, since FD 0.32: you can use equations and require custom tolerances equations = (x**3 + y**3 - 9 == 0, (x - 0.5 * y == 0)(tol=1e-9), (cos(z) + x == 1.5)(tol=1e-9)) # if no tol is assigned for an equation, p.ftol (default 10^-6) will be used for that one startPoint = {x: 8, y: 15, z: 80} p = NLSP(equations, startPoint) # optional: we could set some constraints p.constraints = [z < 70, z > 50, z + sin(x) < 60] r = p.solve( 'nssolve' ) # nssolve is name of solver involved, see OOF doc for more arguments xs, ys, zs = r(x, y, z) print('Solution: x = %f y = %f z = %f' % (xs, ys, zs)) #Solution: x = 0.999999 y = 2.000000 z = 57.595865
""" Solving system of equations: x**3 + y**3 - 9 = 0 x - 0.5*y = 0 cos(z) + x - 1.5 = 0 """ from FuncDesigner import * from openopt import NLSP x, y, z = oovars(3) equations = (x**3 + y**3 - 9, x - 0.5*y, cos(z) + x - 1.5) # alternatively, since FD 0.32: you can use equations and require custom tolerances equations = (x**3 + y**3 - 9==0, (x - 0.5*y==0)(tol=1e-9), (cos(z) + x == 1.5) (tol=1e-9)) # if no tol is assigned for an equation, p.ftol (default 10^-6) will be used for that one startPoint = {x:8, y:15, z:80} p = NLSP(equations, startPoint) # optional: we could set some constraints p.constraints = [z<70, z>50, z + sin(x) < 60] r = p.solve('nssolve') # nssolve is name of solver involved, see OOF doc for more arguments xs, ys, zs = r(x, y, z) print('Solution: x = %f y = %f z = %f' % (xs, ys, zs)) #Solution: x = 0.999999 y = 2.000000 z = 57.595865