Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to speed-up a code containing several symbolic integrations and derivations?

Hi,

After days of confusion now I have a sage code ready to be used. It consists of a set of iterative equations in a loop, at each step the equations containing a number of symbolic integration and differentiation act on a previous estimation of the unknowns to update them to a newer estimation. Sounds good up to this point. However, depending on the initial estimation that I introduce to the iterative loop the code may run fast (like when I give a constant or simply x as initial estimation) or too slow (like when I introduce sin(x) or other even simple polynomials). I understand that having such integrations and differentiations make the expressions rapidly grow large so that the computations would be time consuming but it is extremely slow, not one minute or 10 minutes or so, I just waited for some hours and sage did not answered anything new, and I doubt if the 100% cpu usage shown by the system's monitoring app is really spent on the calculations and sage can give the answer at the end.

As much as I read from documentations and elsewhere adding a %cython command at the beginning of the code can speed up the code but it gives me back an error e.g. of the form "undeclared name not builtin: sin". Does %cython command work also with symbolic calculations? Also I saw something about "fast_callable" function and sympyx, what about they, if they can help speed up such calculations where can I find a good intro toward them?

Best regards

How to speed-up a code containing several symbolic integrations and derivations?

Hi,

After days of confusion now I have a sage code ready to be used. It consists of a set of iterative equations in a loop, at each step the equations containing --containing a number of symbolic integration and differentiation differentiation-- act on a previous estimation of the unknowns to update them to a newer estimation. estimation.

Sounds good up to this point. However, depending on the initial estimation that I introduce to the iterative loop the code may run fast (like when I give a constant or simply x as initial estimation) or too slow (like when I introduce sin(x) or other even simple polynomials). I understand that having such integrations and differentiations make the expressions rapidly grow large so that the computations would be time consuming but it is extremely slow, not one minute or 10 minutes or so, I just waited for some hours and sage did not answered anything new, and I doubt if the 100% cpu usage shown by the system's monitoring app is really spent on the calculations and sage can give the answer at the end.

As much as I read from documentations and elsewhere adding a %cython command at the beginning of the code can speed up the code but it gives me back an error e.g. of the form "undeclared name not builtin: sin". Does %cython command work also with symbolic calculations? Also I saw something about "fast_callable" function and sympyx, what about they, if they can help speed up such calculations where can I find a good intro toward them?

Best regards

How to speed-up a code containing several symbolic integrations and derivations?

Hi,

After days of confusion now I have a sage code ready to be used. It consists of a set of iterative equations in a loop, at each step the equations --containing a number of symbolic integration and differentiation-- act on a previous estimation of the unknowns to update them to a newer estimation.

Sounds good up to this point. However, depending on the initial estimation that I introduce to the iterative loop the code may run fast (like when I give a constant or simply x as initial estimation) or too slow (like when I introduce sin(x) or other even simple polynomials). I understand that having such integrations and differentiations make the expressions rapidly grow large so that the computations would be time consuming but it is extremely slow, not one minute or 10 minutes or so, I just waited for some hours and sage did not answered anything new, and I doubt if the 100% cpu usage shown by the system's monitoring app is really spent on the calculations and sage can give the answer at the end.

As much as I read from documentations and elsewhere adding a %cython command at the beginning of the code can speed up the code but it gives me back an error e.g. of the form "undeclared name not builtin: sin". Does %cython command work also with symbolic calculations? Also I saw something about "fast_callable" function and sympyx, what about they, if they can help speed up such calculations where can I find a good intro toward them?

Best regards

EDIT.

Here is the code:

var('x,y,z,t, x1,y1,z1,t1, x2,y2,z2,t2, x3,y3,z3,t3') 

var('N, Re, x_B1,x_B2, y_B1,y_B2, z_B1,z_B2, T, p0,v0')
N=5         # N: number of iterations
            # or N=int(input("How many iteration do you need for your Adomian Decomposition code to run? "))
