示例#1
0
 def attribute_set(self, v):
     from petram.helper.variables import Variables
     v['_variables'] = Variables()
     v['enabled'] = True
     v['root_path'] = ''
     v['model_path'] = ''        
     return v
    def run_postprocess(self, engine):
        dprint1("running postprocess: " + self.name())

        name = self.variables.strip()
        if name not in engine.model._variables:
            print(list(engine.model._variables))
            assert False, name + " is not defined"

        var1 = engine.model._variables[name]

        emesh_idx1 = var1.get_emesh_idx()
        emesh_idx = emesh_idx1[0]

        fes1 = engine.fespaces[name]
        element = 'L2_FECollection'

        _is_new, fes2 = engine.get_or_allocate_fecfes(
            self.interpolation_name,
            emesh_idx,
            element,
            self.order,
            1,
        )

        gfr = engine.new_gf(fes2)
        if var1.complex:
            gfi = engine.new_gf(fes2)
        else:
            gfi = None

        from petram.helper.operators import Divergence
        opr = Divergence()
        opr._engine = weakref.ref(engine)
        M = opr.assemble(trial=fes1, test=fes2)

        V1 = engine.variable2vector(var1)
        value = M.dot(V1)

        dprint1("operator size", M.shape)

        if var1.complex:
            engine.X2x(value.real, gfr)
            engine.X2x(value.imag, gfi)
        else:
            engine.X2x(value.real, gfr)

        from petram.helper.variables import Variables

        v = Variables()
        names = [self.interpolation_name.strip()]
        self.add_variables(v, names, gfr, gfi)

        engine.add_PP_to_NS(v)
        engine.save_solfile_fespace(self.interpolation_name, emesh_idx, gfr,
                                    gfi)
示例#3
0
    def run(self):
        import petram.debug
        if petram.debug.debug_default_level == 0:
            petram.debug.debug_default_level = int(self.debug_level)

        if not hasattr(self.root(), "_variables"):
            from petram.helper.variables import Variables
            self.root()._variables = Variables()

        self.root()._parameters = {}
        self.root()._init_done = True
示例#4
0
    def __init__(self, **kwargs):
        super(MFEM_ModelRoot, self).__init__(**kwargs)
        self['General'] = MFEM_GeneralRoot()

        if has_geom:
            self['Geometry'] = MFEM_GeomRoot()        
        self['Mesh'] = MFEM_MeshRoot()
        self['Phys'] = MFEM_PhysRoot()
        self['InitialValue'] = MFEM_InitRoot()        
        self['Solver'] = MFEM_SolverRoot()

        from petram.helper.variables import Variables
        self._variables = Variables()
