def viscoElasticUpdater_bgvel(t, y, wdict):
    #interior function for incompressible background flow only
    #split up long vector into individual sections (force pts, Lagrangian pts, stress components)
    pdict = wdict['pdict']
    N = pdict['N']
    M = pdict['M']
    l2 = np.reshape(y[range(2 * N * M)], (N * M, 2))
    l3 = np.reshape(l2, (N, M, 2))
    P2 = np.reshape(y[(2 * N * M):], (N * M, 2, 2))
    P3 = np.reshape(P2, (N, M, 2, 2))
    #calculate tensor derivative
    Pd = pdict['beta'] * SD2D.tensorDiv(P3, pdict['gridspc'], N, M)
    Pd = np.reshape(Pd, (N * M, 2))
    #calculate deformation matrix and its inverse
    F = SD2D.vectorGrad(l3, pdict['gridspc'], N, M)
    F = np.reshape(F, (N * M, 2, 2))
    #calculate new velocities at all points of interest
    ub, gradub = wdict['myVelocity'](pdict, l2)
    lt0 = pdict['gridspc']**2 * CM.matmult(pdict['eps_grid'], pdict['mu'], l2,
                                           l2, Pd)
    # reshape the velocities on the grid
    lt3 = np.reshape(lt0, (N, M, 2))
    lt = ub + lt0
    #    calculate new stress time derivatives
    gradlt = SD2D.vectorGrad(lt3, pdict['gridspc'], N, M)
    gradlt = np.reshape(gradlt, (N * M, 2, 2))
    Pt = CM.stressDeriv(pdict['Wi'], gradub, gradlt, F, P2)
    #    gradlt = pdict['gridspc']**2*CM.derivop(pdict['eps_grid'],pdict['mu'],l2,l2,Pd,F) #grad(Stokeslet) method
    #    Finv = CM.matinv2x2(F) #first grad(u) method
    #    Pt = np.zeros((N*M,2,2)) #first grad(u) method
    #    for j in range(N*M):
    #        Pt[j,:,:] = np.dot(gradub[j,:,:],P2[j,:,:]) + np.dot(np.dot(gradlt[j,:,:],Finv[j,:,:]),P2[j,:,:]) - (1./pdict['Wi'])*(P2[j,:,:] - Finv[j,:,:].transpose())
    return np.append(lt, Pt.flatten())
def viscoElasticUpdater_bgvel(t,y,wdict):
    #interior function for incompressible background flow only
    #split up long vector into individual sections (force pts, Lagrangian pts, stress components)
    pdict = wdict['pdict']
    N = pdict['N']
    M = pdict['M']
    l2 = np.reshape(y[range(2*N*M)],(N*M,2))
    l3 = np.reshape(l2,(N,M,2))
    P2 = np.reshape(y[(2*N*M):],(N*M,2,2))
    P3 = np.reshape(P2,(N,M,2,2))
    #calculate tensor derivative
    Pd = pdict['beta']*SD2D.tensorDiv(P3,pdict['gridspc'],N,M)
    Pd = np.reshape(Pd,(N*M,2))
    #calculate deformation matrix and its inverse
    F = SD2D.vectorGrad(l3,pdict['gridspc'],N,M)
    F = np.reshape(F,(N*M,2,2))
    #calculate new velocities at all points of interest
    ub, gradub = wdict['myVelocity'](pdict,l2)
    lt0 = pdict['gridspc']**2*CM.matmult(pdict['eps_grid'],pdict['mu'],l2,l2,Pd) 
    # reshape the velocities on the grid
    lt3 = np.reshape(lt0,(N,M,2)) 
    lt = ub + lt0
#    calculate new stress time derivatives
    gradlt = SD2D.vectorGrad(lt3,pdict['gridspc'],N,M)   
    gradlt = np.reshape(gradlt,(N*M,2,2))
    Pt = CM.stressDeriv(pdict['Wi'],gradub,gradlt,F,P2)
#    gradlt = pdict['gridspc']**2*CM.derivop(pdict['eps_grid'],pdict['mu'],l2,l2,Pd,F) #grad(Stokeslet) method
#    Finv = CM.matinv2x2(F) #first grad(u) method
#    Pt = np.zeros((N*M,2,2)) #first grad(u) method
#    for j in range(N*M):
#        Pt[j,:,:] = np.dot(gradub[j,:,:],P2[j,:,:]) + np.dot(np.dot(gradlt[j,:,:],Finv[j,:,:]),P2[j,:,:]) - (1./pdict['Wi'])*(P2[j,:,:] - Finv[j,:,:].transpose())        
    return np.append(lt,Pt.flatten())