Re=1000000  # Re: global Reynolds number
x_B1=-1; x_B2=1; y_B1=-1; y_B2=1; z_B1=-1; z_B2=1       # the non-dimensional boundaries of the computational region!
T=10        # T: the ending non-dimensional time of computations
# p0, v0: used in definition of initial condition for the unknown functions

assume(x_B1<=x, 0<t)     # needed for our integrations from X_B1 to x and from 0 to t !


#------------------------------------------------
# the following lists are defined for, 1st, easier addressing and, 2nd, more compact importing
# of terms like "sum_j u_j du_i/dx_j" as otherwise x_j was not known to the code
var('q')                # q is a very dummy variable to fill the zeroth place in all my lists to avoid confusion while coding, instead of always using i-1 or j+1 as indices!
R0=[q,x,y,z,t]          # Only R0 needs a zeroth element of q
R1=[x1,y1,z1,t1]
R2=[x2,y2,z2,t2]
R3=[x3,y3,z3,t3]


#--------------------------
# empty lists that will be filled with functions defined in the loop below
phi0=[q]
phi1=[q]
phi2=[q]
#-----------------
for i in range(1,5):        # i = 1,2,3,4  / indices 1,2,3 are reserved for velocity components and 4 for pressure
    phi0.append([])
    phi1.append([])
    phi2.append([])

    for n in range(N+1):    # N+1 since range(N+1)=(0,..,N)      # here n=0 is required so I don't increase it to 1 !
        phi0[i].append(function('phi0_%s_%s' %(i,n), x,y,z,t,        latex_name='\phi_%s^{0\,(%s)}' %(i,n)))
        phi1[i].append(function('phi1_%s_%s' %(i,n), x,y,z,t,*R1,    latex_name='\phi_%s^{1\,(%s)}' %(i,n)))
        phi2[i].append(function('phi2_%s_%s' %(i,n), x,y,z,t,*R1+R2, latex_name='\phi_%s^{2\,(%s)}' %(i,n)))


##########################################################
####################\the initial estimations/####################

# let define the nonzero values for initial estimates of phi1's components
p0=1; v0=1         # v0 stans for vx0=vy0=vz0

# specifying phi0[i][0], phi1[i][0], phi2[i][0] as the initial estimates (guesses), order 0 approximation
for i in range(1,4):    # which means i=1,2,3, for the velocity components
    phi0[i][0]=0*x      #0  
    phi1[i][0]=0*x+v0*sin(x)     #v0
    phi2[i][0]=0*x      #0
phi0[4][0]=0*x          #0
phi1[4][0]=0*x+p0+(x-x_B1)       #p0
phi2[4][0]=0*x          #0


################################################################
#######\defining the differential and integral operators in the equations/#######

var('td,xd1,xd2')   # dummy variables

g = lambda i,f: diff(f,R0[i]) 

It = lambda f: integral(f(t=td),td,0,t)     #or use: ,algorithm='mathematica_free'
                                                                   #or use: ,algorithm='sympy' 
Ixx = lambda f: integral(integral(f(x=xd1),xd1,x_B1,xd2),xd2,x_B1,x)

DR1 = lambda f: -1/Re*(diff(f,x,2)+diff(f,y,2)+diff(f,z,2))
DR2 = lambda f: diff(f,y,2)+diff(f,z,2)

IntR1 = lambda f:  integral(integral(integral(integral(f,x1,x_B1,x_B2),y1,y_B1,y_B2),z1,z_B1,z_B2),t1,0,T)
IntR2 = lambda f:  integral(integral(integral(integral(f,x2,x_B1,x_B2),y2,y_B1,y_B2),z2,z_B1,z_B2),t2,0,T)
IntR3 = lambda f:  integral(integral(integral(integral(f,x3,x_B1,x_B2),y3,y_B1,y_B2),z3,z_B1,z_B2),t3,0,T)



#####################################################
###########\the iterative equations being imported/###########

