<commit_before>"""
This multigrid solver is build from multigrid/MG.py and implements
a variable coefficient solver for an equation of the form:

div { eta grad phi } = f

where eta is defined on the same grid as phi.

A cell-centered discretization is used throughout.
"""

import multigrid.MG as MG
import numpy
import sys

numpy.set_printoptions(precision=3, linewidth=128)


class _EdgeCoeffs:
    """
    a simple container class to hold the edge-centered coefficients
    """
    def __init__(self, g, eta, empty=False):

        self.grid = g

        if not empty:
            eta_x = g.scratch_array()
            eta_y = g.scratch_array()

            # the eta's are defined on the interfaces, so 
            # eta_x[i,j] will be eta_{i-1/2,j} and 
            # eta_y[i,j] will be eta_{i,j-1/2}

            eta_x[g.ilo:g.ihi+2,g.jlo:g.jhi+1] = \
                0.5*(eta[g.ilo-1:g.ihi+1,g.jlo:g.jhi+1] +
                     eta[g.ilo  :g.ihi+2,g.jlo:g.jhi+1])

            eta_y[g.ilo:g.ihi+1,g.jlo:g.jhi+2] = \
                0.5*(eta[g.ilo:g.ihi+1,g.jlo-1:g.jhi+1] +
                     eta[g.ilo:g.ihi+1,g.jlo  :g.jhi+2])

            eta_x /= g.dx**2
            eta_y /= g.dy**2

            self.x = eta_x
            self.y = eta_y


    def restrict(self):
        """
        restrict the edge values to a coarser grid.  Return a new
        _EdgeCoeffs object
        """

        cg = self.grid.coarse_like(2)

        c_edge_coeffs = _EdgeCoeffs(cg, None, empty=True)

        c_eta_x = cg.scratch_array()
        c_eta_y = cg.scratch_array()

        fg = self.grid
        
        c_eta_x[cg.ilo:cg.ihi+2,cg.jlo:cg.jhi+1] = \
            0.5*(self.x[fg.ilo:fg.ihi+2:2,fg.jlo  :fg.jhi+1:2] +
                 self.x[fg.ilo:fg.ihi+2:2,fg.jlo+1:fg.jhi+1:2]) 
        
        c_edge_coeffs.x = c_eta_x

        c_eta_y[cg.ilo:cg.ihi+1,cg.jlo:cg.jhi+2] = \
            0.5*(self.y[fg.ilo  :fg.ihi+1:2,fg.jlo:fg.jhi+2:2] +
                 self.y[fg.ilo+1:fg.ihi+1:2,fg.jlo:fg.jhi+2:2]) 
        
        c_edge_coeffs.y = c_eta_y


        print "restrict: ", cg.nx
        print c_eta_x
        print c_eta_y
        print " "
        
        return c_edge_coeffs

        
class VarCoeffCCMG2d(MG.CellCenterMG2d):
    """
    this is a multigrid solver that supports variable coefficients
    
    we need to accept a coefficient array, coeffs, defined at each
    level.  We can do this at the fine level and restrict it
    down the MG grids once.
    
    we need a new compute_residual() and smooth() function, that
    understands coeffs.
    """
   
    def __init__(self, nx, ny, xmin=0.0, xmax=1.0, ymin=0.0, ymax=1.0,
                 xl_BC_type="dirichlet", xr_BC_type="dirichlet",
                 yl_BC_type="dirichlet", yr_BC_type="dirichlet",
                 nsmooth=10, nsmooth_bottom=50,
                 verbose=0, 
                 coeffs=None, coeffs_bc=None,
                 true_function=None):

        # we'll keep a list of the coefficients averaged to the interfaces<commit_msg>the normalization on the edge coefficients was wrong.  But the second-order convergence was likely a fluke resulting simply from taking more cycles.<commit_after>"""
This multigrid solver is build from multigrid/MG.py and implements
a variable coefficient solver for an equation of the form:

