def findFWHM(vector, maxPos=None, amplitude=None): """ Find FWHM of vector peak (width at value at maxPos - amplitude /2). If maxPos is None, will find maximum in vector. If amplitude is None, will calculate amplitude from maximum to minimum of vector. """ if maxPos == None: maxPos = vector.argmax() if amplitude == None: maxVal = pl.amax(vector) minVal = pl.amin(vector) amplitude = float(maxVal - minVal) maxSign = pl.sign(vector[maxPos]) for pos, val in enumerate(vector[maxPos:]): if pl.sign(val) != maxSign: # we passed 0 break halfAbove = pos - abs(vector[maxPos + pos]) / abs(vector[maxPos + pos] - vector[maxPos + pos - 1]) for pos, val in enumerate(vector[maxPos:0:-1]): if pl.sign(val) != maxSign: # we passed 0 break halfBelow = pos - abs(vector[maxPos - pos]) / abs(vector[maxPos - pos] - vector[maxPos - pos + 1]) FWHM = halfBelow + halfAbove return FWHM, maxPos, amplitude
def integrator(self, t, state): #print(state) stateVec = [] counter = 0 for massName, massObj in self.objects["masses"].items(): massObj.position = state[counter] massObj.velocity = state[counter + 1] stateVec.append(massObj.velocity) acc = 0 for springConnection in massObj.springs: #print("Pos",massObj.position,findObj(self.objects,springConnection[0]).position) delta = massObj.position - findObj( self.objects, springConnection[0]).position acc -= sign(delta) * (sign(delta) * delta - springConnection[1].defLength ) * springConnection[1].stiffness #print("Acc",acc) for damperConnection in massObj.dampers: acc -= (massObj.velocity - findObj(self.objects, damperConnection[0]).velocity ) * damperConnection[1].dampingCoefficient for forcingConnection in massObj.forcings: acc += forcingConnection.amplitude * sin( forcingConnection.omega * t - forcingConnection.shift) acc /= massObj.mass stateVec.append(acc) counter += 2 return stateVec
def mapc2p(xc,yc): from math import sqrt from pylab import sign # save global parameters in local variables rsqr = rsqr0 rinn = rinn0 rout = rout0 # Initialize physical domain vectors xp = 1.0*xc yp = 1.0*yc for i in range(len(xc)): for j in range(len(xc[0])): xci = xc[i][j] yci = yc[i][j] # Unaffected part of computational grid if (abs(xci) >= rsqr or abs(yci) >= rsqr): xp[i][j] = 1.0*xci yp[i][j] = 1.0*yci else: #! Scale computational grid: map rsqr to unit square xci = xc[i][j]/rsqr yci = yc[i][j]/rsqr # Calculate D,R, center for inner, middle and outer region d = max(abs(xci),abs(yci),1e-10) d = min(0.999999,d) D = rsqr*d/sqrt(2) rat = rinn/rsqr rat2 = (rout/rsqr) # For inner most gird lines inside rinn if d <= rat: # or use 0.5 to make sure it aligns with R = rinn # For middle grid lines between rinn and rout elif d <= rat2: # or use rat2 R = rsqr*d # For outer grid lines outside rout (note D has to be redefined) else: R = rout*((1.-rat2)/(1.-d))**(1.0/rat2 + 1.0/2.0) D = rout/sqrt(2) + (d-rat2)*(rsqr-rout/sqrt(2))/(1.-rat2) center = D - sqrt(R**2 - D**2) # Do mapping (see paper) Have to do for the 3 sections from the diagonals xp[i][j] = (D/d)*abs(xci) yp[i][j] = (D/d)*abs(yci) if abs(xci) >= abs(yci): xp[i][j] = center + sqrt(R**2 - yp[i][j]**2) if abs(yci) >= abs(xci): yp[i][j] = center + sqrt(R**2 - xp[i][j]**2) # Give corresponding sign xp[i][j] = sign(xci)*xp[i][j] yp[i][j] = sign(yci)*yp[i][j] # Uncomment to return no mapped grid at all #xp = 1.0*xc #yp = 1.0*yc return xp,yp
def evt_vy0(self, t, states, traj, p): """ triggers the vy=0 event :args: t (2x float): list of time prior to and after event states (2x array): list of states prior to and after event traj (trajectory patch): a trajectory patch (ignored here) :returns: (bool) vy=0 detected? (both directions) """ return sign(states[0][4]) * sign(states[1][4]) != 1
def surfaceNormal(self, p1) : cv = self.placement.location+self.placement.orientation*self.radcurv # sn = p1-cv # sn = -sn/pl.linalg.norm(sn) sn = pl.sign(self.radcurv)*(cv-p1) sn = sn/pl.linalg.norm(sn) return sn
def as_sdms(self,decimals=0): min_val_size = len(str(int(floor(abs(self.lower_bound)*180/pi)))) max_val_size = len(str(int(floor(abs(self.upper_bound)*180/pi)))) deg_size=max(min_val_size, max_val_size) sign_char = '- +'[int(sign(self.value))+1] d_float = abs(self.value)*180/pi d_int = int(floor(d_float)) m_float = 60*(d_float - d_int) m_int = int(floor(m_float)) s_float = 60*(m_float - m_int) s_int = int(floor(s_float)) frac_int = int(floor(10**decimals*(s_float - s_int)+0.5)) if frac_int >= 10**decimals: frac_int -= 10**decimals s_int +=1 if s_int >= 60: s_int -= 60 m_int += 1 if m_int >= 60: m_int -= 60 d_int += 1 max_d = int(floor(self.upper_bound*180/pi+0.5)) min_d = int(floor(self.lower_bound*180/pi+0.5)) if d_int >= max_d and self.cyclical and not self.include_upper_bound: d_int -= (max_d-min_d) base_str = sign_char+str(d_int).rjust(deg_size,'0')+':'+str(m_int).rjust(2,'0')+':'+str(s_int).rjust(2,'0') if decimals is 0: return base_str else: return base_str+'.'+str(frac_int).rjust(decimals,'0')
def analyzeSound(self): """ highlights N first peaks in frequency diagram """ # on recharge les données data = self.data sample_freq = self.sample_freq from scipy.fftpack import fftfreq freq_vect = fftfreq(data.size) * sample_freq # on trouve les maxima y0 = abs(fft(data)) # y1 = abs(fft(data[:, 1])) maxi0 = ((diff(sign(diff(y0))) < 0) & (y0[1:-1] > y0.max()/10.)).nonzero()[0] + 1 # local max # maxi1 = ((diff(sign(diff(y1))) < 0) & (y1[1:-1] > y1.max()/10.)).nonzero()[0] + 1 # local max # fréquence ax = self.main_figure.figure.add_subplot(212) ax.plot(freq_vect[maxi0], y0[maxi0], "o") # ax.plot(freq_vect[maxi1], y1[maxi1], "o") # annotations au dessus d'une fréquence de coupure fc = 100 for point in maxi0[(freq_vect[maxi0] > fc).nonzero()][:self.ui.spinBox.value()]: plt.annotate("%.2f" % freq_vect[point], (freq_vect[point], y0[point])) # for point in maxi1[(freq_vect[maxi0] > fc).nonzero()][:self.ui.spinBox.value()]: # plt.annotate("%.2f" % freq_vect[point], (freq_vect[point], y1[point])) self.ui.main_figure.canvas.draw()
def datagen(N): """ Produces N pairs of training data and desired output; each sample of training data contains -1 in its first position, this corresponds to the interpretation of the threshold as first element of the weight vector """ fun1 = lambda x1,x2: -2*x1**3-x2+.5*x1**2 fun2 = lambda x1,x2: x1**2*x2+2*x1*x2+1 fun3 = lambda x1,x2: .5*x1*x2**2+x2**2-2*x1**2 rarr1 = rand(1,N) rarr2 = rand(1,N) teacher = sign(rand(1,N)-.5) idplus = (teacher<0) idminus = -idplus rarr1[idplus] = rarr1[idplus]-1 y1=fun1(rarr1,rarr2) y2=fun2(rarr1,rarr2) y3=fun3(rarr1,rarr2) x=transpose(concatenate((-ones((1,N)),y1,y2))) return x, teacher[0]
def followPosition(self, qDes, cmdCountCur): ''' Follows the joint position setpoint as long as : - the corresponding position lies inside the joint limits - no other command has been received ''' # build trajectory to go from qSet to qDes with max velocity qTraj = [] for i in range(N): if self.qSet[i] == qDes[i]: qTraj.append([]) else: qTraj.append( list( arange(self.qSet[i], qDes[i], sign(qDes[i] - self.qSet[i]) * jointVelMax[i])[1:])) for i in range(N): if len(qTraj[i]) == 0: qTraj[i].append(qDes[i]) elif qTraj[i][-1] != qDes[i]: qTraj[i].append(qDes[i]) steps = max([len(t) for t in qTraj]) for i in range(N): qTraj[i] += [qDes[i]] * (steps - len(qTraj[i])) # follow trajectory from qSet to qDes k = 0 while (self.cmdCount == cmdCountCur ) and not rospy.is_shutdown() and k < len(qTraj[0]): # running setpoint self.qSet = [qTraj[i][k] for i in range(N)] k = k + 1 time.sleep(T)
def addnoise(err, noise=0.0): ''' Add noise to the objective function. If noise is a scalar, then use it as a value and supply defaults. If noise is a dict, get kwargs from it. Examples: # Add multiplicative Gaussian noise with std=0.1 to an objective value of 3.67 addnoise(3.67, noise=0.1) # Add additive uniform noise with range=0.1 to an objective value of 3.67 addnoise(3.67, noise={'value':0.1, 'gaussian':False, 'nonnegative':True, 'multiplicative':False, 'verbose':True}) ''' # Ensure it's a dict and set properties if not isinstance(noise, dict): noise = {'value': noise} value = noise.get('value', 0.0) gaussian = noise.get('gaussian', True) nonnegative = noise.get('nonnegative', True) multiplicative = noise.get('multiplicative', True) verbose = noise.get('verbose', True) if value: # If no noise, just skip to the end # Create the zero-mean noise if gaussian: noiseterm = value * pl.randn() else: noiseterm = value * (pl.rand() - 0.5) # Update the error if multiplicative: output = err * (1 + noiseterm) else: output = err + noiseterm # If asked, swap the sign of the output -- WARNING, will mess up the statistical properties! if nonnegative and pl.sign(output) != pl.sign(err): if verbose: print( 'addnoise() warning: noise reversed sign of error; reversing back' ) output = -output else: output = err return output
def square(square_frequency=440, length=0): sample_freq = 44100 if length: end = length else: end = 6./square_frequency t = pl.arange(0., end, 1./sample_freq) msg = pl.sign(pl.sin(2*pl.pi*square_frequency*t)) # sign rounds to -1 or 1 msg = Message(sample_freq, t, msg) return msg
def surface(self) : x = pl.arange(-self.dimension[0],self.dimension[0]+1e-8,self.dimension[0]/5) y = pl.arange(-self.dimension[1],self.dimension[1]+1e-8,self.dimension[1]/5) xx,yy = pl.meshgrid(x,y) cv = self.placement.location+self.placement.orientation*self.radcurv zz = -pl.sign(self.radcurv)*pl.sqrt(self.radcurv**2-(xx-cv[0])**2-(yy-cv[1])**2)+cv[2] return [xx,yy,zz]
def calc_walk_summand(r1, r2, digits=None): """ Calculates the summand along one edge depending on axis crossings. Follows the edge between two points and checks if one or both axes are being crossed. If They are crossed in clockwise sense, it returns +1 otherwise -1. Going through the origin raises the PolygonsTouching exception. Returns one of -2, -1, 0, +1, +2 or raises PolygonsTouching """ x, y = 0, 1 # indices for better readability summand = 0 # the return value tx, ty = None, None # on division by zero, set parameters to None if r1[x] != r2[x]: ty = r1[x] / (r1[x] - r2[x]) # where it crosses the y axis if r1[y] != r2[y]: tx = r1[y] / (r1[y] - r2[y]) # where it crosses the x axis if tx == None: tx = ty if ty == None: ty = tx # if tx == ty and tx==None: # print "R1", r1 # print "R2", r2 # tx = 0 # ty = 0 rsign = pylab.sign if digits != None: rsign = lambda x: pylab.sign(round(x, digits)) sign_x = rsign(r1[x] + tx * (r2[x] - r1[x])) sign_y = rsign(r1[y] + ty * (r2[y] - r1[y])) if (tx >= 0) and (tx < 1): if (sign_x == 0) and (sign_y == 0): raise PolygonsTouching() summand += sign_x * pylab.sign(r2[y] - r1[y]) if (ty >= 0) and (ty < 1): if (sign_x == 0) and (sign_y == 0): raise PolygonsTouching() summand += sign_y * pylab.sign(r1[x] - r2[x]) return summand
def analyzeSound(self): """ highlights N first peaks in frequency diagram """ # on recharge les données data = self.data sample_freq = self.sample_freq from scipy.fftpack import fftfreq freq_vect = fftfreq(data.size) * sample_freq # on trouve les maxima y0 = abs(fft(data)) # y1 = abs(fft(data[:, 1])) maxi0 = ((diff(sign(diff(y0))) < 0) & (y0[1:-1] > y0.max()/10.)).nonzero()[0] + 1 # local max # maxi1 = ((diff(sign(diff(y1))) < 0) & (y1[1:-1] > y1.max()/10.)).nonzero()[0] + 1 # local max # fréquence # ax = self.main_figure.figure.add_subplot(212) # ax.plot(freq_vect[maxi0], y0[maxi0], "o") # # ax.plot(freq_vect[maxi1], y1[maxi1], "o") # annotations au dessus d'une fréquence de coupure fc = 100. max_freq = [] freq_vect_2 = freq_vect[maxi0] maxy0 = y0[maxi0] maxy0_list = maxy0.tolist() while np.size(max_freq) < 12 : F = np.argmax(maxy0_list) maxy0_list[F] = 0 print(freq_vect_2[F]) if np.abs(freq_vect_2[F]) > fc : max_freq.append(F) for i in range(0,4) : if (F+2-i) in range (1,len(maxy0_list)) and np.abs(freq_vect_2[F+2-i]-freq_vect_2[F]) < 4 : maxy0_list[F+2-i] = 0 print(freq_vect_2[max_freq]) for point in max_freq: plt.annotate("%.2f" % freq_vect_2[point], (freq_vect_2[point], maxy0[point])) ax = self.main_figure.figure.add_subplot(212) ax.plot(freq_vect_2[max_freq], maxy0[max_freq], "o") # for point in maxi1[(freq_vect[maxi0] > fc).nonzero()][:self.ui.spinBox.value()]: # plt.annotate("%.2f" % freq_vect[point], (freq_vect[point], y1[point])) # self.ui.main_figure.canvas.draw()
def plot_statistics_file(filename, title, xlabel, ylabel): # parse the content -- data = pylab.loadtxt(filename) num_points = data[:, -1] print("Used at least %i samples per plot point" % min(num_points)) delta_scales = data[:, 0] print("min/max delta_scale ==", (min(delta_scales), max(delta_scales))) # plot the content -- pylab.figure() # new figure pylab.gcf().set_facecolor("w") # set white background pylab.grid(True) pylab.spectral() # set the default colormap to pylab.cm.Spectral labels = ["delta_scale", "$\Delta$score mean", "$\Delta$score min", "$\Delta$score max", "$\Delta$score 1%", "$\Delta$score 5%", "$\Delta$score 50%", "$\Delta$score 95%", "$\Delta$score 99%"] for index in range(1, len(labels)): x = data[:, 0] x_log = [pylab.sign(s)*pylab.log(abs(s)) for s in x] y = data[:, index] #pylab.plot(x, y, label=labels[index]) pylab.plot(x_log, y, label=labels[index], marker=".") #delta_xtick = (max(delta_scales) - min(delta_scales)) / min(10, len(delta_scales)) #pylab.xticks(pylab.arange(min(delta_scales), max(delta_scales), delta_xtick)) pylab.legend(loc ="upper right", fancybox=True) pylab.xlabel(xlabel) pylab.ylabel(ylabel) pylab.title(title) pylab.draw() return
def plot_statistics_file(filename, title, xlabel, ylabel): # parse the content -- data = pylab.loadtxt(filename) num_points = data[:, -1] print("Used at least %i samples per plot point" % min(num_points)) delta_scales = data[:, 0] print("min/max delta_scale ==", (min(delta_scales), max(delta_scales))) # plot the content -- pylab.figure() # new figure pylab.gcf().set_facecolor("w") # set white background pylab.grid(True) pylab.spectral() # set the default colormap to pylab.cm.Spectral labels = [ "delta_scale", "$\Delta$score mean", "$\Delta$score min", "$\Delta$score max", "$\Delta$score 1%", "$\Delta$score 5%", "$\Delta$score 50%", "$\Delta$score 95%", "$\Delta$score 99%" ] for index in range(1, len(labels)): x = data[:, 0] x_log = [pylab.sign(s) * pylab.log(abs(s)) for s in x] y = data[:, index] #pylab.plot(x, y, label=labels[index]) pylab.plot(x_log, y, label=labels[index], marker=".") #delta_xtick = (max(delta_scales) - min(delta_scales)) / min(10, len(delta_scales)) #pylab.xticks(pylab.arange(min(delta_scales), max(delta_scales), delta_xtick)) pylab.legend(loc="upper right", fancybox=True) pylab.xlabel(xlabel) pylab.ylabel(ylabel) pylab.title(title) pylab.draw() return
def followPosition(self, qDes, cmdCountCur): ''' Follows the joint position setpoint as long as : - the corresponding position lies inside the joint limits - no other command has been received ''' # build trajectory to go from qSet to qDes with max velocity qTraj = [list(arange(self.qSet[i],qDes[i],sign(qDes[i]-self.qSet[i])*jointVelMax[i])[1:]) for i in xrange(N)] for i in xrange(N): if len(qTraj[i]) == 0: qTraj[i].append(qDes[i]) elif qTraj[i][-1] != qDes[i]: qTraj[i].append(qDes[i]) steps = max([len(t) for t in qTraj]) for i in xrange(N): qTraj[i] += [qDes[i]] * (steps - len(qTraj[i])) # follow trajectory from qSet to qDes k = 0 while (self.cmdCount == cmdCountCur) and not rospy.is_shutdown() and k < len(qTraj[0]): # running setpoint self.qSet = [qTraj[i][k] for i in xrange(N)] k = k+1 time.sleep(T)
def generate_datafile(name,departures,depart_planet,arrive_planet,errors=0): f = open(name,'w') last = departures[-1] lasty = int(last[0] / 60.0 / 60.0 / 24.0 / 365.0) inacc = 0.0 for departure in departures: if departure[1]: inacc += 1.0 f.write("// Phase angles for the next %i years for %s - %s Hohmann transfers\n"%(lasty,depart_planet.name,arrive_planet.name)) f.write("// Calculated using the KSP Mission Control toolkit\n") f.write("// Angles are valid for Kerbal Space Program 0.19\n") f.write("// Total windows: %i\n"%len(departures)) f.write("// Inaccuracies during calculation: %i (%i%%)\n\n"%(inacc,inacc / len(departures) * 100)) f.write("UT Departure\tPhase angle\tDate time\tAccurate (Error in seconds)\n") for departure in departures: accurate = departure[1] if accurate == 0: accurate = "Yes" else: accurate = str(accurate) departure = departure[0] e1 = depart_planet.eph(departure)[0] e2 = arrive_planet.eph(departure)[0] e1 /= norm(e1) e2 /= norm(e2) PA = degrees(arccos(e1.dot(e2))) * sign(cross(e1,e2)[2]) years = floor(departure/60.0/60.0/24.0/365.0)+1 days = floor((departure/60.0/60.0/24.0)%365.0)+1 f.write("%f\t%f\tYear %i, Day %i\t%s\n"%(departure,PA,years,days,accurate)) f.close()
return (a + b) / 2 nedre = -6 øvre = 6 antall_gjett = 100 xverdier = linspace(nedre, øvre, 100) plot(xverdier, f(xverdier)) grid() show() for gjett_nr in range(1, antall_gjett + 1): gjett = midtpunkt(nedre, øvre) print(f"Jeg gjetter på x = {gjett}") print(f"Da er f(x) = {f(gjett)}") verdi = f(gjett) if abs(verdi) < 0.01: print("Jeg fant et nullpunkt!") print(f"Jeg brukte {gjett_nr} antall forsøk.") break elif sign(f(nedre)) == sign(verdi): nedre = gjett else: # sign(f(nedre)) != sign(verdi) øvre = gjett
def TC_acfLimbPointingAltitudeOffset(root, relativeTime, Initial, Final, Rate, Timeline_settings, configFile, comment=""): """Schedules Pointing Command If the desired pointing altitude is already set, the maximum TEXPMS last set with a CCD_macro is added to *relativeTime*. Otherwise if Rate == 0, a delay equal to *Timeline_settings['pointing_stabilization']* is added to *relativeTime*. If Rate != 0, only *Timeline_settings['CMD_separation']* is added to *relativeTime* to allow the payload to enter operation mode during the sweep. """ if not (configFile.latestRelativeTime <= relativeTime <= Timeline_settings["duration"]["duration"]): Logger.error( "Invalid argument: negative relativeTime, decreasing relativeTime, exceeding timeline duration" ) raise ValueError if not (-60000 <= Initial <= 230000 and type(Initial) == int): Logger.error("Invalid argument: Initial") raise ValueError if not (-60000 <= Final <= 230000 and type(Final) == int): Logger.error("Invalid argument: Final") raise ValueError if not (-5000 <= Rate <= 5000 and (type(Rate) == int or type(Rate) == float)): Logger.error("Invalid argument: Rate") raise ValueError if Rate != 0 and sign(Final - Initial) != sign(Rate): Logger.error("Invalid argument: sign(Final - Initial) != sign(Rate)") raise ValueError current_pointing = configFile.current_pointing Logger.debug("current_pointing: " + str(current_pointing)) Logger.debug("Initial: " + str(Initial) + ", Final: " + str(Final) + ", Rate: " + str(Rate)) # if(current_pointing != Final or current_pointing != Initial ): # Logger.debug('Scheduling pointing command') etree.SubElement(root[1], "command", mnemonic="TC_acfLimbPointingAltitudeOffset") etree.SubElement(root[1][len(root[1]) - 1], "relativeTime") root[1][len(root[1]) - 1][0].text = str(int(relativeTime)) etree.SubElement(root[1][len(root[1]) - 1], "comment") root[1][len(root[1]) - 1][1].text = comment etree.SubElement(root[1][len(root[1]) - 1], "tcArguments") etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="Initial") root[1][len(root[1]) - 1][2][0].text = str(Initial) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="Final") root[1][len(root[1]) - 1][2][1].text = str(Final) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="Rate") root[1][len(root[1]) - 1][2][2].text = str(Rate) if Rate != 0: incremented_time = relativeTime + Timeline_settings["CMD_separation"] configFile.current_pointing = None Logger.debug( 'Rate != 0, meaning a sweep is scheduled. Next CMD is only staggered by Timeline_settings["CMD_separation"]' ) elif current_pointing == Final and current_pointing == Initial: configFile.current_pointing = Final incremented_time = (relativeTime + int(ceil(configFile.LargestSetTEXPMS / 1000)) + Timeline_settings["CMD_separation"]) Logger.debug( 'Satellite is already orientated the right way. Next CMD is staggered by Timeline_settings["CMD_separation"]+Globals.LargestSetTEXPMS/1000, rounded up.' ) Logger.debug("Globals.LargestSetTEXPMS: " + str(configFile.LargestSetTEXPMS)) elif Rate == 0: configFile.current_pointing = Final incremented_time = relativeTime + Timeline_settings[ "pointing_stabilization"] Logger.debug( 'Satellite is not orientated the right way. Next CMD is staggered by Timeline_settings["pointing_stabilization"]' ) # else: # Logger.debug('Skipping pointing command as satellite is already oriented the desired way') # incremented_time = relativeTime # incremented_time = relativeTime+Timeline_settings['CMD_separation'] configFile.latestRelativeTime = relativeTime return incremented_time
class Plot(object): """Glue-code wrapper between pylab (matplotlib) and si. One plot can only have one SI unit per dimension, so you can plot without having to worry about dimensions. Does not yet enforce anything about to which window you plot. (You can have two objects simultaneously and show them at the same time.) >>> from si.common import * >>> p = Plot(s, m) >>> t = [ms*_x for _x in xrange(200)] # numeric arrays not supported yet. >>> g = 10*N/kg >>> y = [90*cm - (_t**2 * g / 2) for _t in t] >>> p.plot(t, y) >>> # p.show() """ def __init__(self, xunit, yunit): self.xunit = xunit self.yunit = yunit self.xlim = None self.ylim = None def _x2number(self, x): if hasattr(self.xunit, "using"): return x.using(self.xunit) else: return x / self.xunit def _y2number(self, y): if hasattr(self.yunit, "using"): return y.using(self.yunit) else: return y / self.yunit def plot(self, x, y, *args, **kwords): """Like pylab.plot().""" try: x = [self._x2number(_x) for _x in x] y = [self._y2number(_y) for _y in y] except MakesNoSense: raise Exception, "units don't match." plot(x, y, *args, **kwords) def show(self): """Like pylab.show().""" self.lim() show() def xlabel(self, label, unit=None): """Set x label; use unit to state that label contains an assertion about the unit. >>> from si.common import * >>> p = Plot(s, m) >>> p.xlabel("Time (in hours)",h) Traceback (most recent call last): ... AssertionError: Plot unit does not match label unit. """ if unit: assert unit == self.xunit, "Plot unit does not match label unit." xlabel(label) def ylabel(self, label, unit=None): """Set y label; use unit to state that label contains an assertion about the unit. >>> from si.common import * >>> p = Plot(s, m) >>> p.ylabel("Length (in metre)",m) """ if unit: assert unit == self.yunit, "Plot unit does not match label unit." ylabel(label) def text(self, x, y, label): """Like pylab.text().""" try: x = self._x2number(x) y = self._y2number(y) except MakesNoSense: raise Exception, "units don't match." text(x, y, label) def arrowhead(self, (x, y), (dirx, diry)): """Plot an arrowhead with its tip in (x,y) coming from the direction of (dirx,diry) (without units, only direction).""" # FIXME: provide an interface for the non-pylab matplotlib api self.lim() xsize = (xlim()[1] - xlim()[0]) / 200 ysize = (ylim()[1] - ylim()[0]) / 200 if diry: raise NotImplementedError, "only left/right supportet until ow" x, y = self._x2number(x), self._y2number(y) plot([x, x + xsize * sign(dirx)], [y, y + ysize], 'k-') plot([x, x + xsize * sign(dirx)], [y, y - ysize], 'k-')
def plot_spectrum(signal): """Plots a signal and its spectrum signal can be a number an abbreviation or an expression: 1 = "wn" = white noise 2 = "sinus" = periodic sinusoidal waveform 3 = "rect" = periodic rectangular pulses 4 = "dsin" = damped sinusoidal waveform 5 = "drect" = damped rectangular waveform 6 = "bits" = random bit sequence expression can be an expression of the abbreviations like: "wn + 0.5*dsin" >>> from t_fdomain import * >>> plot_spectrum("rw") >>> plot_spectrum(5) """ N = 4096 #FFT points D = 64 #sampling/symbol interval rate S = N//D #bit stream lenght x = pylab.arange(N) """ INPUT SIGNALS """ # 1. Random signal I. White noise wn = 0.5 * randn(pylab.size(x)) # 2. Deterministic, periodic sinosoidal waveform sinus = sin(x * 2 * D * pi/N) # 3. Deterministic, periodic sequence of rectangular pulses rect = sign(sin(x * 2 * D * pi/(N+1))) # 4. Deterministic, nonperiodic sinosoidal waveform dsin = exp(-0.005 * x) * sin(x * 2 * D * pi/N) dsinus = dsin # 5. Deterministic, damped sequence of rectangular pulses drect = exp(-0.005 * x) * sign(sin((x-1) * 2 * D * pi/(N+1))) # 6. Random signal II. Bit string generated by random sequence bitvalues = sign(randn(S)) bits = [] for value in bitvalues: bits.extend([value]*D) bits = pylab.array(bits) if isinstance(signal, int): signal = ["wn", "sinus", "rect", "dsin", "drect", "bits"][signal-1] signal = eval(signal) """ SPECTRUMs """ ww = range(N) X = pylab.fft(signal, N) XX = 20 * log10(abs(sys.float_info.min+X/N)) pylab.subplots_adjust(hspace=.4) #See tight space pylab.subplot(2, 1, 2) pylab.plot(ww, XX, "-") pylab.xlim(1, N/8) xlabel ('f [Hz]') ylabel ('absY($j\omega$) [dB]') pylab.title('Frequency domain') grid(True) pylab.subplot(2, 1, 1) pylab.plot(ww, signal, "-") pylab.xlim(1, N/4) xlabel ('t [x0.25 ms]') ylabel ('u(t) [V]') pylab.title('Time domain') grid(True)
def main(): import optparse from numpy import sum # Parse command line parser = optparse.OptionParser(usage=USAGE) parser.add_option("-p", "--plot", action="store_true", help="Generate pdf with IR-spectrum") parser.add_option("-i", "--info", action="store_true", help="Set up/ Calculate vibrations & quit") parser.add_option("-s", "--suffix", action="store", help="Call suffix for binary e.g. 'mpirun -n 4 '", default='') parser.add_option("-r", "--run", action="store", help="path to FHI-aims binary", default='') parser.add_option("-x", "--relax", action="store_true", help="Relax initial geometry") parser.add_option("-m", "--molden", action="store_true", help="Output in molden format") parser.add_option("-w", "--distort", action="store_true", help="Output geometry distorted along imaginary modes") parser.add_option("-t", "--submit", action="store", help="""\ Path to submission script, string <jobname> will be replaced by name + counter, string <outfile> will be replaced by filename""") parser.add_option("-d", "--delta", action="store", type="float", help="Displacement", default=0.0025) options, args = parser.parse_args() if options.info: print __doc__ sys.exit(0) if len(args) != 2: parser.error("Need exactly two arguments") AIMS_CALL = options.suffix + ' ' + options.run hessian_thresh = -1 name = args[0] mode = args[1] delta = options.delta run_aims = False if options.run != '': run_aims = True submit_script = options.submit is not None if options.plot: import matplotlib as mpl mpl.use('Agg') from pylab import figure if options.plot or mode == '1': from pylab import savetxt, transpose, eig, argsort, sort,\ sign, pi, dot, sum, linspace, argmin, r_, convolve # Constant from scipy.constants bohr = constants.value('Bohr radius') * 1.e10 hartree = constants.value('Hartree energy in eV') at_u = constants.value('atomic mass unit-kilogram relationship') eV = constants.value('electron volt-joule relationship') c = constants.value('speed of light in vacuum') Ang = 1.0e-10 hbar = constants.value('Planck constant over 2 pi') Avo = constants.value('Avogadro constant') kb = constants.value('Boltzmann constant in eV/K') hessian_factor = eV / (at_u * Ang * Ang) grad_dipole_factor = (eV / (1. / (10 * c))) / Ang #(eV/Ang -> D/Ang) ir_factor = 1 # Asign all filenames inputgeomerty = 'geometry.in.' + name inputcontrol = 'control.in.' + name atomicmasses = 'masses.' + name + '.dat' xyzfile = name + '.xyz' moldenname = name + '.molden' hessianname = 'hessian.' + name + '.dat' graddipolename = 'grad_dipole.' + name + '.dat' irname = 'ir.' + name + '.dat' deltas = array([-delta, delta]) coeff = array([-1, 1]) c_zero = -1. / (2. * delta) f = open('control.in', 'r') # read control.in template template_control = f.read() f.close if submit_script: f = open(options.submit, 'r') # read submission script template template_job = f.read() f.close folder = '' # Dummy ########### Central Point ################################################## if options.relax and mode == '0': # First relax input geometry filename = name + '.out' folder = name + '_relaxation' if not os.path.exists(folder): os.mkdir(folder) # Create folder shutil.copy('geometry.in', folder + '/geometry.in') # Copy geometry new_control = open(folder + '/control.in', 'w') new_control.write(template_control + 'relax_geometry trm 1E-3\n') # Relax! new_control.close() os.chdir(folder) # Change directoy print 'Central Point' if run_aims: os.system(AIMS_CALL + ' > ' + filename) # Run aims and pipe the output # into a file named 'filename' if submit_script: replace_submission(template_job, name, 0, filename) os.chdir('..') ############################################################################ # Check for relaxed geometry if os.path.exists(folder + '/geometry.in.next_step'): geometry = open(folder + '/geometry.in.next_step', 'r') else: geometry = open('geometry.in', 'r') # Read input geometry n_line = 0 struc = structure() lines = geometry.readlines() for line in lines: n_line = n_line + 1 if line.rfind('set_vacuum_level') != -1: # Vacuum Level struc.vacuum_level = float(split_line(line)[-1]) if line.rfind('lattice_vector') != -1: # Lattice vectors and periodic lat = split_line(line)[1:] struc.lattic_vector = append(struc.lattic_vector, float64(array(lat))[newaxis, :], axis=0) struc.periodic = True if line.rfind('atom') != -1: # Set atoms line_vals = split_line(line) at = Atom(line_vals[-1], line_vals[1:-1]) if n_line < len(lines): nextline = lines[n_line] if nextline.rfind( 'constrain_relaxation') != -1: # constrained? at = Atom(line_vals[-1], line_vals[1:-1], True) else: at = Atom(line_vals[-1], line_vals[1:-1]) struc.join(at) geometry.close() n_atoms = struc.n() n_constrained = n_atoms - sum(struc.constrained) # Atomic mass file mass_file = open(atomicmasses, 'w') mass_vector = zeros([0]) for at_unconstrained in struc.atoms[struc.constrained == False]: mass_vector = append(mass_vector, ones(3) * 1. / sqrt(at_unconstrained.mass())) line = '{0:10.5f}'.format(at_unconstrained.mass()) for i in range(3): line = line + '{0:11.4f}'.format(at_unconstrained.coord[i]) line = line + '{0:}\n'.format(at_unconstrained.kind) mass_file.writelines(line) mass_file.close() # Init dip = zeros([n_constrained * 3, 3]) hessian = zeros([n_constrained * 3, n_constrained * 3]) index = 0 counter = 1 # Set up / Read folders for displaced atoms for atom in arange(n_atoms)[struc.constrained == False]: for coord in arange(3): for delta in deltas: filename=name+'.i_atom_'+str(atom)+'.i_coord_'+str(coord)+'.displ_'+\ str(delta)+'.out' folder=name+'.i_atom_'+str(atom)+'.i_coord_'+str(coord)+'.displ_'+\ str(delta) if mode == '0': # Put new geometry and control.in into folder struc_new = copy.deepcopy(struc) struc_new.atoms[atom].coord[coord]=\ struc_new.atoms[atom].coord[coord]+delta geoname='geometry.i_atom_'+str(atom)+'.i_coord_'+str(coord)+\ '.displ_'+str(delta)+'.in' if not os.path.exists(folder): os.mkdir(folder) new_geo = open(folder + '/geometry.in', 'w') newline='#\n# temporary structure-file for finite-difference '+\ 'calculation of forces\n' newline=newline+'# displacement {0:8.4f} of \# atom '.format(delta)+\ '{0:5} direction {1:5}\n#\n'.format(atom,coord) new_geo.writelines(newline + struc_new.to_str()) new_geo.close() new_control = open(folder + '/control.in', 'w') template_control = template_control.replace( 'relax_geometry', '#relax_geometry') new_control.write(template_control+'compute_forces .true. \n'+\ 'final_forces_cleaned '+\ '.true. \noutput dipole \n') new_control.close() os.chdir(folder) # Change directoy print 'Processing atom: '+str(atom+1)+'/'+str(n_atoms)+', coord.: '+\ str(coord+1)+'/'+str(3)+', delta: '+str(delta) if run_aims: os.system(AIMS_CALL + ' > ' + filename) # Run aims and pipe the output # into a file named 'filename' if submit_script: replace_submission(template_job, name, counter, filename) # os.system('qsub job.sh') # Mind the environment variables os.chdir('..') if mode == '1': # Read output forces_reached = False atom_count = 0 data = open(folder + '/' + filename) for line in data.readlines(): if line.rfind( 'Dipole correction potential jump') != -1: dip_jump = float(split_line(line)[-2]) # Periodic if line.rfind('| Total dipole moment [eAng]') != -1: dip_jump = float64( split_line(line)[-3:]) # Cluster if forces_reached and atom_count < n_atoms: # Read Forces struc.atoms[atom_count].force = float64( split_line(line)[2:]) atom_count = atom_count + 1 if atom_count == n_atoms: forces_reached = False if line.rfind('Total atomic forces') != -1: forces_reached = True data.close() if struc.periodic: dip[index, 2] = dip[ index, 2] + dip_jump * coeff[deltas == delta] * c_zero else: dip[index, :] = dip[index, :] + dip_jump * coeff[ deltas == delta] * c_zero forces = array([]) for at_unconstrained in struc.atoms[struc.constrained == False]: forces = append( forces, coeff[deltas == delta] * at_unconstrained.force) hessian[index, :] = hessian[index, :] + forces * c_zero counter = counter + 1 index = index + 1 if mode == '1': # Calculate vibrations print 'Entering hessian diagonalization' print 'Number of atoms = ' + str(n_atoms) print 'Name of Hessian input file = ' + hessianname print 'Name of grad dipole input file = ' + graddipolename print 'Name of Masses input file = ' + atomicmasses print 'Name of XYZ output file = ' + xyzfile print 'Threshold for Matrix elements = ' + str(hessian_thresh) if (hessian_thresh < 0.0): print ' All matrix elements are taken'+\ ' into account by default\n' savetxt(hessianname, hessian) savetxt(graddipolename, dip) mass_mat = mass_vector[:, newaxis] * mass_vector[newaxis, :] hessian[abs(hessian) < hessian_thresh] = 0.0 hessian = hessian * mass_mat * hessian_factor hessian = (hessian + transpose(hessian)) / 2. # Diagonalize hessian (scipy) print 'Solving eigenvalue system for Hessian Matrix' freq, eig_vec = eig(hessian) print 'Done ... ' eig_vec = eig_vec[:, argsort(freq)] freq = sort(sign(freq) * sqrt(abs(freq))) ZPE = hbar * (freq) / (2.0 * eV) freq = (freq) / (200. * pi * c) grad_dipole = dip * grad_dipole_factor eig_vec = eig_vec * mass_vector[:, newaxis] * ones( len(mass_vector))[newaxis, :] infrared_intensity = sum(dot(transpose(grad_dipole),eig_vec)**2,axis=0)*\ ir_factor reduced_mass = sum(eig_vec**2, axis=0) norm = sqrt(reduced_mass) eig_vec = eig_vec / norm # The rest is output, xyz, IR,... print 'Results\n' print 'List of all frequencies found:' print 'Mode number Frequency [cm^(-1)] Zero point energy [eV] '+\ 'IR-intensity [D^2/Ang^2]' for i in range(len(freq)): print '{0:11}{1:25.8f}{2:25.8f}{3:25.8f}'.format( i + 1, freq[i], ZPE[i], infrared_intensity[i]) print '\n' print 'Summary of zero point energy for entire system:' print '| Cumulative ZPE = {0:15.8f} eV'.format(sum(ZPE)) print '| without first six eigenmodes = {0:15.8f} eV\n'.format( sum(ZPE) - sum(ZPE[:6])) print 'Stability checking - eigenvalues should all be positive for a '+\ 'stable structure. ' print 'The six smallest frequencies should be (almost) zero:' string = '' for zz in ZPE[:6]: string = string + '{0:25.8f}'.format(zz) print string print 'Compare this with the largest eigenvalue, ' print '{0:25.8f}'.format(freq[-1]) nums = arange(n_atoms)[struc.constrained == False] nums2 = arange(n_atoms)[struc.constrained] newline = '' newline_ir = '[INT]\n' if options.molden: newline_molden = '[Molden Format]\n[GEOMETRIES] XYZ\n' newline_molden = newline_molden + '{0:6}\n'.format(n_atoms) + '\n' for i_atoms in range(n_constrained): newline_molden = newline_molden + '{0:6}'.format( struc.atoms[nums[i_atoms]].kind) for i_coord in range(3): newline_molden = newline_molden + '{0:10.4f}'.format( struc.atoms[nums[i_atoms]].coord[i_coord]) newline_molden = newline_molden + '\n' newline_molden = newline_molden + '[FREQ]\n' for i in range(len(freq)): newline_molden = newline_molden + '{0:10.3f}\n'.format(freq[i]) newline_molden = newline_molden + '[INT]\n' for i in range(len(freq)): newline_molden = newline_molden + '{0:17.6e}\n'.format( infrared_intensity[i]) newline_molden = newline_molden + '[FR-COORD]\n' newline_molden = newline_molden + '{0:6}\n'.format(n_atoms) + '\n' for i_atoms in range(n_constrained): newline_molden = newline_molden + '{0:6}'.format( struc.atoms[nums[i_atoms]].kind) for i_coord in range(3): newline_molden = newline_molden + '{0:10.4f}'.format( struc.atoms[nums[i_atoms]].coord[i_coord] / bohr) newline_molden = newline_molden + '\n' newline_molden = newline_molden + '[FR-NORM-COORD]\n' for i in range(len(freq)): newline = newline + '{0:6}\n'.format(n_atoms) if freq[i] > 0: newline = newline + 'stable frequency at ' elif freq[i] < 0: newline = newline + 'unstable frequency at ' if options.distort and freq[i] < -50: struc_new = copy.deepcopy(struc) for i_atoms in range(n_constrained): for i_coord in range(3): struc_new.atoms[i_atoms].coord[i_coord]=\ struc_new.atoms[i_atoms].coord[i_coord]+\ eig_vec[(i_atoms)*3+i_coord,i] geoname = name + '.distorted.vibration_' + str( i + 1) + '.geometry.in' new_geo = open(geoname, 'w') newline_geo = '#\n# distorted structure-file for based on eigenmodes\n' newline_geo=newline_geo+\ '# vibration {0:5} :{1:10.3f} 1/cm\n#\n'.format(i+1,freq[i]) new_geo.writelines(newline_geo + struc_new.to_str()) new_geo.close() elif freq[i] == 0: newline = newline + 'translation or rotation ' newline = newline + '{0:10.3f} 1/cm IR int. is '.format(freq[i]) newline = newline + '{0:10.4e} D^2/Ang^2; red. mass is '.format( infrared_intensity[i]) newline = newline + '{0:5.3f} a.m.u.; force const. is '.format( 1.0 / reduced_mass[i]) newline = newline + '{0:5.3f} mDyne/Ang.\n'.format( ((freq[i] * (200 * pi * c))**2) * (1.0 / reduced_mass[i]) * at_u * 1.e-2) if options.molden: newline_molden=newline_molden+\ 'vibration {0:6}\n'.format(i+1) for i_atoms in range(n_constrained): newline = newline + '{0:6}'.format( struc.atoms[nums[i_atoms]].kind) for i_coord in range(3): newline = newline + '{0:10.4f}'.format( struc.atoms[nums[i_atoms]].coord[i_coord]) for i_coord in range(3): newline = newline + '{0:10.4f}'.format( eig_vec[(i_atoms) * 3 + i_coord, i]) if options.molden: newline_molden = newline_molden + '{0:10.4f}'.format( eig_vec[(i_atoms) * 3 + i_coord, i] / bohr) newline = newline + '\n' if options.molden: newline_molden = newline_molden + '\n' for i_atoms in range(n_atoms - n_constrained): newline = newline + '{0:6}'.format( struc.atoms[nums2[i_atoms]].kind) for i_coord in range(3): newline = newline + '{0:10.4f}'.format( struc.atoms[nums2[i_atoms]].coord[i_coord]) for i_coord in range(3): newline = newline + '{0:10.4f}'.format(0.0) newline = newline + '\n' newline_ir = newline_ir + '{0:10.4e}\n'.format( infrared_intensity[i]) xyz = open(xyzfile, 'w') xyz.writelines(newline) xyz.close() ir = open(irname, 'w') ir.writelines(newline_ir) ir.close() if options.molden: molden = open(moldenname, 'w') molden.writelines(newline_molden) molden.close() if mode == '1' and options.plot: x = linspace(freq.min() - 500, freq.max() + 500, 1000) z = zeros(len(x)) for i in range(len(freq)): z[argmin(abs(x - freq[i]))] = infrared_intensity[i] window_len = 150 gauss = signal.gaussian(window_len, 10) s = r_[z[window_len - 1:0:-1], z, z[-1:-window_len:-1]] z_convolve = convolve(gauss / gauss.sum(), s, mode='same')[window_len - 1:-window_len + 1] fig = figure(0) ax = fig.add_subplot(111) ax.plot(x, z_convolve, 'r', lw=2) ax.set_xlim([freq.min() - 500, freq.max() + 500]) ax.set_ylim([-0.01, ax.get_ylim()[1]]) ax.set_yticks([]) ax.set_xlabel('Frequency [1/cm]', size=20) ax.set_ylabel('Intensity [a.u.]', size=20) fig.savefig(name + '_IR_spectrum.pdf') print '\n Done. '
def CheckConfigFile(configFile): """ Core function of *CheckConfigFile*. Checks the values given in the *Configuration File* set by *Set_ConfigFile* and raises an error if any settings are found to be incompatible. Also prints out the currently selected *Configuration File* and the starting date and TLE it currently uses. """ Library.SetupLogger(configFile.Logger_name()) Timeline_settings = configFile.Timeline_settings() Operational_Science_Mode_settings = ( configFile.Operational_Science_Mode_settings()) # Mode5_settings = configFile.Mode5_settings() Mode100_settings = configFile.Mode100_settings() Mode110_settings = configFile.Mode110_settings() Mode120_settings = configFile.Mode120_settings() Mode121_settings = configFile.Mode121_settings() Mode122_settings = configFile.Mode122_settings() Mode123_settings = configFile.Mode123_settings() Mode121_122_123_settings = configFile.Mode121_122_123_settings() Mode124_settings = configFile.Mode124_settings() Mode130_settings = configFile.Mode130_settings() Mode131_settings = configFile.Mode131_settings() Mode132_settings = configFile.Mode132_settings() Mode133_settings = configFile.Mode133_settings() Mode134_settings = configFile.Mode134_settings() try: Logger.info("Currently used Configuration File: " + configFile.config_file_name) except: Logger.error( "Currently stated Configuration File is invalid. Try running Set_ConfigFile." ) raise ValueError try: Logger.info("Currently used starting date: " + Timeline_settings["start_date"]) except: Logger.error( "Currently stated starting date is invalid. Try running Set_ConfigFile." ) raise ValueError try: Logger.info("Currently used TLE: " + str(configFile.getTLE())) except: Logger.error( "Currently stated TLE is invalid. Try running Set_ConfigFile.") raise ValueError if not (Timeline_settings["duration"]["duration"] > 0 and type(Timeline_settings["duration"]["duration"]) == int): Logger.error('Timeline_settings["duration"]["duration"]') raise ValueError if not (43099.5 < ephem.Date(Timeline_settings["start_date"]) < 73049.5 and type(Timeline_settings["start_date"]) == str): Logger.error('Timeline_settings["start_date"]') raise ValueError if not (30 <= Timeline_settings["CMD_duration"] and type(Timeline_settings["CMD_duration"]) == int): Logger.error('Timeline_settings["CMD_duration"]') raise ValueError if not (15 <= Timeline_settings["mode_separation"] and type(Timeline_settings["mode_separation"]) == int): Logger.error('Timeline_settings["mode_separation"]') raise ValueError if not (1 <= Timeline_settings["CMD_separation"] <= 10 and (type(Timeline_settings["CMD_separation"]) == int or type(Timeline_settings["CMD_separation"]) == float)): Logger.error('Timeline_settings["CMD_separation"]') raise ValueError # if not( Timeline_settings['CMD_separation'] * 8 <= Timeline_settings['mode_separation'] ): # Logger.error("Timeline_settings['CMD_separation'] * 8 <= Timeline_settings['mode_separation']. Possibility of time separation between CMDs are too large causing CMDs from Science Modes to overlap") # raise ValueError if not (40 <= Timeline_settings["pointing_stabilization"] and type(Timeline_settings["pointing_stabilization"]) == int): Logger.error("Timeline_settings['pointing_stabilization']") raise ValueError if not (10000 <= Timeline_settings["StandardPointingAltitude"] <= 300000 and type(Timeline_settings["StandardPointingAltitude"]) == int): Logger.error("Timeline_settings['StandardPointingAltitude']") raise ValueError if not (Timeline_settings["pointing_stabilization"] * 2 < Timeline_settings["Mode1_2_5_minDuration"] and type(Timeline_settings["Mode1_2_5_minDuration"]) == int): Logger.error("Timeline_settings['Mode1_2_5_minDuration']") raise ValueError if not (type(Timeline_settings["yaw_correction"]) == bool): Logger.error("Timeline_settings['yaw_correction']") raise TypeError if not (Timeline_settings["Choose_Operational_Science_Mode"] in [0, 1, 2, 5]): Logger.error( "Timeline_settings['Choose_Operational_Science_Mode'] != 0, 1, 2, or 5" ) raise ValueError if not (0 <= abs(Timeline_settings["yaw_amplitude"]) < 20 and (type(Timeline_settings["yaw_amplitude"]) == int or type(Timeline_settings["yaw_amplitude"]) == float)): Logger.error("Timeline_settings['yaw_amplitude']") raise ValueError if not (0 <= abs(Timeline_settings["yaw_phase"]) and (type(Timeline_settings["yaw_phase"]) == int or type(Timeline_settings["yaw_phase"]) == float)): Logger.error("Timeline_settings['yaw_phase']") raise ValueError for key in Operational_Science_Mode_settings.keys(): if key == "Choose_Mode5CCDMacro": if not (Operational_Science_Mode_settings[key] in [ "CustomBinning", "HighResUV", "HighResIR", "LowPixel", "FullReadout", "BinnedCalibration", ]): Logger.error( 'Operational_Science_Mode_settings["Choose_Mode5CCDMacro"]' ) raise ValueError else: if key == "timestep": if not (Operational_Science_Mode_settings[key] < 50): Logger.error( 'Operational_Science_Mode_settings["timestep"]') raise ValueError elif key == "lat": if not (0 <= Operational_Science_Mode_settings[key] <= 90): Logger.error('Operational_Science_Mode_settings["lat"]') raise ValueError elif not (Operational_Science_Mode_settings[key] > 0 and type(Operational_Science_Mode_settings[key]) == int): Logger.error("Operational_Science_Mode_settings") raise ValueError # if not( 10000 <= Mode5_settings['pointing_altitude'] <= 300000 and type(Mode5_settings['pointing_altitude']) == int ): # Logger.error("Mode5_settings['pointing_altitude']") # raise ValueError if not (32 < Mode100_settings["pointing_duration"] and type(Mode100_settings["pointing_duration"]) == int): Logger.error("Mode100_settings['pointing_duration']") raise ValueError if not (type(Mode100_settings["pointing_altitude_interval"]) == int and type(Mode100_settings["pointing_altitude_to"]) == int and type(Mode100_settings["pointing_altitude_from"]) == int): Logger.error("Mode100_settings") raise TypeError if not (abs(Mode100_settings["pointing_altitude_interval"]) <= abs(Mode100_settings["pointing_altitude_to"] - Mode100_settings["pointing_altitude_from"])): Logger.error("Mode100_settings['pointing_altitude_interval']") raise ValueError if not (0 < Mode100_settings["pointing_altitude_interval"] * (Mode100_settings["pointing_altitude_to"] - Mode100_settings["pointing_altitude_from"])): Logger.error("Mode100_settings") raise ValueError if not (type(Mode100_settings["start_date"]) == str): Logger.error("Mode100_settings['start_date']") raise TypeError if not (type(Mode100_settings["Exp_Time_IR"]) == int and type(Mode100_settings["Exp_Time_UV"]) == int): Logger.error( "Mode100_settings['Exp_Time_IR'] or Mode100_settings['Exp_Time_UV']" ) raise TypeError numberOfAltitudes = int( abs(Mode100_settings["pointing_altitude_to"] - Mode100_settings["pointing_altitude_from"]) / abs(Mode100_settings["pointing_altitude_interval"]) + 1) if not (Mode100_settings["Exp_Time_IR"] + numberOfAltitudes * Mode100_settings["ExpTime_step"] < Mode100_settings["pointing_duration"] * 1000): Logger.error("Mode100_settings['pointing_duration']") raise ValueError if not (Mode100_settings["Exp_Time_UV"] + numberOfAltitudes * Mode100_settings["ExpTime_step"] < Mode100_settings["pointing_duration"] * 1000): Logger.error("Mode100_settings['pointing_duration']") raise ValueError for key in Mode110_settings.keys(): if key == "start_date": if not (type(Mode110_settings[key]) == str): Logger.error("Mode110_settings") raise ValueError elif key == "pointing_altitude_from" or key == "pointing_altitude_to": if not (-60000 <= Mode110_settings[key] <= 230000): Logger.error("Mode110_settings") raise ValueError elif key == "sweep_rate": if not (-5000 <= Mode110_settings[key] <= 5000 and Mode110_settings[key] != 0): Logger.error("Mode110_settings") raise ValueError else: if not (Mode110_settings[key] > 0 and type(Mode110_settings[key]) == int): Logger.error("Mode110_settings") raise ValueError if sign(Mode110_settings["pointing_altitude_to"] - Mode110_settings["pointing_altitude_from"]) != sign( Mode110_settings["sweep_rate"]): Logger.error("Mode110_settings") raise ValueError if not (-60000 <= Mode120_settings["pointing_altitude"] <= 230000 and type(Mode120_settings["pointing_altitude"]) == int): Logger.error("Mode120_settings['pointing_altitude']") raise ValueError if not (0 < Mode120_settings["timestep"] <= 10 and type(Mode120_settings["timestep"]) == int): Logger.error("Mode120_settings['timestep']") raise ValueError if not (Mode120_settings["freeze_start"] >= Timeline_settings["pointing_stabilization"] + 12 * Timeline_settings["CMD_separation"] and type(Mode120_settings["freeze_start"]) == int): Logger.error("Mode120_settings") raise TypeError if not (type(Mode120_settings["V_offset"]) == list): Logger.error("Mode120_settings['V_offset'] != list") for x in range(len(Mode120_settings["V_offset"])): if not (abs(Mode120_settings["V_offset"][x]) <= 10 and 0 < abs(Mode120_settings["H_offset"]) <= 10): Logger.error( "Mode120_settings['V_offset'] or Mode120_settings['H_offset']") raise ValueError if not (type(Mode120_settings["start_date"]) == str): Logger.error("Mode120_settings['date']") raise TypeError if not (type(Mode120_settings["automatic"]) == bool): Logger.error("Mode120_settings['automatic']") raise TypeError if not (type(Mode120_settings["Vmag"]) == str): Logger.error("Mode120_settings['Vmag']") if not (0 < Mode120_settings["SnapshotTime"] and type(Mode120_settings["SnapshotTime"]) == int): Logger.error("Mode120_settings['SnapshotTime']") raise ValueError if not (2 <= Mode120_settings["SnapshotSpacing"] and type(Mode120_settings["SnapshotSpacing"]) == int): Logger.error("Mode120_settings['SnapshotSpacing']") raise ValueError if not (0 < Mode120_settings["TimeSkip"]["TimeSkip"] <= 2 * 3600 * 24 and (type(Mode120_settings["TimeSkip"]["TimeSkip"]) == int or type(Mode120_settings["TimeSkip"]["TimeSkip"]) == float)): Logger.error("Mode120_settings['TimeSkip']['TimeSkip']") raise ValueError if not (Timeline_settings["StandardPointingAltitude"] < Mode120_settings["pointing_altitude"] and type(Mode120_settings["pointing_altitude"]) == int): Logger.error("Mode120_settings['pointing_altitude']") raise ValueError if not (Mode120_settings["TimeToConsider"]["TimeToConsider"] <= Timeline_settings["duration"]["duration"]): Logger.error( "Mode120_settings['TimeToConsider']['TimeToConsider'] > Timeline_settings['duration']['duration']" ) raise ValueError if not (type(Mode120_settings["CCDSELs"]) == list): Logger.error("Mode120_settings['CCDSELs'] != list") raise TypeError if not (Mode120_settings["SnapshotSpacing"] * (len(Mode120_settings["CCDSELs"]) - 1) + Mode120_settings["SnapshotTime"] < Mode120_settings["freeze_duration"] <= 3600): Logger.error( "Mode120_settings['SnapshotSpacing'] * (len(Mode120_settings['CCDSELs'])-1) + Mode120_settings['SnapshotTime'] > Mode120_settings['freeze_duration'] or Mode120_settings['freeze_duration'] > 3600" ) raise ValueError for CCDSEL in Mode120_settings["CCDSELs"]: if not (CCDSEL in [1, 2, 4, 8, 16, 32]): Logger.error( "Mode120_settings['CCDSELs'] element != [1,2,4,8,16,32]") raise ValueError if not (-60000 <= Mode121_122_123_settings["pointing_altitude"] <= 230000 and type(Mode121_122_123_settings["pointing_altitude"]) == int): Logger.error("Mode121_122_123_settings['pointing_altitude']") raise ValueError if not (0 < Mode121_122_123_settings["timestep"] <= 10 and type(Mode121_122_123_settings["timestep"]) == int): Logger.error("Mode121_122_123_settings['timestep']") raise ValueError if not (Mode121_122_123_settings["freeze_start"] >= Timeline_settings["pointing_stabilization"] + 12 * Timeline_settings["CMD_separation"] and type(Mode121_122_123_settings["freeze_start"]) == int): Logger.error("Mode121_122_123_settings") raise TypeError if not (0 < abs(Mode121_122_123_settings["V_FOV"]) <= 5 and 0 < abs(Mode121_122_123_settings["H_FOV"]) <= 10): Logger.error( "Mode121_122_123_settings['V_FOV'] or Mode121_122_123_settings['H_FOV']" ) raise ValueError if not (type(Mode121_122_123_settings["Vmag"]) == str): Logger.error("Mode121_122_123_settings['Vmag']") raise TypeError if not (0 < Mode121_122_123_settings["SnapshotTime"] < Mode121_122_123_settings["freeze_duration"] - 10 and type(Mode121_122_123_settings["SnapshotTime"]) == int): Logger.error("Mode121_122_123_settings['SnapshotTime']") raise ValueError if not (Timeline_settings["StandardPointingAltitude"] < Mode121_122_123_settings["pointing_altitude"] and type(Mode121_122_123_settings["pointing_altitude"]) == int): Logger.error("Mode121_122_123_settings['pointing_altitude']") raise ValueError if not (0 < Mode121_122_123_settings["TimeSkip"]["TimeSkip"] <= 2 * 3600 * 24 and (type(Mode121_122_123_settings["TimeSkip"]["TimeSkip"]) == int or type(Mode121_122_123_settings["TimeSkip"]["TimeSkip"]) == float)): Logger.error("Mode121_122_123_settings['TimeSkip']['TimeSkip']") raise ValueError if not (0 <= Mode121_122_123_settings["SnapshotSpacing"] and type(Mode121_122_123_settings["SnapshotSpacing"]) == int): Logger.error("Mode121_122_123_settings['SnapshotSpacing']") raise ValueError if not (Mode121_122_123_settings["SnapshotSpacing"] * 6 + Mode121_122_123_settings["SnapshotTime"] < Mode121_122_123_settings["freeze_duration"] <= 3600): Logger.error( "Mode121_122_123_settings['SnapshotSpacing'] * 5 + Mode121_122_123_settings['SnapshotTime'] > Mode121_122_123_settings['freeze_duration'] or Mode121_122_123_settings['freeze_duration'] > 3600" ) raise ValueError if not (Mode121_122_123_settings["TimeToConsider"]["TimeToConsider"] <= Timeline_settings["duration"]["duration"]): Logger.error( "Mode121_122_123_settings['TimeToConsider']['TimeToConsider'] > Timeline_settings['duration']['duration']" ) raise ValueError if not (Mode121_122_123_settings["SnapshotSpacing"] * (len(Mode121_settings["CCDSELs"]) - 1) + Mode121_122_123_settings["SnapshotTime"] < Mode121_122_123_settings["freeze_duration"] <= 3600): Logger.error( "Mode121_122_123_settings['SnapshotSpacing'] * (len(Mode121_settings['CCDSELs'])-1) + Mode124_settings['SnapshotTime'] > Mode121_122_123_settings['freeze_duration'] or Mode121_122_123_settings['freeze_duration'] > 3600" ) raise ValueError if not (type(Mode121_settings["CCDSELs"]) == list): Logger.error("Mode121_settings['CCDSELs'] != list") raise TypeError for CCDSEL in Mode121_settings["CCDSELs"]: if not (CCDSEL in [1, 2, 4, 8, 16, 32]): Logger.error( "Mode121_settings['CCDSELs'] element != [1,2,4,8,16,32]") raise ValueError if not (type(Mode121_settings["start_date"]) == str): Logger.error("Mode121_settings['start_date']") raise TypeError if not (type(Mode122_settings["start_date"]) == str): Logger.error("Mode122_settings['start_date']") raise TypeError if not (type(Mode123_settings["start_date"]) == str): Logger.error("Mode123_settings['start_date']") raise TypeError if not (0 <= Mode122_settings["Exp_Time_IR"] and type(Mode122_settings["Exp_Time_IR"]) == int): Logger.error("Mode122_settings['Exp_Time_IR']") raise ValueError if not (0 <= Mode122_settings["Exp_Time_UV"] and type(Mode122_settings["Exp_Time_UV"]) == int): Logger.error("Mode122_settings['Exp_Time_UV']") raise ValueError if not (0 <= Mode123_settings["Exp_Time_IR"] and type(Mode123_settings["Exp_Time_IR"]) == int): Logger.error("Mode123_settings['Exp_Time_IR']") raise ValueError if not (0 <= Mode123_settings["Exp_Time_UV"] and type(Mode123_settings["Exp_Time_UV"]) == int): Logger.error("Mode123_settings['Exp_Time_UV']") raise ValueError if not (type(Mode121_settings["automatic"]) == bool): Logger.error("Mode121_settings['automatic']") raise TypeError if not (type(Mode122_settings["automatic"]) == bool): Logger.error("Mode122_settings['automatic']") raise TypeError if not (type(Mode123_settings["automatic"]) == bool): Logger.error("Mode123_settings['automatic']") raise TypeError if not (-60000 <= Mode124_settings["pointing_altitude"] <= 230000 and type(Mode124_settings["pointing_altitude"]) == int): Logger.error("Mode124_settings['pointing_altitude']") raise ValueError if not (0 < Mode124_settings["timestep"] <= 10 and type(Mode124_settings["timestep"]) == int): Logger.error("Mode124_settings['timestep']") raise ValueError if not (Mode124_settings["freeze_start"] >= Timeline_settings["pointing_stabilization"] + 12 * Timeline_settings["CMD_separation"] and type(Mode124_settings["freeze_start"]) == int): Logger.error("Mode124_settings") raise TypeError if not (type(Mode124_settings["V_offset"]) == list): Logger.error("Mode124_settings['V_offset'] != list") for x in range(len(Mode120_settings["V_offset"])): if not (abs(Mode124_settings["V_offset"][x]) <= 1 and 0 <= abs(Mode124_settings["H_offset"]) <= 10): Logger.error( "Mode124_settings['V_offset'] or Mode124_settings['H_offset']") raise ValueError if not (type(Mode124_settings["start_date"]) == str): Logger.error("Mode124_settings['start_date']") raise TypeError if not (type(Mode124_settings["automatic"]) == bool): Logger.error("Mode124_settings['automatic']") raise TypeError if not (0 < Mode124_settings["SnapshotTime"] < Mode124_settings["freeze_duration"] - 10 and type(Mode124_settings["SnapshotTime"]) == int): Logger.error("Mode124_settings['SnapshotTime']") raise ValueError if not (Timeline_settings["StandardPointingAltitude"] < Mode124_settings["pointing_altitude"] and type(Mode124_settings["pointing_altitude"]) == int): Logger.error("Mode124_settings['pointing_altitude']") raise ValueError if not (0 <= Mode124_settings["SnapshotSpacing"] and type(Mode124_settings["SnapshotSpacing"]) == int): Logger.error("Mode124_settings['SnapshotSpacing']") raise ValueError if not (type(Mode124_settings["CCDSELs"]) == list): Logger.error("Mode124_settings['CCDSELs'] != list") raise TypeError if not (Mode124_settings["SnapshotSpacing"] * (len(Mode124_settings["CCDSELs"]) - 1) + Mode124_settings["SnapshotTime"] < Mode124_settings["freeze_duration"] <= 3600): Logger.error( "Mode124_settings['SnapshotSpacing'] * (len(Mode124_settings['CCDSELs'])-1) + Mode124_settings['SnapshotTime'] > Mode124_settings['freeze_duration'] or Mode124_settings['freeze_duration'] > 3600" ) raise ValueError for CCDSEL in Mode124_settings["CCDSELs"]: if not (CCDSEL in [1, 2, 4, 8, 16, 32]): Logger.error( "Mode124_settings['CCDSELs'] element != [1,2,4,8,16,32]") raise ValueError if not (Mode124_settings["TimeToConsider"]["TimeToConsider"] <= Timeline_settings["duration"]["duration"]): Logger.error( "Mode124_settings['TimeToConsider'] > Timeline_settings['duration']" ) raise ValueError MaximumNumberOfCMDsInMacro = 12 if not (Timeline_settings["CMD_separation"] <= Mode130_settings["SnapshotSpacing"] and type(Mode130_settings["SnapshotSpacing"]) == int): Logger.error("Mode130_settings['SnapshotSpacing']") raise TypeError if not (Timeline_settings["pointing_stabilization"] + Timeline_settings["CMD_separation"] * MaximumNumberOfCMDsInMacro + Timeline_settings["mode_separation"] < Mode131_settings["mode_duration"] and type(Mode131_settings["mode_duration"]) == int): Logger.error("Mode131_settings['mode_duration'] is too short") raise TypeError if not (Timeline_settings["pointing_stabilization"] + Timeline_settings["CMD_separation"] * MaximumNumberOfCMDsInMacro + Timeline_settings["mode_separation"] < Mode134_settings["mode_duration"] and type(Mode134_settings["mode_duration"]) == int): Logger.error("Mode134_settings['mode_duration'] is too short") raise TypeError if not (-60000 <= Mode130_settings["pointing_altitude"] <= 230000 and type(Mode130_settings["pointing_altitude"]) == int): Logger.error("Mode130_settings['pointing_altitude']") raise ValueError if not (-60000 <= Mode131_settings["pointing_altitude"] <= 230000 and type(Mode131_settings["pointing_altitude"]) == int): Logger.error("Mode131_settings['pointing_altitude']") raise ValueError if not (-60000 <= Mode132_settings["pointing_altitude"] <= 230000 and type(Mode132_settings["pointing_altitude"]) == int): Logger.error("Mode132_settings['pointing_altitude']") raise ValueError if not (-60000 <= Mode133_settings["pointing_altitude"] <= 230000 and type(Mode133_settings["pointing_altitude"]) == int): Logger.error("Mode133_settings['pointing_altitude']") raise ValueError if not (-60000 <= Mode134_settings["pointing_altitude"] <= 230000 and type(Mode134_settings["pointing_altitude"]) == int): Logger.error("Mode134_settings['pointing_altitude']") raise ValueError if not (type(Mode130_settings["start_date"]) == str): Logger.error("Mode130_settings['start_date']") raise TypeError if not (type(Mode131_settings["start_date"]) == str): Logger.error("Mode131_settings['start_date']") raise TypeError if not (type(Mode132_settings["start_date"]) == str): Logger.error("Mode132_settings['start_date']") raise TypeError if not (type(Mode133_settings["start_date"]) == str): Logger.error("Mode133_settings['start_date']") raise TypeError if not (type(Mode134_settings["start_date"]) == str): Logger.error("Mode134_settings['start_date']") raise TypeError if not (type(Mode132_settings["Exp_Times_IR"]) == list and type(Mode132_settings["Exp_Times_UV"]) == list): Logger.error( "Mode132_settings['Exp_Times_IR'] or Mode132_settings['Exp_Times_UV']" ) raise TypeError if not (type(Mode133_settings["Exp_Times_IR"]) == list and type(Mode133_settings["Exp_Times_UV"]) == list): Logger.error( "Mode133_settings['Exp_Times_IR'] or Mode133_settings['Exp_Times_UV']" ) raise TypeError if not (len(Mode132_settings["Exp_Times_IR"]) == len( Mode132_settings["Exp_Times_UV"])): Logger.error( "len(Mode132_settings['Exp_Times_IR']) != len(Mode132_settings['Exp_Times_UV'])" ) raise TypeError if not (len(Mode133_settings["Exp_Times_IR"]) == len( Mode133_settings["Exp_Times_UV"])): Logger.error( "len(Mode133_settings['Exp_Times_IR']) != len(Mode133_settings['Exp_Times_UV'])" ) raise TypeError # Check that CCDsync waits for long enough time for full CCD readout (standard) # Standard CCDsettings _, _, FullReadout_synctime, _ = Library.SyncArgCalculator( configFile.CCD_macro_settings("FullReadout"), Timeline_settings["CCDSYNC_ExtraOffset"], Timeline_settings["CCDSYNC_ExtraIntervalTime"], ) _, _, CustomBinning_synctime, _ = Library.SyncArgCalculator( configFile.CCD_macro_settings("CustomBinning"), Timeline_settings["CCDSYNC_ExtraOffset"], Timeline_settings["CCDSYNC_ExtraIntervalTime"], ) _, _, HighResUV_synctime, _ = Library.SyncArgCalculator( configFile.CCD_macro_settings("HighResUV"), Timeline_settings["CCDSYNC_ExtraOffset"], Timeline_settings["CCDSYNC_ExtraIntervalTime"], ) _, _, HighResIR_syctime, _ = Library.SyncArgCalculator( configFile.CCD_macro_settings("HighResIR"), Timeline_settings["CCDSYNC_ExtraOffset"], Timeline_settings["CCDSYNC_ExtraIntervalTime"], ) _, _, BinnedCalibration, _ = Library.SyncArgCalculator( configFile.CCD_macro_settings("BinnedCalibration"), Timeline_settings["CCDSYNC_ExtraOffset"], Timeline_settings["CCDSYNC_ExtraIntervalTime"], ) max_normal_synctime = (max( max(FullReadout_synctime), max(CustomBinning_synctime), max(HighResUV_synctime), max(HighResIR_syctime), max(BinnedCalibration), ) / 1000) if not Timeline_settings["CCDSYNC_Waittime"] > max_normal_synctime: Logger.error("Timeline_settings['CCDSYNC_Waittime']") raise ValueError Logger.info("CheckConfigFile passed.")
def sim_until(IC, params, stop_fcn, tmax = 2.): """ simulated the SLIP_ode until stop_fcn has a zero-crossing includes a refinement of the time at this instant stop_fcn must be a function of the system state, e.g. stop_fcn(IC) must exist this function is especially adapted to the SLIP state, so it uses dot(x1) = x3, dot(x2) = x4 tmax: maximal simulation time [s] """ init_sign = sign(stop_fcn(IC)) #1st: evaluate a certain fraction tvec_0 = .001*arange(50) sim_results = [] sim_tvecs = [] newIC = IC sim_results.append (odeint(SLIP_ode,newIC,tvec_0, args=(params,),rtol=1e-9)) sim_tvecs.append(tvec_0) check_vec = [init_sign*stop_fcn(x) for x in sim_results[-1]] t_tot = 0. while min(check_vec) > 0: newIC = sim_results[-1][-1,:] sim_results.append ( odeint(SLIP_ode, newIC, tvec_0, args=(params,),rtol=1e-9)) sim_tvecs.append(tvec_0) check_vec = [init_sign*stop_fcn(x) for x in sim_results[-1]] t_tot += tvec_0[-1] # time exceeded or ground hit if t_tot > tmax or min(sim_results[-1][:,1] < 0): raise SimFailError, "simulation failed" # now: zero-crossing detected # -> refine! minidx = find(array(check_vec) < 0)[0] if minidx == 0: # this should not happen because the first value in # check_vec should be BEFORE the zero_crossing by # construction raise ValueError, "ERROR: this should not happen!" # refine simulation by factor 50, but only for two # adjacent original time frames newIC = sim_results[-1][minidx-1,:] sim_results[-1] = sim_results[-1][:minidx,:] sim_tvecs[-1] = sim_tvecs[-1][:minidx] # avoid that last position can be the zero-crossing n_refine = 100 tvec_0 = linspace(tvec_0[0], tvec_0[1] + 2./n_refine, n_refine+2) sim_results.append ( odeint(SLIP_ode, newIC, tvec_0, args=(params,),rtol=1e-9)) sim_tvecs.append(tvec_0) # linearly interpolate to zero check_vec = [init_sign*stop_fcn(x) for x in sim_results[-1]] minidx = find(array(check_vec) < 0)[0] if minidx == 0: # this should not happen because the first value in # check_vec should be BEFORE the zero_crossing by # construction raise ValueError, "ERROR: this should not happen! (2)" # compute location of zero-crossing y0 = sim_results[-1][minidx-1,:] y1 = sim_results[-1][minidx,:] fcn0 = stop_fcn(y0) fcn1 = stop_fcn(y1) t0 = tvec_0[minidx-1] t1 = tvec_0[minidx] t_zero = t0 - (t1-t0)*fcn0/(fcn1 - fcn0) # cut last simulation result and replace last values # by interpolated values sim_results[-1] = sim_results[-1][:minidx+1,:] sim_tvecs[-1] = sim_tvecs[-1][:minidx+1] for coord in arange(sim_results[-1].shape[1]): sim_results[-1][-1,coord] = interp( t_zero, [t0,t1], [sim_results[-1][-2,coord], sim_results[-1][-1,coord]] ) sim_tvecs[-1][-1] = t_zero #newIC = sim_results[-1][minidx-1,:] #sim_results[-1] = sim_results[-1][:minidx,:] #sim_tvecs[-1] = sim_tvecs[-1][:minidx] #tvec_0 = linspace(tvec_0[0],tvec_0[1],100) #sim_results.append ( odeint(SLIP_ode, newIC, tvec_0, # args=(params,),rtol=1e-9)) #sim_tvecs.append(tvec_0) # concatenate lists sim_data = vstack( [x[:-1,:] for x in sim_results[:-1] if x.shape[0] > 1] + [sim_results[-1],]) sim_time = [sim_tvecs[0],] for idx in arange(1,len(sim_tvecs)): sim_time.append(sim_tvecs[idx] + sim_time[-1][-1]) sim_time = hstack([x[:-1] for x in sim_time[:-1]] + [sim_time[-1],]) return sim_data, sim_time
def sim_until(IC, params, stop_fcn, tmax=2.): """ simulated the SLIP_ode until stop_fcn has a zero-crossing includes a refinement of the time at this instant stop_fcn must be a function of the system state, e.g. stop_fcn(IC) must exist this function is especially adapted to the SLIP state, so it uses dot(x1) = x3, dot(x2) = x4 tmax: maximal simulation time [s] """ init_sign = sign(stop_fcn(IC)) #1st: evaluate a certain fraction tvec_0 = .001 * arange(50) sim_results = [] sim_tvecs = [] newIC = IC sim_results.append( odeint(SLIP_ode, newIC, tvec_0, args=(params, ), rtol=1e-9)) sim_tvecs.append(tvec_0) check_vec = [init_sign * stop_fcn(x) for x in sim_results[-1]] t_tot = 0. while min(check_vec) > 0: newIC = sim_results[-1][-1, :] sim_results.append( odeint(SLIP_ode, newIC, tvec_0, args=(params, ), rtol=1e-9)) sim_tvecs.append(tvec_0) check_vec = [init_sign * stop_fcn(x) for x in sim_results[-1]] t_tot += tvec_0[-1] # time exceeded or ground hit if t_tot > tmax or min(sim_results[-1][:, 1] < 0): raise SimFailError, "simulation failed" # now: zero-crossing detected # -> refine! minidx = find(array(check_vec) < 0)[0] if minidx == 0: # this should not happen because the first value in # check_vec should be BEFORE the zero_crossing by # construction raise ValueError, "ERROR: this should not happen!" # refine simulation by factor 50, but only for two # adjacent original time frames newIC = sim_results[-1][minidx - 1, :] sim_results[-1] = sim_results[-1][:minidx, :] sim_tvecs[-1] = sim_tvecs[-1][:minidx] # avoid that last position can be the zero-crossing n_refine = 100 tvec_0 = linspace(tvec_0[0], tvec_0[1] + 2. / n_refine, n_refine + 2) sim_results.append( odeint(SLIP_ode, newIC, tvec_0, args=(params, ), rtol=1e-9)) sim_tvecs.append(tvec_0) # linearly interpolate to zero check_vec = [init_sign * stop_fcn(x) for x in sim_results[-1]] minidx = find(array(check_vec) < 0)[0] if minidx == 0: # this should not happen because the first value in # check_vec should be BEFORE the zero_crossing by # construction raise ValueError, "ERROR: this should not happen! (2)" # compute location of zero-crossing y0 = sim_results[-1][minidx - 1, :] y1 = sim_results[-1][minidx, :] fcn0 = stop_fcn(y0) fcn1 = stop_fcn(y1) t0 = tvec_0[minidx - 1] t1 = tvec_0[minidx] t_zero = t0 - (t1 - t0) * fcn0 / (fcn1 - fcn0) # cut last simulation result and replace last values # by interpolated values sim_results[-1] = sim_results[-1][:minidx + 1, :] sim_tvecs[-1] = sim_tvecs[-1][:minidx + 1] for coord in arange(sim_results[-1].shape[1]): sim_results[-1][-1, coord] = interp( t_zero, [t0, t1], [sim_results[-1][-2, coord], sim_results[-1][-1, coord]]) sim_tvecs[-1][-1] = t_zero #newIC = sim_results[-1][minidx-1,:] #sim_results[-1] = sim_results[-1][:minidx,:] #sim_tvecs[-1] = sim_tvecs[-1][:minidx] #tvec_0 = linspace(tvec_0[0],tvec_0[1],100) #sim_results.append ( odeint(SLIP_ode, newIC, tvec_0, # args=(params,),rtol=1e-9)) #sim_tvecs.append(tvec_0) # concatenate lists sim_data = vstack( [x[:-1, :] for x in sim_results[:-1] if x.shape[0] > 1] + [ sim_results[-1], ]) sim_time = [ sim_tvecs[0], ] for idx in arange(1, len(sim_tvecs)): sim_time.append(sim_tvecs[idx] + sim_time[-1][-1]) sim_time = hstack([x[:-1] for x in sim_time[:-1]] + [ sim_time[-1], ]) return sim_data, sim_time
1.3149, 1.3143] # In[31]: #!/usr/bin/env python import pylab import pywt data1 = pylab.array(range(1, 400) + range(398, 600) + range(601, 1024)) x = pylab.arange(612 - 80, 20, -0.5) / 250. data2 = pylab.sin(40 * pylab.log(x)) * pylab.sign((pylab.log(x))) data3 = forex mode = pywt.MODES.sp1 def plot(data, w, title): print title w = pywt.Wavelet(w) a = data ca = [] cd = [] for i in xrange(5): (a, d) = pywt.dwt(a, w, mode) ca.append(a)
import pylab import pywt data1 = pylab.array(range(1, 400) + range(398, 600) + range(601, 1024)) x = pylab.arange(612 - 80, 20, -0.5) / 250. data2 = pylab.sin(40 * pylab.log(x)) * pylab.sign((pylab.log(x))) from sample_data import ecg as data3 mode = pywt.MODES.sp1 def plot(data, w, title): print title w = pywt.Wavelet(w) a = data ca = [] cd = [] for i in xrange(5): (a, d) = pywt.dwt(a, w, mode) ca.append(a) cd.append(d) rec_a = [] rec_d = [] for i, coeff in enumerate(ca): coeff_list = [coeff, None] + [None] * i rec_a.append(pywt.waverec(coeff_list, w))
def sgn(x): sgn = sign(asarray(x)) return where(sgn==0,1,sgn)
yverdier = f(xverdier) for gjett_nr in range(1, antall_gjett + 1): gjett = midtpunkt(min_verdi, maks_verdi) funksjonsverdi = f(gjett) print(f"Jeg gjetter på {gjett}!") print(f"f({gjett}) = {funksjonsverdi}") if abs(funksjonsverdi) < eps: print("Jeg fant et nullpunkt!") print(f"f({gjett}) = {funksjonsverdi}") break elif sign(funksjonsverdi) == sign(f(min_verdi)): min_verdi = gjett else: # elif sign(funksjonsverdi) == sign(f(maks_verdi)) maks_verdi = gjett plot(xverdier, yverdier) xlim(-6, 6) ylim(-6, 6) axvline(min_verdi, color="orange") axvline(maks_verdi, color="orange") grid() plot([gjett], [funksjonsverdi], "o", color="red") show()
def main(): import optparse from numpy import sum # Parse command line parser = optparse.OptionParser(usage=USAGE) parser.add_option("-p", "--plot", action="store_true", help="Generate pdf with IR-spectrum, broadened with Lorentzian") parser.add_option("-i", "--info", action="store_true", help="Set up/ Calculate vibrations & quit") parser.add_option("-s", "--suffix", action="store", help="Call suffix for binary e.g. 'mpirun -n 4 '", default='') parser.add_option("-r", "--run", action="store", help="path to FHI-aims binary",default='') parser.add_option("-x", "--relax", action="store_true", help="Relax initial geometry") parser.add_option("-m", "--molden", action="store_true", help="Output in molden format") parser.add_option("-w", "--distort", action="store_true", help="Output geometry distorted along imaginary modes") parser.add_option("-t", "--submit", action="store", help="""\ Path to submission script, string <jobname> will be replaced by name + counter, string <outfile> will be replaced by filename""") parser.add_option("-d", "--delta", action="store", type="float", help="Displacement", default=0.0025) parser.add_option("-b", "--broadening", action="store", type="float", help="Broadening for IR-spectrum in cm^{-1}", default=5) options, args = parser.parse_args() if options.info: print __doc__ sys.exit(0) if len(args) != 2: parser.error("Need exactly two arguments") AIMS_CALL=options.suffix+' '+options.run hessian_thresh = -1 name=args[0] mode=args[1] delta=options.delta broadening=options.broadening run_aims=False if options.run!='': run_aims=True submit_script = options.submit is not None if options.plot: import matplotlib as mpl mpl.use('Agg') from pylab import figure if options.plot or mode=='1' or mode=='2': from pylab import savetxt, transpose, eig, argsort, sort,\ sign, pi, dot, sum, linspace, argmin, r_, convolve # Constant from scipy.constants bohr=constants.value('Bohr radius')*1.e10 hartree=constants.value('Hartree energy in eV') at_u=constants.value('atomic mass unit-kilogram relationship') eV=constants.value('electron volt-joule relationship') c=constants.value('speed of light in vacuum') Ang=1.0e-10 hbar=constants.value('Planck constant over 2 pi') Avo=constants.value('Avogadro constant') kb=constants.value('Boltzmann constant in eV/K') pi=constants.pi hessian_factor = eV/(at_u*Ang*Ang) grad_dipole_factor=(eV/(1./(10*c)))/Ang #(eV/Ang -> D/Ang) ir_factor = 1 # Asign all filenames inputgeomerty = 'geometry.in.'+name inputcontrol = 'control.in.'+name atomicmasses = 'masses.'+name+'.dat'; xyzfile = name+'.xyz'; moldenname =name+'.molden'; hessianname = 'hessian.'+name+'.dat'; graddipolename = 'grad_dipole.'+name+'.dat'; irname = 'ir.'+name+'.dat'; deltas=array([-delta,delta]) coeff=array([-1,1]) c_zero = - 1. / (2. * delta) f=open('control.in','r') # read control.in template template_control=f.read() f.close if submit_script: f=open(options.submit,'r') # read submission script template template_job=f.read() f.close folder='' # Dummy ########### Central Point ################################################## if options.relax and (mode=='0' or mode=='2'): # First relax input geometry filename=name+'.out' folder=name+'_relaxation' if not os.path.exists(folder): os.mkdir(folder) # Create folder shutil.copy('geometry.in', folder+'/geometry.in') # Copy geometry new_control=open(folder+'/control.in','w') new_control.write(template_control+'relax_geometry trm 1E-3\n') # Relax! new_control.close() os.chdir(folder) # Change directoy print 'Central Point' if run_aims: os.system(AIMS_CALL+' > '+filename) # Run aims and pipe the output # into a file named 'filename' if submit_script: replace_submission(template_job, name, 0, filename) os.chdir('..') ############################################################################ # Check for relaxed geometry if os.path.exists(folder+'/geometry.in.next_step'): geometry=open(folder+'/geometry.in.next_step','r') else: geometry=open('geometry.in','r') # Read input geometry n_line=0 struc=structure() lines=geometry.readlines() for line in lines: n_line= n_line+1 if line.rfind('set_vacuum_level')!=-1: # Vacuum Level struc.vacuum_level=float(split_line(line)[-1]) if line.rfind('lattice_vector')!=-1: # Lattice vectors and periodic lat=split_line(line)[1:] struc.lattice_vector=append(struc.lattice_vector,float64(array(lat)) [newaxis,:],axis=0) struc.periodic=True if line.rfind('atom')!=-1: # Set atoms line_vals=split_line(line) at=Atom(line_vals[-1],line_vals[1:-1]) if n_line<len(lines): nextline=lines[n_line] if nextline.rfind('constrain_relaxation')!=-1: # constrained? at=Atom(line_vals[-1],line_vals[1:-1],True) else: at=Atom(line_vals[-1],line_vals[1:-1]) struc.join(at) geometry.close() n_atoms= struc.n() n_constrained=n_atoms-sum(struc.constrained) # Atomic mass file mass_file=open(atomicmasses,'w') mass_vector=zeros([0]) for at_unconstrained in struc.atoms[struc.constrained==False]: mass_vector=append(mass_vector,ones(3)*1./sqrt(at_unconstrained.mass())) line='{0:10.5f}'.format(at_unconstrained.mass()) for i in range(3): line=line+'{0:11.4f}'.format(at_unconstrained.coord[i]) line=line+'{0:}\n'.format(at_unconstrained.kind) mass_file.writelines(line) mass_file.close() # Init dip = zeros([n_constrained*3,3]) hessian = zeros([n_constrained*3,n_constrained*3]) index=0 counter=1 # Set up / Read folders for displaced atoms for atom in arange(n_atoms)[struc.constrained==False]: for coord in arange(3): for delta in deltas: filename=name+'.i_atom_'+str(atom)+'.i_coord_'+str(coord)+'.displ_'+\ str(delta)+'.out' folder=name+'.i_atom_'+str(atom)+'.i_coord_'+str(coord)+'.displ_'+\ str(delta) if mode=='0' or mode=='2': # Put new geometry and control.in into folder struc_new=copy.deepcopy(struc) struc_new.atoms[atom].coord[coord]=\ struc_new.atoms[atom].coord[coord]+delta geoname='geometry.i_atom_'+str(atom)+'.i_coord_'+str(coord)+\ '.displ_'+str(delta)+'.in' if not os.path.exists(folder): os.mkdir(folder) new_geo=open(folder+'/geometry.in','w') newline='#\n# temporary structure-file for finite-difference '+\ 'calculation of forces\n' newline=newline+'# displacement {0:8.4f} of \# atom '.format(delta)+\ '{0:5} direction {1:5}\n#\n'.format(atom,coord) new_geo.writelines(newline+struc_new.to_str()) new_geo.close() new_control=open(folder+'/control.in','w') template_control=template_control.replace('relax_geometry', '#relax_geometry') new_control.write(template_control+'compute_forces .true. \n'+\ 'final_forces_cleaned '+\ '.true. \n') new_control.close() os.chdir(folder) # Change directoy print 'Processing atom: '+str(atom+1)+'/'+str(n_atoms)+', coord.: '+\ str(coord+1)+'/'+str(3)+', delta: '+str(delta) if run_aims: os.system(AIMS_CALL+' > '+filename)# Run aims and pipe the output # into a file named 'filename' if submit_script: replace_submission(template_job, name, counter, filename) # os.system('qsub job.sh') # Mind the environment variables os.chdir('..') if mode=='1' or mode=='2': # Read output forces_reached=False atom_count=0 data=open(folder+'/'+filename) for line in data.readlines(): if line.rfind('Dipole correction potential jump')!=-1: dip_jump = float(split_line(line)[-2]) # Periodic if line.rfind('| Total dipole moment [eAng]')!=-1: dip_jump = float64(split_line(line)[-3:]) # Cluster if forces_reached and atom_count<n_atoms: # Read Forces struc.atoms[atom_count].force=float64(split_line(line)[2:]) atom_count=atom_count+1 if atom_count==n_atoms: forces_reached=False if line.rfind('Total atomic forces')!=-1: forces_reached=True data.close() if struc.periodic: pass #dip[index,2]=dip[index,2]+dip_jump*coeff[deltas==delta]*c_zero else: dip[index,:]=dip[index,:]+dip_jump*coeff[deltas==delta]*c_zero forces=array([]) for at_unconstrained in struc.atoms[struc.constrained==False]: forces=append(forces,coeff[deltas==delta]*at_unconstrained.force) hessian[index,:]=hessian[index,:]+forces*c_zero counter=counter+1 index=index+1 if mode=='1' or mode=='2': # Calculate vibrations print 'Entering hessian diagonalization' print 'Number of atoms = '+str(n_atoms) print 'Name of Hessian input file = '+hessianname print 'Name of grad dipole input file = '+graddipolename print 'Name of Masses input file = '+atomicmasses print 'Name of XYZ output file = '+xyzfile print 'Threshold for Matrix elements = '+str(hessian_thresh) if (hessian_thresh < 0.0): print ' All matrix elements are taken'+\ ' into account by default\n' savetxt(hessianname,hessian) savetxt(graddipolename,dip) mass_mat=mass_vector[:,newaxis]*mass_vector[newaxis,:] hessian[abs(hessian)<hessian_thresh]=0.0 hessian=hessian*mass_mat*hessian_factor hessian=(hessian+transpose(hessian))/2. # Diagonalize hessian (scipy) print 'Solving eigenvalue system for Hessian Matrix' freq, eig_vec = eig(hessian) print 'Done ... ' eig_vec=eig_vec[:,argsort(freq)] freq=sort(sign(freq)*sqrt(abs(freq))) ZPE=hbar*(freq)/(2.0*eV) freq = (freq)/(200.*pi*c) grad_dipole = dip * grad_dipole_factor eig_vec = eig_vec*mass_vector[:,newaxis]*ones(len(mass_vector))[newaxis,:] infrared_intensity = sum(dot(transpose(grad_dipole),eig_vec)**2,axis=0)*\ ir_factor reduced_mass=sum(eig_vec**2,axis=0) norm = sqrt(reduced_mass) eig_vec = eig_vec/norm # The rest is output, xyz, IR,... print 'Results\n' print 'List of all frequencies found:' print 'Mode number Frequency [cm^(-1)] Zero point energy [eV] '+\ 'IR-intensity [D^2/Ang^2]' for i in range(len(freq)): print '{0:11}{1:25.8f}{2:25.8f}{3:25.8f}'.format(i+1,freq[i],ZPE[i], infrared_intensity[i]) print '\n' print 'Summary of zero point energy for entire system:' print '| Cumulative ZPE = {0:15.8f} eV'.format(sum(ZPE)) print '| without first six eigenmodes = {0:15.8f} eV\n'.format(sum(ZPE)- sum(ZPE[:6])) print 'Stability checking - eigenvalues should all be positive for a '+\ 'stable structure. ' print 'The six smallest frequencies should be (almost) zero:' string='' for zz in ZPE[:6]: string=string+'{0:25.8f}'.format(zz) print string print 'Compare this with the largest eigenvalue, ' print '{0:25.8f}'.format(freq[-1]) nums=arange(n_atoms)[struc.constrained==False] nums2=arange(n_atoms)[struc.constrained] newline='' newline_ir='[INT]\n' if options.molden: newline_molden='[Molden Format]\n[GEOMETRIES] XYZ\n' newline_molden=newline_molden+'{0:6}\n'.format(n_atoms)+'\n' for i_atoms in range(n_constrained): newline_molden=newline_molden+'{0:6}'.format( struc.atoms[nums[i_atoms]].kind) for i_coord in range(3): newline_molden=newline_molden+'{0:10.4f}'.format( struc.atoms[nums[i_atoms]].coord[i_coord]) newline_molden=newline_molden+'\n' newline_molden=newline_molden+'[FREQ]\n' for i in range(len(freq)): newline_molden=newline_molden+'{0:10.3f}\n'.format(freq[i]) newline_molden=newline_molden+'[INT]\n' for i in range(len(freq)): newline_molden=newline_molden+'{0:17.6e}\n'.format( infrared_intensity[i]) newline_molden=newline_molden+'[FR-COORD]\n' newline_molden=newline_molden+'{0:6}\n'.format(n_atoms)+'\n' for i_atoms in range(n_constrained): newline_molden=newline_molden+'{0:6}'.format( struc.atoms[nums[i_atoms]].kind) for i_coord in range(3): newline_molden=newline_molden+'{0:10.4f}'.format( struc.atoms[nums[i_atoms]].coord[i_coord]/bohr) newline_molden=newline_molden+'\n' newline_molden=newline_molden+'[FR-NORM-COORD]\n' for i in range(len(freq)): newline=newline+'{0:6}\n'.format(n_atoms) if freq[i]>0: newline=newline+'stable frequency at ' elif freq[i]<0: newline=newline+'unstable frequency at ' if options.distort and freq[i]<-50: struc_new=copy.deepcopy(struc) for i_atoms in range(n_constrained): for i_coord in range(3): struc_new.atoms[i_atoms].coord[i_coord]=\ struc_new.atoms[i_atoms].coord[i_coord]+\ eig_vec[(i_atoms)*3+i_coord,i] geoname=name+'.distorted.vibration_'+str(i+1)+'.geometry.in' new_geo=open(geoname,'w') newline_geo='#\n# distorted structure-file for based on eigenmodes\n' newline_geo=newline_geo+\ '# vibration {0:5} :{1:10.3f} 1/cm\n#\n'.format(i+1,freq[i]) new_geo.writelines(newline_geo+struc_new.to_str()) new_geo.close() elif freq[i]==0: newline=newline+'translation or rotation ' newline=newline+'{0:10.3f} 1/cm IR int. is '.format(freq[i]) newline=newline+'{0:10.4e} D^2/Ang^2; red. mass is '.format( infrared_intensity[i]) newline=newline+'{0:5.3f} a.m.u.; force const. is '.format( 1.0/reduced_mass[i]) newline=newline+'{0:5.3f} mDyne/Ang.\n'.format(((freq[i]*(200*pi*c))**2)* (1.0/reduced_mass[i])*at_u*1.e-2) if options.molden: newline_molden=newline_molden+\ 'vibration {0:6}\n'.format(i+1) for i_atoms in range(n_constrained): newline=newline+'{0:6}'.format(struc.atoms[nums[i_atoms]].kind) for i_coord in range(3): newline=newline+'{0:10.4f}'.format( struc.atoms[nums[i_atoms]].coord[i_coord]) for i_coord in range(3): newline=newline+'{0:10.4f}'.format(eig_vec[(i_atoms)*3+i_coord,i]) if options.molden: newline_molden=newline_molden+'{0:10.4f}'.format( eig_vec[(i_atoms)*3+i_coord,i]/bohr) newline=newline+'\n' if options.molden: newline_molden=newline_molden+'\n' for i_atoms in range(n_atoms-n_constrained): newline=newline+'{0:6}'.format(struc.atoms[nums2[i_atoms]].kind) for i_coord in range(3): newline=newline+'{0:10.4f}'.format( struc.atoms[nums2[i_atoms]].coord[i_coord]) for i_coord in range(3): newline=newline+'{0:10.4f}'.format(0.0) newline=newline+'\n' newline_ir=newline_ir+'{0:10.4e}\n'.format(infrared_intensity[i]) xyz=open(xyzfile,'w') xyz.writelines(newline) xyz.close() ir=open(irname,'w') ir.writelines(newline_ir) ir.close() if options.molden: molden=open(moldenname,'w') molden.writelines(newline_molden) molden.close() if (mode=='1' or mode=='2') and options.plot: x=linspace(freq.min()-500,freq.max()+500,1000) z=zeros(len(x)) for i in range(len(freq)): z[argmin(abs(x-freq[i]))]=infrared_intensity[i] window_len=150 lorentzian=lorentz(pi,broadening,arange(250))#signal.gaussian(window_len,broadening) s=r_[z[window_len-1:0:-1],z,z[-1:-window_len:-1]] z_convolve=convolve(lorentzian/lorentzian.sum(),s,mode='same')[ window_len-1:-window_len+1] fig=figure(0) ax=fig.add_subplot(111) ax.plot(x,z_convolve,'r',lw=2) ax.set_xlim([freq.min()-500,freq.max()+500]) ax.set_ylim([-0.01,ax.get_ylim()[1]]) ax.set_yticks([]) ax.set_xlabel('Frequency [1/cm]',size=20) ax.set_ylabel('Intensity [a.u.]',size=20) fig.savefig(name+'_IR_spectrum.pdf') print '\n Done. '
def lambert(r1vec,r2vec,tf,m,muC): # original documentation: # ············································· # # This routine implements a new algorithm that solves Lambert's problem. The # algorithm has two major characteristics that makes it favorable to other # existing ones. # # 1) It describes the generic orbit solution of the boundary condition # problem through the variable X=log(1+cos(alpha/2)). By doing so the # graph of the time of flight become defined in the entire real axis and # resembles a straight line. Convergence is granted within few iterations # for all the possible geometries (except, of course, when the transfer # angle is zero). When multiple revolutions are considered the variable is # X=tan(cos(alpha/2)*pi/2). # # 2) Once the orbit has been determined in the plane, this routine # evaluates the velocity vectors at the two points in a way that is not # singular for the transfer angle approaching to pi (Lagrange coefficient # based methods are numerically not well suited for this purpose). # # As a result Lambert's problem is solved (with multiple revolutions # being accounted for) with the same computational effort for all # possible geometries. The case of near 180 transfers is also solved # efficiently. # # We note here that even when the transfer angle is exactly equal to pi # the algorithm does solve the problem in the plane (it finds X), but it # is not able to evaluate the plane in which the orbit lies. A solution # to this would be to provide the direction of the plane containing the # transfer orbit from outside. This has not been implemented in this # routine since such a direction would depend on which application the # transfer is going to be used in. # # please report bugs to [email protected] # # adjusted documentation: # ······················· # # By default, the short-way solution is computed. The long way solution # may be requested by giving a negative value to the corresponding # time-of-flight [tf]. # # For problems with |m| > 0, there are generally two solutions. By # default, the right branch solution will be returned. The left branch # may be requested by giving a negative value to the corresponding # number of complete revolutions [m]. # Authors # .·`·.·`·.·`·.·`·.·`·.·`·.·`·.·`·.·`·.·`·.·`·.·`·.·`·.·`·.·`·. # Name : Dr. Dario Izzo # E-mail : [email protected] # Affiliation: ESA / Advanced Concepts Team (ACT) # Made readible and optimized for speed by Rody P.S. Oldenhuis # Code available in MGA.M on http://www.esa.int/gsp/ACT/inf/op/globopt.htm # last edited 12/Dec/2009 # ADJUSTED FOR EML-COMPILATION 24/Dec/2009 # initial values tol = 1e-12 bad = False days = 1 # work with non-dimensional units r1 = norm(r1vec) #sqrt(r1vec*r1vec.'); r1vec = r1vec/r1; r1vec = r1vec / r1 r2vec = r2vec / r1 V = sqrt(muC/r1) T = r1/V tf= tf*days/T # also transform to seconds # relevant geometry parameters (non dimensional) mr2vec = norm(r2vec) # make 100# sure it's in (-1 <= dth <= +1) dth = arccos( max(-1, min(1, (r1vec.dot(r2vec)/mr2vec)))) # decide whether to use the left or right branch (for multi-revolution # problems), and the long- or short way leftbranch = sign(m) longway = sign(tf) m = abs(m) tf = abs(tf) if (longway < 0): dth = 2*pi - dth # derived quantities c = sqrt(1.0 + mr2vec**2 - 2*mr2vec*cos(dth)) # non-dimensional chord s = (1.0 + mr2vec + c)/2.0 # non-dimensional semi-perimeter a_min = s/2.0 # minimum energy ellipse semi major axis Lambda = sqrt(mr2vec)*cos(dth/2.0)/s # lambda parameter (from BATTIN's book) crossprd = cross(r1vec,r2vec) mcr = norm(crossprd) # magnitues thereof nrmunit = crossprd/mcr # unit vector thereof # Initial values # ························································· # ELMEX requires this variable to be declared OUTSIDE the IF-statement logt = log(tf); # avoid re-computing the same value # single revolution (1 solution) if (m == 0): # initial values inn1 = -0.5233 # first initial guess inn2 = +0.5233 # second initial guess x1 = log(1 + inn1)# transformed first initial guess x2 = log(1 + inn2)# transformed first second guess # multiple revolutions (0, 1 or 2 solutions) # the returned soltuion depends on the sign of [m] else: # select initial values if (leftbranch < 0): inn1 = -0.5234 # first initial guess, left branch inn2 = -0.2234 # second initial guess, left branch else: inn1 = +0.7234 # first initial guess, right branch inn2 = +0.5234 # second initial guess, right branch x1 = tan(inn1*pi/2)# transformed first initial guess x2 = tan(inn2*pi/2)# transformed first second guess # since (inn1, inn2) < 0, initial estimate is always ellipse xx = array([inn1, inn2]) aa = a_min/(1 - xx**2) bbeta = longway * 2*arcsin(sqrt((s-c)/2./aa)) # make 100.4% sure it's in (-1 <= xx <= +1) if xx[0] > 1: xx[0] = 1 if xx[0] < -1: xx[0] = -1 if xx[1] > 1: xx[1] = 1 if xx[1] < -1: xx[1] = -1 aalfa = 2*arccos( xx ) # evaluate the time of flight via Lagrange expression y12 = aa*sqrt(aa)*((aalfa - sin(aalfa)) - (bbeta-sin(bbeta)) + 2*pi*m) # initial estimates for y if m == 0: y1 = log(y12[0]) - logt y2 = log(y12[1]) - logt else: y1 = y12[0] - tf y2 = y12[1] - tf # Solve for x # ························································· # Newton-Raphson iterations # NOTE - the number of iterations will go to infinity in case # m > 0 and there is no solution. Start the other routine in # that case err = 1e99 iterations = 0 xnew = 0 while (err > tol): # increment number of iterations iterations += 1 # new x xnew = (x1*y2 - y1*x2) / (y2-y1); # copy-pasted code (for performance) if m == 0: x = exp(xnew) - 1 else: x = arctan(xnew)*2/pi a = a_min/(1 - x**2); if (x < 1): # ellipse beta = longway * 2*arcsin(sqrt((s-c)/2/a)) # make 100.4% sure it's in (-1 <= xx <= +1) alfa = 2*arccos( max(-1, min(1, x)) ) else: # hyperbola alfa = 2*arccosh(x); beta = longway * 2*arcsinh(sqrt((s-c)/(-2*a))) # evaluate the time of flight via Lagrange expression if (a > 0): tof = a*sqrt(a)*((alfa - sin(alfa)) - (beta-sin(beta)) + 2*pi*m) else: tof = -a*sqrt(-a)*((sinh(alfa) - alfa) - (sinh(beta) - beta)) # new value of y if m ==0: ynew = log(tof) - logt else: ynew = tof - tf # save previous and current values for the next iterarion # (prevents getting stuck between two values) x1 = x2; x2 = xnew; y1 = y2; y2 = ynew; # update error err = abs(x1 - xnew); # escape clause if (iterations > 15): bad = True break # If the Newton-Raphson scheme failed, try to solve the problem # with the other Lambert targeter. if bad: # NOTE: use the original, UN-normalized quantities #[V1, V2, extremal_distances, exitflag] = ... # lambert_high_LancasterBlanchard(r1vec*r1, r2vec*r1, longway*tf*T, leftbranch*m, muC); print "FAILZ0r" return # convert converged value of x if m==0: x = exp(xnew) - 1 else: x = arctan(xnew)*2/pi #{ # The solution has been evaluated in terms of log(x+1) or tan(x*pi/2), we # now need the conic. As for transfer angles near to pi the Lagrange- # coefficients technique goes singular (dg approaches a zero/zero that is # numerically bad) we here use a different technique for those cases. When # the transfer angle is exactly equal to pi, then the ih unit vector is not # determined. The remaining equations, though, are still valid. #} # Solution for the semi-major axis a = a_min/(1-x**2); # Calculate psi if (x < 1): # ellipse beta = longway * 2*arcsin(sqrt((s-c)/2/a)) # make 100.4# sure it's in (-1 <= xx <= +1) alfa = 2*arccos( max(-1, min(1, x)) ) psi = (alfa-beta)/2 eta2 = 2*a*sin(psi)**2/s eta = sqrt(eta2); else: # hyperbola beta = longway * 2*arcsinh(sqrt((c-s)/2/a)) alfa = 2*arccosh(x) psi = (alfa-beta)/2 eta2 = -2*a*sinh(psi)**2/s eta = sqrt(eta2) # unit of the normalized normal vector ih = longway * nrmunit; # unit vector for normalized [r2vec] r2n = r2vec/mr2vec; # cross-products # don't use cross() (emlmex() would try to compile it, and this way it # also does not create any additional overhead) #crsprd1 = [ih(2)*r1vec(3)-ih(3)*r1vec(2),... # ih(3)*r1vec(1)-ih(1)*r1vec(3),... # ih(1)*r1vec(2)-ih(2)*r1vec(1)]; crsprd1 = cross(ih,r1vec) #crsprd2 = [ih(2)*r2n(3)-ih(3)*r2n(2),... # ih(3)*r2n(1)-ih(1)*r2n(3),... # ih(1)*r2n(2)-ih(2)*r2n(1)]; crsprd2 = cross(ih,r2n) # radial and tangential directions for departure velocity Vr1 = 1/eta/sqrt(a_min) * (2*Lambda*a_min - Lambda - x*eta) Vt1 = sqrt(mr2vec/a_min/eta2 * sin(dth/2)**2) # radial and tangential directions for arrival velocity Vt2 = Vt1/mr2vec Vr2 = (Vt1 - Vt2)/tan(dth/2) - Vr1 # terminal velocities V1 = (Vr1*r1vec + Vt1*crsprd1)*V V2 = (Vr2*r2n + Vt2*crsprd2)*V # exitflag #exitflag = 1 # (success) #print "V1:",V1 #print "V2:",V2 return V1,V2
def phiPrime1(self,t): return M.sign(t)
hessian = [ \ [ 4.31555686e+27, -8.12601299e+07, -1.50721747e+21, -2.24466888e+27, \ -6.40887956e+07, 6.64242815e+20], \ [ -8.12601299e+07, 4.31555537e+27, 1.33622795e+21, 1.73705009e+08, \ -2.24467067e+27, -7.89181980e+20], \ [ -1.50721747e+21, 1.33622795e+21, 1.00924243e+29, 5.49874236e+20, \ -8.30018741e+20, -6.64344894e+28], \ [ -2.24466888e+27, 1.73705009e+08, 5.49874236e+20, 1.16750303e+27, \ -3.54607612e+07, -5.54914332e+20], \ [ -6.40887956e+07, -2.24467067e+27, - 8.30018741e+20, -3.54607612e+07, \ 1.16750530e+27, 2.98894228e+20], \ [ 6.64242815e+20, -7.89181980e+20, -6.64344894e+28, -5.54914332e+20, \ 2.98894228e+20, 6.01133870e+28] \ ] print 'Solving eigenvalue system for Hessian Matrix' freq, eig_vec = eig(hessian) freq=sort(sign(freq)*sqrt(abs(freq))) freq = (freq)/(200.*pi*c) print 'Done ... ' print(freq) for i in range(6): for j in range(6): if(abs(hessian[i][j])<1e+28): hessian[i][j] = 0.0 freq, eig_vec = eig(hessian) freq=sort(sign(freq)*sqrt(abs(freq))) freq = (freq)/(200.*pi*c) print(freq)