Esempio n. 1
0
def awefem(mesh, t, source_loc=None):

    # Function space
    V = FunctionSpace(mesh, "Lagrange", 1)

    # Boundary condition
    bc = DirichletBC(V, Constant(0), "on_boundary")

    # Trial and test functions
    u = TrialFunction(V)
    v = TestFunction(V)

    # Discretization
    c = 6
    dt = t[1] - t[0]
    u0 = Function(V)  # u0 = uN-1
    u1 = Function(V)  # u1 = uN1

    # Variational formulation
    F = (u - 2 * u1 + u0) * v * dx + (dt * c) ** 2 * dot(
        grad(u + 2 * u1 + u0) / 4, grad(v) ) * dx
    a, L = lhs(F), rhs(F)

    # Solver
    A, b = assemble_system(a, L)
    solver = LUSolver(A, "mumps")
    solver.parameters["symmetric"] = True
    bc.apply(A, b)

    # Solution
    u = Function(V)  # uN+1

    # Source
    if source_loc is None:
        mesh_center = np.mean(mesh.coordinates(), axis=0)
        source_loc = Point(mesh_center)
    else:
        source_loc = Point(source_loc)

    # Time stepping
    printc('\bomb Hit F1 to interrupt.', c='yellow')
    pb = ProgressBar(0, len(t))
    for i, t_ in enumerate(t[1:]):
        pb.print()
        b = assemble(L)
        delta = PointSource(V, source_loc, ricker_source(t_) * dt**2)
        delta.apply(b)
        solver.solve(u.vector(), b)

        u0.assign(u1)
        u1.assign(u)

        if t_>0.03:
            plot(u,
                 warpZfactor=20, # set elevation along z
                 vmin=.0,     # sets a minimum to the color scale
                 vmax=0.003,
                 cmap='rainbow', # the color map style
                 alpha=1,        # transparency of the mesh
                 lw=0.1,         # linewidth of mesh
                 scalarbar=None,
                 #lighting='plastic',
                 #elevation=-.3,
                 interactive=0)  # continue execution

    interactive()
