def add_partial_differential_equation_category(settings, coordinates=None): import expresso.pycas as pc sb = settings.simulation_box pde = settings.create_category( 'partial_differential_equation', short_name='PDE', info="parameters of the partial differential equation") arg_attrs = [sb.coordinate_dict[x] for x in coordinates ] if coordinates is not None else sb.coordinates pde.add_attribute('coordinates', arg_attrs) x, y, z = [a.symbol for a in arg_attrs] dx, dy, dz = [s.step for s in arg_attrs] args = (x, y, z) pde.create_function('A', args) pde.create_function('C', args, pde.A) pde.create_function('F', args) pde.create_function('ra', args, pde.A * dz / dx**2, info="finite difference paramter") pde.create_function('rc', args, pde.C * dz / dy**2, info="finite difference paramter") pde.create_function('rf', args, pde.F * dz / 2, info="finite difference paramter") pde.create_function('u', args, info='solution to the PDE') pde.lock('u') pde.add_attribute( 'equation', pc.equal( pc.derivative(pde.u, z), pde.A * pc.derivative(pc.derivative(pde.u, x), x) + pde.C * pc.derivative(pc.derivative(pde.u, y), y) + pde.F * pde.u)) pde.create_key('u0', pc.Function('u_0_PDE')(*args), info="field initial condition") pde.create_function('u_boundary', args, info="field boundary condition") pde.create_key(arg_attrs[1].name + '0', pc.Symbol(y.name + '_0_PDE'), 0, info='Value to which the %s is set for 1D solvers' % y.name) pde.lock() return pde
def add_coordinate(settings, category, name): import expresso.pycas as pc x = category.create_key(name, pc.Symbol(name, type=pc.Types.Real), info='coordinate') xmin = category.create_key('%smin' % name, pc.Symbol('%s_min' % name), info='minimum value') xmax = category.create_key('%smax' % name, pc.Symbol('%s_max' % name), info='maximum value') xi = pc.Symbol('%s_i' % name, type=pc.Types.Natural) xif = category.create_key('%si' % name, pc.Function('%s_i' % name)(x), info='numerical index') Nx = category.create_key('N%s' % name, pc.Symbol('N_%s' % name, type=pc.Types.Natural), info='numerical steps') sx = category.create_key('s%s' % name, pc.Symbol('s_%s' % name), info='total size') dx = category.create_key('d%s' % name, pc.Symbol('Delta %s' % name), info='step size') setattr(category, name, xmin + xi * dx) setattr(category, '%si' % name, (x - xmin) / dx) setattr(category, 's%s' % name, xmax - xmin) setattr(category, 'd%s' % name, sx / (Nx - 1)) category.lock(name, 'defined by %si' % name) category.lock('s%s' % name, 'defined by %smin and %smax' % (name, name)) category.lock('d%s' % name, 'defined by s%s and N%s' % (name, name)) settings.unitless.add_key('%s_coordinate' % name, x)
import expresso.pycas as pc import rule_symbols as s logic_evaluator = pc.RewriteEvaluator(recursive=True, split_binary=True) evaluator = logic_evaluator is_atomic = pc.Function('is atomic') is_symbol = pc.Function('is symbol') is_explicit_natural = pc.Function('is explicit natural') is_pure_numeric = pc.Function('is pure numeric') is_numeric = pc.Function('is numeric') is_mpmath = pc.Function('is mpmath') is_function = pc.Function('is function') contains_atomic = pc.Function('contains atomic') def is_explicit_natural_evaluator(m): for expr in m: if not isinstance(expr[1].value, pc.Number): m[s.y] = False return m[s.y] = True logic_evaluator.add_rule(is_explicit_natural(s.x), s.y, is_explicit_natural_evaluator) def is_function_type(expr, function): return is_function(expr, pc.expresso.create_object(function))
def create_function(name, args, value=None, info=None, **kwargs): prefix = '_'.join(cat_path) return cat.create_key( name, pc.Function("%s_%s" % (name, prefix), **kwargs)(*args), value, info)