def viscoElasticUpdater_force(t, y, wdict):
    #interior function for force pts only
    #split up long vector into individual sections (force pts, Lagrangian pts, stress components)
    pdict = wdict['pdict']
    pdict['forcedict']['t'] = t
    N = pdict['N']
    M = pdict['M']
    Q = len(y) / 2 - N * M - 2 * N * M
    fpts = np.reshape(y[:2 * Q], (Q, 2))
    l2 = np.reshape(y[range(2 * Q, 2 * Q + 2 * N * M)], (N * M, 2))
    l3 = np.reshape(l2, (N, M, 2))
    allpts = np.reshape(
        y[:2 * Q + 2 * N * M],
        (Q + N * M, 2))  # both force points and Lagrangian points
    P2 = np.reshape(y[(2 * Q + 2 * N * M):], (N * M, 2, 2))
    P3 = np.reshape(P2, (N, M, 2, 2))
    #calculate tensor derivative
    Pd = pdict['beta'] * SD2D.tensorDiv(P3, pdict['gridspc'], N, M)
    Pd = np.reshape(Pd, (N * M, 2))
    #calculate spring forces
    f = wdict['myForces'](fpts, **pdict['forcedict'])
    #calculate new velocities at all points of interest (Lagrangian points and force points)
    lt = pdict['gridspc']**2 * CM.matmult(
        pdict['eps_grid'], pdict['mu'], allpts, l2, Pd) + CM.matmult(
            pdict['eps_obj'], pdict['mu'], allpts, fpts, f)
    # reshape the velocities on the grid
    lt3 = np.reshape(lt[2 * Q:], (N, M, 2))
    #calculate deformation matrix and its inverse
    F = SD2D.vectorGrad(l3, pdict['gridspc'], N, M)
    F = np.reshape(F, (N * M, 2, 2))
    #calculate new stress time derivatives
    #    gradlt = pdict['gridspc']**2*CM.derivop(pdict['eps_grid'],pdict['mu'],l2,l2,Pd,F) + CM.derivop(pdict['eps_obj'],pdict['mu'],l2,fpts,f,F)   #grad(Stokeslet) method
    gradlt = SD2D.vectorGrad(lt3, pdict['gridspc'], N, M)
    gradlt = np.reshape(gradlt, (N * M, 2, 2))
    gradub = np.zeros(gradlt.shape)
    #    Finv = CM.matinv2x2(F)   # first grad(u) method
    #    Pt = np.zeros((N*M,2,2))
    #    for j in range(N*M):
    #        Pt[j,:,:] = np.dot(np.dot(gradlt[j,:,:],Finv[j,:,:]),P2[j,:,:]) - (1./pdict['Wi'])*(P2[j,:,:] - Finv[j,:,:].transpose())
    Pt = CM.stressDeriv(pdict['Wi'], gradub, gradlt, F, P2)
    return np.append(lt, Pt.flatten())
def stokesFlowUpdaterWithMarkers(t, y, wdict):
    pdict = wdict['pdict']
    pdict['forcedict']['t'] = t
    N = pdict['N']
    M = pdict['M']
    Q = len(y) / 2 - N * M
    fpts = np.reshape(y[:2 * Q], (Q, 2))
    ap = np.reshape(y, (Q + N * M, 2))
    #calculate spring forces
    f = pdict['myForces'](fpts, **pdict['forcedict'])
    #calculate new velocities at all points of interest (Lagrangian points and force points)
    lt = CM.matmult(pdict['eps_obj'], pdict['mu'], ap, fpts, f)
    return lt
def stokesFlowUpdaterWithMarkers(t,y,wdict):
    pdict = wdict['pdict']
    pdict['forcedict']['t'] = t
    N = pdict['N']
    M = pdict['M']
    Q = len(y)/2 - N*M
    fpts = np.reshape(y[:2*Q],(Q,2))
    ap = np.reshape(y,(Q+N*M,2)) 
    #calculate spring forces
    f = pdict['myForces'](fpts,**pdict['forcedict'])
    #calculate new velocities at all points of interest (Lagrangian points and force points)
    lt = CM.matmult(pdict['eps_obj'],pdict['mu'],ap,fpts,f)
    return lt