def lfa_test_line_location(
        A0=25e-9,  #1.3e-8
        P0=11.4e-9,  #2.28e-8
        D_A=2.6073971259391082e-11,  #1e-10
        D_P=1.507422684843226e-11,  #1e-12,
        D_PA=1.1843944324646026e-11,
        ka1=1e6,
        kd1=1e-3,
        U=2.22e-4,
        T=1800,
        num_steps=600,
        L=0.025,
        C=0.000183,
        r=0.00855,
        width=0.006,  # meters
        PA_coef=0.8):

    # Time stepping
    #T = 1800.0           # final time
    #num_steps = 1800     # number of time steps 500
    dt = T / num_steps  # time step size

    # System parameters, constants

    # concentration of analite on the sample pad (or chamber)
    # concentration of detection probe on the sample pad (or chamber)

    # total ligand concentration

    # analyte diffusion coeficient from Stokes–Einstein equation
    # probe diffusion coeficient from Stokes–Einstein equation

    # association rate (1/Ms)
    # dissociation rate (1/Ms)
    # association rate (1/Ms)
    # dissociation rate (1/Ms)
    # association rate (1/Ms)
    # dissociation rate (1/Ms)
    # association rate (1/Ms)
    # dissociation rate (1/Ms)

    #L = 0.041      # juostelės ilgis

    def quadratic(opt):
        a = ka1
        b = -(ka1 * A0 + ka1 * P0 + kd1)
        c = ka1 * A0 * P0
        D = b**2 - 4 * a * c
        #opt = 0.90

        PA1 = (-b + sqrt(D)) / (2 * a)
        PA2 = (-b - sqrt(D)) / (2 * a)

        if (PA1 and PA2) < (A0 or P0):
            if PA1 > PA2:
                return PA2 * opt
            else:
                return PA1 * opt
        else:
            print("Critical error")
            return 0

    PA_eq = quadratic(PA_coef)
    max_PA = 0
    x_opt = 0

    # Create mesh
    domain = Rectangle(Point(0, 0), Point(L, 0.006))  # 2.2, 0.41
    # išsiaiškinti kaip veikia mesh funkcija (ar x ir y kryptimis same)
    mesh = generate_mesh(domain, 32)

    # Define function space for system of concentrations
    P1 = FiniteElement("P", triangle, 2)
    #element = MixedElement([P1, P1, P1, P1, P1, P1])
    element = MixedElement([P1, P1, P1])
    V = FunctionSpace(mesh, element)

    # Define test functions
    v_A, v_PA, v_P = TestFunctions(V)

    # Define initial conditions
    u_0 = Expression(('0', '0', '0'), degree=0)

    u = Function(V)
    u_n = project(u_0, V)
    #u_n = Function(V)

    u_A, u_PA, u_P = split(u)
    u_nA, u_nPA, u_nP = split(u_n)

    # Define expressions used in variational forms
    k = Constant(dt)

    A0 = Constant(A0)
    P0 = Constant(P0)

    ka1 = Constant(ka1)
    kd1 = Constant(kd1)

    D_A = Constant(D_A)
    D_P = Constant(D_P)

    L = Constant(L)
    # 2e-4 fuild velocity, obtained from experimental data (m/s or ~0.2mm/s) 20s/4cm
    U = Constant((U, 0))

    C = Constant(C)
    r = Constant(r)

    U = Expression(('C*exp(-r*t)', '0'), degree=2, C=C, r=r, t=0)

    # Define boundray conditions

    u_D_A1 = Expression('(T-t)>0 ? A0 : 0', degree=0, t=0, T=T, A0=A0)
    #u_D_PA1 = Expression('0', degree=0)
    u_D_P1 = Expression('(T-t)>0 ? P0 : 0', degree=0, t=0, T=T, P0=P0)

    #u_D_A1 = Expression('A0', degree=0, A0=A0)
    #u_D_PA1 = Expression('3e-16', degree=0)
    #u_D_P1 = Expression('P0', degree=0, P0=P0)

    u_D_PA1 = Constant(0.0)

    inflow = 'near(x[0], 0)'  # boundary kai x = 0
    inflow_2 = 'x[0] < DOLFIN_EPS'

    bc_A1 = DirichletBC(V.sub(0), u_D_A1, inflow)
    bc_PA1 = DirichletBC(V.sub(1), u_D_PA1, inflow)
    bc_P1 = DirichletBC(V.sub(2), u_D_P1, inflow)

    # i lista reikia boundary apjungti
    bcs = [bc_A1, bc_P1, bc_PA1]
    #bcs = [bc_A1, bc_P1]

    F_PA = ka1 * u_A * u_P - kd1 * u_PA


    F = ((u_A - u_nA) / k)*v_A*dx + D_A*dot(grad(u_A), grad(v_A))*dx + dot(U, grad(u_A))*v_A*dx \
        + ((u_PA - u_nPA) / k)*v_PA*dx + D_PA*dot(grad(u_PA), grad(v_PA))*dx + dot(U, grad(u_PA))*v_PA*dx \
        + ((u_P - u_nP) / k)*v_P*dx + D_P*dot(grad(u_P), grad(v_P))*dx + dot(U, grad(u_P))*v_P*dx \
        - (-F_PA*v_A*dx + F_PA*v_PA*dx - F_PA*v_P*dx)

    # Create VTK files for visualization output
    #vtkfile_u_A = File('lfa_diffusion_reduction/u_A.pvd')
    #vtkfile_u_PA = File('lfa_diffusion_reduction/u_PA.pvd')
    #vtkfile_u_P = File('lfa_diffusion_reduction/u_P.pvd')

    # Create progress bar
    #progress = Progress('Time-stepping', num_steps)
    pb = ProgressBar(0, num_steps, c='red')
    # set_log_level(PROGRESS)

    # Time-stepping
    t = 0
    # for n in range(num_steps):

    for n in pb.range():

        print(str(round(t / T * 100, 1)) + "%")
        # Update boundaries
        u_D_A1.t = t
        u_D_P1.t = t
        U.t = t

        # Update current time
        t += dt

        vtkfile_u_PA = File('lfa_diffusion_reduction/u_PA.pvd')

        # Solve variational problem for time step
        solve(F == 0,
              u,
              bcs,
              solver_parameters={
                  "newton_solver": {
                      "absolute_tolerance": 1e-20,
                      "relative_tolerance": 1e-20
                  }
              })

        # Save solution to file (VTK)
        #_u_A, _u_PA, _u_P, _u_RA, _u_RPA, _u_R = u.split()
        _u_A, _u_PA, _u_P = u.split()
        #vtkfile_u_A << (_u_A, t)
        vtkfile_u_PA << (_u_PA, t)
        #vtkfile_u_P << (_u_P, t)

        # Update previous solution
        u_n.assign(u)

        img = load("lfa_diffusion_reduction/u_PA000000.vtu")
        p1, p2 = (0, 0.003, 0), (L, 0.003, 0)
        pl = probeLine(img, p1, p2, res=5000).lineWidth(4)
        xvals = pl.points()[:, 0]
        yvals = pl.getPointArray()
        max_PA = max(yvals)
        if debug:
            print(PA_eq)
            print(max_PA)
            print("")

        if max_PA >= PA_eq:
            print(
                "PA complex concentration reached equilibrium, terminating simulation."
            )
            x_opt = xvals[yvals.argmax()]
            #x_opt
            return x_opt
