############################################################################################
###  README for modeling code used in:
### "Rapid propagation of membrane tension at retinal bipolar neuron presynaptic terminals" 
###  Last updated 10-27-21 
############################################################################################


###############################
######## Functions ##########
###############################

### EK_tensionDiffusion.m: Integrates Diffusion equation starting
    inputs:
        Dsigma: Tension Diffusion constant, kDarcy*Em/eta , in microns^2/second
	sigmaic: Initial condition. The tension across the entire tension field
	sigmalbc = left boundary condition, the tension at the cell tether boundary (sigamt, pN/ micron)
	sigmarbc = right boundary condition, the initial or far-field tension (sigam0, pN/micron)
	rt  = radius of the tether (in microns)
	tmax = how long to evolve the tension equation (seconds = tstep)
     Outputs:
	sol : solutions to the diffusion equation across the time interval 0->tmax, 
	x: x-coordinates of solutions  (unused)
	t: time coordinates  of solutions (unused)

### FitExperimentalTension.m:
    inputs:
	data_tense = array of tension values from experiment(in mN / micron)
	data_time = array of time values corresponding to each point in data_tense(s)
	sigma0 = initial tension of the experimental data (mN / micron)
	Distance = distance from membrane (microns)
    Outputs:
        time_lag: Optimal shift to match experimental tension trace with simulated
	sigma_max : Optimal amplitude change to match the experimental tension trace with simulated
	MSE = mean-squared error of the fits
	Plots showing predicted, fit, tether, and experimental tensions
    Calls: TensionDiffusionSimulation.m	,EK_TensionDiffusion.m

### TensionDiffusionSimulation.m: Simulates membrane tension propagation as a tether is pulled from  
   Inputs :
   	  ft0 = initial tether force(pN)
    	  L0 = initial tether length, of tether being pulled (micrometer)
    	  Em = membrane elastic modulus (in pN / micron)
    	  kDarcy = Membrane Darcy permeability (n=units?) note d_sigma ~ kDarcy*Em/eta 
    	  deltaLmax (micrometer)
    	  Ldot = speed of pulling (micrometer/s)
    	  Trelax = time between pulling and retracting (s) 
    	  tstep = time-step for updating tether length (s)
    Outputs :
    	  soln: TxD matrix where T is the total time of the extension protocol and D is the spatial extent of the cell e.g:
	  	soln(1,:) corresponds to tension in the tether as the trace evolves
		soln(:,end) corresponds to the tension profile at the end of the extension protocol
	  L_arr: Array storing the length of the tether at ech timepoint
    Calls : EK_tension_diffusion.m
    Note:
	Can change the experimental protocol by changing the array "deltaL_arr" and updating Nmax accordingly 
	Very fast diffusion may require larger "cell": change Ncells in the script
##################################
####### Scripts #################
#################################

### ProcessDoubleTetherData.m: Script for processing experimental DT data and fitting to simulated traces, generating plots
    Requires:
	Double tether pulling data in .xlsx format.
	       should have columns with tension change, normalized tension change, and time
	Intial tension of each experiment (sigam0, pN / micron)
	Inter-tether distance of each experiment
    Outputs:
	Experimental traces smoothed, averaged, and aligned in time to one another
	t_sense (best-fit shift, see above) of each experimental trace
	sigma_max : max tension change for each experiment	
    Calls: FitExperimentalTension.m, TensionDiffusionSimulation.m, EK_TensionDiffusion.m
    To - run:
       run through each code-block subsequently. 
       Early steps will prompt for excel sheets pertaining Double-tether data
       Fitting steps take considerable time.
       Set "plotting =1" in fitting script to monitor quality
    