示例#5
0
    def run_postprocess(self, engine):
        dprint1("running postprocess: " + self.name())

        names = [x.strip() for x in self.projection_name.split(',')]

        emesh_idx = self.get_emesh_idx(engine)
        sdim = self.geom_dim

        if (self.element.startswith('ND') or self.element.startswith('RT')):
            vdim = self.vdim // self.geom_dim
            #coeff_dim = self.geom_dim
        else:
            vdim = self.vdim
            #coeff_dim = vdim

        is_new, fec, fes = engine.get_or_allocate_fecfes(
            ''.join(names), emesh_idx, self.element, self.order, vdim)
        gfr = engine.new_gf(fes)
        if self.is_complex_valued:
            gfi = engine.new_gf(fes)
        else:
            gfi = None

        self.vt_coeff.preprocess_params(self)
        c = self.vt_coeff.make_value_or_expression(self)

        phys = self.root()['Phys'].values()
        for p in phys:
            if p.enabled:
                ind_vars = p.ind_vars
                break
        else:
            assert False, "no phys is enabled"
        from petram.helper.variables import var_g
        global_ns = self._global_ns.copy()
        for k in engine.model._variables:
            global_ns[k] = engine.model._variables[k]
        local_ns = {}

        from petram.helper.variables import project_variable_to_gf

        project_variable_to_gf(c[0],
                               ind_vars,
                               gfr,
                               gfi,
                               global_ns=global_ns,
                               local_ns=local_ns)
        '''                       
        def project_coeff(gf, coeff_dim, c, ind_vars, real):
            if coeff_dim > 1:
                 #print("vector coeff", c)
                 coeff = VCoeff(coeff_dim, c[0], ind_vars,
                                local_ns, global_ns, real = real)
            else:
                 #print("coeff", c)                
                 coeff = SCoeff(c[0], ind_vars,
                                local_ns, global_ns, real = real)
            gf.ProjectCoefficient(coeff)
            
        project_coeff(gfr, coeff_dim, c, ind_vars, True)
        if gfi is not None:
            project_coeff(gfi, coeff_dim, c, ind_vars, False)
        '''

        from petram.helper.variables import Variables
        v = Variables()

        self.add_variables(v, names, gfr, gfi)

        engine.add_PP_to_NS(v)
        engine.save_solfile_fespace(''.join(names), emesh_idx, gfr, gfi)
    def eval_ns(self):

        chain = self.get_ns_chain()
        l = self.get_default_ns()

        from petram.helper.variables import var_g
        g = var_g.copy()

        import mfem
        if mfem.mfem_mode == 'serial':
            g['mfem'] = mfem.ser
        elif mfem.mfem_mode == 'parallel':
            g['mfem'] = mfem.par
        else:
            assert False, "PyMFEM is not loaded"

        import numpy
        g['np'] = numpy
        from petram.helper.variables import variable, coefficient
        g['variable'] = variable
        g['coefficient'] = coefficient

        if self.root() is self:
            if not hasattr(self.root(), "_variables"):
                from petram.helper.variables import Variables
                self.root()._variables = Variables()
        else:
            self._local_ns = self.root()._variables

        if len(chain) == 0:
            raise ValueError("namespace chain is not found")
        # step1 (fill ns using upstream + constant (no expression)
        if chain[-1] is not self:
            if len(l) == 0:
                self._global_ns = chain[-1]._global_ns
                #self._local_ns = chain[-1]._local_ns
            else:
                self._global_ns = g
                for k in l:
                    g[k] = l[k]
                for k in chain[-1]._global_ns:
                    g[k] = chain[-1]._global_ns[k]
                #self._local_ns = {}
        elif len(chain) > 1:
            # step 1-1 evaluate NS chain except for self and store dataset to
            # g including mine
            self._global_ns = g
            for p in chain[:-1]:  #self.parents:
                if not isinstance(p, NS_mixin): continue
                ll = p.get_default_ns()
                if (p.ns_string == '' or p.ns_string is None and len(ll) == 0):
                    continue
                for k in ll:
                    g[k] = ll[k]
                if p.ns_name is not None:
                    try:
                        if p.dataset is not None:
                            for k in p.dataset:
                                g[k] = p.dataset[k]
                        for k in p.attribute_mirror_ns():
                            g[k] = chain[-2]._global_ns[k]
                        if (p.ns_string != '' and p.ns_string is not None):
                            exec(p.ns_string, g, ll)
                            for k in ll:
                                g[k] = ll[k]

                    except Exception as e:
                        import traceback
                        assert False, traceback.format_exc()
            if self.dataset is not None:
                for k in self.dataset:
                    g[k] = self.dataset[k]
        else:
            self._global_ns = g
            for k in l:
                g[k] = l[k]
            if self.dataset is not None:
                for k in self.dataset:
                    g[k] = self.dataset[k]
        # step2 eval attribute using upstream + non-expression
        result, invalid = self.eval_attribute_expr()
        for k in result:
            setattr(self, k, result[k])

        # step 3 copy attributes to ns
        attrs = self.attribute_mirror_ns()
        for a in attrs:
            if not a in invalid: g[a] = getattr(self, a)

        # step 4 run namespace scripts otherise exit
        for k in l:
            g[k] = l[k]  # copying default ns

        try:
            l = {}
            if (self.ns_string != '' and self.ns_string is not None):
                #exec(self.ns_string, g, l)
                exec(self.ns_string, g)
            else:
                pass  ###return
        except Exception as e:
            import traceback
            assert False, traceback.format_exc()

        # 2021.08.25. passing g only above allows for list comprehension to work.
        # for k in l:
        #     g[k] = l[k]

        # step 5  re-eval attribute with self-namespace
        #         passing previous invalid as a list of variables
        #         to evaluate
        result, invalid = self.eval_attribute_expr(invalid)
        for k in result:
            setattr(self, k, result[k])

        #if self is not self.root()["General"] (Let's set it in General too)
        from petram.helper.dot_dict import DotDict
        g['general'] = DotDict(self.root()["General"]._global_ns)

        # if something is still not known,,, raise
        if len(invalid) != 0:
            raise ValueError("failed to evaluate variable " +
                             ', '.join(invalid))