def advance(self, time, time_step): # TOCHECK: can we take advance out of this class? """ Advance the surface by one time step. """ # move points along point direction while True: try: new_surface = deepcopy(self) normal_velocities = -new_surface.fluxes / new_surface.material_densities displacements = normal_velocities * time_step new_surface.old_positions = copy(new_surface.positions) new_surface.positions[:, 0] += new_surface.directions[:, 0] * displacements # change shape direction ? new_surface.positions[:, 1] += new_surface.directions[:, 1] * displacements # change shape direction ? new_pos_x = new_surface.positions[:, 0] new_pos_z = new_surface.positions[:, 1] new_surface.material_indexes = self.mat_track.get_materials( np.array([new_pos_x, np.zeros(new_pos_x.shape), new_pos_z])) new_surface.material_names = extract_names( new_surface.material_indexes) new_surface.material_densities = extract_densities( new_surface.material_indexes) if par.INTERPOLATE: new_surface.detect_loops() break except CrossedPointsError: time_step = time_step / 2 print_log('time, reduced time step:', time, time_step) # interpolate surface back to original grid if par.INTERPOLATE: new_surface.interpolate_to_grid_of(self) # refine grid if necessary if par.ADAPTIVE_GRID: while True: try: refined = new_surface.adapt_grid() if not refined: break except TooLargeSegmentError: #go back to old surface new_surface = deepcopy(self) raise HasUndefinedFluxError return new_surface, time_step
def write_contour(self, time, linestyle): """ Write contour to SURFACE_FILE. """ header = 'contour: ' + str(time) + ' ' + str( self.len_x) + ' ' + linestyle header += ' x-positions' if par.SAVE_POSITIONS: header += ' z-positions' if par.SAVE_ANGLES: header += ' angles' if par.SAVE_FLUXES: header += ' fluxes' if par.SAVE_BEAM_FLUXES: header += ' beam-fluxes' if par.SAVE_PRECURSOR: header += ' precursor' if par.SAVE_MATERIAL_NAMES: header += ' material(%d)' % (len(par.MATERIAL_NAMES)) header += '\n' f = open(par.SURFACE_FILE, 'a') f.write(header) for position, costheta, flux, beam_flux, precursor, material in \ zip(self.positions, self.costhetas, self.fluxes, self.beam_fluxes, self.coverages, self.material_names): if par.SAVE_BINARY: line = str(float(position[0]).hex()) else: line = str(float(position[0])) if par.SAVE_POSITIONS: if par.SAVE_BINARY: line += ' ' + str(float(position[1]).hex()) else: line += ' ' + str(float(position[1])) if par.SAVE_ANGLES: line += ' ' + str(float(np.degrees(np.arccos(costheta)))) if par.SAVE_FLUXES: line += ' ' + str(float(flux)) if par.SAVE_BEAM_FLUXES: line += ' ' + str(float(beam_flux)) if par.SAVE_PRECURSOR: line += ' ' + str(float(precursor)) if par.SAVE_MATERIAL_NAMES: line += ' ' + str(material) line += '\n' f.write(line) f.write('end of contour \n') f.close() # print information to log file print_log('time zmin zmax =', \ time, min(self.positions[:,1]), max(self.positions[:,1]))
def generate_movie_show_materials(spin=False): """ Generate a movie of the contour evolution using contours from file SURFACE_FILE. """ contour = read_contour_from_file_materials() counter = 1 files = [] fig = plt.figure() fig.suptitle("Title") ax = Axes3D(fig) rotation = 150.0 while True: try: x, y, z ,mat= contour.next() ax.plot_wireframe(x, y, z, color='k') mat_use = ['Si','W','poly'] color_use = ['b','r','g'] for material,c in zip(mat_use,color_use): mask = mat == material ax.scatter(x[mask],y[mask],z[mask],c=c,s=35) print x.shape, y.shape, z.shape ax.plot(x[:,-4],y[:,-4],z[:,-4],linewidth=8, color = 'm') ax.plot(x[:,-21],y[:,-21],z[:,-21],linewidth=8, color = 'y') #Xstart = (x.min()) #Zstart = (z.min()) #Xend = (x.max()) ax.set_zlim3d(-1000, 0) ax.view_init(elev=30.0,azim=rotation) if spin: rotation += 0.2 #ax.set_zlim3d(Zstart, Zstart+(Xend-Xstart)) tmp_file = '_tmp%012d.png' % counter files.append(tmp_file) fig.savefig(tmp_file) ax.cla() print_log("Frame", counter) counter += 1 except StopIteration: break basename, ext = os.path.splitext(par.SURFACE_FILE) if spin: basename = basename + '_spin' avifile = basename + '.avi' os.system("mencoder 'mf://_tmp*.png' -mf type=png:fps=60 \ -ovc lavc -lavcopts vcodec=msmpeg4v2 -oac copy -o " + avifile) pngfile = basename + '.png' os.rename(files[-1], pngfile) for file in files[:-1]: os.remove(file)
def generate_movie(spin=False): """ Generate a movie of the contour evolution using contours from file SURFACE_FILE. """ contour = read_contour_from_file() counter = 1 files = [] fig = plt.figure() fig.suptitle("Title") ax = Axes3D(fig) rotation = 0.0 while True: try: x, y, z = contour.next() ax.plot_wireframe(x, y, z) #Xstart = (x.min()) #Zstart = (z.min()) #Xend = (x.max()) ax.set_zlim3d(-400, 0) if spin: ax.view_init(elev=30.0,azim=rotation) rotation += 0.2 #ax.set_zlim3d(Zstart, Zstart+(Xend-Xstart)) tmp_file = '_tmp%012d.png' % counter files.append(tmp_file) fig.savefig(tmp_file) ax.cla() print_log("Frame", counter) counter += 1 except StopIteration: break basename, ext = os.path.splitext(par.SURFACE_FILE) if spin: basename = basename + '_spin' avifile = basename + '.avi' os.system("mencoder 'mf://_tmp*.png' -mf type=png:fps=24 \ -ovc lavc -lavcopts vcodec=msmpeg4v2 -oac copy -o " + avifile) pngfile = basename + '.png' os.rename(files[-1], pngfile) for file in files[:-1]: os.remove(file)
def write_contour(self, time, linestyle): """ Write contour to SURFACE_FILE. """ header = 'contour: ' + str(time) + ' ' + linestyle if par.SAVE_POSITIONS: header += ' z-positions' if par.SAVE_ANGLES: header += ' angles' if par.SAVE_FLUXES: header += ' fluxes' if par.SAVE_BEAM_FLUXES: header += ' beam-fluxes' if par.SAVE_PRECURSOR: header += ' precursor' header += '\n' f = open(par.SURFACE_FILE, 'a') f.write(header) for position, costheta, flux, beam_flux, precursor in \ zip(self.positions, self.costhetas, self.fluxes, self.beam_fluxes, self.coverages): line = str(float(position[0])) if par.SAVE_POSITIONS: line += ' ' + str(float(position[1])) if par.SAVE_ANGLES: line += ' ' + str(float(np.degrees(np.arccos(costheta)))) if par.SAVE_FLUXES: line += ' ' + str(float(flux)) if par.SAVE_BEAM_FLUXES: line += ' ' + str(float(beam_flux)) if par.SAVE_PRECURSOR: line += ' ' + str(float(precursor)) line += '\n' f.write(line) f.write('end of contour \n') f.close() # print information to log file print_log('time z= ', time, self.positions[:])
def get_sputter_2D_angular_dist(material_names, cosalpha, costheta, positive_phi): """ Evaluate distribution function (including the sputter yield) using spline function for given local incidence angles (theta = angle between vertex normal and beam) and exit angle (alpha = angle between vertex normal and destination of redeposition). cosalpha: cos of the angle between vertex normal and direction of ejection costheta: cos of the angle between the beam and the vertex normal positive_phi: boolean array indicating that phi is positive (i.e. beam source and direction of ejection are on opposite sides of the vertex normal vector) All inputs are 1D (masked) arrays. """ if False: # Debug print_log('Unmasked point pairs:', cosalpha.size, ' positive phi:', cosalpha[positive_phi].size) if cosalpha.size == 0: return cosalpha # determine theta and alpha in rad theta = np.degrees(np.arccos(costheta)) negative_phi = np.logical_not( positive_phi) #in numpy !boolean_array = -mask alpha = np.arccos(cosalpha) * 180 / np.pi alpha[negative_phi] = -alpha[negative_phi] dist = np.empty_like(theta) syields = np.ones_like(theta) for material_name in par.MATERIAL_NAMES: mask = (material_names == material_name) if len(theta[mask]) > 0: dist[mask] = SPUTTER_ANG_DIST_FUN[material_name](theta[mask], alpha[mask]) if ANG_DIST_FILE_EXT[material_name] == '.npz': syields[mask] = SYIELD_FUN[material_name](theta[mask]) dist = np.nan_to_num(dist / syields) dist *= 180 / np.pi return dist
def get_new_precursor_coverages(surface, sputter_fluxes, time_step): """ Return precursor coverages at surface.positions. """ if par.DIFFUSION: precursor_consumptions = par.A_PC * par.N_PC * sputter_fluxes diffusion_matrix, rhs, nx, ny = \ surface.get_precursor_diffusion_matrix(precursor_consumptions, time_step) new_coverages = solve_equation_system(diffusion_matrix, rhs, nx, ny) if par.VERBOSE: print_log('max. precursor:', np.max(new_coverages), '\n') else: new_coverages = (surface.coverages + par.A_PC*par.F_PC*par.S_PC*time_step) / \ (1.0 + par.A_PC*(par.F_PC*par.S_PC + sputter_fluxes*par.N_PC)*time_step) return new_coverages
def adapt_grid(self): """ Adapt the grid as to meet the grid criteria. """ adapted = False # check on deletions in x-direction positions = self.positions.reshape(self.len_x, self.len_y, 3) x_segment_lengths_after_deletion = distance_between_array( positions[2:, :], positions[:-2, :]) max_x_segment_lengths_after_deletion = np.amax( x_segment_lengths_after_deletion, axis=1) obsolete_x_points = (max_x_segment_lengths_after_deletion < par.MAX_SEGLEN[0]) obsolete_x_points = np.concatenate( ((False, ), obsolete_x_points, (False, ))) if any(obsolete_x_points): # points may only be deleted if the two neighbors are not deleted isolated_x_points = np.logical_not(obsolete_x_points[:-2]) & \ np.logical_not(obsolete_x_points[2:]) isolated_x_points = np.concatenate( ((False, ), isolated_x_points, (False, ))) # if there are consecutive obsolete points, only every second must be deleted odd_x_points = (np.arange(self.len_x) % 2 == 1) remove_x_points = obsolete_x_points & (isolated_x_points | odd_x_points) indices = np.ravel(np.where(remove_x_points)) print_log('delete at x-indices', indices) self.remove_points(indices, axis=0) adapted = True # check on deletions in y-direction positions = self.positions.reshape(self.len_x, self.len_y, 3) y_segment_lengths_after_deletion = distance_between_array( positions[:, 2:], positions[:, :-2]) max_y_segment_lengths_after_deletion = np.amax( y_segment_lengths_after_deletion, axis=0) obsolete_y_points = (max_y_segment_lengths_after_deletion < par.MAX_SEGLEN[1]) obsolete_y_points = np.concatenate( ((False, ), obsolete_y_points, (False, ))) if any(obsolete_y_points): # points may only be deleted if the two neighbors are not deleted isolated_y_points = np.logical_not(obsolete_y_points[:-2]) & \ np.logical_not(obsolete_y_points[2:]) isolated_y_points = np.concatenate( ((False, ), isolated_y_points, (False, ))) # if there are consecutive obsolete points, only every second must be deleted odd_y_points = (np.arange(self.len_y) % 2 == 1) remove_y_points = obsolete_y_points & (isolated_y_points | odd_y_points) indices = np.ravel(np.where(remove_y_points)) print_log('delete at y-indices', indices) self.remove_points(indices, axis=1) adapted = True # refine segments in x-direction positions = self.positions.reshape(self.len_x, self.len_y, 3) x_segment_lengths = distance_between_array(positions[:-1, :], positions[1:, :]) max_x_segment_lengths = np.amax(x_segment_lengths, axis=1) too_large_x_segments = (max_x_segment_lengths > (1 + par.GRID_LAZINESS/100)*par.MAX_SEGLEN[0]) & \ self.refine_mask_x[:-1] if any(too_large_x_segments): refined_delta_x = (positions[1:, :, 0] - positions[:-1, :, 0]) / 2 min_refined_delta_x = np.amin(np.absolute(refined_delta_x), axis=1) # minimum of each row refine_x_segments = too_large_x_segments & (min_refined_delta_x >= par.MIN_DELTA[0]) if any(refine_x_segments): indices = np.ravel(np.where(refine_x_segments)) + 1 print_log('refine at x-indices', indices) self.insert_points(indices, axis=0) adapted = True # refine segments in y-direction positions = self.positions.reshape(self.len_x, self.len_y, 3) y_segment_lengths = distance_between_array(positions[:, :-1], positions[:, 1:]) max_y_segment_lengths = np.amax(y_segment_lengths, axis=0) too_large_y_segments = (max_y_segment_lengths > (1 + par.GRID_LAZINESS/100)*par.MAX_SEGLEN[1]) & \ self.refine_mask_y[:-1] if any(too_large_y_segments): refined_delta_y = (positions[:, 1:, 1] - positions[:, :-1, 1]) / 2 min_refined_delta_y = np.amin(np.absolute(refined_delta_y), axis=0) #minimum of each column refine_y_segments = too_large_y_segments & (min_refined_delta_y >= par.MIN_DELTA[1]) if any(refine_y_segments): indices = np.ravel(np.where(refine_y_segments)) + 1 print_log('refine at y-indices', indices) self.insert_points(indices, axis=1) adapted = True #only remove points if number of points = MAX_POINTS #if len(self.positions) == par.MAX_POINTS: # pass return adapted
def generate_movie_slice_rotate(spin=False, zslice=(float('-Inf'),float('Inf')), xrot=0.0 ): """ Generate a movie of the contour evolution using contours from file SURFACE_FILE. """ contour = read_contour_from_file() counter = 1 files = [] top_files =[] spin_files = [] back_files = [] left_files = [] comp_files = [] flat_2D_files = [] fig_spin = plt.figure(figsize=(8,6),dpi=100) fig_back = plt.figure(figsize=(8,6),dpi=100) fig_top = plt.figure(figsize=(8,6),dpi=100) fig_left = plt.figure(figsize=(8,6),dpi=100) fig_2D = plt.figure(figsize=(8,6),dpi=100) fig_left.suptitle("Left Side") fig_spin.suptitle("Rotating") fig_back.suptitle("Front View") fig_top.suptitle("Top View") ax_spin = Axes3D(fig_spin) ax_left = Axes3D(fig_left) ax_back = Axes3D(fig_back) ax_top = Axes3D(fig_top) front_2D = fig_2D.add_subplot(2,2,1) top_2D = fig_2D.add_subplot(2,2,3) side_2D = fig_2D.add_subplot(1,2,2) rotation = 140.0 basename, ext = os.path.splitext(par.SURFACE_FILE) while True: try: x, y, z = contour.next() x,y,z = rotate_by_angle(x,y,z,xrot) newx = [] newy = [] newz = [] for x_in in range(z.shape[0]): x_run =[] y_run =[] z_run =[] for y_in in range(z.shape[1]): element = z[x_in,y_in] test = (element >= zslice[0]) and (element <= zslice[1]) if test: x_run.append(x[x_in,y_in]) y_run.append(y[x_in,y_in]) z_run.append(element) if len(z_run)>0: if len(z_run) == z.shape[1]: newx.append(x_run) newy.append(y_run) newz.append(z_run) n_x = np.array(newx, object) n_y = np.array(newy, object) n_z = np.array(newz, object) #z_mask = np.bitwise_and((z>=zslice[0]) , (z<=zslice[1])) #ax_spin.plot_wireframe(x, y, z) ax_spin.plot_surface(x,y,z,rstride=1, cstride=1, linewidth=0,shade=True, antialiased=False) samples = 5 #print n_z.shape if n_z.shape[0]>samples and n_z.shape[1]>samples: #ax_spin.plot_wireframe(n_x, n_y, n_z, linewidth=5.0).set_color('red') for index in range(samples): fraction = float(index)/float(samples-1) indexY = np.ceil(n_z.shape[0]*fraction) indexX = np.ceil(n_z.shape[1]*fraction) if indexY == n_z.shape[0]:indexY+=-1 if indexX == n_z.shape[1]:indexX+=-1 front_2D.plot(n_y[indexY,],n_z[indexY,]) top_2D.plot(n_y[indexY],n_x[indexY]-77.89,label= 'z=%4d'%(n_z[indexY,0])) side_2D.plot(n_x.T[indexX,]-77.893,n_z.T[indexX,], label= 'y=%4d'%(n_y.T[indexX,0])) tmp_left_file = '_%s_left_tmp%012d.png' % (basename,counter) front_2D.set_title('\nY-Z Plot') front_2D.set_ylabel('Z(nm)') front_2D.set_xlabel('Y(nm)') top_2D.set_title('\nY-X Plot') top_2D.set_ylabel('X(nm)') top_2D.set_xlabel('Y(nm)') side_2D.set_title('\nX-Z Plot') side_2D.set_ylabel('Z(nm)') side_2D.set_xlabel('X(nm)') thelegend=side_2D.legend(loc='best') if thelegend != None: thelegend.get_frame().set_alpha(0.6) for t in thelegend.get_texts(): t.set_fontsize('x-small') thetoplegend=top_2D.legend(loc='best') if thetoplegend != None: thetoplegend.get_frame().set_alpha(0.6) for t in thetoplegend.get_texts(): t.set_fontsize('xx-small') fig_2D.tight_layout() fig_2D.savefig(tmp_left_file) front_2D.cla() top_2D.cla() side_2D.cla() #fig_2D.clf() ax_back.plot_wireframe(x, y, z) ax_top.plot_wireframe(x, y, z) #ax_left.plot_wireframe(x, y, z) #Xstart = (x.min()) #Zstart = (z.min()) #Xend = (x.max()) #ax.set_zlim3d(-400, 0) ax_spin.set_zlim3d(-1000, 0) ax_back.set_zlim3d(-1000, 0) ax_top.set_zlim3d(-1000, 0) ax_left.set_zlim3d(-1000, 0) ax_back.view_init(elev=0.0,azim=180.0) ax_top.view_init(elev=80.0,azim=180.0) ax_left.view_init(elev=0.0,azim=90.0) #if spin: ax_spin.view_init(elev=30.0,azim=rotation) rotation += 0.000 #ax.set_zlim3d(Zstart, Zstart+(Xend-Xstart)) tmp_spin_file = '_%s_spin_tmp%012d.png' % (basename,counter) tmp_back_file = '_%s_back_tmp%012d.png' % (basename,counter) tmp_top_file = '_%s_top_tmp%012d.png' % (basename,counter) tmp_left_file = '_%s_left_tmp%012d.png' % (basename,counter) files.append(tmp_spin_file) files.append(tmp_back_file) files.append(tmp_top_file) files.append(tmp_left_file) spin_files.append(tmp_spin_file) back_files.append(tmp_back_file) top_files.append(tmp_top_file) left_files.append(tmp_left_file) ax_back.grid(False) ax_top.grid(False) ax_spin.grid(False) fig_spin.savefig(tmp_spin_file) fig_back.savefig(tmp_back_file) fig_top.savefig(tmp_top_file) #fig_left.savefig(tmp_left_file) ax_spin.cla() ax_back.cla() ax_top.cla() ax_left.cla() #fig_spin.clf() #fig_back.clf() #fig_top.clf() #plt.close(fig_spin) #plt.close(fig_back) #plt.close(fig_top) #plt.close(fig_2D) #del front_2D #del side_2D #del top_2D #del ax_spin #del ax_back #del ax_top tmp_comp = composite_frame(tmp_spin_file, tmp_top_file, tmp_left_file, tmp_back_file) comp_files.append(tmp_comp) files.append(tmp_comp) print_log("Frame", counter) counter += 1 except StopIteration: break #basename, ext = os.path.splitext(par.SURFACE_FILE) #if spin: basename = basename + '_spin' avifile = basename + '.avi' spinfile = basename + '_spin' + '.avi' topfile = basename + '_top' + '.avi' leftfile = basename + '_left' + '.avi' backfile = basename + '_back' + '.avi' os.system("mencoder 'mf://*_%s_comp_tmp*.png' -mf type=png:fps=24 \ -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=4800 -oac copy -o "%basename + avifile) os.system("mencoder 'mf://*_%s_spin_tmp*.png' -mf type=png:fps=24 \ -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=4800 -oac copy -o "%basename + spinfile) os.system("mencoder 'mf://*_%s_top_tmp*.png' -mf type=png:fps=24 \ -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=4800 -oac copy -o "%basename + topfile) os.system("mencoder 'mf://*_%s_left_tmp*.png' -mf type=png:fps=24 \ -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=4800 -oac copy -o "%basename + leftfile) os.system("mencoder 'mf://*_%s_back_tmp*.png' -mf type=png:fps=24 \ -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=4800 -oac copy -o "%basename + backfile) pngfile = basename + '.png' os.rename(files[-1], pngfile) for file in files[:-1]: os.remove(file)
def generate_movie_slice(spin=False, zslice=(float('-Inf'),float('Inf')) ): """ Generate a movie of the contour evolution using contours from file SURFACE_FILE. """ contour = read_contour_from_file() counter = 1 files = [] top_files =[] spin_files = [] back_files = [] left_files = [] comp_files = [] flat_2D_files = [] fig_spin = plt.figure(figsize=(8,6),dpi=100) fig_back = plt.figure(figsize=(8,6),dpi=100) fig_top = plt.figure(figsize=(8,6),dpi=100) fig_left = plt.figure(figsize=(8,6),dpi=100) fig_2D = plt.figure(figsize=(8,6),dpi=100) fig_spin.suptitle("Rotating") fig_back.suptitle("Back View") fig_top.suptitle("Top View") fig_left.suptitle("Left Side") ax_spin = Axes3D(fig_spin) ax_back = Axes3D(fig_back) ax_top = Axes3D(fig_top) ax_left = Axes3D(fig_left) front_2D = fig_2D.add_subplot(2,2,1) top_2D = fig_2D.add_subplot(2,2,3) side_2D = fig_2D.add_subplot(1,2,2) rotation = 0.0 basename, ext = os.path.splitext(par.SURFACE_FILE) while True: try: x, y, z = contour.next() newx = [] newy = [] newz = [] for x_in in range(z.shape[0]): x_run =[] y_run =[] z_run =[] for y_in in range(z.shape[1]): element = z[x_in,y_in] test = (element >= zslice[0]) and (element <= zslice[1]) if test: x_run.append(x[x_in,y_in]) y_run.append(y[x_in,y_in]) z_run.append(element) if len(z_run)>0: if len(z_run) == z.shape[1]: newx.append(x_run) newy.append(y_run) newz.append(z_run) n_x = np.array(newx, object) n_y = np.array(newy, object) n_z = np.array(newz, object) #z_mask = np.bitwise_and((z>=zslice[0]) , (z<=zslice[1])) ax_spin.plot_wireframe(x, y, z) print n_z.shape if len(newz)>0: ax_spin.plot_wireframe(n_x, n_y, n_z, linewidth=5.0).set_color('red') samples = 5 for index in range(samples): fraction = float(index)/float(samples) indexY = np.ceil(n_z.shape[0]*fraction) indexX = np.ceil(n_z.shape[1]*fraction) front_2D.plot(n_y[indexY,],n_z[indexY,]) top_2D.plot(n_y[indexY],n_x[indexY]) side_2D.plot(n_x.T[indexX,],n_z.T[indexX,]) tmp_left_file = '_%s_left_tmp%012d.png' % (basename,counter) fig_2D.savefig(tmp_left_file) front_2D.cla() top_2D.cla() side_2D.cla() ax_back.plot_wireframe(x, y, z) ax_top.plot_wireframe(x, y, z) ax_left.plot_wireframe(x, y, z) #Xstart = (x.min()) #Zstart = (z.min()) #Xend = (x.max()) #ax.set_zlim3d(-400, 0) ax_spin.set_zlim3d(-1000, 0) ax_back.set_zlim3d(-1000, 0) ax_top.set_zlim3d(-1000, 0) ax_left.set_zlim3d(-1000, 0) ax_back.view_init(elev=0.0,azim=180.0) ax_top.view_init(elev=80.0,azim=0.0) ax_left.view_init(elev=0.0,azim=90.0) #if spin: ax_spin.view_init(elev=30.0,azim=rotation) rotation += 0.2 #ax.set_zlim3d(Zstart, Zstart+(Xend-Xstart)) tmp_spin_file = '_%s_spin_tmp%012d.png' % (basename,counter) tmp_back_file = '_%s_back_tmp%012d.png' % (basename,counter) tmp_top_file = '_%s_top_tmp%012d.png' % (basename,counter) tmp_left_file = '_%s_left_tmp%012d.png' % (basename,counter) files.append(tmp_spin_file) files.append(tmp_back_file) files.append(tmp_top_file) files.append(tmp_left_file) spin_files.append(tmp_spin_file) back_files.append(tmp_back_file) top_files.append(tmp_top_file) left_files.append(tmp_left_file) fig_spin.savefig(tmp_spin_file) fig_back.savefig(tmp_back_file) fig_top.savefig(tmp_top_file) #fig_left.savefig(tmp_left_file) ax_spin.cla() ax_back.cla() ax_top.cla() ax_left.cla() tmp_comp = composite_frame(tmp_spin_file, tmp_top_file, tmp_left_file, tmp_back_file) comp_files.append(tmp_comp) files.append(tmp_comp) print_log("Frame", counter) counter += 1 except StopIteration: break #basename, ext = os.path.splitext(par.SURFACE_FILE) #if spin: basename = basename + '_spin' avifile = basename + '.avi' spinfile = basename + '_spin' + '.avi' topfile = basename + '_top' + '.avi' leftfile = basename + '_left' + '.avi' backfile = basename + '_back' + '.avi' os.system("mencoder 'mf://*_%s_comp_tmp*.png' -mf type=png:fps=60 \ -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=2400 -oac copy -o "%basename + avifile) os.system("mencoder 'mf://*_%s_spin_tmp*.png' -mf type=png:fps=60 \ -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=2400 -oac copy -o "%basename + spinfile) os.system("mencoder 'mf://*_%s_top_tmp*.png' -mf type=png:fps=60 \ -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=2400 -oac copy -o "%basename + topfile) os.system("mencoder 'mf://*_%s_left_tmp*.png' -mf type=png:fps=60 \ -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=2400 -oac copy -o "%basename + leftfile) os.system("mencoder 'mf://*_%s_back_tmp*.png' -mf type=png:fps=24 \ -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=2400 -oac copy -o "%basename + backfile) pngfile = basename + '.png' os.rename(files[-1], pngfile) for file in files[:-1]: os.remove(file)
def generate_movie_windowed(spin=False): """ Generate a movie of the contour evolution using contours from file SURFACE_FILE. """ contour = read_contour_from_file() counter = 1 files = [] top_files =[] spin_files = [] back_files = [] left_files = [] comp_files = [] fig_spin = plt.figure(figsize=(8,6),dpi=100) fig_back = plt.figure(figsize=(8,6),dpi=100) fig_top = plt.figure(figsize=(8,6),dpi=100) fig_left = plt.figure(figsize=(8,6),dpi=100) fig_spin.suptitle("Rotating") fig_back.suptitle("Back View") fig_top.suptitle("Top View") fig_left.suptitle("Left Side") ax_spin = Axes3D(fig_spin) ax_back = Axes3D(fig_back) ax_top = Axes3D(fig_top) ax_left = Axes3D(fig_left) rotation = 0.0 basename, ext = os.path.splitext(par.SURFACE_FILE) while True: try: x, y, z = contour.next() ax_spin.plot_wireframe(x, y, z) ax_back.plot_wireframe(x, y, z) ax_top.plot_wireframe(x, y, z) ax_left.plot_wireframe(x, y, z) #Xstart = (x.min()) #Zstart = (z.min()) #Xend = (x.max()) #ax.set_zlim3d(-400, 0) ax_spin.set_zlim3d(-1000, 0) ax_back.set_zlim3d(-1000, 0) ax_top.set_zlim3d(-1000, 0) ax_left.set_zlim3d(-1000, 0) ax_back.view_init(elev=0.0,azim=180.0) ax_top.view_init(elev=80.0,azim=0.0) ax_left.view_init(elev=0.0,azim=90.0) #if spin: ax_spin.view_init(elev=30.0,azim=rotation) rotation += 0.2 #ax.set_zlim3d(Zstart, Zstart+(Xend-Xstart)) tmp_spin_file = '_%s_spin_tmp%012d.png' % (basename,counter) tmp_back_file = '_%s_back_tmp%012d.png' % (basename,counter) tmp_top_file = '_%s_top_tmp%012d.png' % (basename,counter) tmp_left_file = '_%s_left_tmp%012d.png' % (basename,counter) files.append(tmp_spin_file) files.append(tmp_back_file) files.append(tmp_top_file) files.append(tmp_left_file) spin_files.append(tmp_spin_file) back_files.append(tmp_back_file) top_files.append(tmp_top_file) left_files.append(tmp_left_file) fig_spin.savefig(tmp_spin_file) fig_back.savefig(tmp_back_file) fig_top.savefig(tmp_top_file) fig_left.savefig(tmp_left_file) ax_spin.cla() ax_back.cla() ax_top.cla() ax_left.cla() tmp_comp = composite_frame(tmp_spin_file, tmp_top_file, tmp_left_file, tmp_back_file) comp_files.append(tmp_comp) files.append(tmp_comp) print_log("Frame", counter) counter += 1 except StopIteration: break #basename, ext = os.path.splitext(par.SURFACE_FILE) #if spin: basename = basename + '_spin' avifile = basename + '.avi' spinfile = basename + '_spin' + '.avi' topfile = basename + '_top' + '.avi' leftfile = basename + '_left' + '.avi' backfile = basename + '_back' + '.avi' os.system("mencoder 'mf://*_%s_comp_tmp*.png' -mf type=png:fps=60 \ -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=2400 -oac copy -o "%basename + avifile) os.system("mencoder 'mf://*_%s_spin_tmp*.png' -mf type=png:fps=60 \ -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=2400 -oac copy -o "%basename + spinfile) os.system("mencoder 'mf://*_%s_top_tmp*.png' -mf type=png:fps=60 \ -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=2400 -oac copy -o "%basename + topfile) os.system("mencoder 'mf://*_%s_left_tmp*.png' -mf type=png:fps=60 \ -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=2400 -oac copy -o "%basename + leftfile) os.system("mencoder 'mf://*_%s_back_tmp*.png' -mf type=png:fps=60 \ -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=2400 -oac copy -o "%basename + backfile) pngfile = basename + '.png' os.rename(files[-1], pngfile) for file in files[:-1]: os.remove(file)
def main(): """ Main function of TopSim. """ CPU_start_time = TTT.clock() # initialize input parameters and closely related parameters init_input_parameters() # further initializations init_physical_properties() time = 0.0 # time at beginning of time step old_time = 0.0 # time at beginning of previous time step time_step = get_timestep(time) n_write = 1 surface = init_surface() if par.ADAPTIVE_GRID: while True: refined = surface.adapt_grid() if not refined: break beam = TiltedScannedBeam() old_surface = deepcopy(surface) surface.write_contour(time, 'bx-') surface.calc_point_directions() surface.calc_point_areas() surface, time_step = surface.advance( 0.0, 0.0) #initialize and subdivide if necessary surface.calc_point_directions() surface.calc_point_areas() surface.calc_thetas(beam) surface.write_contour(time, 'bx-') # loop over time steps while True: # update surface properties surface.calc_point_directions() surface.calc_point_areas() # beam fluxes try: beam.update_angle() surface.beam_fluxes, time_step = beam.get_fluxes( surface.positions, time, time_step) except StopIteration: # exit loop when there is no more beam break # update local incidence angles surface.calc_thetas(beam) # calculate fluxes try: time_step = calc_fluxes(surface, old_surface, beam, time, time_step) # remember surface, so we can later revert to it old_surface = deepcopy(surface) except TooLargeFluxVariationsError: #not implemented # reject surface and revert to old_surface surface = deepcopy(old_surface) time = old_time time_step = time_step / 2 # advance surface while True: try: surface, time_step = surface.advance(time, time_step) break except HasUndefinedFluxError: print 'WARNING: undefined_fluxes not yet implemented.' #calc_undefined_fluxes(surface) # increment time and get new time step old_time = time time += time_step if par.VERBOSE: print_log('time=', time) if time >= 0.9999999 * n_write * par.WRITE_TIME_STEP: n_write += 1 surface.write_contour(time, 'bx-') time_step = get_timestep(time) surface.write_contour(time, 'bx-') if par.DISPLAY_SURFACE: surface.plot_contour() #f.close() print_log('\nCPU time:', TTT.clock() - CPU_start_time)
def adapt_grid(self): """ Adapt the grid as to meet the grid criteria. """ adapted = False # check on deletions segment_lengths_after_deletion = distance_between( self.positions[2:], self.positions[:-2]) obsolete_points = (segment_lengths_after_deletion < par.MAX_SEGLEN[0]) obsolete_points = np.concatenate( ((False, ), obsolete_points, (False, ))) if any(obsolete_points): # points may only be deleted if the two neighbors are not deleted isolated_points = np.logical_not(obsolete_points[:-2]) & \ np.logical_not(obsolete_points[2:]) isolated_points = np.concatenate( ((False, ), isolated_points, (False, ))) # if there are consecutive obsolete points, only every second must be deleted odd_points = (np.arange(self.len_x) % 2 == 1) remove_points = obsolete_points & (isolated_points | odd_points) indices = np.ravel(np.where(remove_points)) print_log('delete at indices', indices) self.remove_points(indices) adapted = True # check on insertions segment_lengths = distance_between(self.positions[1:], self.positions[:-1]) too_large_segments = (segment_lengths > (1.0 + par.GRID_LAZINESS/100.0)*par.MAX_SEGLEN[0]) & \ self.refine_mask[:-1] #too_small_segments = (segment_lengths > (1 - par.GRID_LAZINESS/100)*par.MAX_SEGLEN) & \ # self.refine_mask[:-1] #reject = (segment_lengths > (1 + 2*par.GRID_LAZINESS/100)*par.MAX_SEGLEN) & \ # self.refine_mask[:-1] if any(too_large_segments): refined_delta_x = (self.positions[1:, 0] - self.positions[:-1, 0]) / 2 refine_segments = too_large_segments & \ (np.absolute(refined_delta_x) >= par.MIN_DELTA[0]) #comparing a 2d distance to a 1d distance if any(refine_segments): indices = np.ravel(np.where(refine_segments)) + 1 print_log('refine at indices', indices) self.insert_points(indices) adapted = True #if any(reject): raise TooLargeSegmentError #only remove points if number of points = MAX_POINTS #if len(self.positions) == par.MAX_POINTS: # segment_lengths = distance_between(self.positions[1:,:], self.positions[:-1,:]) # if any(too_small_segments): # error = segment_lengths[too_small_segments] - par.MAX_SEGLEN # second_next_points = np.insert(too_small_segments, (0,0), (False, False), 0) # new_segment_lengths = distance_between(self.positions[too_small_segments], # self.positions[second_next_points]) # new_error = new_segment_lengths - par.MAX_SEGLEN # remove = (abs(new_error) < abs(error)) # indices = np.ravel(np.where(remove == True)) # self.remove_points(indices) # old_surface.remove_points(indices) return adapted