def lfa_time_volume(
        xL1=0.01064,
        A0=25e-9,
        P0=11.4e-9,
        R0=8.61e-6,  #1.7e-8 8.61e-6
        D_A=2.6073971259391082e-11,
        D_P=1.507422684843226e-11,
        D_PA=1.1843944324646026e-11,
        ka1=1e6,
        kd1=1e-3,
        ka2=1e6,
        kd2=1e-3,
        ka3=1e6,
        kd3=1e-3,
        ka4=1e6,
        kd4=1e-3,
        L=0.025,
        T=1800,
        num_steps=60,
        U=2.22e-4,
        thickness=0.000135,
        width=0.0006,
        RA_const=0,
        C=0.000183,
        r=0.00855):

    # Time stepping
    #T = 1800.0            # final time
    #num_steps = 60    # number of time steps T/200 optimum num_steps
    dt = T / num_steps  # time step size

    # System parameters, constants

    # concentration of analite on the sample pad (or chamber)
    # concentration of detection probe on the sample pad (or chamber)

    # total ligand concentration

    # analyte diffusion coeficient from Stokes–Einstein equation
    # probe diffusion coeficient from Stokes–Einstein equation

    # association rate (1/Ms)
    # dissociation rate (1/Ms)
    # association rate (1/Ms)
    # dissociation rate (1/Ms)
    # association rate (1/Ms)
    # dissociation rate (1/Ms)
    # association rate (1/Ms)
    # dissociation rate (1/Ms)

    #L = 0.041      # juostelės ilgis
    #xL1 = L/2      # R ribos
    #xL2 = 3*L/4    # R ribos
    xL2 = xL1 + 0.004

    #xL1 = 0.034276
    #xL2 = 0.034276 + 0.003

    RPA_signal = 0.8 * R0

    #thickness = 0.000135
    #width = 0.0005
    velocity = U
    test_line_location = xL1

    #xL1 = 0.034276
    #xL2 = xL1 + 0.005

    # Create mesh
    domain = Rectangle(Point(0, 0), Point(L, 0.006))  # 2.2, 0.41
    # išsiaiškinti kaip veikia mesh funkcija (ar x ir y kryptimis same)
    mesh = generate_mesh(domain, 32)

    # Define function space for system of concentrations
    P1 = FiniteElement("P", triangle, 2)
    #element = MixedElement([P1, P1, P1, P1, P1, P1])
    element = MixedElement([P1, P1, P1, P1, P1, P1])
    V = FunctionSpace(mesh, element)

    # Define boundary condition
    #u_D = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]', degree=2)

    # Define test functions
    v_A, v_PA, v_P, v_R, v_RA, v_RPA = TestFunctions(V)
    #v_A, v_PA, v_P, v_RA, v_RPA, v_R = TestFunctions(V)

    # Define functions for concentrations

    # Define initial conditions
    u_0 = Expression(
        ('0', '0', '0', 'x[0]>xL1 && x[0]<xL2 ? R0 : 0', '0', '0'),
        degree=0,
        R0=R0,
        xL1=xL1,
        xL2=xL2)

    #u_0 = Expression(('0', '0', '0'), degree=0)

    u = Function(V)
    u_n = project(u_0, V)
    #u_n = Function(V)

    # Split system functions to access components

    u_A, u_PA, u_P, u_R, u_RA, u_RPA = split(u)
    u_nA, u_nPA, u_nP, u_nR, u_nRA, u_nRPA = split(u_n)

    # Define expressions used in variational forms
    k = Constant(dt)

    A0 = Constant(A0)
    P0 = Constant(P0)
    R0 = Constant(R0)

    ka1 = Constant(ka1)
    kd1 = Constant(kd1)
    ka2 = Constant(ka2)
    kd2 = Constant(kd2)
    ka3 = Constant(ka3)
    kd3 = Constant(kd3)
    ka4 = Constant(ka4)
    kd4 = Constant(kd4)

    D_A = Constant(D_A)
    D_P = Constant(D_P)

    L = Constant(L)
    # fuild velocity, obtained from experimental data (m/s or ~0.2mm/s) 20s/4cm
    U = Constant((2e-4, 0))

    xL1 = Constant(xL1)
    xL2 = Constant(xL2)

    RA_const = Constant(RA_const)

    C = Constant(C)
    r = Constant(r)

    U = Expression(('C*exp(-r*t)', '0'), degree=2, C=C, r=r, t=0)

    # Define boundray conditions

    u_D_A1 = Expression('(T-t)>0 ? A0 : 0', degree=0, t=0, T=1800, A0=A0)
    #u_D_PA1 = Expression('0', degree=0)
    u_D_P1 = Expression('(T-t)>0 ? P0 : 0', degree=0, t=0, T=1800, P0=P0)

    u_D_PA1 = Constant(0.0)

    inflow = 'near(x[0], 0)'  # boundary kai x = 0
    inflow_2 = 'x[0] < DOLFIN_EPS'

    bc_A1 = DirichletBC(V.sub(0), u_D_A1, inflow)
    bc_PA1 = DirichletBC(V.sub(1), u_D_PA1, inflow)
    bc_P1 = DirichletBC(V.sub(2), u_D_P1, inflow)

    bc_R1 = DirichletBC(V.sub(3), u_D_PA1, inflow)
    bc_RA1 = DirichletBC(V.sub(4), u_D_PA1, inflow)
    bc_RAP1 = DirichletBC(V.sub(5), u_D_PA1, inflow)

    # outflow = 'near(x[0], L)' # boundary kai x = 1, mum reikėtų kai x = L, reikės dar parametra įvesti L=L
    # boundary = 'near(x[0], 0)' # reikia funkcijos kuri paskytu ties kurio x ar y pasirenktu boundary
    # walls = 'near(x[1], 0) || near(x[1], 1)' # boundary kai y = 0 ir kai y = 1

    # Boundaries combined into a list
    bcs = [bc_A1, bc_P1, bc_PA1, bc_R1, bc_RA1, bc_RAP1]

    F_PA = ka1 * u_A * u_P - kd1 * u_PA

    F_RA = RA_const * (ka2 * u_A * u_R - kd2 * u_RA - ka4 * u_RA * u_P +
                       kd4 * u_RPA)
    F1_RPA = ka3 * u_PA * u_R - kd3 * u_RPA
    F2_RPA = RA_const * (ka4 * u_RA * u_P - kd4 * u_RPA)
    F_RPA = F1_RPA + F2_RPA

    # F1_RPA padauginti is kokio nors n, kuris reiškia kiek plaukeliu turi analite ten kur PA lygtis tik ten

    F = ((u_A - u_nA) / k)*v_A*dx + D_A*dot(grad(u_A), grad(v_A))*dx + dot(U, grad(u_A))*v_A*dx \
        + ((u_PA - u_nPA) / k)*v_PA*dx + D_P*dot(grad(u_PA), grad(v_PA))*dx + dot(U, grad(u_PA))*v_PA*dx \
        + ((u_P - u_nP) / k)*v_P*dx + D_P*dot(grad(u_P), grad(v_P))*dx + dot(U, grad(u_P))*v_P*dx \
        + ((u_R - u_nR) / k)*v_R*dx \
        + ((u_RA - u_nRA) / k)*v_RA*dx \
        + ((u_RPA - u_nRPA) / k)*v_RPA*dx \
        - (-F_PA*v_A*dx - F_RA*v_A*dx
        + F_PA*v_PA*dx - F1_RPA*v_PA*dx
        - F_PA*v_P*dx - F2_RPA*v_P*dx
        - F_RA*v_R*dx - F1_RPA*v_R*dx
        + F_RA*v_RA*dx
        + F_RPA*v_RPA*dx)

    # Create VTK files for visualization output
    #vtkfile_u_A = File('lfa_time_volume/u_A.pvd')
    #vtkfile_u_PA = File('lfa_time_volume/u_PA.pvd')
    #vtkfile_u_P = File('lfa_time_volume/u_P.pvd')
    #vtkfile_u_R = File('lfa_time_volume/u_R.pvd')
    #vtkfile_u_RA = File('lfa_time_volume/u_RA.pvd')
    #vtkfile_u_RPA = File('lfa_time_volume/u_RPA.pvd')

    # Create progress bar
    #progress = Progress('Time-stepping', num_steps)
    pb = ProgressBar(0, num_steps, c='red')
    # set_log_level(PROGRESS)

    # Time-stepping
    t = 0
    # for n in range(num_steps):

    #average_concentrations = []

    time_points = []
    RPA_concentrations = []

    for n in pb.range():
        print(str(round(t / T * 100, 1)) + "%")
        # Update boundaries
        u_D_A1.t = t
        #u_D_PA1.t = t
        u_D_P1.t = t
        U.t = t

        # Update current time
        t += dt

        #vtkfile_u_A = File('lfa_time_volume/u_A.pvd')
        #vtkfile_u_PA = File('lfa_time_volume/u_PA.pvd')
        #vtkfile_u_P = File('lfa_time_volume/u_P.pvd')
        #vtkfile_u_R = File('lfa_time_volume/u_R.pvd')
        #vtkfile_u_RA = File('lfa_time_volume/u_RA.pvd')
        vtkfile_u_RPA = File('lfa_time_volume/u_RPA.pvd')

        # Solve variational problem for time step
        solve(F == 0,
              u,
              bcs,
              solver_parameters={
                  "newton_solver": {
                      "absolute_tolerance": 1e-20,
                      "relative_tolerance": 1e-20
                  }
              })

        # Save solution to file (VTK)
        #_u_A, _u_PA, _u_P, _u_RA, _u_RPA, _u_R = u.split()
        _u_A, _u_PA, _u_P, _u_R, _u_RA, _u_RPA = u.split()
        #vtkfile_u_A << (_u_A, t)
        #vtkfile_u_PA << (_u_PA, t)
        #vtkfile_u_P << (_u_P, t)
        #vtkfile_u_R << (_u_R, t)
        #vtkfile_u_RA << (_u_RA, t)
        vtkfile_u_RPA << (_u_RPA, t)

        # Update previous solution
        u_n.assign(u)

        img = load("lfa_time_volume/u_RPA000000.vtu")
        p1, p2 = (xL1, 0.003, 0), (xL2, 0.003, 0)
        pl = probeLine(img, p1, p2, res=500).lineWidth(4)
        xvals = pl.points()[:, 0]
        yvals = pl.getPointArray()
        #average_concentration = sum(yvals) / len(yvals)
        # average_concentrations.append(average_concentration)
        max_concentration = max(yvals)

        time_points.append(t)
        RPA_concentrations.append(max_concentration)

        #print(max_concentration)
        #print(t)
        #print(volume)

        #if max_concentration >= RPA_signal:
        #print("hey hey hey, stop it!")
        #print(max_concentration)
        #print(t)
        #print(volume)
        #return t,volume
        #x_opt = xvals[yvals.argmax()]
        # print(x_opt)
        #break

        # pb.print(t)
    #plot()

    # Update progress bar
    #Progress('Time-stepping', t / T)
    # Progress('Time-stepping')
    # set_log_level(LogLevel.PROGRESS)
    # progress+=1
    # set_log_level(LogLevel.ERROR)

    # Hold plot
    # interactive()
    RPA_opt = 0.9 * max(RPA_concentrations)
    index_opt = min(range(len(RPA_concentrations)),
                    key=lambda i: abs(RPA_concentrations[i] - RPA_opt))
    test_to_test_line = test_line_location / velocity
    volume = (time_points[index_opt] -
              test_to_test_line) * thickness * width * velocity * 1000000000

    return index_opt, RPA_opt, RPA_concentrations[index_opt], time_points[
        index_opt], volume
