Exemple #1
0
 def generate_stencil(self, grid):
     pos_x = self.position[0]
     pos_y = self.position[1]
     width_x, width_y = grid.spacing
     entries = [
         ((0, 0), (((self.get_coefficient(
             (pos_x + (0.5 * width_x)), pos_y) + self.get_coefficient(
                 (pos_x - (0.5 * width_x)), pos_y)) / (width_x * width_x)) +
                   ((self.get_coefficient(pos_x,
                                          (pos_y + (0.5 * width_y))) +
                     self.get_coefficient(pos_x,
                                          (pos_y - (0.5 * width_y)))) /
                    (width_y * width_y)))),
         ((1, 0), ((-1.0 * self.get_coefficient(
             (pos_x + (0.5 * width_x)), pos_y)) / (width_x * width_x))),
         ((-1, 0), ((-1.0 * self.get_coefficient(
             (pos_x - (0.5 * width_x)), pos_y)) / (width_x * width_x))),
         ((0, 1),
          ((-1.0 * self.get_coefficient(pos_x, (pos_y + (0.5 * width_y)))) /
           (width_y * width_y))),
         ((0, -1),
          ((-1.0 * self.get_coefficient(pos_x, (pos_y - (0.5 * width_y)))) /
           (width_y * width_y)))
     ]
     return constant.Stencil(entries)
Exemple #2
0
 def generate_stencil(self, grid):
     eps = 1.0
     h0, h1 = grid.spacing
     entries = [((0, -1), -1 / (h1 * h1)), ((-1, 0), -1 / (h0 * h0) * eps),
                ((0, 0), 2 / (h0 * h0) * eps + 2 / (h1 * h1)),
                ((1, 0), -1 / (h0 * h0) * eps), ((0, 1), -1 / (h1 * h1))]
     return constant.Stencil(entries)
Exemple #3
0
 def generate_stencil(self, grid):
     h0, h1, h2 = grid.spacing
     entries = [((0, 0, 0), 2 / (h0 * h0) + 2 / (h1 * h1) + 2 / (h2 * h2)),
                ((-1, 0, 0), -1 / (h0 * h0)), ((1, 0, 0), -1 / (h0 * h0)),
                ((0, -1, 0), -1 / (h1 * h1)), ((0, 1, 0), -1 / (h1 * h1)),
                ((0, 0, -1), -1 / (h2 * h2)), ((0, 0, 1), -1 / (h2 * h2))]
     return constant.Stencil(entries)
Exemple #4
0
 def generate_stencil(self, grid):
     h, = grid.spacing
     entries = [
         ((-1, ), -1 / (h * h)),
         ((0, ), 2 / (h * h)),
         ((1, ), -1 / (h * h)),
     ]
     return constant.Stencil(entries)
Exemple #5
0
 def generate_stencil(self, grid):
     vf_nodePosition_x, vf_nodePosition_y, vf_nodePosition_z = self.position
     vf_gridWidth_x, vf_gridWidth_y, vf_gridWidth_z = grid.spacing
     getCoefficient = self.get_coefficient
     entries = [
         ((0, 0, 0),
          ((((getCoefficient(
              (vf_nodePosition_x + (0.5 * vf_gridWidth_x)),
              vf_nodePosition_y, vf_nodePosition_z) + getCoefficient(
                  (vf_nodePosition_x - (0.5 * vf_gridWidth_x)),
                  vf_nodePosition_y, vf_nodePosition_z)) /
             (vf_gridWidth_x * vf_gridWidth_x)) +
            ((getCoefficient(vf_nodePosition_x,
                             (vf_nodePosition_y +
                              (0.5 * vf_gridWidth_y)), vf_nodePosition_z) +
              getCoefficient(vf_nodePosition_x,
                             (vf_nodePosition_y -
                              (0.5 * vf_gridWidth_y)), vf_nodePosition_z)) /
             (vf_gridWidth_y * vf_gridWidth_y))) +
           ((getCoefficient(vf_nodePosition_x, vf_nodePosition_y,
                            (vf_nodePosition_z + (0.5 * vf_gridWidth_z))) +
             getCoefficient(vf_nodePosition_x, vf_nodePosition_y,
                            (vf_nodePosition_z - (0.5 * vf_gridWidth_z)))) /
            (vf_gridWidth_z * vf_gridWidth_z)))),
         ((1, 0, 0), ((-1.0 * getCoefficient(
             (vf_nodePosition_x + (0.5 * vf_gridWidth_x)),
             vf_nodePosition_y, vf_nodePosition_z)) /
                      (vf_gridWidth_x * vf_gridWidth_x))),
         ((-1, 0, 0), ((-1.0 * getCoefficient(
             (vf_nodePosition_x - (0.5 * vf_gridWidth_x)),
             vf_nodePosition_y, vf_nodePosition_z)) /
                       (vf_gridWidth_x * vf_gridWidth_x))),
         ((0, 1, 0),
          ((-1.0 *
            getCoefficient(vf_nodePosition_x,
                           (vf_nodePosition_y +
                            (0.5 * vf_gridWidth_y)), vf_nodePosition_z)) /
           (vf_gridWidth_y * vf_gridWidth_y))),
         ((0, -1, 0),
          ((-1.0 *
            getCoefficient(vf_nodePosition_x,
                           (vf_nodePosition_y -
                            (0.5 * vf_gridWidth_y)), vf_nodePosition_z)) /
           (vf_gridWidth_y * vf_gridWidth_y))),
         ((0, 0, 1),
          ((-1.0 * getCoefficient(vf_nodePosition_x, vf_nodePosition_y,
                                  (vf_nodePosition_z +
                                   (0.5 * vf_gridWidth_z)))) /
           (vf_gridWidth_z * vf_gridWidth_z))),
         ((0, 0, -1),
          ((-1.0 * getCoefficient(vf_nodePosition_x, vf_nodePosition_y,
                                  (vf_nodePosition_z -
                                   (0.5 * vf_gridWidth_z)))) /
           (vf_gridWidth_z * vf_gridWidth_z)))
     ]
     return constant.Stencil(entries)