for n in range(N):       # note that in equations I have n+1 , so it couldn't be N+1 !
    for i in range(1,4):   # pressure (i=4) has different equations, so would be denoted individually
        phi0[i][n+1] = phi0[i][0] - It(DR1(phi0[i][n])) - It(g(i,phi0[4][n]))                                    \
                                 - It( sum( phi0[j][n]*g(j,phi0[i][n]) + IntR1(phi1[j][n]*g(j,phi1[i][n])) +     \
                                 2*IntR2(IntR1(phi2[j][n]*g(j,phi2[i][n]))) for j in range(1,4)) )
        show(phi0[i][n+1])
        phi1[i][n+1] = phi1[i][0] - It(DR1(phi1[i][n])) - It(g(i,phi1[4][n]))                                    \
                                 - It(  sum( phi0[j][n]*g(j,phi1[i][n]) +  phi1[j][n]*g(j,phi0[i][n]) +          \
                                 2*IntR2(phi1[j][n](x1=x2,y1=y2,z1=z2,t1=t2)*g(j,phi2[i][n]) +                   \
                                 phi2[j][n]*g(j,phi1[i][n](x1=x2,y1=y2,z1=z2,t1=t2))) for j in range(1,4)) )
        show(phi1[i][n+1])
        phi2[i][n+1] = phi2[i][0] - It(DR1(phi2[i][n])) - It(g(i,phi2[4][n]))                                    \
                                 - It(  sum( phi0[j][n]*g(j,phi2[i][n]) +  phi2[j][n]*g(j,phi0[i][n])            \
                                 + 0.5*( phi1[j][n]*g(j,phi1[i][n](x1=x2,y1=y2,z1=z2,t1=t2)) +                   \
                                 phi1[j][n](x1=x2,y1=y2,z1=z2,t1=t2)*g(j,phi1[i][n]) )                           \
                                 + 2*IntR3( phi2[j][n](x2=x3,y2=y3,z2=z3,t2=t3)                                  \
                                 *g(j,phi2[i][n](x1=x2,y1=y2,z1=z2,t1=t2, x2=x3,y2=y3,z2=z3,t2=t3))              \
                                 + phi2[j][n](x1=x2,y1=y2,z1=z2,t1=t2, x2=x3,y2=y3,z2=z3,t2=t3)                  \
                                 *g(j,phi2[i][n](x2=x3,y2=y3,z2=z3,t2=t3)) ) for j in range(1,4)) )
        show(phi2[i][n+1])
    phi0[4][n+1] = phi0[4][0] - Ixx(DR2(phi0[4][n]))                                                             \
                              - Ixx( sum( g(i,phi0[j][n+1])*g(j,phi0[i][n+1])                                    \
                              + IntR1(g(i,phi1[j][n+1])*g(j,phi1[i][n+1]))                                       \
                              + 2*IntR2(IntR1(g(i,phi2[j][n+1])*g(j,phi2[i][n+1])))                              \
                              for i in range (1,4) for j in range(1,4) ) )
    show(phi0[4][n+1])
    phi1[4][n+1] = phi1[4][0] - Ixx(DR2(phi1[4][n]))                                                             \
                              - Ixx( sum(   2*g(j,phi0[i][n+1])*g(i,phi1[j][n+1])                                \
                              + 4*IntR2(g(j,phi1[i][n+1](x1=x2,y1=y2,z1=z2,t1=t2))*g(i,phi2[j][n+1]))            \
                              for i in range (1,4) for j in range(1,4) ) )
    show(phi1[4][n+1])
    phi2[4][n+1] = phi2[4][0] - Ixx(DR2(phi2[4][n]))                                                             \
                              - Ixx( sum(   2*g(j,phi0[i][n+1])*g(i,phi2[j][n+1])                                \
                              + g(j,phi1[i][n+1])*g(i,phi1[j][n+1](x1=x2,y1=y2,z1=z2,t1=t2))                     \
                              + 4*IntR3( g(j,phi2[i][n+1](x2=x3,y2=y3,z2=z3,t2=t3))                              \
                              *g(i,phi2[j][n+1](x1=x2,y1=y2,z1=z2,t1=t2, x2=x3,y2=y3,z2=z3,t2=t3)) )             \
                              for i in range (1,4) for j in range(1,4) ) )
    show(phi2[4][n+1])