def viscoElasticUpdater_force(t,y,wdict):
    #interior function for force pts only
    #split up long vector into individual sections (force pts, Lagrangian pts, stress components)
    pdict = wdict['pdict']
    pdict['forcedict']['t'] = t
    N = pdict['N']
    M = pdict['M']
    Q = len(y)/2 - N*M - 2*N*M
    fpts = np.reshape(y[:2*Q],(Q,2))
    l2 = np.reshape(y[range(2*Q,2*Q+2*N*M)],(N*M,2))
    l3 = np.reshape(l2,(N,M,2))
    allpts = np.reshape(y[:2*Q+2*N*M],(Q+N*M,2)) # both force points and Lagrangian points
    P2 = np.reshape(y[(2*Q+2*N*M):],(N*M,2,2))
    P3 = np.reshape(P2,(N,M,2,2))
    #calculate tensor derivative
    Pd = pdict['beta']*SD2D.tensorDiv(P3,pdict['gridspc'],N,M)
    Pd = np.reshape(Pd,(N*M,2))
    #calculate spring forces
    f = wdict['myForces'](fpts,**pdict['forcedict'])
    #calculate new velocities at all points of interest (Lagrangian points and force points)
    lt = pdict['gridspc']**2*CM.matmult(pdict['eps_grid'],pdict['mu'],allpts,l2,Pd) + CM.matmult(pdict['eps_obj'],pdict['mu'],allpts,fpts,f)
    # reshape the velocities on the grid
    lt3 = np.reshape(lt[2*Q:],(N,M,2)) 
    #calculate deformation matrix and its inverse
    F = SD2D.vectorGrad(l3,pdict['gridspc'],N,M)
    F = np.reshape(F,(N*M,2,2))
    #calculate new stress time derivatives
#    gradlt = pdict['gridspc']**2*CM.derivop(pdict['eps_grid'],pdict['mu'],l2,l2,Pd,F) + CM.derivop(pdict['eps_obj'],pdict['mu'],l2,fpts,f,F)   #grad(Stokeslet) method
    gradlt = SD2D.vectorGrad(lt3,pdict['gridspc'],N,M)   
    gradlt = np.reshape(gradlt,(N*M,2,2))
    gradub = np.zeros(gradlt.shape)
#    Finv = CM.matinv2x2(F)   # first grad(u) method
#    Pt = np.zeros((N*M,2,2))
#    for j in range(N*M):
#        Pt[j,:,:] = np.dot(np.dot(gradlt[j,:,:],Finv[j,:,:]),P2[j,:,:]) - (1./pdict['Wi'])*(P2[j,:,:] - Finv[j,:,:].transpose())        
    Pt = CM.stressDeriv(pdict['Wi'],gradub,gradlt,F,P2)
    return np.append(lt,Pt.flatten())
def stokesFlowUpdater(t, y, wdict):
    '''
    t = current time, y = [fpts.flatten(), l.flatten(), P.flatten()], pdict 
    contains: K is spring constant, xr is resting position, blob is regularized 
    Stokeslet object, myForces is a function handle, forcedict is a dictionary
    containing optional parameters for calculating forces.
    
    '''
    pdict = wdict['pdict']
    pdict['forcedict']['t'] = t
    Q = len(y) / 2
    fpts = np.reshape(y, (Q, 2))
    f = wdict['myForces'](fpts, **pdict['forcedict'])
    yt = CM.matmult(pdict['eps_obj'], pdict['mu'], fpts, fpts, f)
    return yt
def stokesFlowUpdater(t,y,wdict):
    '''
    t = current time, y = [fpts.flatten(), l.flatten(), P.flatten()], pdict 
    contains: K is spring constant, xr is resting position, blob is regularized 
    Stokeslet object, myForces is a function handle, forcedict is a dictionary
    containing optional parameters for calculating forces.
    
    '''
    pdict = wdict['pdict']
    pdict['forcedict']['t'] = t
    Q=len(y)/2
    fpts = np.reshape(y,(Q,2))
    f = wdict['myForces'](fpts,**pdict['forcedict'])
    yt = CM.matmult(pdict['eps_obj'],pdict['mu'],fpts,fpts,f)
    return yt