Esempio n. 1
0
 def initialize_direct_space_solver(self, time_level, delta_time, mg_level=None):
     if mg_level is None:
         mg_level = self._mg_level
     assert_is_instance(mg_level, IMultigridLevel, descriptor="Multigrid Level", checking_obj=self)
     _stencil = Stencil(self.mg_stencil(delta_time, mg_level.h))
     # LOG.debug("Stencil for dt=%f, h=%f: %s" % (delta_time, mg_level.h, _stencil.arr))
     time_level = str(time_level)
     delta_time = str(delta_time)
     if self._direct_solvers.get(time_level) is None:
         # LOG.debug("Initializing Solvers for Time Level '%s'" % time_level)
         self._direct_solvers[time_level] = {}
     # LOG.debug("  Initializing Solver for Time Level '%s' and Delta Node '%s'" % (time_level, delta_time))
     # LOG.debug("    shape: %s" % (mg_level.mid.shape))
     self._direct_solvers[time_level][delta_time] = {
         'mg_level': mg_level,
         'stencil': _stencil,
         'solver': _stencil.generate_direct_solver(mg_level.mid.shape)
     }
Esempio n. 2
0
from pypint.plugins.multigrid.multigrid_problem import MultigridProblem
from pypint.plugins.multigrid.multigrid_level_provider import MultiGridLevelProvider
from pypint.plugins.multigrid.multigrid_solution import MultiGridSolution
from pypint.plugins.multigrid.level import MultigridLevel1D
from pypint.plugins.multigrid.multigrid_smoother import SplitSmoother, DirectSolverSmoother, WeightedJacobiSmoother
from pypint.utilities import assert_is_callable, assert_is_instance, assert_condition
from pypint.plugins.multigrid.stencil import Stencil
from pypint.plugins.multigrid.interpolation import InterpolationByStencilListIn1D, InterpolationByStencilForLevels, InterpolationByStencilForLevelsClassical
from pypint.plugins.multigrid.restriction import RestrictionStencilPure, RestrictionByStencilForLevels, RestrictionByStencilForLevelsClassical
from operator import iadd,add
import networkx as nx

if __name__ == '__main__':
    print("Lets solve, you guessed it, the heat equation")
    # heat equation needs a stencil
    laplace_stencil = Stencil(np.asarray([1, -2, 1]), None, 2)
    # test stencil to some extend
    print("===== Stencil tests =====")
    print("stencil.b :", laplace_stencil.b)
    print("stencil.positions :", laplace_stencil.positions)

    for i in laplace_stencil.positions:
        print(laplace_stencil.arr[i])

    print("stencil.relative_position :", laplace_stencil.relative_positions)
    print("stencil.relative_position_woc :", laplace_stencil.relative_positions_woc)
    # geometry is a 1 dimensional line
    geo = np.asarray([[0, 1]])
    print(geo.shape)
    # the boundary conditions, in this case dirichlet boundary conditions
    boundary_type = ["dirichlet"]*2
Esempio n. 3
0
    """
    def __init__(self, stencil_form, mg_problem, numb_levels=3, coarse_level_shape=(5,)):
        pass



if __name__ == '__main__':
    # check if level2d is working properly
    # but first one has to define a proper level
    # and for this one needs a useful stencil

    np.set_printoptions(precision=4, edgeitems=4, threshold=10)

    print("===== Stencil =====")
    laplace_array = np.asarray([[0.0, 1.0, 0.0], [1.0, -4.0, 1.0], [0.0, 1.0, 0.0]])
    laplace_stencil = Stencil(np.asarray([[0, 1, 0], [1, -4, 1], [0, 1, 0]]), None, 2)
    print("stencil.arr\n", laplace_stencil.arr)
    print("stencil.reversed_arr\n", laplace_stencil.reversed_arr)
    print("stencil.b \n", laplace_stencil.b)
    print("stencil.positions \n", laplace_stencil.positions)
    print("stencil.center \n", laplace_stencil.center)
    print("===== MgProblem =====")
    # define geometry
    geo = np.asarray([[0, 1], [0, 1]])
    # the boundary conditions, in this case dirichlet boundary conditions
    boundary_type = ["dirichlet"]*2
    # east_f = lambda x: 2.0
    # west_f = lambda x: 8.0
    # north_f = lambda x: 1.0
    # south_f = lambda x: 4.0
    east_f = lambda x: np.sin(x[1]*np.pi)
Esempio n. 4
0
    omega = 1.0
    l_plus = np.asarray([0, -2.0/omega, 0])
    l_minus = np.asarray([1.0, -2.0*(1.0 - 1.0/omega), 1.0])

    top_jacobi_smoother = SplitSmoother(l_plus / top_level.h**2,
                                        l_minus / top_level.h**2,
                                        top_level)
    mid_jacobi_smoother = SplitSmoother(l_plus / mid_level.h**2,
                                        l_minus / mid_level.h**2,
                                        mid_level)
    low_jacobi_smoother = SplitSmoother(l_plus / low_level.h**2,
                                        l_minus / low_level.h**2,
                                        low_level)


    top_stencil = Stencil(laplace_array / top_level.h**2)
    mid_stencil = Stencil(laplace_array / mid_level.h**2)
    low_stencil = Stencil(laplace_array / low_level.h**2)

    print("jacobi minus: %s" % top_stencil.l_minus_jacobi(1.))
    print("jacobi plus: %s" % top_stencil.l_plus_jacobi(1.))

    low_direct_smoother = DirectSolverSmoother(low_stencil, low_level)
    center = np.asarray([0])

    # restriction
    rst_stencil = Stencil(np.asarray([0.25, 0.5, 0.25]))
    rst_top_to_mid = RestrictionByStencilForLevelsClassical(top_level, mid_level, rst_stencil)
    rst_mid_to_low = RestrictionByStencilForLevelsClassical(mid_level, low_level, rst_stencil)

    # ipl
Esempio n. 5
0
    top_level = MultigridLevel2D((259, 259), mg_problem=mg_problem,
                                 max_borders=borders, role="FL")



    mid_level = MultigridLevel2D((129, 129), mg_problem=mg_problem,
                                 max_borders=borders, role="ML")

    low_level = MultigridLevel2D((64, 64), mg_problem=mg_problem,
                                 max_borders=borders, role="CL")
    mg_problem.fill_rhs(top_level)
    top_level.pad()


    # define the different stencils
    top_stencil = Stencil(laplace_array/top_level.h[0]**2, None, 2)
    mid_stencil = Stencil(laplace_array/mid_level.h[0]**2, None, 2)
    low_stencil = Stencil(laplace_array/low_level.h[0]**2, None, 2)
    top_stencil.modify_rhs(top_level)

    omega = 2.0/3.0
    l_plus = np.asarray([[0, 0, 0],
                         [0, -4.0/omega, 0],
                         [0, 0, 0]])
    l_minus = np.asarray([[0, 1.0, 0], [1.0, -4.0*(1.0 - 1.0/omega), 1.0], [0., 1., 0.]])

    # define the different smoothers on each level, works just for symmetric grids
    top_jacobi_smoother = SplitSmoother(l_plus / top_level.h[0]**2,
                                        l_minus / top_level.h[0]**2,
                                        top_level)
    mid_jacobi_smoother = SplitSmoother(l_plus / mid_level.h[0]**2,