How to speed-up a code containing several symbolic integrations and derivations?derivatives?

Hi,

After days of confusion now I have a sage code ready to be used. It consists of a set of iterative equations in a loop, at each step the equations --containing a number of symbolic integration and differentiation-- act on a previous estimation of the unknowns to update them to a newer estimation.

Sounds good up to this point. However, depending on the initial estimation that I introduce to the iterative loop the code may run fast (like when I give a constant or simply x as initial estimation) or too slow (like when I introduce sin(x) or other even simple polynomials). I understand that having such integrations and differentiations make the expressions rapidly grow large so that the computations would be time consuming but it is extremely slow, not one minute or 10 minutes or so, I just waited for some hours and sage did not answered anything new, and I doubt if the 100% cpu usage shown by the system's monitoring app is really spent on the calculations and sage can give the answer at the end.

As much as I read from documentations and elsewhere adding a %cython command at the beginning of the code can speed up the code but it gives me back an error e.g. of the form "undeclared name not builtin: sin". Does %cython command work also with symbolic calculations? Also I saw something about "fast_callable" function and sympyx, what about they, if they can help speed up such calculations where can I find a good intro toward them?

Best regards

EDIT.

Here is the code:

var('x,y,z,t, x1,y1,z1,t1, x2,y2,z2,t2, x3,y3,z3,t3') 

var('N, Re, x_B1,x_B2, y_B1,y_B2, z_B1,z_B2, T, p0,v0')
N=5         # N: number of iterations
            # or N=int(input("How many iteration do you need for your Adomian Decomposition code to run? "))
Re=1000000  # Re: global Reynolds number
x_B1=-1; x_B2=1; y_B1=-1; y_B2=1; z_B1=-1; z_B2=1       # the non-dimensional boundaries of the computational region!
T=10        # T: the ending non-dimensional time of computations
# p0, v0: used in definition of initial condition for the unknown functions

assume(x_B1<=x, 0<t)     # needed for our integrations from X_B1 to x and from 0 to t !


#------------------------------------------------
# the following lists are defined for, 1st, easier addressing and, 2nd, more compact importing
# of terms like "sum_j u_j du_i/dx_j" as otherwise x_j was not known to the code
var('q')                # q is a very dummy variable to fill the zeroth place in all my lists to avoid confusion while coding, instead of always using i-1 or j+1 as indices!
R0=[q,x,y,z,t]          # Only R0 needs a zeroth element of q
R1=[x1,y1,z1,t1]
R2=[x2,y2,z2,t2]
R3=[x3,y3,z3,t3]


#--------------------------
# empty lists that will be filled with functions defined in the loop below
phi0=[q]
phi1=[q]
phi2=[q]
#-----------------
for i in range(1,5):        # i = 1,2,3,4  / indices 1,2,3 are reserved for velocity components and 4 for pressure
    phi0.append([])
    phi1.append([])
    phi2.append([])

    for n in range(N+1):    # N+1 since range(N+1)=(0,..,N)      # here n=0 is required so I don't increase it to 1 !
        phi0[i].append(function('phi0_%s_%s' %(i,n), x,y,z,t,        latex_name='\phi_%s^{0\,(%s)}' %(i,n)))
        phi1[i].append(function('phi1_%s_%s' %(i,n), x,y,z,t,*R1,    latex_name='\phi_%s^{1\,(%s)}' %(i,n)))
        phi2[i].append(function('phi2_%s_%s' %(i,n), x,y,z,t,*R1+R2, latex_name='\phi_%s^{2\,(%s)}' %(i,n)))


##########################################################
####################\the initial estimations/####################

# let define the nonzero values for initial estimates of phi1's components
p0=1; v0=1         # v0 stans for vx0=vy0=vz0

