<commit_before>import numpy as nm


class DGTerm:

    def __init__(self, mesh):
        self.mesh = mesh

    def evaluate(self, mode="weak", diff_var="u",
                 standalone=True, ret_status=False, **kwargs):
        raise NotImplemented

    def assemble_to(self, asm_obj, val, iels, mode="vector"):
        if (asm_obj is not None) and (iels is not None):
            if mode == "vector":
                if (len(iels) == 2) and (nm.shape(val)[0] == len(iels[0])):
                    for ii in iels[0]:
                        asm_obj[ii][iels[1]] = (asm_obj[ii][iels[1]].T + val[ii]).T
                else:
                    asm_obj[iels] = asm_obj[iels] + val

            elif mode == "matrix":
                if (len(iels) == 3) and (nm.shape(val)[0] == len(iels[0])):
                    for ii in iels[0]:
                        asm_obj[ii][iels[1], iels[2]] = asm_obj[ii][iels[1], iels[2]] + val[ii]
                else:
                    asm_obj[iels] = asm_obj[iels] + val
            else:
                raise ValueError("Unknown assebmly mode '%s'" % mode)


class AdvIntDGTerm(DGTerm):
    # TODO try inheritigng directly from Term?
    def __init__(self, mesh):
        DGTerm.__init__(self, mesh)
        self.vvar = "v"
        self.diff_var = "u"

    def evaluate(self, mode="weak", diff_var="u",
                 standalone=True, ret_status=False, **kwargs):
        # TODO use evaluate from super class, move all calculations to get_fargs() and function()
        if diff_var == self.diff_var:

            val = nm.vstack(((self.mesh.coors[1:] - self.mesh.coors[:-1]).T,
                             (self.mesh.coors[1:] - self.mesh.coors[:-1]).T/3))
            # integral over element with constant test
            # function is just volume of the element
            iels = ([0, 1], nm.arange(len(self.mesh.coors) - 1), nm.arange(len(self.mesh.coors) - 1))
            # values go on to the diagonal, in sfepy this is assured
            # by mesh connectivity induced by basis
        else:
            val = None
            iels = None

        return val, iels


class AdvFluxDGTerm(DGTerm):

    def __init__(self, mesh, a):
        DGTerm.__init__(self, mesh)
        self.a = a
        self.vvar = None
        self.diff_var = None

    def evaluate(self, mode="weak", diff_var=None,
                 standalone=True, ret_status=False, **kwargs):

        u = kwargs.pop('u', None)
        if diff_var == self.diff_var:
            # exact integral
            intg = self.a * (u[0, 1:-1] * (self.mesh.coors[1:] - self.mesh.coors[:-1]) +
                             1/2*u[1, 1:-1]**2 * (self.mesh.coors[1:] - self.mesh.coors[:-1])).T

            fp = self.a * u[0, 2:].T if self.a > 0 else self.a * u[0, 1:-1].T
            fl = self.a * u[0, 1:-1].T if self.a > 0 else self.a * u[0, :-2].T

            val = nm.vstack((fl - fp, - fl - fp + intg))
            # TODO values of flux terms are functions of solution on previous time step,
            # how to pas these values to the term?

            # placement is simple, bud getting the values requires looping over neighbours
            iels = ([0, 1], nm.arange(len(self.mesh.coors) - 1))  # just fill the vector
        else:
            val = None
            iels = None

        return val, iels<commit_msg>Add ic and bc treatement, proper assembly of the terms for higher orders of approximation, proper calculation of flux in FluxDGTerm<commit_after>import numpy as nm
from sfepy.terms.terms import Term

class DGTerm:

    def __init__(self, mesh):
        self.mesh = mesh

    def evaluate(self, mode="weak", diff_var="u",
                 standalone=True, ret_status=False, **kwargs):
        raise NotImplemented

    def assemble_to(self, asm_obj, val, iels, mode="vector"):
        if (asm_obj is not None) and (iels is not None):
            if mode == "vector":
                if (len(iels) == 2) and (nm.shape(val)[0] == len(iels[0])):
                    for ii in iels[0]:
                        asm_obj[ii][iels[1]] = (asm_obj[ii][iels[1]].T + val[ii]).T
                else:
                    asm_obj[iels] = asm_obj[iels] + val

            elif mode == "matrix":
                if (len(iels) == 3) and (nm.shape(val)[0] == len(iels[0])):
                    for ii in iels[0]:
                        asm_obj[ii][iels[1], iels[2]] = asm_obj[ii][iels[1], iels[2]] + val[ii]
                else:
                    asm_obj[iels] = asm_obj[iels] + val
            else:
                raise ValueError("Unknown assebmly mode '%s'" % mode)


class AdvIntDGTerm(Term):
    # TODO try inheritigng directly from Term?
    def __init__(self, mesh):
        DGTerm.__init__(self, mesh)
        self.vvar = "v"
        self.diff_var = "u"

    def get_fargs(self, *args, **kwargs):

        val = nm.vstack(((self.mesh.coors[1:] - self.mesh.coors[:-1]).T,
                         (self.mesh.coors[1:] - self.mesh.coors[:-1]).T/3))
        # integral over element with constant test
        # function is just volume of the element
        iels = ([0, 1], nm.arange(len(self.mesh.coors) - 1), nm.arange(len(self.mesh.coors) - 1))
        # values go on to the diagonal, in sfepy this is assured
        # by mesh connectivity induced by basis
        fargs = (val, iels)
        return fargs


    def function(self, out, *fargs):

        status = None
        return status

class AdvFluxDGTerm(Term):

    def __init__(self, mesh, a):
        DGTerm.__init__(self, mesh)
        self.a = a
        self.vvar = None
        self.diff_var = None

    def get_fargs(self, state, mode="weak", diff_var=None,
                 standalone=True, ret_status=False, **kwargs):

        u = self.get(state, 'val', step=-1)
        if diff_var == self.diff_var:
            # exact integral
            intg = self.a * (u[0, 1:-1] * (self.mesh.coors[1:] - self.mesh.coors[:-1]) +
                             1/2*u[1, 1:-1]**2 * (self.mesh.coors[1:] - self.mesh.coors[:-1])).T

            fp = self.a * u[0, 2:].T if self.a > 0 else self.a * u[0, 1:-1].T
            fl = self.a * u[0, 1:-1].T if self.a > 0 else self.a * u[0, :-2].T

            val = nm.vstack((fl - fp, - fl - fp + intg))
            # TODO values of flux terms are functions of solution on previous time step,
            # how to pas these values to the term?

            # placement is simple, bud getting the values requires looping over neighbours
            iels = ([0, 1], nm.arange(len(self.mesh.coors) - 1))  # just fill the vector
        else:
            val = None
            iels = None
        fargs = (val, iels)
        return fargs

    def function(selfself, out, *fargs):


        status = None
        return status
{'fraction_non_alphanumeric': 0.13581712928603235, 'fraction_numerical': 0.015288704170995992, 'mean_word_length': 1.7912178956089477, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 7, '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': '6412479', 'n_tokens_mistral': 2273, 'n_tokens_neox': 2141, 'n_words': 603}