Esempio n. 4
0
a3 = inner(u, v) * dx
L3 = inner(u1, v) * dx - k * inner(grad(p1), v) * dx

# Assemble matrices
A1 = assemble(a1)
A2 = assemble(a2)
A3 = assemble(a3)

# Use amg preconditioner if available
prec = "amg" if has_krylov_solver_preconditioner("amg") else "default"

# Use nonzero guesses - essential for CG with non-symmetric BC
parameters['krylov_solver']['nonzero_initial_guess'] = True

# Time-stepping
pb = ProgressBar(0, T, dt, c='green')
for t in pb.range():

    # Update pressure boundary condition
    p_in.t = t

    # Compute tentative velocity step
    b1 = assemble(L1)
    [bc.apply(A1, b1) for bc in bcu]
    solve(A1, u1.vector(), b1, "bicgstab", "default")

    # Pressure correction
    b2 = assemble(L2)
    [bc.apply(A2, b2) for bc in bcp]
    [bc.apply(p1.vector()) for bc in bcp]
    solve(A2, p1.vector(), b2, "bicgstab", prec)
Esempio n. 5
0
# Define variational problem
F = ((u_1 - u_n1) / k)*v_1*dx + dot(w, grad(u_1))*v_1*dx \
  + eps*dot(grad(u_1), grad(v_1))*dx + K*u_1*u_2*v_1*dx  \
  + ((u_2 - u_n2) / k)*v_2*dx + dot(w, grad(u_2))*v_2*dx \
  + eps*dot(grad(u_2), grad(v_2))*dx + K*u_1*u_2*v_2*dx  \
  + ((u_3 - u_n3) / k)*v_3*dx + dot(w, grad(u_3))*v_3*dx \
  + eps*dot(grad(u_3), grad(v_3))*dx - K*u_1*u_2*v_3*dx + K*u_3*v_3*dx \
  - f_1*v_1*dx - f_2*v_2*dx - f_3*v_3*dx

# Create time series for reading velocity data
timeseries_w = TimeSeries('navier_stokes_cylinder/velocity_series')

# Time-stepping
from vedo.dolfin import plot, ProgressBar
pb = ProgressBar(0, num_steps, c='red')
t = 0
for n in pb.range():

    # Update current time
    t += dt

    # Read velocity from file
    timeseries_w.retrieve(w.vector(), t)

    # Solve variational problem for time step
    solve(F == 0, u)

    _u_1, _u_2, _u_3 = u.split()

    # Update previous solution