# specifying phi0[i][0], phi1[i][0], phi2[i][0] as the initial estimates (guesses), order 0 approximation
for i in range(1,4):    # which means i=1,2,3, for the velocity components
    phi0[i][0]=0*x      #0  
    phi1[i][0]=0*x+v0*sin(x)     #v0
    phi2[i][0]=0*x      #0
phi0[4][0]=0*x          #0
phi1[4][0]=0*x+p0+(x-x_B1)       #p0
phi2[4][0]=0*x          #0


################################################################
#######\defining the differential and integral operators in the equations/#######

var('td,xd1,xd2')   # dummy variables

g = lambda i,f: diff(f,R0[i]) 

It = lambda f: integral(f(t=td),td,0,t)     #or use: ,algorithm='mathematica_free'
                                                                   #or use: ,algorithm='sympy' 
Ixx = lambda f: integral(integral(f(x=xd1),xd1,x_B1,xd2),xd2,x_B1,x)

DR1 = lambda f: -1/Re*(diff(f,x,2)+diff(f,y,2)+diff(f,z,2))
DR2 = lambda f: diff(f,y,2)+diff(f,z,2)

IntR1 = lambda f:  integral(integral(integral(integral(f,x1,x_B1,x_B2),y1,y_B1,y_B2),z1,z_B1,z_B2),t1,0,T)
IntR2 = lambda f:  integral(integral(integral(integral(f,x2,x_B1,x_B2),y2,y_B1,y_B2),z2,z_B1,z_B2),t2,0,T)
IntR3 = lambda f:  integral(integral(integral(integral(f,x3,x_B1,x_B2),y3,y_B1,y_B2),z3,z_B1,z_B2),t3,0,T)



#####################################################
###########\the iterative equations being imported/###########

for n in range(N):       # note that in equations I have n+1 , so it couldn't be N+1 !
    for i in range(1,4):   # pressure (i=4) has different equations, so would be denoted individually
        phi0[i][n+1] = phi0[i][0] - It(DR1(phi0[i][n])) - It(g(i,phi0[4][n]))                                    \
                                 - It( sum( phi0[j][n]*g(j,phi0[i][n]) + IntR1(phi1[j][n]*g(j,phi1[i][n])) +     \
                                 2*IntR2(IntR1(phi2[j][n]*g(j,phi2[i][n]))) for j in range(1,4)) )
        show(phi0[i][n+1])
        phi1[i][n+1] = phi1[i][0] - It(DR1(phi1[i][n])) - It(g(i,phi1[4][n]))                                    \
                                 - It(  sum( phi0[j][n]*g(j,phi1[i][n]) +  phi1[j][n]*g(j,phi0[i][n]) +          \
                                 2*IntR2(phi1[j][n](x1=x2,y1=y2,z1=z2,t1=t2)*g(j,phi2[i][n]) +                   \
                                 phi2[j][n]*g(j,phi1[i][n](x1=x2,y1=y2,z1=z2,t1=t2))) for j in range(1,4)) )
        show(phi1[i][n+1])
        phi2[i][n+1] = phi2[i][0] - It(DR1(phi2[i][n])) - It(g(i,phi2[4][n]))                                    \
                                 - It(  sum( phi0[j][n]*g(j,phi2[i][n]) +  phi2[j][n]*g(j,phi0[i][n])            \
                                 + 0.5*( phi1[j][n]*g(j,phi1[i][n](x1=x2,y1=y2,z1=z2,t1=t2)) +                   \
                                 phi1[j][n](x1=x2,y1=y2,z1=z2,t1=t2)*g(j,phi1[i][n]) )                           \
                                 + 2*IntR3( phi2[j][n](x2=x3,y2=y3,z2=z3,t2=t3)                                  \
                                 *g(j,phi2[i][n](x1=x2,y1=y2,z1=z2,t1=t2, x2=x3,y2=y3,z2=z3,t2=t3))              \
                                 + phi2[j][n](x1=x2,y1=y2,z1=z2,t1=t2, x2=x3,y2=y3,z2=z3,t2=t3)                  \
                                 *g(j,phi2[i][n](x2=x3,y2=y3,z2=z3,t2=t3)) ) for j in range(1,4)) )
        show(phi2[i][n+1])
    phi0[4][n+1] = phi0[4][0] - Ixx(DR2(phi0[4][n]))                                                             \
                              - Ixx( sum( g(i,phi0[j][n+1])*g(j,phi0[i][n+1])                                    \
                              + IntR1(g(i,phi1[j][n+1])*g(j,phi1[i][n+1]))                                       \
                              + 2*IntR2(IntR1(g(i,phi2[j][n+1])*g(j,phi2[i][n+1])))                              \
                              for i in range (1,4) for j in range(1,4) ) )
    show(phi0[4][n+1])
    phi1[4][n+1] = phi1[4][0] - Ixx(DR2(phi1[4][n]))                                                             \
                              - Ixx( sum(   2*g(j,phi0[i][n+1])*g(i,phi1[j][n+1])                                \
                              + 4*IntR2(g(j,phi1[i][n+1](x1=x2,y1=y2,z1=z2,t1=t2))*g(i,phi2[j][n+1]))            \
                              for i in range (1,4) for j in range(1,4) ) )
    show(phi1[4][n+1])
    phi2[4][n+1] = phi2[4][0] - Ixx(DR2(phi2[4][n]))                                                             \
                              - Ixx( sum(   2*g(j,phi0[i][n+1])*g(i,phi2[j][n+1])                                \
                              + g(j,phi1[i][n+1])*g(i,phi1[j][n+1](x1=x2,y1=y2,z1=z2,t1=t2))                     \
                              + 4*IntR3( g(j,phi2[i][n+1](x2=x3,y2=y3,z2=z3,t2=t3))                              \
                              *g(i,phi2[j][n+1](x1=x2,y1=y2,z1=z2,t1=t2, x2=x3,y2=y3,z2=z3,t2=t3)) )             \
                              for i in range (1,4) for j in range(1,4) ) )
    show(phi2[4][n+1])

