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 setUpClass(self): # store state self.save_matrix_and_rhs = agros2d.options.save_matrix_and_rhs self.dump_format = agros2d.options.dump_format # dump format agros2d.options.save_matrix_and_rhs = True agros2d.options.dump_format = "matlab_mat" # read reference matrix and rhs from file self.reference_mat, self.reference_rhs = self.read_matrix_and_rhs(pythonlab.datadir("/resources/test/test_suite/core/matrix_solvers_matrix.mat"), pythonlab.datadir("/resources/test/test_suite/core/matrix_solvers_rhs.mat"))
def setUpClass(self): # store state self.save_matrix_and_rhs = agros2d.options.save_matrix_and_rhs self.dump_format = agros2d.options.dump_format # dump format agros2d.options.save_matrix_and_rhs = True agros2d.options.dump_format = "matlab_mat" # read reference matrix and rhs from file self.reference_mat, self.reference_rhs = self.read_matrix_and_rhs( pythonlab.datadir( "/resources/test/test_suite/core/matrix_solvers_matrix.mat"), pythonlab.datadir( "/resources/test/test_suite/core/matrix_solvers_rhs.mat"))
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 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 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)))
if (extension == '.a2d'): setattr(case, method, get_test(file)) def get_test(file): def test(self): agros2d.open_file(file) agros2d.problem().solve() return test tests = list() data_dirs = [ pythonlab.datadir('/resources/test/test_suite/core/data_files/0/'), pythonlab.datadir('/resources/test/test_suite/core/data_files/21/'), pythonlab.datadir('/resources/test/test_suite/core/data_files/30/') ] for dir in data_dirs: for (path, dirs, files) in os.walk(dir): if (dirs): continue rest, field = os.path.split(path) rest, version = os.path.split(rest) name = "TestXSLT{0}{1}".format(version, field.title()).replace("~1", "") code = compile('class {0}(Agros2DTestCase): pass'.format(name),
import agros2d import pythonlab from math import pi agros2d.open_file( pythonlab.datadir( 'resources/examples/Examples/Magnetic Field/Magnetostatics/Switched Reluctance Machine.a2d' )) def get_energy(dphi, Jext): problem = agros2d.problem() magnetic = agros2d.field("magnetic") for coil in Jext: magnetic.modify_material( "Copper {0}+".format(coil), {"magnetic_current_density_external_real": +Jext[coil]}) magnetic.modify_material( "Copper {0}-".format(coil), {"magnetic_current_density_external_real": -Jext[coil]}) geometry = agros2d.geometry geometry.select_edges(range(64, 80)) geometry.rotate_selection(0, 0, dphi) problem.solve() return magnetic.volume_integrals()['Wm'] def get_density(phi):
os.path.split(path)[-1].replace(" ", "_"), name.replace(" ", "_")).lower() example = '{0}/{1}'.format(path, file) if (extension == '.a2d'): setattr(case, method, get_test(example)) def get_test(example): def test(self): agros2d.open_file(example) script = agros2d.get_script_from_model() exec script in globals(), locals() agros2d.problem().solve() return test class TestGenerator(Agros2DTestCase): pass create_tests(TestGenerator, pythonlab.datadir('/resources/examples/Examples')) if __name__ == '__main__': import unittest as ut suite = ut.TestSuite() result = Agros2DTestResult() suite.addTest(ut.TestLoader().loadTestsFromTestCase(TestGenerator)) suite.run(result)
from variant.optimization import ContinuousParameter, Functionals, Functional from variant.genetic import GeneticOptimization import pythonlab parameters = [ContinuousParameter('left', 0, 0.02), ContinuousParameter('right', 0.03, 0.05), ContinuousParameter('bottom', 0, 0.02), ContinuousParameter('top', 0.03, 0.05), ContinuousParameter('eps', 5, 10)] functionals = Functionals([Functional("C", "max")]) optimization = GeneticOptimization(parameters, functionals) optimization.directory = pythonlab.datadir('/data/sweep/simple_capacitor/solutions/') optimization.modelSetManager.solver = pythonlab.datadir('agros2d_solver') optimization.populationSize = 25 optimization.run(10, False)
from test_suite.scenario import Agros2DTestResult def create_tests(case, dir): for (path, dirs, files) in os.walk(dir): for file in files: name, extension = os.path.splitext(file) method = 'test_{0}_{1}'.format(os.path.split(path)[-1].replace(" ", "_"), name.replace(" ", "_")).lower() file = '{0}/{1}'.format(path, file) if (extension == '.a2d'): setattr(case, method, get_test(file)) def get_test(file): def test(self): agros2d.open_file(file) agros2d.problem().solve() return test class XSLT(Agros2DTestCase): pass create_tests(XSLT, pythonlab.datadir('/resources/test/test_suite/internal/data_files')) if __name__ == '__main__': import unittest as ut suite = ut.TestSuite() result = Agros2DTestResult() suite.addTest(ut.TestLoader().loadTestsFromTestCase(XSLT)) suite.run(result)
setattr(case, method, get_py_test(example)) def get_a2d_test(example): def test(self): agros2d.open_file(example) agros2d.problem().solve() return test def get_py_test(example): def test(self): with open(example) as f: exec f.read() in globals() return test class Examples(Agros2DTestCase): pass create_tests(Examples, pythonlab.datadir('/resources/examples/Examples')) class Other(Agros2DTestCase): pass create_tests(Other, pythonlab.datadir('/resources/examples/Other')) class PythonLab(Agros2DTestCase): pass create_tests(PythonLab, pythonlab.datadir('/resources/examples/PythonLab')) class Tutorials(Agros2DTestCase): pass create_tests(Tutorials, pythonlab.datadir('/resources/examples/Tutorials')) if __name__ == '__main__': import unittest as ut suite = ut.TestSuite() result = Agros2DTestResult()
# import libraries import pythonlab from math import * # add actual directory to the path import sys sys.path.insert(0, ".") sys.path.append(pythonlab.datadir("resources/python")) # user functions def sgn(number): return (number >= 0) and 1 or -1 # from rope.base.project import Project # pythonlab_rope_project = Project(".", ropefolder=None) # get completion list def python_engine_get_completion_script(script, row, column, filename=None): import jedi jedi.settings.additional_dynamic_modules = [pythonlab] s = jedi.Script(script, row, column, filename) completions = s.completions() comps = [] for completion in completions: comps.append(completion.name)
import agros2d import pythonlab from math import pi agros2d.open_file(pythonlab.datadir('resources/examples/Examples/Magnetic Field/Magnetostatics/Switched Reluctance Machine.a2d')) def get_energy(dphi, Jext): problem = agros2d.problem() magnetic = agros2d.field("magnetic") for coil in Jext: magnetic.modify_material("Copper {0}+".format(coil), {"magnetic_current_density_external_real" : +Jext[coil]}) magnetic.modify_material("Copper {0}-".format(coil), {"magnetic_current_density_external_real" : -Jext[coil]}) geometry = agros2d.geometry geometry.select_edges(range(64, 80)) geometry.rotate_selection(0, 0, dphi) problem.solve() return magnetic.volume_integrals()['Wm'] def get_density(phi): Jext = {'A' : 0.0, 'B' : 0.0, 'C' : 0.0} J_EXT = 2e6 if ((phi >= 60 and phi < 90) or (phi >= 150 and phi < 180)): Jext['A'] = J_EXT if ((phi >= 0 and phi < 30) or (phi >= 90 and phi < 120) or
method = 'test_{0}_{1}'.format(os.path.split(path)[-1].replace(" ", "_"), name.replace(" ", "_")).lower().replace("~1", "") file = '{0}/{1}'.format(path, file) if (extension == '.a2d'): setattr(case, method, get_test(file)) def get_test(file): def test(self): agros2d.open_file(file) agros2d.problem().solve() return test tests = list() data_dirs = [pythonlab.datadir('/resources/test/test_suite/core/data_files/0/'), pythonlab.datadir('/resources/test/test_suite/core/data_files/21/'), pythonlab.datadir('/resources/test/test_suite/core/data_files/30/')] for dir in data_dirs: for (path, dirs, files) in os.walk(dir): if (dirs): continue rest, field = os.path.split(path) rest, version = os.path.split(rest) name = "TestXSLT{0}{1}".format(version, field.title()).replace("~1", "") code = compile('class {0}(Agros2DTestCase): pass'.format(name), '<string>', 'exec') exec code create_tests(globals()[name], path)
def create_tests(case, dir): for (path, dirs, files) in os.walk(dir): for file in files: name, extension = os.path.splitext(file) method = 'test_{0}_{1}'.format(os.path.split(path)[-1].replace(" ", "_"), name.replace(" ", "_")).lower() example = '{0}/{1}'.format(path, file) if(extension == '.a2d'): setattr(case, method, get_test(example)) def get_test(example): def test(self): agros2d.open_file(example) script = agros2d.get_script_from_model() exec script in globals(), locals() agros2d.problem().solve() return test class TestGenerator(Agros2DTestCase): pass create_tests(TestGenerator, pythonlab.datadir('/resources/examples/Examples')) if __name__ == '__main__': import unittest as ut suite = ut.TestSuite() result = Agros2DTestResult() suite.addTest(ut.TestLoader().loadTestsFromTestCase(TestGenerator)) suite.run(result)
setattr(case, method, get_py_test(example)) def get_a2d_test(example): def test(self): agros2d.open_file(example) agros2d.problem().solve() return test def get_py_test(example): def test(self): with open(example) as f: exec(f.read() in globals()) return test tests = list() data_dirs = [pythonlab.datadir('/resources/examples/Examples'), pythonlab.datadir('/resources/examples/Other'), pythonlab.datadir('/resources/examples/PythonLab'), pythonlab.datadir('/resources/examples/Tutorials')] for dir in data_dirs: for (path, dirs, files) in os.walk(dir): if not (any("a2d" in file for file in files) or any("py" in file for file in files)): continue name = "TestExamples{0}".format(os.path.split(path)[-1].replace(" ", "")).replace("~1", "") code = compile('class {0}(Agros2DTestCase): pass'.format(name), '<string>', 'exec') exec(code) create_tests(globals()[name], path) tests.append(globals()[name])
import pylab as pl import numpy as np import pythonlab from scipy import interpolate data = np.loadtxt(pythonlab.datadir('resources/examples/PythonLab/Polynomial regression.csv'), delimiter=",") x = data[:, 0] y = data[:, 1] xi = np.linspace(x[0], x[-1], 1e2) f1 = interpolate.interp1d(x, y, kind = "linear") y1 = f1(xi) f2 = interpolate.interp1d(x, y, kind = "quadratic") y2 = f2(xi) f3 = interpolate.interp1d(x, y, kind = "cubic") 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")
import pylab as pl import numpy as np import pythonlab from scipy import interpolate data = np.loadtxt(pythonlab.datadir( 'resources/examples/PythonLab/Polynomial regression.csv'), delimiter=",") x = data[:, 0] y = data[:, 1] xi = np.linspace(x[0], x[-1], 1e2) f1 = interpolate.interp1d(x, y, kind="linear") y1 = f1(xi) f2 = interpolate.interp1d(x, y, kind="quadratic") y2 = f2(xi) f3 = interpolate.interp1d(x, y, kind="cubic") 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")
# import libraries import pythonlab from math import * # add actual directory to the path import sys sys.path.insert(0, ".") sys.path.append(pythonlab.datadir("resources/python")) # user functions def sgn(number): return (number >= 0) and 1 or -1 # from rope.base.project import Project # pythonlab_rope_project = Project(".", ropefolder=None) # get completion list def python_engine_get_completion_script(script, row, column, filename = None): import jedi jedi.settings.additional_dynamic_modules = [pythonlab] s = jedi.Script(script, row, column, filename) completions = s.completions() comps = [] for completion in completions: comps.append(completion.name) return comps