def setup_terms(self, regions, variables, materials, caches=None, user=None): """Parse equations and create term instances. Grabs references to materials and variables.""" if caches is None: self.caches = DataCaches() else: self.caches = caches self.materials = materials self.variables = variables for eq in self: eq.setup_terms(regions, variables, materials, self.caches, user)
def assign_caches(self, caches=None): from sfepy.terms import cache_table, DataCaches if not hasattr(self, "use_caches"): return if caches is None: caches = DataCaches() ## print self.name for name, arg_lists in self.use_caches.iteritems(): ## print self.arg_names ## print name, arg_lists for args in arg_lists: ## Order should be handled in terms... args = copy(args) if len(args) and (type(args[-1]) == dict): history_sizes = args.pop() else: history_sizes = None ans = [self.get_arg_name(arg, full=True) for arg in args] cname = "_".join([name] + ans) ## print self.name, name, arg_lists, args, cname ## print history_sizes if caches.has_key(cname): caches[cname].merge_history_sizes(history_sizes) else: ## print 'new' try: constructor = cache_table[name] except: raise RuntimeError, "cache not found! %s in %s" % (name, sorted(cache_table.keys())) cache = constructor(cname, ans, history_sizes) caches.insert_cache(cache) caches.insert_term(cname, self.name, ans) self.caches = caches
class Equations(Container): ## # c: 18.04.2006, r: 20.02.2008 def from_conf(conf): objs = OneTypeList(Equation) conf = copy(conf) tps = conf.pop("namespaces", {}) itps = invert_dict(tps, True) ii = 0 for name, desc in conf.iteritems(): output('equation "%s":' % name) output(desc) eq = Equation(name=name, desc=desc, itps=itps) objs.append(eq) ii += 1 obj = Equations(objs, itps=itps) return obj from_conf = staticmethod(from_conf) def setup_terms(self, regions, variables, materials, caches=None, user=None): """Parse equations and create term instances. Grabs references to materials and variables.""" if caches is None: self.caches = DataCaches() else: self.caches = caches self.materials = materials self.variables = variables for eq in self: eq.setup_terms(regions, variables, materials, self.caches, user) ## # c: ??, r: 26.02.2008 def describe_geometry(self, geometries, variables, integrals): output("describing geometries...") tt = time.clock() for eq in self: eq.describe_geometry(geometries, variables, integrals) output("...done in %.2f s" % (time.clock() - tt)) ## # 24.08.2006, c # 24.04.2007 def get_term_geometries(self): tgs = set() for eq in self: tgs.update(eq.get_term_geometries()) return tgs ## # 16.11.2007, c def get_term_integral_names(self): i_names = set() for eq in self: i_names.update(eq.get_term_integral_names()) return i_names def get_variable_names(self): """Return the list of names of all variables used in equations.""" vns = set() for eq in self: for term in eq.terms: vns.update(term.get_variable_names()) return list(vns) ## # 27.02.2007, c def invalidate_term_caches(self): for cache in self.caches.itervalues(): cache.clear() ## # c: 07.05.2008, r: 07.05.2008 def reset_term_caches(self): for cache in self.caches.itervalues(): cache.reset() ## # 02.03.2007, c def set_cache_mode(self, cache_override): for cache in self.caches.itervalues(): cache.set_mode(cache_override) def time_update(self, ts): for eq in self: for term in eq.terms: term.time_update(ts) ## # c: 02.04.2008, r: 02.04.2008 def init_time(self, ts): for cache in self.caches.itervalues(): cache.init_time(ts) ## # 08.06.2007, c def advance(self, ts): for cache in self.caches.itervalues(): cache.advance(ts.step + 1)
def eval_term( state, term_desc, conf, domain, variables, materials, ts, funmod=None, chunk_size=1000, term_prefixes=None, caches=None, ret_caches=False, override=True, new_geometries=True, dw_mode="vector", tangent_matrix=None, **kwargs ): """Evaluate a term. May not succeed!""" if term_prefixes is None: term_prefixes = {} if caches is None: caches = DataCaches() equation = Equation.from_desc("tmp", term_desc, term_prefixes) equation.setup_terms(domain.regions, variables, materials, caches, kwargs) for cache in caches.itervalues(): cache.set_mode(override=override) if new_geometries: i_names = equation.get_term_integral_names() integrals = Integrals.from_conf(conf.integrals, i_names) integrals.set_quadratures(quadratures) geometries = {} equation.describe_geometry(geometries, variables, integrals) variables.data_from_state(state) if "call_mode" in kwargs: itype = kwargs["call_mode"].split("_")[0] else: # itype according to the first term in term_desc! itype = equation.terms[0].itype if itype == "dw": variables.setup_dof_conns() if not variables.has_eq_map: variables.equation_mapping(conf.ebcs, conf.epbcs, domain.regions, ts, funmod) variables.setup_lcbc_operators(conf.lcbcs, domain.regions) variables.setup_a_dof_conns() if dw_mode == "vector": residual = variables.create_stripped_state_vector() assemble_vector(residual, equation, variables, materials, chunk_size, group_can_fail=False, **kwargs) if variables.has_lcbc: op_lcbc = variables.op_lcbc residual = op_lcbc.T * residual ret_val = residual elif dw_mode == "matrix": if tangent_matrix is None: tangent_matrix = variables.create_matrix_graph() tangent_matrix.data[:] = 0.0 assemble_matrix(tangent_matrix, equation, variables, materials, chunk_size, group_can_fail=False, **kwargs) if variables.has_lcbc: op_lcbc = variables.op_lcbc tangent_matrix = op_lcbc.T * tangent_matrix * op_lcbc tangent_matrix = tangent_matrix.tocsr() tangent_matrix.sort_indices() ret_val = tangent_matrix else: print dw_mode raise ValueError elif itype == "d": kwargs.setdefault("call_mode", "d_eval") val = 0.0 for term in equation.terms: args = build_args(term, variables, materials, **kwargs) for ig in term.iter_groups(): for aux, iels, status in term(chunk_size=chunk_size, **args): val += term.sign * aux ret_val = val elif itype == "di": val = None for term in equation.terms: args = build_args(term, variables, materials, **kwargs) for ig in term.iter_groups(): for aux, iels, status in term(chunk_size=chunk_size, **args): if val is None: val = term.sign * aux else: val += term.sign * aux ret_val = val elif (itype == "de") or (itype == "dq"): val = None for term in equation.terms: args = build_args(term, variables, materials, **kwargs) for ig in term.iter_groups(): for aux, iels, status in term(chunk_size=chunk_size, **args): if val is None: val = term.sign * aux else: val = nm.concatenate((val, term.sign * aux), axis=0) ret_val = val else: raise NotImplementedError, "unknown term integration type: %s" % itype if ret_caches: return ret_val, caches else: return ret_val