def evolve_floc(self, Deform = True, MaxTimes = 100, TimeRange = None, FullOutput = False): """ Inputs: MaxTimes int; number of time points to compute the stress over. The deformation integral returns more than we want to use for efficiency reasons when doing these computations over thousands of flocs at once. TimeRange [t0, t1, dt] or None. If None (default), will use dfm's set_tau_cap function to automatically determine the time step and final time. FullOutput Bool; if True will set attributes Ra, wV, and T (see below). Sets Attributes:: forces[MaxTimes][NEdges] float, NEdges is the number of edges in the floc. i j entry is the fragmentation force acting against the j plane at time i aV[MaxTimes][3] float, axes lenghts at each time step (optional) Ra[MaxTimes][2] float, the two unique entries defining rotation R (cos theta, sin theta) wV[MaxTimes][3] float, angular velocity at each time step T[MaxTimes] float, times """ if len(self.mst_sph) == 0: # then the floc is size 1 and won't fragment so we don't bother deforming it Ra = np.array([]) wV = np.array([]) T = np.array([]) forces = np.array([]) else: # compute the time interval if we don't have one if TimeRange == None: t0, t1, dt, tau, cap = dfm.set_tau_cap(self.a0, self.lam, self.mu, self.gammadot, self.Gamma) else: t0,t1,dt = TimeRange # deform the floc (or just rotate if not deforming or is too elongated already) if ( (Deform == True) & (self.a0[0] / self.a0[1] < 6.0) ): aV, Ra, wV, T = dfm.deform(t0, t1, dt, self.a0, self.lam, self.mu, self.gammadot, self.Gamma, True) else: aV, Ra, wV, T = dfm.solid_rotations(t0, t1, dt, self.a0, self.gammadot, True) # shorten the length of the output if np.shape(T)[0] > MaxTimes: StepSize = int(np.floor(np.shape(T)[0] / MaxTimes)) aV = np.ascontiguousarray( aV[::StepSize] ) Ra = np.ascontiguousarray( Ra[::StepSize] ) wV = np.ascontiguousarray( wV[::StepSize] ) T = np.ascontiguousarray( T[::StepSize] ) # get the force matrix forces = frc.py_set_force_Vectorized(aV, Ra, wV, self.pnV_sph, \ self.pxV_sph, self.gammadot, self.p0, self.mu) self.aV = aV if FullOutput == True: self.Ra = Ra self.wV = wV self.T = T self.forces = forces
def deformPicture(): global rimg1, img2, img2_canvas p, q = getPoints() print("List of points p:", p) print("List of points q:", q) image = getPicture(rimg1) real_p = np.array(p).astype(np.int) real_q = np.array(q).astype(np.int) deformed = deform(image, p, q) img2 = ImageTk.PhotoImage(arrayToPicture(deformed)) w.itemconfigure(img2_canvas, image=img2)
def deform_floc( num_particles ): #============================================================================== # location matrix loc_mat -- coordinate1--coordinate2--coordinate3-- living or # dead -- age after division #============================================================================== floc = dla.dla_generator( num_particles = num_particles ) init_loc_mat = np.zeros( ( len(floc) , 7 ) ) init_loc_mat[ : , 0:3 ] = floc init_loc_mat[ : , 3 ] = 1 deform_radg = np.zeros( num_loop ) move_radg = np.zeros( num_loop ) loc_mat = init_loc_mat.copy() just_move = floc.copy() axes = np.zeros( ( num_loop + 1 , 3 ) ) G_vector = np.zeros( ( num_loop + 1 , 6 ) ) loc_mat_list = [] just_move_list = [] #dbs = DBSCAN(eps=2 , min_samples = 1 ) frag_list = [] move_frag_list = [] for tt in range( num_loop ): #Append loc_mat at each half generation if np.mod(tt, int( num_loop / 10 ) -1 )==0 or tt == num_loop - 1: loc_mat_list.append([ loc_mat.copy() , tt]) just_move_list.append( [ just_move.copy() , tt ] ) #============================================================================== # Since new cells were added we need to change the ellipsoid axis # in the body frame #============================================================================== # set initial radii and return points in body frame points, radii , shape_tens = dfm.set_initial_pars( loc_mat[ : , 0:3] ) axes[tt] = radii #Convert shape_tensor to 6x1 vector G_vector[tt] = dfm.tens2vec( shape_tens ) #============================================================================== # deform the cell cluster #============================================================================== axes[tt+1] , G_vector[tt+1] , Rot = dfm.deform(0 , sim_step , dt , G_vector[tt] , lam , mu , L , Gamma ) dfm_frac = axes[ tt+1 ] / axes[ tt ] if np.max( dfm_frac ) < 2 and np.min(dfm_frac) > 0.5: rotation = Rot * dfm_frac loc_mat[: , 0:3] = np.inner( points , rotation ) #============================================================================== # move the cells #============================================================================== loc_mat = md.hertzian_move( loc_mat , sim_step=sim_step ) #radius of gyration c_mass = np.mean( loc_mat[: , 0:3] , axis=0 ) deform_radg[tt] = ( 1 / len(loc_mat) * np.sum( (loc_mat[: , 0:3] - c_mass )**2 ) ) **(1/2) #============================================================================== # Measure the volume of just_move at that time #============================================================================== just_move = md.hertzian_move( just_move , sim_step = sim_step ) #radius of gyration c_mass = np.mean( just_move[: , 0:3] , axis=0 ) move_radg[tt] = ( 1 / len(just_move) * np.sum( ( just_move[: , 0:3] - c_mass )**2 ) ) **(1/2) data_dict = { 'init_loc_mat' : init_loc_mat , 'loc_mat' : loc_mat , 'loc_mat_list' : loc_mat_list , 'just_move_list' : just_move_list , 'frag_list' : frag_list , 'move_frag_list' : move_frag_list , 'deform_radg' : deform_radg , 'move_radg' : move_radg , 'num_loop' : num_loop , 'axes' : axes, 'G_vector' : G_vector, 'tau_p' : tau_p , 'sim_step' : sim_step , 'lam' : lam , 'mu' : mu , 'floc' : floc, 'gammadot' : gammadot, 'Gamma' : Gamma } return data_dict
def grow_floc( lam , flow_type = flow_type ): ksi = 6*cell_rad*np.pi *mu_si*lam sim_step = 1 / gammadot dt = sim_step / 10 L = np.zeros( [ 3 , 3 ] ) if flow_type == 0: # Simple shear in one direction L[0,1] = gammadot elif flow_type == 1: # Shear plus elongation flow L[0,1] = gammadot L[0,0] = gammadot L[1, 1] = -gammadot elif flow_type == 2: #Elongational flow L[0,0] = gammadot L[1, 1] = -gammadot #L[2, 2] = -gammadot #L *= 0.1 else: raise Exception("Please specify a valid flow type") ########### #Number of generations for to be simulated num_gen = 8 #Loop adjustment due to number of generation and generation time of a single cell num_loop = int( tau_p * num_gen / sim_step ) #============================================================================== # location matrix loc_mat -- coordinate1--coordinate2--coordinate3-- living or # dead -- age after division #============================================================================== shape = 60 scale = 1 / shape cycle_time = tau_p * np.random.gamma( shape , scale , 10**5 ) floc = dla.dla_generator( num_particles = 5 ) init_loc_mat = np.zeros( ( len(floc) , 7 ) ) init_loc_mat[ : , 0:3 ] = floc init_loc_mat[ : , 3 ] = 1 deform_radg = np.zeros( num_loop ) deform_cells = np.zeros( num_loop ) loc_mat = init_loc_mat.copy() axes = np.zeros( ( num_loop + 1 , 3 ) ) G_vector = np.zeros( ( num_loop + 1 , 6 ) ) loc_mat_list = [] frag_list = [] for tt in range( num_loop ): deform_cells[tt] = len(loc_mat) #Append loc_mat at each half generation if np.mod(tt, int( num_loop / 10 ) -1 )==0 or tt == num_loop - 1: loc_mat_list.append([ loc_mat.copy() , tt]) #============================================================================== # Since new cells were added we need to change the ellipsoid axis # in the body frame #============================================================================== # set initial radii and return points in body frame points, radii , shape_tens = dfm.set_initial_pars( loc_mat[ : , 0:3] ) axes[tt] = radii #Convert shape_tensor to 6x1 vector G_vector[tt] = dfm.tens2vec( shape_tens ) #============================================================================== # deform the cell cluster #============================================================================== axes[tt+1] , G_vector[tt+1] , Rot = dfm.deform(0 , sim_step , dt , G_vector[tt] , lam , mu , L , Gamma ) dfm_frac = axes[ tt+1 ] / axes[ tt ] if np.max( dfm_frac ) < 2 and np.min(dfm_frac) > 0.5: rotation = Rot * dfm_frac loc_mat[: , 0:3] = np.inner( points , rotation ) #============================================================================== # move the cells #============================================================================== loc_mat = md.hertzian_move( loc_mat , sim_step=sim_step , ksi=ksi ) #radius of gyration c_mass = np.mean( loc_mat[: , 0:3] , axis=0 ) deform_radg[tt] = ( 1 / len(loc_mat) * np.sum( (loc_mat[: , 0:3] - c_mass )**2 ) ) **(1/2) #============================================================================== # divide the cells in loc_mat #============================================================================== loc_mat[: , 4] = loc_mat[: , 4] + sim_step # Cells that have reached cycle time mitotic_cells1 = np.nonzero( loc_mat[ : , 4 ] > cycle_time[ range( len(loc_mat) ) ] )[0] # Cells that are not quescent mitotic_cells2 = np.nonzero( loc_mat[ : , 3] > 0 )[0] mitotic_cells = np.intersect1d( mitotic_cells1 , mitotic_cells2 ) if len(mitotic_cells) > 0: loc_mat = md.cell_divide( loc_mat , mitotic_cells , tt) data_dict = { 'init_loc_mat' : init_loc_mat , 'loc_mat' : loc_mat , 'loc_mat_list' : loc_mat_list , 'frag_list' : frag_list , 'deform_radg' : deform_radg , 'deform_cells' : deform_cells , 'num_loop' : num_loop , 'axes' : axes, 'G_vector' : G_vector, 'tau_p' : tau_p , 'sim_step' : sim_step , 'lam' : lam , 'mu' : mu , 'floc' : floc , 'gammadot' : gammadot , 'Gamma' : Gamma } return data_dict
x = R.T * x' Discussion: =========== R from deform is the map from the body-frame to the laboratory frame. The way we check this is to apply R to the vector (0,1,0). We can think of this like a point on the ellipsoid, scaled to unit length. We know that the shear field we apply will cause the ellipsoid to rotate clockwise in the XY plane. Applying R to (0,1,0) causes this vector to rotate clockwise in the XY plane, which means it sends the point (0,1,0) in the body-fixed frame to its coordinates in the lab frame. """ # import the constants lam, mu, gammadot, Gamma, max_stress, p0 = import_constants() # set the initial axes a0 = np.array([180., 160., 140.]) # compute the time interval t0,t1,dt,tau,cap = dfm.set_tau_cap(a0, lam, mu, gammadot, Gamma) # get the rotations, axes and angular velocity axes, R, w, T = dfm.deform(t0,t1,dt,a0,lam,mu,gammadot,Gamma, JustAngles = True) Rm = np.zeros([len(T),3,3]) for i in range(len(T)): Rm[i] = dfm.angles_to_matrix(R[i]) # how does R rotate things? let's see. x0 = np.array([0,1.,0]) xR = np.dot(Rm,x0) # xR is a unit vector rotating clockwise in the XY plane, as desired.
############################################################################### ############################################################################### ##################### TEST FARG FUNCTIONS #################### ############################################################################### ############################################################################### ## Use the deformation code to generate some data # import the constants lam, mu, gammadot, Gamma, max_stress, p0 = import_constants() # set the initial axes a0 = np.array([180., 160., 140.]) # compute the time interval t0,t1,dt,tau,cap = dfm.set_tau_cap(a0, lam, mu, gammadot, Gamma) # get the rotations, axes and angular velocity aV, RV, wV, T = dfm.deform(t0,t1,dt,a0,lam,mu,gammadot,Gamma) # set some quantities for testing ind = 0 a = aV[ind] R = RV[ind] w = wV[ind] L = dfm.set_L(gammadot,R) ## Test some functions so make sure they run (not if they are right) print("\n TESTING FORCE FUNCTIONS \n") # Test py_set_farg print('\n set_farg \n') farg = frc.py_set_farg(a, w, L, p0, mu) print('runs')
#Elongational flow L[0,0] = gammadot L[1, 1] = -gammadot else: raise Exception("Please specify a valid flow type") a1 = a0 for nn in range(10): # set up the initial shape tensor G0 = np.diag( 1.0 / a1**2 ) G0v = dfm.tens2vec( G0 ) a1 = dfm.deform( t0 , sim_step , dt , G0v , lam , mu , L , Gamma )[0] print a1 print a1/a0 aaa = np.prod( a1 ) / np.prod( a0 ) vol_err = round( 100 * np.abs( 1- aaa ) , 6 ) print 'Error in volume', vol_err, 'percent' end = time.time() print 'Time elapsed' , round( end - start, 2), 'seconds' G0 = np.diag( 1.0 / a0**2 ) G0v = dfm.tens2vec( G0 )
import sys sys.path.append("/home/eric/fragnew/simulation/") import deformation as dfm import pywrappers as frc sys.path.append("/home/eric/fragnew/input/") from constants import import_constants import numpy as np # import the constants lam, mu, gammadot, Gamma, max_stress, p0 = import_constants() # set the initial axes a0 = np.array([180., 160., 140.]) # compute the time interval t0,t1,dt,tau,cap = dfm.set_tau_cap(a0, lam, mu, gammadot, Gamma) # get the rotations, axes and angular velocity axes, R, w, T = dfm.deform(t0,t1,dt,a0,lam,mu,gammadot,Gamma) # test force_facets, which sets the force on the centers of the facets # for a fixed time point time_ind = 0 force_on_facets, srf_centers_scaled = frc.force_facets(axes[time_ind], w[time_ind], R[time_ind], gammadot, p0, mu) # test py_scale_triangulations, which scales the surface quantities stored # for a sphere to the dimensions of the ellipsoid srf_centers_scaled, srf_areas_scaled, srf_normals_scaled = frc.py_scale_triangulations(a0) # sum up the facet areas and compare to the ellipsoid area
# set initial radii and return points in body frame points, radii , shape_tens = dfm.set_initial_pars( loc_mat[ : , 0:3] ) axes[tt] = radii #Convert shape_tensor to 6x1 vector G_vector[tt] = dfm.tens2vec( shape_tens ) #============================================================================== # deform the cell cluster #============================================================================== axes[tt+1] , G_vector[tt+1] , Rot = dfm.deform(0 , sim_step , dt , G_vector[tt] , lam , mu , L , Gamma ) dfm_frac = axes[ tt+1 ] / axes[ tt ] if np.max( dfm_frac ) < 2 and np.min(dfm_frac) > 0.5: rotation = Rot * dfm_frac loc_mat[: , 0:3] = np.inner( points , rotation ) #============================================================================== # move the cells #============================================================================== loc_mat = md.hertzian_move( loc_mat ) #radius of gyration
import sys sys.path.append("/home/eric/fragnew/simulation/") import deformation as dfm sys.path.append("/home/eric/fragnew/input/") from constants import import_constants import numpy as np """ Test the core functions of deformation.py """ # import the constants lam, mu, gammadot, Gamma, max_stress, p0 = import_constants() # set the initial axes a0 = np.array([180., 160., 140.]) # compute the time interval t0,t1,dt,tau,cap = dfm.set_tau_cap(a0, lam, mu, gammadot, Gamma) # run the deformation integral Y,T = dfm.integrate_dgdt(t0,t1,dt,a0,lam,mu,gammadot,Gamma) # get the rotations and the axes axes, R = dfm.shapetensors_to_axes_rots(Y) # test angular velocity computations w = dfm.angular_velocity(R, dt) # test the wrapper function deform axes2, R2, w2, T2 = dfm.deform(t0,t1,dt,a0,lam,mu,gammadot,Gamma)
mu_si = 8.953e-4 # matrix viscosity, Pa s=(N s)/(m ^2) gammadot_si = 10. # shear rate, 1/s Gamma_si = 4.1e-9 # interfacial tension, N/m max_stress_si = 100 # stress, Pa = N / m^2 p0_si = 0 # pressure, Pa = N / m^2 lam, mu, gammadot, Gamma, max_stress, p0 = unit_conversion(lam, mu_si, gammadot_si, Gamma_si, max_stress_si, p0_si) # set the axes a0 = np.array([180., 160., 140.]) # set the time parameters t0, t1, dt, tau, cap = dfm.set_tau_cap(a0, lam, mu, gammadot, Gamma) # evolve the ellipsoid aV, Ra, wV, T = dfm.deform(t0, t1, dt, a0, lam, mu, gammadot, Gamma, True) # get the forces at three time points corresponding to rotations of # 0, pi/4 and pi/2 rads (the indicies for these are chosen by hand from # looking at the plots) ind0 = 0 ind1 = 508 ind2 = 612 ind3 = 720 #axes0, w0, rotAngles0 = aV[ind0], wV[ind0], Ra[ind0] #axes1, w1, rotAngles1 = aV[ind1], wV[ind1], Ra[ind1] #axes2, w2, rotAngles2 = aV[ind2], wV[ind2], Ra[ind2] #axes3, w3, rotAngles3 = aV[ind3], wV[ind3], Ra[ind3]
def grow_floc( num_particles ): #============================================================================== # location matrix loc_mat -- coordinate1--coordinate2--coordinate3-- living or # dead -- age after division #============================================================================== shape = 60 scale = 1 / shape cycle_time = tau_p * np.random.gamma( shape , scale , 10**5 ) floc = dla.dla_generator( num_particles = num_particles ) init_loc_mat = np.zeros( ( len(floc) , 7 ) ) init_loc_mat[ : , 0:3 ] = floc init_loc_mat[ : , 3 ] = 1 deform_radg = np.zeros( num_loop ) move_radg = np.zeros( num_loop ) deform_cells = np.zeros( num_loop ) move_cells = np.zeros( num_loop ) loc_mat = init_loc_mat.copy() just_move = init_loc_mat.copy() axes = np.zeros( ( num_loop + 1 , 3 ) ) G_vector = np.zeros( ( num_loop + 1 , 6 ) ) loc_mat_list = [] just_move_list = [] for tt in range( num_loop ): deform_cells[tt] = len(loc_mat) move_cells[tt] = len(just_move) #Append loc_mat at each half generation if np.mod(tt, int( num_loop / 10 ) -1 )==0 or tt == num_loop - 1: loc_mat_list.append([ loc_mat.copy() , tt]) just_move_list.append( [ just_move.copy() , tt ] ) #============================================================================== # Since new cells were added we need to change the ellipsoid axis # in the body frame #============================================================================== # set initial radii and return points in body frame points, radii , shape_tens = dfm.set_initial_pars( loc_mat[ : , 0:3] ) axes[tt] = radii #Convert shape_tensor to 6x1 vector G_vector[tt] = dfm.tens2vec( shape_tens ) #============================================================================== # deform the cell cluster #============================================================================== axes[tt+1] , G_vector[tt+1] , Rot = dfm.deform(0 , sim_step , dt , G_vector[tt] , lam , mu , L , Gamma ) dfm_frac = axes[ tt+1 ] / axes[ tt ] if np.max( dfm_frac ) < 2 and np.min(dfm_frac) > 0.5: rotation = Rot * dfm_frac loc_mat[: , 0:3] = np.inner( points , rotation ) #============================================================================== # move the cells #============================================================================== loc_mat = md.hertzian_move( loc_mat , sim_step=sim_step ) #radius of gyration c_mass = np.mean( loc_mat[: , 0:3] , axis=0 ) deform_radg[tt] = ( 1 / len(loc_mat) * np.sum( (loc_mat[: , 0:3] - c_mass )**2 ) ) **(1/2) #============================================================================== # Measure the volume of just_move at that time #============================================================================== just_move = md.hertzian_move( just_move , sim_step=sim_step ) #radius of gyration c_mass = np.mean( just_move[: , 0:3] , axis=0 ) move_radg[tt] = ( 1 / len(just_move) * np.sum( ( just_move[: , 0:3] - c_mass )**2 ) ) **(1/2) #============================================================================== # divide the cells in just_move #============================================================================== loc_mat[: , 4] = loc_mat[: , 4] + sim_step # Cells that have reached cycle time mitotic_cells1 = np.nonzero( loc_mat[ : , 4 ] > cycle_time[ range( len(loc_mat) ) ] )[0] # Cells that are not quescent mitotic_cells2 = np.nonzero( loc_mat[ : , 3] > 0 )[0] mitotic_cells = np.intersect1d( mitotic_cells1 , mitotic_cells2 ) if len(mitotic_cells) > 0: loc_mat = md.cell_divide( loc_mat , mitotic_cells , tt) #============================================================================== # divide the cells in just_move #============================================================================== just_move[: , 4] = just_move[: , 4] + sim_step # Cells that have reached cycle time mitotic_cells1 = np.nonzero( just_move[ : , 4 ] > cycle_time[ range( len( just_move ) ) ] )[0] # Cells that are not quescent mitotic_cells2 = np.nonzero( just_move[ : , 3] > 0 )[0] mitotic_cells = np.intersect1d( mitotic_cells1 , mitotic_cells2 ) if len(mitotic_cells) > 0: just_move = md.cell_divide( just_move , mitotic_cells , tt) data_dict = { 'init_loc_mat' : init_loc_mat , 'loc_mat' : loc_mat , 'loc_mat_list' : loc_mat_list , 'just_move_list' : just_move_list , 'deform_radg' : deform_radg , 'move_radg' : move_radg , 'deform_cells' : deform_cells , 'move_cells' : move_cells , 'num_loop' : num_loop , 'axes' : axes, 'G_vector' : G_vector, 'tau_p' : tau_p , 'sim_step' : sim_step , 'lam' : lam , 'mu' : mu , 'floc' : floc, 'gammadot' : gammadot, 'Gamma' : Gamma } return data_dict