Exemple #6
0
 def generate_prolongation_and_restriction(
         weights_, default_restriction_: system.Restriction,
         default_prolongation_: system.Prolongation):
     offset = 0
     restriction_operators = []
     prolongation_operators = []
     default_restriction_entries = default_restriction_.entries
     default_prolongation_entries = default_prolongation_.entries
     for kk, nn in enumerate(number_of_restriction_weights):
         restriction_weights = weights_[offset:nn]
         offset += nn
         entries = []
         index = 0
         for ii in range(-operator_range, operator_range + 1):
             for jj in range(-operator_range, operator_range + 1):
                 entries.append(((ii, jj), restriction_weights[index]))
                 index += 1
         stencil = constant.Stencil(entries)
         stencil_generator = base.ConstantStencilGenerator(stencil)
         restriction_ = base.Restriction(
             default_restriction_entries[kk][kk].name, grid[kk],
             coarse_grid[kk], stencil_generator)
         restriction_operators.append(restriction_)
     for kk, nn in enumerate(number_of_prolongation_weights):
         prolongation_weights = weights_[offset:(nn + offset)]
         offset += nn
         entries = []
         index = 0
         for ii in range(-operator_range, operator_range + 1):
             for jj in range(-operator_range, operator_range + 1):
                 entries.append(((ii, jj), prolongation_weights[index]))
                 index += 1
         stencil = constant.Stencil(entries)
         stencil_generator = base.ConstantStencilGenerator(stencil)
         prolongation = base.Prolongation(
             default_prolongation_entries[kk][kk].name, grid[kk],
             coarse_grid[kk], stencil_generator)
         prolongation_operators.append(prolongation)
     restriction_ = system.Restriction(default_restriction_.name,
                                       restriction_operators)
     prolongation = system.Prolongation(default_prolongation_.name,
                                        prolongation_operators)
     return restriction_, prolongation
Exemple #7
0
from evostencils.stencils import constant, periodic

a = constant.Stencil([((0, 0), 4), ((0, 1), -1)], 2)
b = constant.Stencil([((0, 0), 4), ((0, -1), -1)], 2)

c = periodic.Stencil([[a, b]], 2)
nentries = periodic.count_number_of_entries(c)
foo = 1
Exemple #8
0
def extract_l2_information(file_path: str, dimension: int):
    equations = []
    operators = []
    fields = []
    with open(file_path, 'r') as file:
        line = file.readline()
        while line:
            tokens = line.split(' ')
            if tokens[0] == 'Operator':
                stencil_entries = []
                tmp = tokens[1].split('@')
                name = tmp[0]
                level = int(tmp[1])
                line = file.readline()
                tokens = line.split('from')
                is_prolongation = False
                is_restriction = False
                if 'gen_restrictionForSol_' in name:
                    is_restriction = True
                elif 'gen_prolongationForSol_' in name:
                    is_prolongation = True
                while True:
                    tmp = tokens[1].split('with')
                    value = float(tmp[1])
                    offsets = parse_stencil_offsets(tmp[0], is_prolongation)
                    stencil_entries.append((offsets, value))
                    line = file.readline()
                    tokens = line.split('from')
                    if '}' in line:
                        break
                if is_restriction:
                    operator_type = base.Restriction
                elif is_prolongation:
                    operator_type = base.Prolongation
                else:
                    operator_type = base.Operator
                op_info = initialization.OperatorInfo(
                    name, level, constant.Stencil(stencil_entries, dimension),
                    operator_type)
                operators.append(op_info)
            elif tokens[0] == 'Equation':
                tmp = tokens[1].split('@')
                eq_info = initialization.EquationInfo(tmp[0], int(tmp[1]),
                                                      file.readline())
                equations.append(eq_info)
                file.readline()
            line = file.readline()
    max_level = max(equations, key=lambda info: info.level).level
    equations = [
        eq_info for eq_info in equations if eq_info.level == max_level
    ]
    equations.sort(key=lambda info: info.name)
    for eq_info in equations:
        sympy_expr = eq_info.sympy_expr
        for symbol in sympy_expr.free_symbols:
            if symbol.name not in (
                    op_info.name
                    for op_info in operators) and symbol not in fields:
                fields.append(symbol)
    fields.sort(key=lambda s: s.name)
    for eq_info in equations:
        rhs_name = eq_info.rhs_name
        tmp = rhs_name.split('_')
        if len(tmp) == 1:
            if len(fields) != 1:
                raise RuntimeError(
                    'Could not extract associated field for rhs')
            eq_info._associated_field = fields[0]
        else:
            field_name = tmp[1]
            eq_info._associated_field = next(field for field in fields
                                             if field.name == field_name)
    equations.sort(key=lambda ei: ei.associated_field.name)
    for op_info in operators:
        if op_info.operator_type == base.Restriction or op_info.operator_type == base.Prolongation:
            name = op_info.name
            tmp = name.split('_')
            field_name = tmp[-1]
            op_info._associated_field = next(field for field in fields
                                             if field.name == field_name)
    assert len(equations) == len(
        fields
    ), 'The number of equations does not match with the number of fields'
    return equations, operators, fields