div { eta grad phi } = f

where eta is defined on the same grid as phi.

A cell-centered discretization is used throughout.
"""

import multigrid.MG as MG
import numpy
import sys

numpy.set_printoptions(precision=3, linewidth=128)


class _EdgeCoeffs:
    """
    a simple container class to hold the edge-centered coefficients
    """
    def __init__(self, g, eta, empty=False):

        self.grid = g

        if not empty:
            eta_x = g.scratch_array()
            eta_y = g.scratch_array()

            # the eta's are defined on the interfaces, so 
            # eta_x[i,j] will be eta_{i-1/2,j} and 
            # eta_y[i,j] will be eta_{i,j-1/2}

            eta_x[g.ilo:g.ihi+2,g.jlo:g.jhi+1] = \
                0.5*(eta[g.ilo-1:g.ihi+1,g.jlo:g.jhi+1] +
                     eta[g.ilo  :g.ihi+2,g.jlo:g.jhi+1])

            eta_y[g.ilo:g.ihi+1,g.jlo:g.jhi+2] = \
                0.5*(eta[g.ilo:g.ihi+1,g.jlo-1:g.jhi+1] +
                     eta[g.ilo:g.ihi+1,g.jlo  :g.jhi+2])

            eta_x /= g.dx**2
            eta_y /= g.dy**2

            self.x = eta_x
            self.y = eta_y


    def restrict(self):
        """
        restrict the edge values to a coarser grid.  Return a new
        _EdgeCoeffs object
        """

        cg = self.grid.coarse_like(2)

        c_edge_coeffs = _EdgeCoeffs(cg, None, empty=True)

        c_eta_x = cg.scratch_array()
        c_eta_y = cg.scratch_array()

        fg = self.grid
        
        c_eta_x[cg.ilo:cg.ihi+2,cg.jlo:cg.jhi+1] = \
            0.5*(self.x[fg.ilo:fg.ihi+2:2,fg.jlo  :fg.jhi+1:2] +
                 self.x[fg.ilo:fg.ihi+2:2,fg.jlo+1:fg.jhi+1:2]) 
        
        # redo the normalization
        c_edge_coeffs.x = c_eta_x*fg.dx**2/cg.dx**2

        c_eta_y[cg.ilo:cg.ihi+1,cg.jlo:cg.jhi+2] = \
            0.5*(self.y[fg.ilo  :fg.ihi+1:2,fg.jlo:fg.jhi+2:2] +
                 self.y[fg.ilo+1:fg.ihi+1:2,fg.jlo:fg.jhi+2:2]) 
        
        c_edge_coeffs.y = c_eta_y*fg.dy**2/cg.dy**2


        print "restrict: ", cg.nx
        print c_eta_x
        print c_eta_y
        print " "
        
        return c_edge_coeffs

        
class VarCoeffCCMG2d(MG.CellCenterMG2d):
    """
    this is a multigrid solver that supports variable coefficients
    
    we need to accept a coefficient array, coeffs, defined at each
    level.  We can do this at the fine level and restrict it
    down the MG grids once.
    
    we need a new compute_residual() and smooth() function, that
    understands coeffs.
    """
   
    def __init__(self, nx, ny, xmin=0.0, xmax=1.0, ymin=0.0, ymax=1.0,
                 xl_BC_type="dirichlet", xr_BC_type="dirichlet",
                 yl_BC_type="dirichlet", yr_BC_type="dirichlet",
                 nsmooth=10, nsmooth_bottom=50,
                 verbose=0, 
                 coeffs=None, coeffs_bc=None,
                 true_function=None):

        # we'll keep a list of the coefficients averaged to the interfaces
{'fraction_non_alphanumeric': 0.121414913957935, 'fraction_numerical': 0.022944550669216062, 'mean_word_length': 1.889963167587477, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7363617', 'n_tokens_mistral': 2510, 'n_tokens_neox': 2389, 'n_words': 497}