usnistgov / fipy

FiPy is a Finite Volume PDE solver written in Python
http://pages.nist.gov/fipy/en/latest
Other
510 stars 149 forks source link

Can I change an equation mid-run? and a few more questions #839

Closed Yifath7 closed 2 years ago

Yifath7 commented 2 years ago

Hello, I have a few questions:

  1. I need to implement the simplest ODE -

    dR/dt = const for 0 < t < 100 dR/dt = 0 for t > 100

How can I do that? can I stop R.updateOld() after t reaches 100 or should I define the equation differently somehow?

  1. I have two types of PDEs' terms which I implemented as I understood from the documentation, however I could find a I'm not entirely sure if I implement correctly using DiffusionTerm: image

    where in the first equation the nabla represents the Laplacian operator. A,M,P are variables, alpha and lambda are constants. My implementation is as follows:

# Eq 1:
eqn_P = TransientTerm(var=P) == DiffusionTerm(coeff=1, var=P) + lambda * A - alpha * P

# Eq 2:
eqn_M = TransientTerm(var=M) == -DiffusionTerm(coeff=M, var=A) + alpha * M + lambda * A

Thanks in advance, Yifat

guyer commented 2 years ago

Hello, I have a few questions:

  1. I need to implement the simplest ODE -

dR/dt = const for 0 < t < 100 dR/dt = 0 for t > 100

How can I do that? can I stop R.updateOld() after t reaches 100 or should I define the equation differently somehow?

You can stop evaluating the equation altogether, but I would change the value of the RHS to be a FiPy Variable:

const = Variable(const_value)
eq = TransientTerm(var=R) == const

t = 0
for step in range(steps):
    R.updateOld()
    if t > 100:
        const.value = 0
    eq.solve(dt=dt)
    t += dt
  1. ... My implementation is as follows:
    
    # Eq 1:
    eqn_P = TransientTerm(var=P) == DiffusionTerm(coeff=1, var=P) + lambda * A - alpha * P

Eq 2:

eqn_M = TransientTerm(var=M) == -DiffusionTerm(coeff=M, var=A) + alpha M + lambda A

Should be

eqn_P = TransientTerm(var=P) == DiffusionTerm(coeff=DP, var=P) + lambda * A - alpha * P

but otherwise fine. Presumably you have a third equation?

Yifath7 commented 2 years ago

Thanks a lot! I have 18 coupled PDEs, just wanted to make sure I implement the different equation types correctly.