12
SymFields: An Open Source Symbolic Fields Analysis Tool for General Curvilinear Coordinates in Python CHU Nan Institute of Plasma Physics, Chinese Academy of Sciences, Hefei 230031, China December 22, 2020 [email protected] Abstract An open source symbolic tool for vector fields analysis ’SymFields’ is developed in Python. The Sym- Fields module is constructed upon Python symbolic module sympy, which could only conduct scaler field analysis. With SymFields module, you can conduct vector analysis for general curvilinear coordi- nates regardless whether it is orthogonal or not. In SymFields, the differential operators based on metric tensor are normalized to real physical values, which means your can use real physical value of the vector fields as inputs. This could greatly free the physicists from the tedious calculation under complicated coordinates. 1 Introduction The plasma physics MHD theory is a combination of fluid equations and Maxwell’s equations. The MHD equations involve the solution to multiple vector fields: electric field ~ E( ~ R), magnetic field ~ B( ~ R), displacement vector ~ ξ ( ~ R), etc. Thus, the derivation in MHD theory usually becomes very complicated not because of physics issue but due to tremendous long terms in equations. To avoid this kind of tedious work, symbolic calculation is developed in many programming languages. For example, the General Vector Analysis (GVA) toolbox for Mathematica software developed by Prof. H. Qin, which could conduct symbolic fields analysis for general coordinates with elegant expression after simplification [1]. Matlab and Python also have some elementary symbolic calculation functions. However, Mathematica and Matlab are commercial software with high price, which is hardly affordable to me. In open source Python language, there is already a fundamental symbolic calculation module sympy. However, it does not provide the general analysis functions for vector fields. The SageMath project is another useful open source symbolic calculation software (https://www.sagemath.org/). It provides many useful functions for symbolic calculation in vector fields. Although it depends on many modules in python, its source codes is not compatible with Python. Since we do not wish to get involved with a new language, we would like to develop an open source symbolic fields analysis tool solely within Python. arXiv:2012.10723v1 [cs.SC] 19 Dec 2020

SymFields: An Open Source Symbolic Fields Analysis Tool

  • Upload
    others

  • View
    22

  • Download
    0

Embed Size (px)

Citation preview

Page 1: SymFields: An Open Source Symbolic Fields Analysis Tool

SymFields: An Open Source Symbolic Fields Analysis Tool forGeneral Curvilinear Coordinates in Python

CHU Nan

Institute of Plasma Physics, Chinese Academy of Sciences, Hefei 230031, ChinaDecember 22, 2020

[email protected]

Abstract

An open source symbolic tool for vector fields analysis ’SymFields’ is developed in Python. The Sym-Fields module is constructed upon Python symbolic module sympy, which could only conduct scalerfield analysis. With SymFields module, you can conduct vector analysis for general curvilinear coordi-nates regardless whether it is orthogonal or not. In SymFields, the differential operators based on metrictensor are normalized to real physical values, which means your can use real physical value of the vectorfields as inputs. This could greatly free the physicists from the tedious calculation under complicatedcoordinates.

1 Introduction

The plasma physics MHD theory is a combination of fluid equations and Maxwell’s equations. TheMHD equations involve the solution to multiple vector fields: electric field ~E(~R), magnetic field ~B(~R),displacement vector ~ξ(~R), etc. Thus, the derivation in MHD theory usually becomes very complicatednot because of physics issue but due to tremendous long terms in equations. To avoid this kind of tediouswork, symbolic calculation is developed in many programming languages. For example, the GeneralVector Analysis (GVA) toolbox for Mathematica software developed by Prof. H. Qin, which couldconduct symbolic fields analysis for general coordinates with elegant expression after simplification [1].Matlab and Python also have some elementary symbolic calculation functions. However, Mathematicaand Matlab are commercial software with high price, which is hardly affordable to me. In open sourcePython language, there is already a fundamental symbolic calculation module sympy. However, it doesnot provide the general analysis functions for vector fields. The SageMath project is another usefulopen source symbolic calculation software (https://www.sagemath.org/). It provides manyuseful functions for symbolic calculation in vector fields. Although it depends on many modules inpython, its source codes is not compatible with Python. Since we do not wish to get involved with a newlanguage, we would like to develop an open source symbolic fields analysis tool solely within Python.

arX

iv:2

012.

1072

3v1

[cs

.SC

] 1

9 D

ec 2

020

Page 2: SymFields: An Open Source Symbolic Fields Analysis Tool

Therefore, we developed the SymFields module in Python for fields analysis in general coordinates.It could not only deal with the vector analysis in commonly seeing orthogonal coordinates, but alsocapable to analyse fields in non-orthogonal coordinates. In this paper, the second section talks aboutfields analysis related mathematics for general coordinates (orthogonal and non-orthogonal). The thirdsection discusses the realization of field analysis symbolic calculation in SymFields module in python.The fourth section give benchmark examples to use SymFields in both orthogonal and non-orthogonalcoordinates. The last section is summary. The SymFields module is available on github at (https://github.com/DocNan/SymFields) under GNU General Public License v3.0.

2 Mathematics of general coordinates

The multiple choose of coordinates in Mathematics can sometime make the formulas in fields analysiscomplicated. In orthogonal coordinates, the vector analysis can be simplified due to the orthogonality.The differential field operators can be easily expressed with Lame coefficients in orthogonal coordinates[2]. However, for the more general non-orthogonal coordinates, one must use metric tensor to expressthese operators. Therefore, we shall start our discussion from the general coordinates.

2.1 Basic definitions in general curvilinear coordinates

2.1.1 Covariant and contra-variant vector basis

In general R3 curvilinear coordinates, we can define two sets of basis vectors. First we pick a point P inCartesian coordinate with location vector: ~R = (x, y, z) = x~ex + y ~ey + z ~ez. Suppose for the same pointP in general curvilinear coordinate, its coordinate is: (ξ1, ξ2, ξ3), where ξi(~R) = ξi(x, y, z) is a functionof the Cartesian coordinates. The contra-variant vector basis is defined as:

~g1 = ∇ξ1

~g2 = ∇ξ2

~g3 = ∇ξ3

(1)

The reciprocal covariant basis vector is defined as:~g1 = ∂ ~R

∂ξ1

~g2 = ∂ ~R∂ξ2

~g3 = ∂ ~R∂ξ3

(2)

They can be converted through the simple definition that:

~gi =~gj × ~gk

~gi · (~gj × ~gk), ~gi =

~gj × ~gk~gi · (~gj × ~gk)

(3)

With the above definition we can easily find the orthogonality between the vector basis as:

~gi · ~gj = δij =

{0 (i 6= j)

1 (i = j)(4)

2

Page 3: SymFields: An Open Source Symbolic Fields Analysis Tool

With the two set of basis vectors, any vector filed ~A = ~A(~R) can also be expressed in two equal ways:

~A =∑i

Ai ~gj =∑i

Ai~gi (5)

But we must pay attention that unlike the Cartesian coordinates, the basis vector of general curvilinearcoordinates: ~gi and ~gj are not unit vector: |~gi| 6= 1 6= |~gj|. Thus the vector components Ai and Ai undercontra and covariant vector basis are not real physical value, instead they are called the contra- andcovariant components of this vector field [3].

2.1.2 Contra- and covariant metric tensors

The contra variant metric tensor is defined as:

gij = ~gi · ~gj =

~g1 · ~g1 ~g1 · ~g2 ~g1 · ~g3

~g2 · ~g1 ~g2 · ~g2 ~g2 · ~g3

~g3 · ~g1 ~g3 · ~g2 ~g3 · ~g3

=

g11 g12 g13

g21 g22 g23

g31 g32 g33

(6)

Similarly, the covariant metric tensor is defined as: gij = ~gi · ~gj . Due to the position exchange propertyof dot product between vectors (~gi · ~gj = ~gj · ~gi), we can easily find that the transposition of the metrictensor equals to itself as: gij = gji = gTij . The contra and covariant basis vector share one import relationas the product of the two metric tensors is unit matrix: gijgij = I .

2.1.3 Jacobian for the coordinates

The Jacobian of a coordinate represent the element volume under this coordinate, it is defined as:

J = ~g1 · (~g2 × ~g3) =√|gij| =

∂(x, y, z)

∂(ξ1, ξ2, ξ3)=∂ ~R

∂ξ1· ( ∂

~R

∂ξ2× ∂ ~R

∂ξ3) (7)

Due to the reciprocal relation between contra and covariant basis vectors, their Jacobian also hasrelationship as:

J ′ = ~g1 · (~g2 × ~g3) =√|gij| = 1√

|gij|=

1

J=∂(ξ1, ξ2, ξ3)

∂(x, y, z)= ∇ξ1 · (∇ξ2 ×∇ξ3) (8)

2.2 Differential operators in general coordinates

2.2.1 Expression of differential operators with metric tensor

Suppose we have a scaler field: U = U(ξ1, ξ2, ξ3) and a vector field: ~A = A1 ~g1+A2 ~g2+A3 ~g3, where thecontra-variant component Aj = Aj(ξ1, ξ2, ξ3) is a scaler field, then the differential operators in generalcurvilinear coordinates are [3, 4]:

* Nabla operator:

∇ ∼ ∂

∂ξi∇ξi = ~gi

∂ξi(9)

3

Page 4: SymFields: An Open Source Symbolic Fields Analysis Tool

* Gradient:∇U =

∑i

∂U

∂ξi∇ξi =

∑i

∂U

∂ξi~gi =

∑i,j

gij∂U

∂ξi~gj (10)

* Divergence:

∇ · ~A =∑i

1

J

∂ξi(JAi) (11)

* Curl:

∇× ~A =1

J

∣∣∣∣∣∣~g1 ~g2 ~g3∂∂ξ1

∂∂ξ2

∂∂ξ3

A1 A2 A3

∣∣∣∣∣∣ =1

Jεijk

∂Ak∂ξj

~gi (12)

* Laplacian: Since Laplacian is the combination of gradient and divergence, and can be calculateddirectly with ∆ ~A = ∇2 ~A = ∇ · ∇U , we will not give the specific expression to it here.

2.2.2 Normalization of differential operators in physical value unit

A type of common error frequently occurs in using the differential operators with metric tensor. For anexample, in cylinder coordinate, the Jacobian is: J = HrHφHz = r2. According to formula (11), thedivergence is calculated via:

∇ · ~A =∑

i1J

∂∂ξi

(JAi)

= 1r2

( ∂∂r

(r2Ar) + ∂∂θ

(r2Aφ) + ∂∂z

(r2Az))

= 1r2

(r2Ar) +∂Aφ∂φ

+ ∂Az∂z

(Wrong)

(13)

However, this expression for divergence operator in cylinder coordinate is wrong, the correct one shallbe: ∇ · ~A = 1

r∂∂r

(rAr) + 1r

∂Aφ∂φ

+ ∂Az∂z

. The reason that cause this error is that the differential operatorsgiven in the above section use non-unity basis vectors. If we want to really use them in physics, weneed to make normalization and convert the basis vectors to unity vectors to get the real meaningfulphysical components values. Which will be critical for the realization of the SymFields we will betalking about in the next section. Suppose the unity contra and covariant basis vector as: ~ei = ~gi

|~gi| = ~giHi

,~ei =

~gi

|~gi|= Hicosθi~gi. Where we uses the relation that:

~gi · ~gi = δii = 1⇔ |~gi||~gi|cosθi = |~gi|Hicosθi = 1 (14)

Among themHi = |~gi| = | ∂~R

∂ξi| is the Lame coefficient in ξi coordinate direction [2], θi is the angle be-

tween the vectors ~gi and ~gi. Here we make the physical value normalization to the differential operatorswith unity covariant basis vector ~ei.

Suppose we have a vector field: ~A =∑

iAi~gi =

∑iAi

~gi, its normalization shall be: ~A =∑

i Ai~ei =∑

i Ai~ei, where: Ai = AiHi, Ai = Ai|~gi|. Thus the contra and covariant components of the field

function for differential operator can be easily replaced with physical value as:{Ai = Ai

Hi

Ai = Ai|~gi|

= Ai|∇ξi| = AiHicosθi

(15)

4

Page 5: SymFields: An Open Source Symbolic Fields Analysis Tool

* Dot product normalization:

~A · ~B =∑ij

(Ai~ei) · (Bj ~ej) =∑ij

AiBj~ei · ~ej =∑ij

AiBjgijHiHj

(16)

Where ~ei · ~ej = ~giHi· ~gjHj

=gijHiHj

= eij = cosθij , θij is the angle between covariant basis vectors ~gi and~gj .

* Cross product normalization:

~A× ~B =∑

ij Ai~gi ×Bj

~gj =∑

ijk AiBjεijkJ~gk =

∑ijk AiBj

εijkJHk ~ek

= 1J

∣∣∣∣∣∣~g1 ~g2 ~g3

A1 A2 A3

B1 B2 B3

∣∣∣∣∣∣ = 1J

∣∣∣∣∣∣H1~e1 H2~e2 H3~e3

g1iAi/Hi g2iAi/Hi g3iAi/Hi

g1iBj/Hj g3jBj/Hj g3jBj/Hj

∣∣∣∣∣∣ (17)

Where εijk is Levi–Civita symbol and Ai = gijAj [5].

* Gradient normalization:

∇U(ξ1, ξ2, ξ3) =∑i,j

gij∂U

∂ξi~gj =

∑i,j

gij∂U

∂ξiHj ~ej (18)

Notice that for scaler field function, U = U(~ξ) is already a normalized physical value. Thus we onlyneed to convert the basis vectors of gradient to unity ones.

* Divergence normalization:

∇ · ~A =∑i

1

J

∂ξi(JAi) =

∑i

1

J

∂ξi(JAi

Hi

) (19)

* Curl normalization:

∇× ~A =1

J

∣∣∣∣∣∣~g1 ~g2 ~g3∂∂ξ1

∂∂ξ2

∂∂ξ3

A1 A2 A3

∣∣∣∣∣∣ =1

J

∣∣∣∣∣∣H1~e1 H2~e2 H3~e3∂∂ξ1

∂∂ξ2

∂∂ξ3

g1iAi/Hi g2iAi/Hi g3iAi/Hi

∣∣∣∣∣∣ (20)

Where Ai = gijAj = gijAj/Hj , also notice that Einstein’s summation assumption is used here.

2.2.3 Special case: differential operators in orthogonal coordinates

In orthogonal coordinates, the Jacobian is the product of all Lame coefficients: J = H1H2H3. Thecontra-variant metric tensor shall be:

(gij) =

1/H21 0 0

0 1/H22 0

0 0 1/H23

= (δij

HiHj

) (21)

Where δij is delta function, when i = j, δii = 1, otherwise it takes 0 value. And the angle between ~giand ~gi is:

θi = 0⇔ ~gi ‖ ~gi ⇔ cosθi = 1⇔ Hi|~gi| = 1 (22)

5

Page 6: SymFields: An Open Source Symbolic Fields Analysis Tool

Thus the general normalized differential operators can be rewritten as:* Gradient:

∇U =∑i,j

gij∂U

∂ξiHi~ei =

∑i

∂U/∂ξi

Hi

~ei (23)

* Divergence:

∇ · ~A =∑i

1

J

∂ξi(JAi

Hi

) =1

H1H2H3

∑i

∂ξi(AiHjHk) (24)

* Curl:

∇× ~A =1

J

∣∣∣∣∣∣H1~e1 H2~e2 H3

~33∂∂ξ1

∂∂ξ2

∂∂ξ3

g1iAi/Hi g2iAi/Hi g3iAi/Hi

∣∣∣∣∣∣ =1

H1H2H3

∣∣∣∣∣∣H1~e1 H2~e2 H3~e3∂∂ξ1

∂∂ξ2

∂∂ξ3

H1A1 H2A2 H3A3

∣∣∣∣∣∣ (25)

The above expression of differential operators in orthogonal coordinates are the same as the ones intextbooks [2]. This also proves the correctness of our normalization.

3 Realization of symbolic calculation in Python

To make the calculation inputs in a minimum state, we only need to set the coordinates (ξ1, ξ2, ξ3) andcontra-variant metric tensor gij as inputs for Python symbolic calculation. The related Lame coefficientsand covariant metric tensor could all be calculate from the two inputs.

3.1 Calculation of covariant metric tensor

Since the covariant metric tensor is the inverse matrix of contra-variant metric tensor. It is very easyto use sympy module to calculate the inverse matrix with Gaussian elimination method: gij = (gij)−1.

3.2 Calculation of Lame coefficients

Whether the coordinates is orthogonal or not, we can always get the Lame coefficients from thecovariant metric tensor as: Hi = |~gi| =

√gii and similarly we have: |~gi| = |∇ξi| =

√gii.

3.3 Calculation of normalized Contra-variant components

For the cross product and curl operator, if we set the vector field components as: ~A = Ai~gi. Then aftercalculation we can get the results with covariant basis as: ~gj . However, to make an agreement duringnormalization, we choose the contra-variant components of field Aj by default. Thus we have to convertthe covariant componentsAi to normalized contra-variant components. This could also be achieved withthe help of co-variant metric tensor as: Ai = gijA

j = gijAj/Hj .

3.4 Functions in SymFields module

Following the formulas in section 2, we developed several functions in SymFields module to realizethe symbolic field analysis. They are:

6

Page 7: SymFields: An Open Source Symbolic Fields Analysis Tool

M e t r i c ( ) # c a l c u l a t e c o n t r a − or c o v a r i a n t m e t r i c t e n s o r from c u r v i l i n e a r c o o r d i n a t e sJ a c o b i a n ( ) # c a l c u l a t e J a c o b i a n wi th g i v e n m e t r i c t e n s o rLame ( ) # c a l c u l a t e Lame c o e f f i c i e n t s from g i v e n m e t r i c t e n s o rDot ( ) # c a l c u l a t e d o t p r o d u c t o f two v e c t o r sCross ( ) # c a l c u l a t e c r o s s p r o d u c t o f two v e c t o r sGrad ( ) # c a l c u l a t e g r a d i e n t from g i v e n s c a l a r f i e l d f u n c t i o nDiv ( ) # c a l c u l a t e d i v e r g e n c e from g i v e n v e c t o r f i e l d f u n c t i o nCur l ( ) # c a l c u l a t e c u r l from g i v e n v e c t o r f i e l d f u n c t i o n

After import * from SymFields module, you can use the above functions to realize fields analysis forgeneral curvilinear coordinates, regardless whether it is orthogonal or not.

4 Benchmark of vector field analysis with SymFields module

4.1 Benchmark of differential operators in cylinder coordinates

4.1.1 Curl of gradient

In any curvilinear coordinates, the curl of gradient for a scaler field will always be zero: ∇× (∇U) = 0.Thus we can use this rule to test our code. The benchmark test code is presented here:i m p o r t sympyfrom SymFie lds i m p o r t *

# c y l i n d e r c o o r d i n a t e sr , phi , z = sympy . symbols ( ’ r , phi , z ’ )X = [ r , phi , z ]U = sympy . F u n c t i o n ( ’U’ )U = U( r , phi , z )

g r ad = Grad (U, X, c o o r d i n a t e = ’ C y l i n d e r ’ )c u r l g r a d = Cur l ( grad , X, c o o r d i n a t e = ’ C y l i n d e r ’ )

The output results are:In : g r adOut :

In : c u r l g r a dOut :

4.1.2 Divergence of curl

Similarly, the divergence of curl operator for a vector field will also be zero: ∇ · (∇ × ~A) = 0. Therelated code is presented here:

7

Page 8: SymFields: An Open Source Symbolic Fields Analysis Tool

A r = sympy . F u n c t i o n ( ’ A r ’ )A phi = sympy . F u n c t i o n ( ’ A phi ’ )A z = sympy . F u n c t i o n ( ’ A z ’ )A r = A r ( r , phi , z )A phi = A phi ( r , phi , z )A z = A z ( r , phi , z )A = [ A r , A phi , A z ]

c u r l = Cur l (A, X, c o o r d i n a t e = ’ C y l i n d e r ’ )d i v c u r l = Div ( c u r l , X, c o o r d i n a t e = ’ C y l i n d e r ’ )d i v c u r l . d o i t ( )

In : c u r lOut :

In : d i v c u r l . d o i t ( )Out :

4.1.3 Expression of Laplacian operator

We also calculated the Laplacian operator with: ∇2U = ∇ · ∇U . The detailed codes goes below:L a p l a c i a n = Div ( grad , X, c o o r d i n a t e = ’ C y l i n d e r ’ , e v a l u a t i o n =1)

In : L a p l a c i a nOut :

After manually simplification, you will find it is the correct expression of Laplacian operator in cylin-der coordinate as the formula in textbooks.

8

Page 9: SymFields: An Open Source Symbolic Fields Analysis Tool

4.1.4 Build-in coordinates in SymFields module

Figure 1: Build-in coordinates in SymFields module. From left to right, Cartesian, Cylinder coordinate,Sphere and Toroidal coordinates.

In SymFields module, we have already constructed the metric tensors for several frequently used or-thogonal coordinates. They are: Cartesian, Cylinder, Sphere and Toroidal coordinates as shown in Fig.. To use these build in coordinates, you just need to set string value in the optional input like this:functionA(coordinate=’Cylinder’). For other coordinates, you can first get the mapping function rela-tion between this curvilinear coordinates (ξ1, ξ2, ξ3) and Cartesian coordinates (x, y, z). Then you cancalculate the related metric with function Metric() in SymFields module. Finally, you can conduct allthe rest differential operations with the calculated metric tensor as inputs for the operator functions inSymFields module.

4.2 Benchmark of differential operators in non-orthogonal coordinates

Figure 2: Non-orthogonal z axis shifted cylinder coordinate.

The above subsection, we have tested the SymFields module in an orthogonal (cylinder) coordinates,now we will benchmark it under non-orthogonal coordinates by given an α angle shift to the originalZ coordinate of (orthogonal) cylinder coordinate. As shown in Fig. 2, the z′ axis is shifted with angleα to z axis in the y-o-z plane. The mapping from original cylinder coordinates to the shifted cylinder

9

Page 10: SymFields: An Open Source Symbolic Fields Analysis Tool

coordinates shall be: x = r′cosφ′

y = r′sinφ′ + z′sinα

z = z′cosα

(26)

With SymFields, we can easily get its covariant metric tensor by:# t e s t non − o r t h o g o n a l s h i f t e d c y l i n d e r c o o r d i n a t er 2 , ph i 2 , z 2 , a l p h a = sympy . symbols ( ’ r 2 , ph i 2 , z 2 , a lpha ’ )Xi = [ r 2 , ph i 2 , z 2 ]x = r 2 *sympy . cos ( p h i 2 )y = r 2 *sympy . s i n ( p h i 2 ) + z 2 *sympy . s i n ( a l p h a )z = z 2 *sympy . cos ( a l p h a )R = [ x , y , z ]M co = M e t r i c ( Xi=Xi , R=R , c o o r d i n a t e = ’ s h i f t e d c y l i n d e r ’ , c o n t r a =0 , e v a l u a t i o n =1)M co = sympy . s i m p l i f y ( M co )M cont ra = M co . i n v ( method = ’GE’ )

In : M coOut :

Let’s further check the curl of gradient and divergence of curl in this non-orthogonal coordinate. Tomake the expressions more simple, we value the shift angle as: α = π/3.

* curl of gradienta l p h a = p i / 3Xi = [ r 2 , ph i 2 , z 2 ]x = r 2 *sympy . cos ( p h i 2 )y = r 2 *sympy . s i n ( p h i 2 ) + z 2 *sympy . s i n ( a l p h a )z = z 2 *sympy . cos ( a l p h a )R = [ x , y , z ]M co = M e t r i c ( Xi=Xi , R=R , c o o r d i n a t e = ’ s h i f t e d c y l i n d e r ’ , c o n t r a =0 , e v a l u a t i o n =1)M co = sympy . s i m p l i f y ( M co )M cont ra = M co . i n v ( method = ’GE’ )

U2 = sympy . F u n c t i o n ( ’ U2 ’ )U2 = U2 ( r 2 , ph i 2 , z 2 )

g rad2 = Grad ( U2 , Xi , c o o r d i n a t e = ’ s h i f t e d c y l i n d e r ’ , m e t r i c =M contra , e v a l u a t i o n =1)c u r l g r a d 2 = Cur l ( grad2 , Xi , c o o r d i n a t e = ’ s h i f t e d c y l i n d e r ’ , m e t r i c =M contra , e v a l u a t i o n =1)

In : g rad2Out :

10

Page 11: SymFields: An Open Source Symbolic Fields Analysis Tool

From the above result, we can find the analytical expression for the gradient in a non-orthogonal co-ordinate can be very long and complicated so that it could not be full placed in one line on the page.However, here we only need to verify whether the curl of the gradient under this shifted cylinder coor-dinate is zero. The result is:In : sympy . s i m p l i f y ( c u r l g r a d 2 [ 0 ] )In : sympy . s i m p l i f y ( c u r l g r a d 2 [ 1 ] )In : sympy . s i m p l i f y ( c u r l g r a d 2 [ 2 ] )Out :

We can find that all 3 components of the curl of gradient is 0.Then let’s check the divergence of curl.

A r2 = sympy . F u n c t i o n ( ’ A r2 ’ )A phi2 = sympy . F u n c t i o n ( ’ A phi2 ’ )A z2 = sympy . F u n c t i o n ( ’ A z2 ’ )A r2 = A r2 ( r 2 , ph i 2 , z 2 )A phi2 = A phi2 ( r 2 , ph i 2 , z 2 )A z2 = A z2 ( r 2 , ph i 2 , z 2 )A2 = [ A r2 , A phi2 , A z2 ]

c u r l 2 = Cur l ( A2 , Xi , c o o r d i n a t e = ’ s h i f t e d c y l i n d e r ’ , m e t r i c =M contra , e v a l u a t i o n =1)d i v c u r l 2 = Div ( c u r l 2 , Xi , c o o r d i n a t e = ’ s h i f t e d c y l i n d e r ’ , m e t r i c =M contra , e v a l u a t i o n =1)

In : sympy . s i m p l i f y ( c u r l 2 )Out :

We can also find the expression of curl in this α = π/3 shifted cylinder coordinate is also very longand complicated. However, we can also verify that the divergence of curl remains zero.In : sympy . s i m p l i f y ( d i v c u r l 2 )Out :

5 Summary

In this paper, we report the development of an open source symbolic calculation tool for vector fieldanalysis in Python. The SymFields module is constructed upon Python symbolic module sympy, whichcould only conduct scaler field analysis. Within SymFields module, the vector fields operation aredefined upon the metric tensor of a general curvilinear coordinates. Which means you can conductvector analysis for any curvilinear coordinates regardless whether it is orthogonal or not. Four orthogonal

11

Page 12: SymFields: An Open Source Symbolic Fields Analysis Tool

coordinates: Cartesian, Cylinder, Sphere and Toroidal are set at build-in coordinates. You can extend itto any other coordinates by providing a new metric tensor. In SymFields, the differential operators basedon metric tensor are normalized to real physical values, which means your can use real physical valueof the vector fields as inputs. Thus could greatly free the physicists from the tedious calculation undercomplicated coordinates.

References

[1] H. Qin, W. M. Tang, and G. Rewoldt. Symbolic vector analysis in plasma physics. ComputerPhysics Communications, 116(1):107 – 120, 1999.

[2] Introduction to Advanced Mathematics (printed in Chinese), University of Science and Technologyof China Press, by group author of Mathematics teaching and research team, 2008 Hefei, China.

[3] V. I. Piercey. Lame and metric coefficients for curvilinear coordinates in R3. Lecture notes, Univer-sity of Arizona.

[4] William Denis D’haeseleer, William Nicholas Guy Hitchon, James D. Callen, and J. Leon Shohet.Flux Coordinates and Magnetic Field Structure. Springer Berlin Heidelberg, 1991.

[5] Leonid P Lebedev, Michael J Cloud, and Victor A Eremeyev. Tensor Analysis with Applications inMechanics. WORLD SCIENTIFIC, 2010.

12