Writing a vectorial symbolic expression

39 views
Skip to first unread message

Alexis Prel

unread,
Jul 24, 2018, 1:25:46 PM7/24/18
to sympy
Hi all,

As this is my first message in this group, I will first introduce myself, my actual question starts in the next paragraph. I am a PhD student in physics. I am working on the modelling of organic solar cells by the so-called drift-diffusion approach, hence I plan on using sympy to deal with PDE and some systematic manipulations of them that I do by hand for now. I am quite familiar with python and its mainstream scientific libraries (I teach it as a TA) but I am new to sympy.

One of the first thing I tried to write in sympy was an equation of the following form, where  the divergence of some vector appears:

$$\frac{\partial c }{\partial t} - \vec\nabla . \vec{j_c} = 0 $$

However I found no way of writing this properly in sympy.
Indeed, using the vector module does not take into account derivatives the way I expect it to, consider the following code:
import sympy as sp
import sympy.vector as spv

# Initialise coordinate system
S
= spv.CoordSysCartesian('S')
delop
= spv.Del(S)

# Create a new field without explicit expression
u0
, u1, u2 = sp.symbols('u0:3')
J1
= u0*S.i + u1*S.j + u2* S.k
J2
= S.x*S.i + S.y*S.j + S.z* S.k

# Derivatives surprised me
print('div(J1) = ', delop.dot(J1))
print('div(J2) = ', delop.dot(J2))
It's output goes as follow:
div(J1) =  0
div(J2) =  Derivative(S.x, S.x) + Derivative(S.y, S.y) + Derivative(S.z, S.z)

I assumed, however that the output would look like this:
div(J1) =  Derivative(u0, S.x) + Derivate(u1, S.y) + Derivative(u2, S.z)
div(J2) =  Derivative(S.x, S.x) + Derivative(S.y, S.y) + Derivative(S.z, S.z)

Is there a misunderstanding somewhere on my part or was sympy.vector not designed for this kind of purposes?

Right now I am planning on writing my own set of classes and method to handle these vectorial expressions, but before I do so I would like to be sure that I haven't misunderstood the issue.

Thank you very much for reading through,
Best regards,
Alexis Prel

Alexis Prel

unread,
Jul 27, 2018, 10:32:58 AM7/27/18
to sympy
Hi again,

I believe that I found the proper way to do it, so I am putting this here in case it can be of any help to anyone.

import sympy as sp
import sympy.vector as spv
# from IPython.display import Latex # uncomment in Jupyter notebook


S
= spv.CoordSysCartesian('S')

n
= sp.Function('n')(S.x, S.y, S.z, t)
Lap = S.delop.dot(S.delop(n))
print(sp.latex(Lap))
# Latex('${}$'.format(sp.latex(Lap))) # uncomment in Jupyter notebook

This produces the expected behavior.
Reply all
Reply to author
Forward
0 new messages