def form(name, obj): """ Pyphs formating of argument format 'obj' to a symbol Parameters ---------- argname : str argobj : {str, float, (str, float)} Outputs ------- symb : PHSCore.symbol subs : PHSCore.subs """ if isinstance(obj, tuple): assert isinstance(obj[0], str), 'for tupple parameter, \ first element should be a str, got {0!s}'.format(type(obj[0])) assert isinstance(obj[1], (float, int)), 'for tupple parameter, \ second element should be numeric, got\ {0!s}'.format(type(obj[1])) string = obj[0] symb = symbols(string) sub = {symb: obj[1]} par = None elif isinstance(obj, (float, int)): string = name symb = symbols(string) sub = {symb: obj} par = None elif isinstance(obj, str): string = obj symb = symbols(string) sub = {} par = symb return symb, sub, par
def __init__(self, label, nodes, **kwargs): # update default parameters parameters = {'ctrl': '?', 'const': None} parameters.update(kwargs) PHSGraph.__init__(self, label=label) # set starting node to datum if not provided if nodes.__len__() == 1: node1 = datum node2 = nodes[0] elif nodes.__len__() == 2: node1 = nodes[0] node2 = nodes[1] # define symbols u, y = symbols((nicevarlabel('u', label), nicevarlabel('y', label))) # add port to phs self.core.add_ports([u], [y]) # check edge control type (dual of input control type in values[0]) assert parameters['ctrl'] in ('e', 'f', '?') # define edge data edge_data = { 'label': y, 'type': 'port', 'ctrl': parameters['ctrl'], 'link': None } # add edge to phs.Graph self.add_edges_from([(node1, node2, edge_data)]) # check if constant value is provided if parameters['const'] is not None: self.core.subs.update({u: parameters['const']})
def mappars(graph, **kwargs): """ map dictionary of 'par':('label', value) to dictionary of substitutions \ for parameters in component expression 'dicpars' and for parameters in phs \ 'subs'. """ dicpars = {} subs = {} for key in kwargs.keys(): symb, sub, par = form(graph.label + '_' + str(key), kwargs[key]) dicpars.update({symbols(key): symb}) subs.update(sub) if par is not None: graph.core.add_parameters(par) return dicpars, subs
def __init__(self, label, nodes, **kwargs): # parameters pars = ['C0', 'Csat', 'phisat'] for par in pars: assert par in kwargs.keys() K0, Ksat, xsat = symbols(pars) # state variable x = symbols("x" + label) # storage funcion Hlin = x**2 / 2 t1 = sp.pi * x / (2 * xsat) c1 = (8 * xsat / (sp.pi * (4 - sp.pi))) Hsat = c1 * (sp.log(sp.cos(t1)) + (t1**2) / 2.) H = K0 * (Hlin - Ksat * Hsat) N1, N2 = nodes # edge data data = {'label': x, 'type': 'storage', 'ctrl': 'f', 'link': None} # edge edge = (N1, N2, data) # init component PHSStorageNonLinear.__init__(self, label, [edge], x, H, **kwargs)
def __init__(self, label, nodes, **kwargs): # instanciate a PHSGraph object PHSGraph.__init__(self, label=label) assert 'file' in kwargs, "pwl.storage component need 'file' argument" path = kwargs.pop('file') vals = np.vstack(map(np.array, data_generator(path))) x_vals = vals[0, :] h_vals = vals[1, :] assert all(h_vals[np.nonzero(x_vals >= 0)] >= 0), 'All values h(x) for\ x>=0 must be non-negative (component {})'.format(label) if kwargs['integ']: assert all(h_vals[np.nonzero(x_vals < 0)] < 0), 'All values dxh(x)\ for x<0 must be negative (component {})'.format(label) else: assert all(h_vals[np.nonzero(x_vals < 0)] >= 0), 'All values h(x)\ for x<0 must be non-negative (component {})'.format(label) assert h_vals[np.nonzero(x_vals == 0)] == 0, 'dxh(0) and h(0) must be \ zero (component {})'.format(label) ctrl = kwargs.pop('ctrl') # state variable x = symbols("x" + label) # storage funcion h = pwl_func(x_vals, h_vals, x, **kwargs) # edge data data = {'label': x, 'type': 'storage', 'ctrl': ctrl, 'link': None} N1, N2 = nodes # edge edge = (N1, N2, data) # init component self += edges.PHSStorageNonLinear(label, [edge], x, h, **kwargs)
def __init__(self, label, nodes, **kwargs): # instanciate a PHSGraph object PHSGraph.__init__(self, label=label) assert 'file' in kwargs, "pwl.dissipative component need 'file' argument" path = kwargs.pop('file') data = np.vstack(map(np.array, data_generator(path))) w_vals = data[0, :] z_vals = data[1, :] assert all(z_vals[np.nonzero(w_vals >= 0)] >= 0), 'All values z(w) for\ w>=0 must be non-negative (component {})'.format(label) assert all(z_vals[np.nonzero(w_vals >= 0)] >= 0), 'All values z(w) for\ w<0 must be negative (component {})'.format(label) assert all(z_vals[np.nonzero(w_vals == 0)] == 0), 'z(0) must be zero \ (component {})'.format(label) ctrl = kwargs.pop('ctrl') # state variable w = symbols("w" + label) # storage funcion z = pwl_func(w_vals, z_vals, w, **kwargs) # edge data data = {'label': w, 'type': 'dissipative', 'ctrl': ctrl, 'link': None} N1, N2 = nodes # edge edge = (N1, N2, data) # init component self += edges.PHSDissipativeNonLinear(label, [ edge, ], w, z, **kwargs)