def format(x): """Function, then an integer. """ print x radius = "lambda theta: {}".format(x[0]) try: evaluate(radius)[0](0) evaluate(radius)[0](np.pi) except: return [] return [radius] + clip(np.fabs(x[1:2]), int, 1, 1000)
def format(x): """Function, then an integer. """ print x radius = "lambda theta: {}".format(x[0]) try: evaluate(radius)[0](0) evaluate(radius)[0](np.pi) except: return [] return [radius] + clip(np.fabs(x[1:2]), int, 1, 1000)
def initializeConditions(self, conditions): """ Try to parse logical conditions given by the user. """ try: new = self.preprocessConditions(conditions) evaluate(new) # check if valid self.conditions = conditions self.check = new except: try: new = self.preprocessConditions(self.conditions) evaluate(new) self.check = new except: self.conditions = "True" self.check = "lambda x, y, z=0: True"
def initializeConditions(self, conditions): """ Try to parse logical conditions given by the user. """ try: new = self.preprocessConditions(conditions) evaluate(new) # check if valid self.conditions = conditions self.check = new except: try: new = self.preprocessConditions(self.conditions) evaluate(new) self.check = new except: self.conditions = "True" self.check = "lambda x, y, z=0: True"
def update(self, applyTo='global', conditions='True', param="None"): """ Parse boundary condition parameters. applyTo sets the type of the boundary specification: sides: list of boundary sides/triangles to which BC should apply conditions: logical conditions for (x,y,z) OR is placed between elements if a list is provided global: apply to the whole boundary param is used if a value is associated with a boundary condition. self.on = True: forces BC to apply only on the mesh boundary False: interior points can be a part of BC """ self.on = True applyTo = applyTo.lower() self.applyTo = applyTo if applyTo == 'sides': self.initializeSides(conditions) elif applyTo == 'conditions': self.initializeConditions(conditions) else: # global self.applyTo = "global" try: self.parValue = evaluate(param)[0] self.param = param except: pass
def on_functional_returnPressed(self): """ Modify list of eigenvalues according to supplied functional. """ f = self.functional.text() if not len(f): self.functional.setText(self.lastFunctional) return saved = f short = f[0] != ':' if short: f = 'e*' + f else: f = f[1:] e = np.array([self.eigList.item(i).eigenvalue for i in range(self.eigList.count())]) s = np.cumsum(e) subs = dict(self.data['geometry']) subs['V'] = subs['A'] subs['L'] = subs['P'] subs['S'] = subs['P'] subs['e'] = e subs['s'] = s try: lst = evaluate(f, subs) assert len(lst) and (len(lst) == len(e) or short) e = np.copy(e) e[:] = np.NaN e[:len(lst)] = lst for i in range(self.eigList.count()): if abs(e[i]) < 1E-9: e[i] = 0.0 self.eigList.item(i).setText(str(i+1)+': '+str(e[i])) self.lastFunctional = saved except: pass
def update(self, applyTo='global', conditions='True', param="None"): """ Parse boundary condition parameters. applyTo sets the type of the boundary specification: sides: list of boundary sides/triangles to which BC should apply conditions: logical conditions for (x,y,z) OR is placed between elements if a list is provided global: apply to the whole boundary param is used if a value is associated with a boundary condition. self.on = True: forces BC to apply only on the mesh boundary False: interior points can be a part of BC """ self.on = True applyTo = applyTo.lower() self.applyTo = applyTo if applyTo == 'sides': self.initializeSides(conditions) elif applyTo == 'conditions': self.initializeConditions(conditions) else: # global self.applyTo = "global" try: self.parValue = evaluate(param)[0] self.param = param except: pass
def getTest(self): """ Return a function which can check if a point belongs to bc. """ if self.applyTo == 'global': return lambda x, on: on fun = evaluate(self.check)[0] if self.on: return lambda x, on: fun(*x) and on else: return lambda x, on: fun(*x)
def getTest(self): """ Return a function which can check if a point belongs to bc. """ if self.applyTo == 'global': return lambda x, on: on fun = evaluate(self.check)[0] if self.on: return lambda x, on: fun(*x) and on else: return lambda x, on: fun(*x)
def update(self, params): """Create a function with or without parameters.""" self.full = [evaluate(self.def_pref + self.default)[0], 3] formula, ind = self.pad(self.eval(params, self.noparam)) if self.params != params: formula, ind = self.pad(self.eval(params, self.def_pref)) ind -= 1 self.full = None self.values = None return formula, ind
def format(x): """Two floats. Then at least one function. Finally an integer. """ x = flatten(x) if len(x) < 3: return [] domain = sorted(clip(x[:2], float)) import re var = 'x' if re.search(r'\bx\b', ''.join(x[2:4])) else 'y' top = "lambda {}: {}".format(var, x[2]) if len(x) == 3: bottom = "lambda {}: -({})".format(var, x[2]) else: bottom = "lambda {}: {}".format(var, x[3]) try: evaluate(top)[0](domain[0]) evaluate(bottom)[0](domain[0]) except: return [] return domain + [top, bottom, var] + clip(np.fabs(x[4:5]), int, 1, 1000)
def format(x): """Two floats. Then at least one function. Finally an integer. """ x = flatten(x) if len(x) < 3: return [] domain = sorted(clip(x[:2], float)) import re var = 'x' if re.search(r'\bx\b', ''.join(x[2:4])) else 'y' top = "lambda {}: {}".format(var, x[2]) if len(x) == 3: bottom = "lambda {}: -({})".format(var, x[2]) else: bottom = "lambda {}: {}".format(var, x[3]) try: evaluate(top)[0](domain[0]) evaluate(bottom)[0](domain[0]) except: return [] return domain + [top, bottom, var] + clip(np.fabs(x[4:5]), int, 1, 1000)
def from_file(name): """ Read polyhedron data from file. Format: first line: numer of vertices, numer of faces followed by: one vertex per line followed by: one face per line """ import os data_path = os.path.dirname(__file__) name = os.path.join(data_path, name) f = open(name) v, c = evaluate(f.readline())[:2] vertices = [] for _ in range(v): vertices.append(evaluate(f.readline())) faces = [] for _ in range(c): faces.append(evaluate(f.readline())) return (vertices, faces)
def from_file(name): """ Read polyhedron data from file. Format: first line: numer of vertices, numer of faces followed by: one vertex per line followed by: one face per line """ import os data_path = os.path.dirname(__file__) name = os.path.join(data_path, name) f = open(name) v, c = evaluate(f.readline())[:2] vertices = [] for _ in range(v): vertices.append(evaluate(f.readline())) faces = [] for _ in range(c): faces.append(evaluate(f.readline())) return (vertices, faces)
def get(self): """ Generate polygon from top and bottom curves. """ m, M, top, bottom, var, N = self.pad(self.values) top = evaluate(top)[0] bottom = evaluate(bottom)[0] if var == 'x': points = [Point(x, top(x)) for x in np.linspace(m, M, N + 2)] else: points = [Point(top(x), x) for x in np.linspace(m, M, N + 2)] if abs(top(M) - bottom(M)) < 1E-4: points.pop() if var == 'x': points += [Point(x, bottom(x)) for x in np.linspace(M, m, N + 2)] else: points += [Point(bottom(x), x) for x in np.linspace(M, m, N + 2)] if abs(top(m) - bottom(m)) < 1E-4: points.pop() try: # may fail if not counterclockwise vertices poly = Polygon(points) except: poly = Polygon(points[::-1]) return generate_mesh(poly, 1)
def get(self): """ Generate polygon from top and bottom curves. """ m, M, top, bottom, var, N = self.pad(self.values) top = evaluate(top)[0] bottom = evaluate(bottom)[0] if var == 'x': points = [Point(x, top(x)) for x in np.linspace(m, M, N+2)] else: points = [Point(top(x), x) for x in np.linspace(m, M, N+2)] if abs(top(M)-bottom(M)) < 1E-4: points.pop() if var == 'x': points += [Point(x, bottom(x)) for x in np.linspace(M, m, N+2)] else: points += [Point(bottom(x), x) for x in np.linspace(M, m, N+2)] if abs(top(m)-bottom(m)) < 1E-4: points.pop() try: # may fail if not counterclockwise vertices poly = Polygon(points) except: poly = Polygon(points[::-1]) return generate_mesh(poly, 1)
def get(self): """ Generate polygon from top and bottom curves. """ radius, N = self.pad(self.values) radius = evaluate(radius)[0] points = [Point(*(radius(theta)*np.array([cos(theta), sin(theta)]))) for theta in np.linspace(0, 2*np.pi, N+2)] if abs(radius(0)-radius(2*np.pi)) < 1E-4: points.pop() try: # may fail if not counterclockwise vertices poly = Polygon(points) except: poly = Polygon(points[::-1]) return generate_mesh(poly, 1)
def get(self): """ Generate polygon from top and bottom curves. """ radius, N = self.pad(self.values) radius = evaluate(radius)[0] points = [ Point(*(radius(theta) * np.array([cos(theta), sin(theta)]))) for theta in np.linspace(0, 2 * np.pi, N + 2) ] if abs(radius(0) - radius(2 * np.pi)) < 1E-4: points.pop() try: # may fail if not counterclockwise vertices poly = Polygon(points) except: poly = Polygon(points[::-1]) return generate_mesh(poly, 1)
def on_functional_returnPressed(self): """ Modify list of eigenvalues according to supplied functional. """ f = self.functional.text() if not len(f): self.functional.setText(self.lastFunctional) return saved = f short = f[0] != ':' if short: f = 'e*' + f else: f = f[1:] e = np.array([ self.eigList.item(i).eigenvalue for i in range(self.eigList.count()) ]) s = np.cumsum(e) subs = dict(self.data['geometry']) subs['V'] = subs['A'] subs['L'] = subs['P'] subs['S'] = subs['P'] subs['e'] = e subs['s'] = s try: lst = evaluate(f, subs) assert len(lst) and (len(lst) == len(e) or short) e = np.copy(e) e[:] = np.NaN e[:len(lst)] = lst for i in range(self.eigList.count()): if abs(e[i]) < 1E-9: e[i] = 0.0 self.eigList.item(i).setText(str(i + 1) + ': ' + str(e[i])) self.lastFunctional = saved except: pass