How to speed-up a code containing several symbolic integrations and derivatives?

Hi,

After days of confusion now I have a sage code ready to be used. It consists of a set of iterative equations in a loop, at each step the equations --containing a number of symbolic integration and differentiation-- act on a previous estimation of the unknowns to update them to a newer estimation.

Sounds good up to this point. However, depending on the initial estimation that I introduce to the iterative loop the code may run fast (like when I give a constant or simply x as initial estimation) or too slow (like when I introduce sin(x) or other even simple polynomials). I understand that having such integrations and differentiations make the expressions rapidly grow large so that the computations would be time consuming but it is extremely slow, not one minute or 10 minutes or so, I just waited for some hours and sage did not answered anything new, and I doubt if the 100% cpu usage shown by the system's monitoring app is really spent on the calculations and sage can give the answer at the end.

As much as I read from documentations and elsewhere adding a %cython command at the beginning of the code can speed up the code but it gives me back an error e.g. of the form "undeclared name not builtin: sin". Does %cython command work also with symbolic calculations? Also I saw something about "fast_callable" function and sympyx, what about they, if they can help speed up such calculations where can I find a good intro toward them?

Best regards

EDIT.

Here is the code:

var('x,y,z,t, x1,y1,z1,t1, x2,y2,z2,t2, x3,y3,z3,t3') 

var('N, Re, x_B1,x_B2, y_B1,y_B2, z_B1,z_B2, T, p0,v0')
N=5         # N: number of iterations
            # or N=int(input("How many iteration do you need for your Adomian Decomposition code to run? "))
Re=1000000  # Re: global Reynolds number
x_B1=-1; x_B2=1; y_B1=-1; y_B2=1; z_B1=-1; z_B2=1       # the non-dimensional boundaries of the computational region!
T=10        # T: the ending non-dimensional time of computations
# p0, v0: used in definition of initial condition for the unknown functions

