1
$\begingroup$

I am new to python and programming for that matter. I am trying to solve a special type of wave equation using finite element method and carry out simulations in python. I am getting some errors which I do not understand and I need help. Below is the error and subsequently a part of the code.


ValueError                                Traceback (most recent call last)
<ipython-input-138-b33b0055e38e> in <module>
      6     # Finite Element Method
      7     #unew = (dt**2) * Minv @ (f*src[it]  -  K @ u) + 2*u - uold
----> 8     vnew = dt * Minv @ (f*src[it]  -  (Kv @ v) -  (Ku @ u)) + v
      9     v = v, vnew
     10     unew = (dt) * vnew

ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 2 is different from 1000)

Time extrapolation

for it in range(nt):
    # --------------------------------------
    # Finite Element Method
    #unew = (dt**2) * Minv @ (f*src[it]  -  K @ u) + 2*u - uold 
    vnew = dt * Minv @ (f*src[it]  -  (Kv @ v) -  (Ku @ u)) + v 
    v = v, vnew
    unew = (dt) * vnew
    #uold, u = u, unew
    u = u, unew


# --------------------------------------
# Finite Difference Method
pnew = (dt**2) * Mfd @ ( f/dx*src[it]+ D @ p) + 2*p #- pold
p = p, pnew

# --------------------------------------   
# Animation plot. Display both solutions
if not it % iplot:
    for l in line1:
        l.remove()
        del l
    for l in line2:
        l.remove()
        del l
    line1 = plt.plot(x, v, 'k', lw=1.5, label='FEM')
    line2 = plt.plot(x, p, 'r', lw=1.5, label='FDM')
    plt.legend()
    plt.gcf().canvas.draw()
$\endgroup$

1 Answer 1

0
$\begingroup$

It has to do with your coding style. Your code is too similar to mathematical notion. Issues include doing too much on single line and operator precedence in Python

Your code:

vnew = dt * Minv @ (f*src[it]  -  (Kv @ v) -  (Ku @ u)) + v 

Breaking into simpler statements would allow for easier debugging.

term_1 = (f*src[it])  -  (Kv @ v) -  (Ku @ u)
term_2 = term_1 + v
term_3 =  Minv @ term_2
vnew = dt * term_3
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.