def test_load_files_with_wrong_directory(self): model = ModelBase() self.md.add_model(model) with self.assertRaises(RuntimeError): self.md.load(ModelBase, mask='{0}/models'.format(pythonlab.tempname()))
def setUp(self): self.md = ModelDictExternal() tmp = pythonlab.tempname() self.md.directory = '{0}/models'.format(tmp) self.md.solver = pythonlab.datadir('agros2d_solver') copyfile('{0}/resources/test/test_suite/optilab/examples/quadratic_function.py'.format(pythonlab.datadir()), '{0}/problem.py'.format(tmp))
def setUp(self): self.md = ModelDictExternal() tmp = pythonlab.tempname() self.md.directory = '{0}/models'.format(tmp) self.md.solver = pythonlab.datadir('agros2d_solver') copyfile( '{0}/resources/test/test_suite/optilab/examples/quadratic_function.py' .format(pythonlab.datadir()), '{0}/problem.py'.format(tmp))
def test_save_and_load(self): file_name = '{0}/model.pickle'.format('{0}'.format(pythonlab.tempname())) model = quadratic_function.QuadraticFunction() model.parameters['x'] = 7 model.create() model.solve() model.process() model.save(file_name) model.clear() model.load(file_name) self.assertEqual(7**2, model.variables['F'])
def test_save_and_load(self): file_name = '{0}/model.pickle'.format('{0}'.format( pythonlab.tempname())) model = quadratic_function.QuadraticFunction() model.parameters['x'] = 7 model.create() model.solve() model.process() model.save(file_name) model.clear() model.load(file_name) self.assertEqual(7**2, model.variables['F'])
def chart(x, y, xlabel = "", ylabel = ""): import pylab as pl fig = pl.figure() pl.plot(x, y) pl.grid(True) pl.xlabel(xlabel) pl.ylabel(ylabel) fn_chart = pythonlab.tempname("png") pl.savefig(fn_chart, dpi=60) pl.clf() pl.close(fig) # show in console pythonlab.image(fn_chart)
def process(self, name): image_file = pythonlab.tempname('png') reference_image_file = pythonlab.datadir('resources/test/test_suite/script/images/{0}.png'.format(name)) if os.path.exists(reference_image_file): a2d.view.save_image(image_file) image = self.auto_crop(imread(image_file)) reference_image = imread(reference_image_file) difference = self.compare(image, reference_image) print(difference) self.assertLess(difference, 0.25) else: a2d.view.save_image(reference_image_file) imsave(reference_image_file, self.auto_crop(imread(reference_image_file)))
def chart(x, y, xlabel="", ylabel="", marker='o'): import pylab as pl fig = pl.figure() pl.plot(x, y, marker) pl.grid(True) pl.xlabel(xlabel) pl.ylabel(ylabel) fn_chart = pythonlab.tempname("png") pl.savefig(fn_chart, dpi=60) pl.clf() pl.close(fig) # show in console pythonlab.image(fn_chart)
def test_parameteric_study(self): mg = ModelGenerator() n = 50 k = 2 mg.add_parameter('x', np.linspace(-10, 10, n)) mg.add_parameter('y', np.linspace(-10, 10, n)) mg.combination() cwd = '{0}/models'.format(pythonlab.tempname()) mg.save(cwd) md = ModelDict() md.directory = cwd md.load(booths_function.BoothsFunction) md.solve() self.assertEqual(len(md.solved_models), n**k)
def process(self, name): image_file = pythonlab.tempname('png') reference_image_file = pythonlab.datadir( 'resources/test/test_suite/script/images/{0}.png'.format(name)) if os.path.exists(reference_image_file): a2d.view.save_image(image_file) image = self.auto_crop(imread(image_file)) reference_image = imread(reference_image_file) difference = self.compare(image, reference_image) print(difference) self.assertLess(difference, 0.25) else: a2d.view.save_image(reference_image_file) imsave(reference_image_file, self.auto_crop(imread(reference_image_file)))
def analyse_matrix_and_rhs(filename_matrix, filename_rhs): # read matrix and rhs from file mat_object = sio.loadmat(filename_matrix) matrix = mat_object["matrix"] rhs_object = sio.loadmat(filename_rhs) rhs = rhs_object["rhs"] # size of the matrix print("Matrix size: " + str(len(rhs))) print("Number of nonzeros: " + str(matrix.getnnz()) + " (" + str(round(float(matrix.getnnz()) / (len(rhs)**2) * 100.0, 3)) + " %)") # visualize matrix sparsity pattern fig = pl.figure() pl.spy(matrix, markersize=1) fn_pattern = pythonlab.tempname("png") pl.savefig(fn_pattern, dpi=60) pl.close(fig) # show in console pythonlab.image(fn_pattern)
def save_solution_test(): problem = agros2d.problem() field = agros2d.field('magnetic') problem.solve() values_from_solution = [field.local_values(0.05, 0), field.surface_integrals([0, 1, 2]), field.volume_integrals()] from os import path filename = '{0}/temp.a2d'.format(path.dirname(pythonlab.tempname())) agros2d.save_file(filename, True) agros2d.open_file(filename, True) field = agros2d.field('magnetic') values_from_file = [field.local_values(0.05, 0), field.surface_integrals([0, 1, 2]), field.volume_integrals()] return compare(values_from_solution, values_from_file)
def save_solution_test(): problem = agros2d.problem() field = agros2d.field('magnetic') problem.solve() values_from_solution = [ field.local_values(0.05, 0), field.surface_integrals([0, 1, 2]), field.volume_integrals() ] from os import path filename = '{0}/temp.a2d'.format(path.dirname(pythonlab.tempname())) agros2d.save_file(filename, True) agros2d.open_file(filename, True) field = agros2d.field('magnetic') values_from_file = [ field.local_values(0.05, 0), field.surface_integrals([0, 1, 2]), field.volume_integrals() ] return compare(values_from_solution, values_from_file)
materials={"electrostatic": "Dielectric n.2"}) geometry.add_label(0.0284191, 0.123601, materials={"electrostatic": "Air"}) a2d.view.zoom_best_fit() # calculation of capacity r = [] C = [] print("C = f(r) (F):") for i in range(15): if i > 0: geometry.select_edges([6, 7, 8]) geometry.move_selection(dr, 0, False) problem.solve() result = electrostatic.volume_integrals([0, 1, 2]) r.append(r1 + (i * dr)) C.append(2 * 2 * result["We"] / (U ^ 2)) print(r[-1], C[-1]) # chart pl.close() pl.plot(r, C) pl.grid(True) pl.xlabel("r (m)") pl.ylabel("C (F)") fn_chart = pythonlab.tempname("png") pl.savefig(fn_chart, dpi=60) pl.close() # show in console pythonlab.image(fn_chart)
def setUp(self): self.md = ModelDict() self.md.directory = '{0}/models'.format(pythonlab.tempname())
from scipy.interpolate import interp1d phi = [0.0, 4.0, 8.0, 12.0, 16.0, 20.0, 24.0, 28.0, 32.0, 36.0, 40.0, 44.0, 48.0, 52.0, 56.0, 60.0, 64.0, 68.0, 72.0, 76.0, 80.0, 84.0, 88.0, 92.0, 96.0, 100.0, 104.0, 108.0, 112.0, 116.0, 120.0, 124.0, 128.0, 132.0, 136.0, 140.0, 144.0, 148.0, 152.0, 156.0, 160.0, 164.0, 168.0, 172.0, 176.0, 180.0] T_magnetic_torque = [-4.2099750000000006e-07, -0.0006306440000000001, -0.0012431405000000002, -0.0027673415, -0.0037627800000000007, -0.004668125, -0.005372255, -0.005509490000000001, -0.006019965000000001, -0.006202910000000001, -0.007951475000000001, -0.009308495000000002, -0.00903189, -0.009031260000000001, -0.011202345, -0.01173942, -0.013634670000000002, -0.013729415, -0.013753075000000002, -0.014419475000000001, -0.0097538, -0.008428175000000001, -0.0028582085000000003, 0.001922431, 0.00836486, 0.010786545, 0.013908825000000001, 0.013557495000000001, 0.013209665, 0.013566455000000002, 0.011872665000000001, 0.011166470000000001, 0.009009595, 0.009028250000000002, 0.009307900000000001, 0.007950670000000002, 0.006194965, 0.0060320750000000005, 0.00558495, 0.0053764550000000005, 0.0046711700000000005, 0.003763025, 0.0026294870000000007, 0.001254253, 0.000597345, -4.944730000000001e-07] T_torque = [7.600635000000001e-08, -0.00017802715, -0.00043366050000000005, -0.0013786395, -0.002051854, -0.0025863285000000003, -0.0029615285000000003, -0.0029484280000000003, -0.008016085000000001, -0.008393595, -0.01086897, -0.012900475000000002, -0.012870795, -0.01335537, -0.016747500000000002, -0.018461975000000002, -0.022139145000000002, -0.024000515000000004, -0.025957925, -0.030677990000000002, -0.029933050000000006, -0.037302300000000004, -0.03650815, -0.0453334, -0.02398515, -0.012230330000000003, -0.005922595000000001, -0.0013065115, 0.0007364700000000001, 0.0028762475000000003, 0.0035826000000000005, 0.0041284600000000005, 0.0029878625, 0.0038398150000000003, 0.004532675000000001, 0.0039266150000000005, 0.00301847, 0.0031519530000000003, 0.0030171505000000003, 0.0029608460000000005, 0.0025858875000000004, 0.002052134, 0.001297366, 0.0004423615, 0.00016526405, 1.6689750000000002e-08] # static characteristic pl.close() pl.figure(figsize = [8, 5]) pl.plot(phi, T_magnetic_torque, 'b', label="0.0 A") pl.plot(phi, T_torque, 'r', label="0.8 A") pl.plot([0, 180], [0, 0], '--k') pl.xlabel("phi (deg.)") pl.ylabel("T (Nm)") pl.legend(loc="lower right") fn_chart_static = pythonlab.tempname("png") pl.savefig(fn_chart_static, dpi=60) pl.close() # show in console pythonlab.image(fn_chart_static) J = 7.5e-5; k = 2e-4 T_f = interp1d(phi, T_torque, kind = "linear") def func(x, t): dx = [0., 0.] dx[0] = x[1] dx[1] = (- T_f((x[0]*180/np.pi) % 180) - k*x[1]) * 1/J return dx
geometry.add_label(0.0379, 0.051, materials = {"electrostatic" : "Dielectric n.2"}) geometry.add_label(0.0284191, 0.123601, materials = {"electrostatic" : "Air"}) a2d.view.zoom_best_fit() # calculation of capacity r = [] C = [] print("C = f(r) (F):") for i in range(15): if i > 0: geometry.select_edges([6, 7, 8]) geometry.move_selection(dr, 0, False) problem.solve() result = electrostatic.volume_integrals([0, 1, 2]) r.append(r1 + (i*dr)) C.append(2*2*result["We"]/(U^2)) print(r[-1], C[-1]) # chart pl.close() pl.plot(r, C) pl.grid(True) pl.xlabel("r (m)") pl.ylabel("C (F)") fn_chart = pythonlab.tempname("png") pl.savefig(fn_chart, dpi=60) pl.close() # show in console pythonlab.image(fn_chart)
geometry.add_edge(0, -0.25, 0, -1, boundaries = {"electrostatic" : "Border"}) geometry.add_edge(0, -1, 1.5, 0.5, angle = 90, boundaries = {"electrostatic" : "Border"}) geometry.add_edge(1.5, 0.5, 0, 2, angle = 90, boundaries = {"electrostatic" : "Border"}) geometry.add_edge(0, 1, 0.2, 1, boundaries = {"electrostatic" : "Source"}) geometry.add_edge(0, 2, 0, 1, boundaries = {"electrostatic" : "Border"}) geometry.add_edge(0, -0.25, 0.25, 0, angle = 90, boundaries = {"electrostatic" : "Ground"}) geometry.add_edge(0.25, 0, 0, 0.25, angle = 90, boundaries = {"electrostatic" : "Ground"}) geometry.add_label(0.879551, 0.764057, area = 0.06, materials = {"electrostatic" : "Air"}) a2d.view.zoom_best_fit() problem.solve() # solve model model() fn_geometry = pythonlab.tempname("vtk") a2d.geometry.export_vtk(fn_geometry) geometry = a2d.vtk_geometry_actor(fn_geometry) fn_contour = pythonlab.tempname("vtk") a2d.view.post2d.contour_view_parameters["variable"] = "electrostatic_potential" a2d.view.post2d.export_contour_vtk(fn_contour) contours = a2d.vtk_contours_actor(fn_contour, count = 15, color = False) fn_scalar = pythonlab.tempname("vtk") a2d.view.post2d.scalar_view_parameters["variable"] = "electrostatic_electric_field" a2d.view.post2d.scalar_view_parameters["component"] = "magnitude" a2d.view.post2d.export_scalar_vtk(fn_scalar) scalar = a2d.vtk_scalar_actor(fn_scalar) fn = pythonlab.tempname("png")
import pythonlab from numpy import arange from scipy.integrate import odeint import pylab as pl R = 1e3 L = 1e-3 U0 = 10 # initial condition y0 = [0] def func(y, t): return [-R / L * y[0] + U0 / L] t = arange(0, 5 * L / R, L / R / 10) y = odeint(func, y0, t) # chart pl.plot(t, y) pl.grid(True) pl.xlabel("$t\,\mathrm{(s)}$") pl.ylabel("$i\,\mathrm{(A)}$") fn_diff_equation = pythonlab.tempname("png") pl.savefig(fn_diff_equation, dpi=60) pl.close() # show in console pythonlab.image(fn_diff_equation)
a2d.view.config.grid = False a2d.view.post2d.scalar = False # calculation of torque dphi = 180.0 / (N - 1) phiWm = [] Wm = [] for i in range(N): print("angle = " + str(i * dphi)) problem.solve() geometry.select_none() a2d.view.post2d.contours_variable = "magnetic_potential_real" a2d.view.post2d.contours = True a2d.view.zoom_region(-0.04, -0.037, 0.04, 0.037) fn_chart_dynamic = pythonlab.tempname("png") a2d.view.save_image(fn_chart_dynamic) # pythonlab.image(fn_chart_dynamic, 450) result = magnetic.volume_integrals([10]) phiWm.append(i * dphi) Wm.append(result["Wm"]) if i < N: geometry.select_edges([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) geometry.rotate_selection(0, 0, dphi, False) geometry.select_edges([10, 11, 12, 13, 14, 15, 16, 17]) geometry.rotate_selection(0, 0, dphi, False) geometry.select_edges([18, 19, 20, 21, 22, 23, 24, 25])
y3 = f3(xi) f4 = interpolate.interp1d(x, y, kind = "nearest") y4 = f4(xi) pl.figure() pl.subplot(2,2,1) pl.plot(x, y, '.k', label="original data") pl.plot(xi, y1, label="linear spline interpolation") pl.title(r"linear") pl.subplot(2,2,2) pl.plot(x, y, '.k', label="original data") pl.plot(xi, y2, label="quadratic spline interpolation") pl.title(r"quadratic") pl.subplot(2,2,3) pl.plot(x, y, '.k', label="original data") pl.plot(xi, y3, label="cubic spline interpolation") pl.title(r"cubic") pl.subplot(2,2,4) pl.plot(x, y, '.k', label="original data") pl.plot(xi, y4, label="nearest interpolation") pl.title(r"nearest") chart_file = pythonlab.tempname("png") pl.savefig(chart_file, dpi=60) pl.close() pythonlab.image(chart_file)
0.25, angle=90, boundaries={"electrostatic": "Ground"}) geometry.add_label(0.879551, 0.764057, area=0.06, materials={"electrostatic": "Air"}) a2d.view.zoom_best_fit() problem.solve() # solve model model() fn_geometry = pythonlab.tempname("vtk") a2d.geometry.export_vtk(fn_geometry) geometry = a2d.vtk_geometry_actor(fn_geometry) fn_contour = pythonlab.tempname("vtk") a2d.view.post2d.contour_view_parameters["variable"] = "electrostatic_potential" a2d.view.post2d.export_contour_vtk(fn_contour) contours = a2d.vtk_contours_actor(fn_contour, count=15, color=False) fn_scalar = pythonlab.tempname("vtk") a2d.view.post2d.scalar_view_parameters[ "variable"] = "electrostatic_electric_field" a2d.view.post2d.scalar_view_parameters["component"] = "magnitude" a2d.view.post2d.export_scalar_vtk(fn_scalar) scalar = a2d.vtk_scalar_actor(fn_scalar)
geometry.add_label(0.00316251, 0.011224, materials = {"heat" : "Iron"}) geometry.add_label(0.0163723, 0.144289, materials = {"heat" : "Copper"}) geometry.add_label(0.00380689, 0.151055, materials = {"heat" : "Air"}) geometry.add_label(0.0112064, 0.0336487, materials = {"heat" : "Brass"}) a2d.view.zoom_best_fit() problem.solve() # current pl.figure(figsize=[8,3]) pl.bar(total_time, current, width = problem.time_total / problem.time_steps) pl.xlabel("time (s)") pl.ylabel("current (A)") pl.grid(True) chart_current = pythonlab.tempname("png") pl.savefig(chart_current, dpi=60) pl.close() pythonlab.image(chart_current) # temperature pl.figure(figsize=[8,3]) pl.plot(total_time, temp, '-') pl.xlabel("time (s)") pl.ylabel("temperature (K)") pl.grid(True) chart_temp = pythonlab.tempname("png") pl.savefig(chart_temp, dpi=60) pl.close()
y3 = f3(xi) f4 = interpolate.interp1d(x, y, kind="nearest") y4 = f4(xi) pl.figure() pl.subplot(2, 2, 1) pl.plot(x, y, '.k', label="original data") pl.plot(xi, y1, label="linear spline interpolation") pl.title(r"linear") pl.subplot(2, 2, 2) pl.plot(x, y, '.k', label="original data") pl.plot(xi, y2, label="quadratic spline interpolation") pl.title(r"quadratic") pl.subplot(2, 2, 3) pl.plot(x, y, '.k', label="original data") pl.plot(xi, y3, label="cubic spline interpolation") pl.title(r"cubic") pl.subplot(2, 2, 4) pl.plot(x, y, '.k', label="original data") pl.plot(xi, y4, label="nearest interpolation") pl.title(r"nearest") chart_file = pythonlab.tempname("png") pl.savefig(chart_file, dpi=60) pl.close() pythonlab.image(chart_file)
geometry.add_label(0.00316251, 0.011224, materials={"heat": "Iron"}) geometry.add_label(0.0163723, 0.144289, materials={"heat": "Copper"}) geometry.add_label(0.00380689, 0.151055, materials={"heat": "Air"}) geometry.add_label(0.0112064, 0.0336487, materials={"heat": "Brass"}) a2d.view.zoom_best_fit() problem.solve() # current pl.figure(figsize=[8, 3]) pl.bar(total_time, current, width=problem.time_total / problem.time_steps) pl.xlabel("time (s)") pl.ylabel("current (A)") pl.grid(True) chart_current = pythonlab.tempname("png") pl.savefig(chart_current, dpi=60) pl.close() pythonlab.image(chart_current) # temperature pl.figure(figsize=[8, 3]) pl.plot(total_time, temp, '-') pl.xlabel("time (s)") pl.ylabel("temperature (K)") pl.grid(True) chart_temp = pythonlab.tempname("png") pl.savefig(chart_temp, dpi=60) pl.close()
import pythonlab from numpy import arange from scipy.integrate import odeint import pylab as pl R = 1e3 L = 1e-3 U0 = 10 # initial condition y0 = [0] def func(y, t): return [- R/L*y[0] + U0/L] t = arange(0, 5*L/R, L/R/10) y = odeint(func, y0, t) # chart pl.plot(t, y) pl.grid(True) pl.xlabel("$t\,\mathrm{(s)}$") pl.ylabel("$i\,\mathrm{(A)}$") fn_diff_equation = pythonlab.tempname("png") pl.savefig(fn_diff_equation, dpi=60) pl.close() # show in console pythonlab.image(fn_diff_equation)