assume(x_B1<=x, 0<t)     # needed for our integrations from X_B1 to x and from 0 to t !


#------------------------------------------------
# the following lists are defined for, 1st, easier addressing and, 2nd, more compact importing
# of terms like "sum_j u_j du_i/dx_j" as otherwise x_j was not known to the code
var('q')                # q is a very dummy variable to fill the zeroth place in all my lists to avoid confusion while coding, instead of always using i-1 or j+1 as indices!
R0=[q,x,y,z,t]          # Only R0 needs a zeroth element of q
R1=[x1,y1,z1,t1]
R2=[x2,y2,z2,t2]
R3=[x3,y3,z3,t3]


#--------------------------
# empty lists that will be filled with functions defined in the loop below
phi0=[q]
phi1=[q]
phi2=[q]
#-----------------
for i in range(1,5):        # i = 1,2,3,4  / indices 1,2,3 are reserved for velocity components and 4 for pressure
    phi0.append([])
    phi1.append([])
    phi2.append([])

    for n in range(N+1):    # N+1 since range(N+1)=(0,..,N)      # here n=0 is required so I don't increase it to 1 !
        phi0[i].append(function('phi0_%s_%s' %(i,n), x,y,z,t,        latex_name='\phi_%s^{0\,(%s)}' %(i,n)))
        phi1[i].append(function('phi1_%s_%s' %(i,n), x,y,z,t,*R1,    latex_name='\phi_%s^{1\,(%s)}' %(i,n)))
        phi2[i].append(function('phi2_%s_%s' %(i,n), x,y,z,t,*R1+R2, latex_name='\phi_%s^{2\,(%s)}' %(i,n)))


##########################################################
####################\the initial estimations/####################

# let define the nonzero values for initial estimates of phi1's components
p0=1; v0=1         # v0 stans for vx0=vy0=vz0

# specifying phi0[i][0], phi1[i][0], phi2[i][0] as the initial estimates (guesses), order 0 approximation
for i in range(1,4):    # which means i=1,2,3, for the velocity components
    phi0[i][0]=0*x      #0  
    phi1[i][0]=0*x+v0*sin(x)     #v0
    phi2[i][0]=0*x      #0
phi0[4][0]=0*x          #0
phi1[4][0]=0*x+p0+(x-x_B1)       #p0
phi2[4][0]=0*x          #0


################################################################
#######\defining the differential and integral operators in the equations/#######

var('td,xd1,xd2')   # dummy variables

g = lambda i,f: diff(f,R0[i]) 

It = lambda f: integral(f(t=td),td,0,t)     #or use: ,algorithm='mathematica_free'
                                                                   #or use: ,algorithm='sympy' 
Ixx = lambda f: integral(integral(f(x=xd1),xd1,x_B1,xd2),xd2,x_B1,x)

DR1 = lambda f: -1/Re*(diff(f,x,2)+diff(f,y,2)+diff(f,z,2))
DR2 = lambda f: diff(f,y,2)+diff(f,z,2)

IntR1 = lambda f:  integral(integral(integral(integral(f,x1,x_B1,x_B2),y1,y_B1,y_B2),z1,z_B1,z_B2),t1,0,T)
IntR2 = lambda f:  integral(integral(integral(integral(f,x2,x_B1,x_B2),y2,y_B1,y_B2),z2,z_B1,z_B2),t2,0,T)
IntR3 = lambda f:  integral(integral(integral(integral(f,x3,x_B1,x_B2),y3,y_B1,y_B2),z3,z_B1,z_B2),t3,0,T)



#####################################################
###########\the iterative equations being imported/###########

for n in range(N):       # note that in equations I have n+1 , so it couldn't be N+1 !
    for i in range(1,4):   # pressure (i=4) has different equations, so would be denoted individually
        phi0[i][n+1] = phi0[i][0] - It(DR1(phi0[i][n])) - It(g(i,phi0[4][n]))                                    \
                                 - It( sum( phi0[j][n]*g(j,phi0[i][n]) + IntR1(phi1[j][n]*g(j,phi1[i][n])) +     \
                                 2*IntR2(IntR1(phi2[j][n]*g(j,phi2[i][n]))) for j in range(1,4)) )
        show(phi0[i][n+1])
        phi1[i][n+1] = phi1[i][0] - It(DR1(phi1[i][n])) - It(g(i,phi1[4][n]))                                    \
                                 - It(  sum( phi0[j][n]*g(j,phi1[i][n]) +  phi1[j][n]*g(j,phi0[i][n]) +          \
                                 2*IntR2(phi1[j][n](x1=x2,y1=y2,z1=z2,t1=t2)*g(j,phi2[i][n]) +                   \
                                 phi2[j][n]*g(j,phi1[i][n](x1=x2,y1=y2,z1=z2,t1=t2))) for j in range(1,4)) )
        show(phi1[i][n+1])
        phi2[i][n+1] = phi2[i][0] - It(DR1(phi2[i][n])) - It(g(i,phi2[4][n]))                                    \
                                 - It(  sum( phi0[j][n]*g(j,phi2[i][n]) +  phi2[j][n]*g(j,phi0[i][n])            \
                                 + 0.5*( phi1[j][n]*g(j,phi1[i][n](x1=x2,y1=y2,z1=z2,t1=t2)) +                   \
                                 phi1[j][n](x1=x2,y1=y2,z1=z2,t1=t2)*g(j,phi1[i][n]) )                           \
                                 + 2*IntR3( phi2[j][n](x2=x3,y2=y3,z2=z3,t2=t3)                                  \
                                 *g(j,phi2[i][n](x1=x2,y1=y2,z1=z2,t1=t2, x2=x3,y2=y3,z2=z3,t2=t3))              \
                                 + phi2[j][n](x1=x2,y1=y2,z1=z2,t1=t2, x2=x3,y2=y3,z2=z3,t2=t3)                  \
                                 *g(j,phi2[i][n](x2=x3,y2=y3,z2=z3,t2=t3)) ) for j in range(1,4)) )
        show(phi2[i][n+1])
    phi0[4][n+1] = phi0[4][0] - Ixx(DR2(phi0[4][n]))                                                             \
                              - Ixx( sum( g(i,phi0[j][n+1])*g(j,phi0[i][n+1])                                    \
                              + IntR1(g(i,phi1[j][n+1])*g(j,phi1[i][n+1]))                                       \
                              + 2*IntR2(IntR1(g(i,phi2[j][n+1])*g(j,phi2[i][n+1])))                              \
                              for i in range (1,4) for j in range(1,4) ) )
    show(phi0[4][n+1])
    phi1[4][n+1] = phi1[4][0] - Ixx(DR2(phi1[4][n]))                                                             \
                              - Ixx( sum(   2*g(j,phi0[i][n+1])*g(i,phi1[j][n+1])                                \
                              + 4*IntR2(g(j,phi1[i][n+1](x1=x2,y1=y2,z1=z2,t1=t2))*g(i,phi2[j][n+1]))            \
                              for i in range (1,4) for j in range(1,4) ) )
    show(phi1[4][n+1])
    phi2[4][n+1] = phi2[4][0] - Ixx(DR2(phi2[4][n]))                                                             \
                              - Ixx( sum(   2*g(j,phi0[i][n+1])*g(i,phi2[j][n+1])                                \
                              + g(j,phi1[i][n+1])*g(i,phi1[j][n+1](x1=x2,y1=y2,z1=z2,t1=t2))                     \
                              + 4*IntR3( g(j,phi2[i][n+1](x2=x3,y2=y3,z2=z3,t2=t3))                              \
                              *g(i,phi2[j][n+1](x1=x2,y1=y2,z1=z2,t1=t2, x2=x3,y2=y3,z2=z3,t2=t3)) )             \
                              for i in range (1,4) for j in range(1,4) ) )
    show(phi2[4][n+1])