Exemple #9
0
def lfa_sparse_stencil_to_constant_stencil(stencil: lfa_lab.SparseStencil):
    return constant.Stencil(tuple(entry for entry in stencil), stencil.dim)
Exemple #10
0
def extract_l2_information(file_path: str,
                           dimension: int,
                           solution_equations=None):
    equations = []
    operators = []
    fields = []
    with open(file_path, 'r') as file:
        line = file.readline()
        while line:
            tokens = line.split(' ')
            if tokens[0] == 'Operator':
                stencil_entries = []
                tmp = tokens[1].split('@')
                name = tmp[0]
                level = int(tmp[1])
                line = file.readline()
                tokens = line.split('from')
                is_prolongation = False
                is_restriction = False
                if 'restriction' in name.lower():
                    is_restriction = True
                elif 'prolongation' in name.lower():
                    is_prolongation = True
                while True:
                    tmp = tokens[1].split('with')
                    value = parse_expr(tmp[1])
                    # value = value.evalf()
                    offsets = parse_stencil_offsets(tmp[0], is_prolongation)
                    stencil_entries.append((offsets, value))
                    line = file.readline()
                    tokens = line.split('from')
                    if '}' in line:
                        break
                if is_restriction:
                    operator_type = base.Restriction
                elif is_prolongation:
                    operator_type = base.Prolongation
                else:
                    operator_type = base.Operator
                op_info = initialization.OperatorInfo(
                    name, level, constant.Stencil(stencil_entries, dimension),
                    operator_type)
                operators.append(op_info)
            elif tokens[0] == 'Equation':
                equation_name, level = tokens[1].split('@')
                eq_info = initialization.EquationInfo(equation_name,
                                                      int(level),
                                                      file.readline())
                if solution_equations is None or equation_name in solution_equations:
                    equations.append(eq_info)
                file.readline()
            line = file.readline()
    assert len(equations) > 0 and len(operators) > 0, "No equations specified"
    # max_level = max(equations, key=lambda info: info.level).level
    # equations = [eq_info for eq_info in equations if eq_info.level == max_level]
    equations.sort(key=lambda info: info.name)
    names_of_used_operators = set()
    for eq_info in equations:
        sympy_expr = eq_info.sympy_expr
        for symbol in sympy_expr.free_symbols:
            if any(symbol.name == op_info.name
                   and eq_info.level == op_info.level
                   for op_info in operators):
                names_of_used_operators.add(symbol.name + '@' +
                                            str(eq_info.level))
            elif symbol not in fields:
                fields.append(symbol)
    fields.sort(key=lambda s: s.name)
    for eq_info in equations:
        rhs_name = eq_info.rhs_name
        tmp = rhs_name.split('_')
        if len(tmp) == 1:
            if len(fields) != 1:
                raise RuntimeError(
                    'Could not extract associated field for rhs')
            eq_info._associated_field = fields[0]
        else:
            field_name = tmp[1]
            eq_info._associated_field = next(field for field in fields
                                             if field.name == field_name)
    equations.sort(key=lambda ei: ei.associated_field.name)
    for op_info in operators:
        if op_info.operator_type == base.Restriction or op_info.operator_type == base.Prolongation:
            name = op_info.name
            tmp = name.split('_')
            field_name = tmp[-1]
            try:
                associated_field = next(field for field in fields
                                        if field.name == field_name)
                op_info._associated_field = associated_field
                names_of_used_operators.add(op_info.name + '@' +
                                            str(op_info.level))
            except StopIteration as _:
                pass
    operators = [
        op_info for op_info in operators
        if op_info.name + '@' + str(op_info.level) in names_of_used_operators
    ]

    # assert len(equations) == len(fields), 'The number of equations does not match with the number of fields'
    return equations, operators, fields