Пример #1
0
"""

import numpy as np
import matplotlib.pyplot as plt
    
"""
We create a grid with equidistant vectors and call the solver by providing  values for A (the material property) and the values at the four boundary with the markers 1 to 4.
"""

grid = pg.createGrid(x=np.linspace(-1.0, 1.0, 10), y=np.linspace(-1.0, 1.0, 10))

#material?

u = solvePoisson(grid, f=1.,
                 uBoundary=[grid.findBoundaryByMarker(1,5), 0.0],
                 verbose=True)

"""
.. error::

    can we calculate analytical solution?

The result is drawn with the function showMesh.    
"""

ax = showMesh(grid, data=u, filled=True, showLater=True, colorBar=True, orientation='vertical', label='P1 Solution $u$')[0]
drawMesh(ax, grid)

"""
.. image:: PLOT2RST.current_figure
Пример #2
0
def uDirichlet(b):
    '''
        Return a solution value for coordinate p.
    '''
    return 4.0

dirichletBC = [[1, 1.0], # left
               [grid.findBoundaryByMarker(2), 2.0], # right
               [grid.findBoundaryByMarker(3), lambda p: 3.0 + p[0]], # top
               [grid.findBoundaryByMarker(4), uDirichlet]] # bottom

"""
The BC are passed using the uBoundary keyword.
Note that showMesh returns the created figure axes ax while drawMesh plots on it and it can also be used as a class with plotting or decoration methods.
"""
u = solvePoisson(grid, f=1.,
                 uBoundary=dirichletBC)

ax = showMesh(grid, data=u, filled=True, colorBar=True,
              orientation='vertical', label='Solution $u$',
              levels=np.linspace(1.0, 4.0, 17), showLater=True)[0]

drawMesh(ax, grid)

ax.text( 0.0, 1.02, '$u=1$')
ax.text(-1.08, 0.0, '$u=2$', rotation='vertical')
ax.text( 0.0, -1.08, '$u=3+x$')
ax.text( 1.02, 0.0, '$u=4$', rotation='vertical')

ax.set_title('$\\nabla\cdot(1\\nabla u)=1$')

ax.set_xlim([-1.1, 1.1]) # some boundary for the text
Пример #3
0
#grid = grid.createH2()
grid = grid.createP2()

sourcePosA = [-5.0, -4.0]
sourcePosB = [ 5.0, -4.0]

neumannBC = [[1, mixedBC], #left boundary
             [2, mixedBC], #right boundary
             [4, mixedBC]] #bottom boundary

"""

"""
k = 1e-3
u = solvePoisson(grid, a=1, b=k*k, f=pointSource,
                 duBoundary=neumannBC,
                 userData={'sourcePos': sourcePosA, 'k': k},
                 verbose=True)

u -= solvePoisson(grid, a=1, b=k*k, f=pointSource,
                  duBoundary=neumannBC,
                  userData={'sourcePos': sourcePosB, 'k': k},
                  verbose=True)

#uAna = pg.RVector(map(lambda p__: uAnalytical(p__, sourcePosA, k), grid.positions()))
#uAna -= pg.RVector(map(lambda p__: uAnalytical(p__, sourcePosB, k), grid.positions()))

#err = (1.0 -u/uAna)*100.0

#print "error min max", min(err), max(err)

ax = showMesh(grid, data=u, filled=True, colorBar=True,
    nodes.append(poly.createNode(pg.RVector3(b)*0.1))

for i in range(len(nodes)):
    poly.createEdge(nodes[i], nodes[(i+1)%len(nodes)], 1)

mesh = createMesh(poly, quality=34, area=0.001, smooth=[0,10])
f = pg.RVector(mesh.cellCount(), 10)
a = pg.RVector(mesh.cellCount(), 0.1)

#Start FEM solution
swatch = pg.Stopwatch(True)

uDirichlet = [1, lambda p_: np.sin(np.arctan2(p_.center()[1],
                                              p_.center()[0]))/p_.center().abs()]

uFEM = solver.solvePoisson(mesh, a=a, f=f, uBoundary=uDirichlet)

print('FEM:', swatch.duration(True))

ax1, cbar = showMesh(mesh, data=uFEM,
                     nLevs=12, cMin=0, cMax=10, colorBar=True,
                     showLater=True)
drawMesh(ax1, mesh)


#print(min(u), max(u))
uAna = np.array(list(map(lambda p_: np.sin(np.arctan2(p_[1],
                                                      p_[0]))/p_.abs(),
                       mesh.positions())))

#drawStreamLines2(ax1, mesh, data=u)
for i in range(len(nodes)):
    poly.createEdge(nodes[i], nodes[(i + 1) % len(nodes)], 1)

mesh = createMesh(poly, quality=34, area=0.001, smooth=[0, 10])
f = pg.RVector(mesh.cellCount(), 10)
a = pg.RVector(mesh.cellCount(), 0.1)

#Start FEM solution
swatch = pg.Stopwatch(True)

uDirichlet = [
    1, lambda p_: np.sin(np.arctan2(p_.center()[1],
                                    p_.center()[0])) / p_.center().abs()
]

uFEM = solver.solvePoisson(mesh, a=a, f=f, uBoundary=uDirichlet)

print('FEM:', swatch.duration(True))

ax1, cbar = showMesh(mesh,
                     data=uFEM,
                     nLevs=12,
                     cMin=0,
                     cMax=10,
                     colorBar=True,
                     showLater=True)
drawMesh(ax1, mesh)

#print(min(u), max(u))
uAna = np.array(
    list(
ax1.plot(x, uI[timeProbeID], '.-', label='FD implicit')
ax2.plot(t, uI[:, spaceProbeID], '.-', label='FD implicit')

#ax1.plot(x, uFEM[timeProbeID], label='FE implicit')
#ax2.plot(t, uFEM[:,spaceProbeID], label='FE implicit')

uFVM = solveFiniteVolume(grid, a=tempLF, f=0.0, times=t, u0=u0,
                         uDirichlet=[0.0, 0.0])

print(len(uFVM), len(uFVM[0]))
ax1.plot(pg.x(grid.cellCenters()), uFVM[timeProbeID], '.-',
         label='FV implicit')
ax2.plot(t, uFVM[:, spaceProbeID], '.-', label='FV implicit')

uFE = solver.solvePoisson(grid, times=t, a=tempLF, theta=0.5, u0=u0,
                          uBoundary=dirichletBC)

ax1.plot(x, uFE[timeProbeID], label='FE Crank-Nicolsen')
ax2.plot(t, uFE[:, spaceProbeID], label='FE Crank-Nicolsen')

ax1.legend()
ax2.legend()


"""
.. image:: PLOT2RST.current_figure
    :scale: 75

"""
plt.show()
Пример #7
0
    solve.solve(b, ut)
            
    u[n, :] = ut
    
#u = solver.solvePoisson(grid, times=times, theta=1.0,
                 #u0=lambda r: np.sin(np.pi * r[0]),
                 #uBoundary=dirichletBC)
plt.plot(times, u[:, probeID], label='Implicit Euler')


"""


"""
u = solver.solvePoisson(grid, times=times, theta=0.5,
                 u0=lambda r: np.sin(np.pi * r[0]),
                 uBoundary=dirichletBC)
plt.plot(times, u[:, probeID], label='Crank-Nicolson')

"""

"""
plt.xlabel("t[s] at x = " + str(round(grid.node(probeID).pos()[0],2)))
plt.ylabel("u")
plt.ylim(0.0, 1.0)
plt.xlim(0.0, 0.5)
plt.legend()
plt.grid()

plt.show()
Пример #8
0
#for i in range( 4 ):
    #grid = grid.createH2()
    #grid.createNeighbourInfos()
    #u = solvePoisson( grid, f = 1., u0 = grid.findBoundaryByMarker( 1 ), verbose = True )
    
    #print grid.findCell( [1. / 3., 1. / 3.] ).pot( [1. / 3., 1. / 3.], u)

    ##grid.createNeighbourInfos( True )

from pygimli.viewer import showMesh
from pygimli.mplviewer import *
import pylab as P

grid = g.createGrid( np.linspace( -1, 1, 20 ), np.linspace( -1, 1, 20 ), np.linspace( -1, 1, 20 ) )
u = solvePoisson( grid, f = 1., uBoundary = [ grid.findBoundaryByMarker( 1 ), 0], verbose = True )

#ax = showMesh( grid, data = u, filled = True, showLater = True, colorBar = True, orientation = 'vertical', label = 'Solution er$u$' )[0]
#drawMesh( ax, grid )

"""
.. error::
    
    do we find an analytical solution for this example?
"""

for b in grid.boundaries():
    if b.marker() == 1:
        if b.norm()[0] == 1:
            b.setMarker( 2 )
        if b.norm()[1] == 1:
Пример #9
0
#grid = grid.createH2()
#grid.createNeighbourInfos()
#u = solvePoisson( grid, f = 1., u0 = grid.findBoundaryByMarker( 1 ), verbose = True )

#print grid.findCell( [1. / 3., 1. / 3.] ).pot( [1. / 3., 1. / 3.], u)

##grid.createNeighbourInfos( True )

from pygimli.viewer import showMesh
from pygimli.mplviewer import *
import pylab as P

grid = g.createGrid(np.linspace(-1, 1, 20), np.linspace(-1, 1, 20),
                    np.linspace(-1, 1, 20))
u = solvePoisson(grid,
                 f=1.,
                 uBoundary=[grid.findBoundaryByMarker(1), 0],
                 verbose=True)

#ax = showMesh( grid, data = u, filled = True, showLater = True, colorBar = True, orientation = 'vertical', label = 'Solution er$u$' )[0]
#drawMesh( ax, grid )
"""
.. error::
    
    do we find an analytical solution for this example?
"""

for b in grid.boundaries():
    if b.marker() == 1:
        if b.norm()[0] == 1:
            b.setMarker(2)
        if b.norm()[1] == 1: