diff --git a/anuga/__init__.py b/anuga/__init__.py index 5f475700d..380bbc271 100644 --- a/anuga/__init__.py +++ b/anuga/__init__.py @@ -21,15 +21,17 @@ # ----------------------------------------------------- +from builtins import filter __version__ = '2.0.3' -__svn_revision__ = filter(str.isdigit, "$Revision: 9737 $") +__svn_revision__ = ''.join(filter(str.isdigit, "$Revision: 9737 $")) __svn_revision_date__ = "$Date: 2016-10-04 16:13:00 +1100 (Tue, 04 Oct 2016) $"[7:-1] # We first need to detect if we're being called as part of the anuga setup # procedure itself in a reliable manner. + try: __ANUGA_SETUP__ except NameError: @@ -41,17 +43,8 @@ _sys.stderr.write('Running from anuga source directory.\n') del _sys else: - - try: - from anuga.__config__ import show as show_config - except ImportError: - msg = """Error importing anuga: you should not try to import anuga from - its source directory; please exit the anuga source tree, and relaunch - your python interpreter from there.""" - raise ImportError(msg) - - # --------------------------------- - # NetCDF changes stdout to terminal\ + # ---------------------------------- + # NetCDF changes stdout to terminal # Causes trouble when using jupyter # --------------------------------- import sys @@ -63,6 +56,8 @@ from numpy.testing import Tester test = Tester().test + from anuga.__config__ import show as show_config + # -------------------------------- # Important basic classes # -------------------------------- diff --git a/anuga/compile_all.py b/anuga/compile_all.py index 61ed467a3..e29ddd1fe 100644 --- a/anuga/compile_all.py +++ b/anuga/compile_all.py @@ -1,5 +1,6 @@ """ Script to compile all C extensions in ANUGA. """ +from past.builtins import execfile import os import subprocess import sys diff --git a/anuga/error_api.py b/anuga/error_api.py index 0ca646193..c11a32cae 100644 --- a/anuga/error_api.py +++ b/anuga/error_api.py @@ -1,3 +1,6 @@ +from __future__ import division +from builtins import range +from past.utils import old_div import matplotlib import matplotlib.pyplot as plt import matplotlib.tri as tri @@ -29,7 +32,7 @@ def addRandomNoiseToStage(domain, samples = 50, dthr = 0.1, mag = 1E-15): # Sample centroid values with depth greater than dthr n_tri = int(num.sum(domain.tri_full_flag)) - tri_index = num.array(range(0,n_tri)) + tri_index = num.array(list(range(0,n_tri))) submerged = tri_index[depth > dthr] submerged = random.sample(submerged, min(samples, len(submerged))) @@ -70,7 +73,7 @@ def plotCentroidError(domain, control_data, rthr = 1E-7, athr = 1E-12, stage = domain.get_quantity(quantity) actual_data = stage.centroid_values[:n_triangles] adiff = num.fabs((actual_data - local_control_data)) - rdiff = adiff/num.fabs(local_control_data) + rdiff = old_div(adiff,num.fabs(local_control_data)) # Compute masks for error (err_mask) and non-error (acc_mask) vertex indices based on thresholds vertices = domain.get_vertex_coordinates() @@ -102,8 +105,8 @@ def plotCentroidError(domain, control_data, rthr = 1E-7, athr = 1E-12, # Plot non-error triangles in green for i in range(0,size()): - n = int(len(fx[i])/3) - triang = num.array(range(0,3*n)) + n = int(old_div(len(fx[i]),3)) + triang = num.array(list(range(0,3*n))) triang.shape = (n, 3) if len(fx[i]) > 0: @@ -111,9 +114,9 @@ def plotCentroidError(domain, control_data, rthr = 1E-7, athr = 1E-12, # Plot error triangles in blue for i in range(0,size()): - n = int(len(gx[i])/3) + n = int(old_div(len(gx[i]),3)) - triang = num.array(range(0,3*n)) + triang = num.array(list(range(0,3*n))) triang.shape = (n, 3) if len(gx[i]) > 0: diff --git a/anuga/operators/base_operator.py b/anuga/operators/base_operator.py index 1838afd75..7575399d6 100644 --- a/anuga/operators/base_operator.py +++ b/anuga/operators/base_operator.py @@ -1,3 +1,4 @@ +from __future__ import print_function from anuga.utilities.system_tools import log_to_file @@ -83,11 +84,11 @@ def timestepping_statistics(self): def print_statistics(self): - print self.statistics() + print(self.statistics()) def print_timestepping_statistics(self): - print self.timestepping_statistics() + print(self.timestepping_statistics()) def log_timestepping_statistics(self): diff --git a/anuga/operators/boundary_flux_integral_operator.py b/anuga/operators/boundary_flux_integral_operator.py index 8c8f2a194..70254900b 100644 --- a/anuga/operators/boundary_flux_integral_operator.py +++ b/anuga/operators/boundary_flux_integral_operator.py @@ -56,7 +56,7 @@ def __call__(self): elif(ts_method=='rk3'): self.boundary_flux_integral = self.boundary_flux_integral + 1.0/6.0*dt*(self.domain.boundary_flux_sum[0] + self.domain.boundary_flux_sum[1] + 4.0*self.domain.boundary_flux_sum[2]) else: - raise Exception, 'Cannot compute boundary flux integral with this timestepping method' + raise Exception('Cannot compute boundary flux integral with this timestepping method') # Zero the boundary_flux_sum self.domain.boundary_flux_sum[:]=0. diff --git a/anuga/operators/change_friction_operator.py b/anuga/operators/change_friction_operator.py index 70f42922a..a1dfdf091 100644 --- a/anuga/operators/change_friction_operator.py +++ b/anuga/operators/change_friction_operator.py @@ -3,6 +3,7 @@ """ +from __future__ import print_function __author__="steve" __date__ ="$09/03/2012 4:46:39 PM$" @@ -112,7 +113,7 @@ def __call__(self): self.stage_c[:] = self.elev_c + height_c # self.domain.distribute_to_vertices_and_edges() - print 'time in erosion ',self.get_time(), dt + print('time in erosion ',self.get_time(), dt) diff --git a/anuga/operators/elliptic_operator.py b/anuga/operators/elliptic_operator.py index df842e101..2d82d716b 100644 --- a/anuga/operators/elliptic_operator.py +++ b/anuga/operators/elliptic_operator.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import from anuga import Domain from anuga import Quantity from anuga.utilities.sparse import Sparse, Sparse_CSR @@ -5,7 +6,7 @@ import anuga.abstract_2d_finite_volumes.neighbour_mesh as neighbour_mesh from anuga import Dirichlet_boundary import numpy as num -import kinematic_viscosity_operator_ext +from . import kinematic_viscosity_operator_ext import anuga.utilities.log as log from anuga.operators.base_operator import Operator diff --git a/anuga/operators/erosion_operators.py b/anuga/operators/erosion_operators.py index ca8ce7474..52e732acc 100644 --- a/anuga/operators/erosion_operators.py +++ b/anuga/operators/erosion_operators.py @@ -3,6 +3,7 @@ """ +from __future__ import print_function __author__="steve" __date__ ="$09/03/2012 4:46:39 PM$" @@ -257,7 +258,7 @@ def dump_triangulation(self): import matplotlib.pyplot as plt import matplotlib.tri as tri except: - print "Couldn't import module from matplotlib, probably you need to update matplotlib" + print("Couldn't import module from matplotlib, probably you need to update matplotlib") raise domain = self.domain @@ -281,9 +282,9 @@ def dump_triangulation(self): triang = domain.get_triangles() #triang.shape = (n, 3) - print triang.shape - print fx.shape - print Z.shape + print(triang.shape) + print(fx.shape) + print(Z.shape) #plt.tricontourf(fx, fy, triang, Z) plt.triplot(fx, fy, triang) @@ -315,9 +316,9 @@ def dump_triangulation(self): fx1 = fx[self.vols].flatten() fy1 = fy[self.vols].flatten() - print 'fx1', fx1.shape + print('fx1', fx1.shape) - print self.vols + print(self.vols) #gx = vertices[ghost_mask,0] #gy = vertices[ghost_mask,1] @@ -326,7 +327,7 @@ def dump_triangulation(self): n = int(len(fx1)/3) triang = num.array(range(0,3*n)) triang.shape = (n, 3) - print triang + print(triang) plt.triplot(fx1, fy1, triang, 'go-') @@ -340,9 +341,9 @@ def dump_triangulation(self): fx0 = fx[self.indices].flatten() fy0 = fy[self.indices].flatten() - print 'fx0', fx0.shape + print('fx0', fx0.shape) - print self.indices + print(self.indices) #gx = vertices[ghost_mask,0] #gy = vertices[ghost_mask,1] @@ -351,7 +352,7 @@ def dump_triangulation(self): n = int(len(fx0)/3) triang = num.array(range(0,3*n)) triang.shape = (n, 3) - print triang + print(triang) plt.triplot(fx0, fy0, triang, 'bo-') @@ -361,9 +362,9 @@ def dump_triangulation(self): fx0 = fx[self.indices].flatten() fy0 = fy[self.indices].flatten() - print 'fx0', fx0.shape + print('fx0', fx0.shape) - print self.indices + print(self.indices) #gx = vertices[ghost_mask,0] #gy = vertices[ghost_mask,1] @@ -372,7 +373,7 @@ def dump_triangulation(self): n = int(len(fx0)/3) triang = num.array(range(0,3*n)) triang.shape = (n, 3) - print triang + print(triang) plt.triplot(fx0, fy0, triang, 'bo-') @@ -381,7 +382,7 @@ def dump_triangulation(self): fx2 = fx[self.vol_ids,self.vert_ids] fy2 = fy[self.vol_ids,self.vert_ids] - print 'fx2', fx2.shape + print('fx2', fx2.shape) plt.plot(fx2,fy2,'yo') @@ -809,7 +810,7 @@ def update_quantities(self): else: try: value = self.elevation(t) - print value + print(value) if value > num.max(self.elev_v[ind]): self.elev_v[ind] = num.where(self.elev_v[ind] < value, value, self.elev_v[ind]) else: @@ -836,13 +837,13 @@ def lineno(): def stage_elev_info(self): - print 80*"=" + print(80*"=") - print 'In Evolve: line number ', lineno() + print('In Evolve: line number ', lineno()) import inspect - print inspect.getfile(lineno) + print(inspect.getfile(lineno)) - print 80*"=" + print(80*"=") ind = num.array([ 976, 977, 978, 979, 980, 981, 982, 983, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023]) elev_v = self.get_quantity('elevation').vertex_values @@ -851,11 +852,11 @@ def stage_elev_info(self): stage_c = self.get_quantity('stage').centroid_values from pprint import pprint - print 'elev_v, elev_c, elev_avg \n' + print('elev_v, elev_c, elev_avg \n') pprint( num.concatenate( (elev_v[ind], (elev_c[ind]).reshape(16,1), num.mean(elev_v[ind],axis=1).reshape(16,1)), axis = 1)) - print 'stage_v, stage_c, stage_avg \n' + print('stage_v, stage_c, stage_avg \n') pprint( num.concatenate( (stage_v[ind], (stage_c[ind]).reshape(16,1), num.mean(stage_v[ind],axis=1).reshape(16,1)), axis = 1)) - print 80*"=" + print(80*"=") diff --git a/anuga/operators/kinematic_viscosity_operator.py b/anuga/operators/kinematic_viscosity_operator.py index 5f19c57c3..f2d5b5a80 100644 --- a/anuga/operators/kinematic_viscosity_operator.py +++ b/anuga/operators/kinematic_viscosity_operator.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import from anuga import Domain from anuga import Quantity from anuga.utilities.sparse import Sparse, Sparse_CSR @@ -5,7 +6,7 @@ import anuga.abstract_2d_finite_volumes.neighbour_mesh as neighbour_mesh from anuga import Dirichlet_boundary import numpy as num -import kinematic_viscosity_operator_ext +from . import kinematic_viscosity_operator_ext import anuga.utilities.log as log from anuga.operators.base_operator import Operator diff --git a/anuga/operators/tests/test_boundary_flux_integral_operator.py b/anuga/operators/tests/test_boundary_flux_integral_operator.py index 41dfdfbbd..0ee5fbcf7 100644 --- a/anuga/operators/tests/test_boundary_flux_integral_operator.py +++ b/anuga/operators/tests/test_boundary_flux_integral_operator.py @@ -1,3 +1,4 @@ +from __future__ import print_function import unittest import anuga import numpy @@ -73,13 +74,13 @@ def test_boundary_flux_operator_DE0(self): #domain.print_statistics() for t in domain.evolve(yieldstep=1.0,finaltime=5.0): if verbose: domain.print_timestepping_statistics() - if verbose: print domain.get_water_volume() + if verbose: print(domain.get_water_volume()) pass # The domain was initially dry vol=domain.get_water_volume() boundaryFluxInt=domain.get_boundary_flux_integral() - if verbose: print flowalg, vol, boundaryFluxInt + if verbose: print(flowalg, vol, boundaryFluxInt) assert(numpy.allclose(vol,boundaryFluxInt)) @@ -95,13 +96,13 @@ def test_boundary_flux_operator_DE1(self): #domain.print_statistics() for t in domain.evolve(yieldstep=1.0,finaltime=5.0): if verbose: domain.print_timestepping_statistics() - if verbose: print domain.get_water_volume() + if verbose: print(domain.get_water_volume()) pass # The domain was initially dry vol=domain.get_water_volume() boundaryFluxInt=domain.get_boundary_flux_integral() - if verbose: print flowalg, vol, boundaryFluxInt + if verbose: print(flowalg, vol, boundaryFluxInt) assert(numpy.allclose(vol,boundaryFluxInt)) @@ -118,13 +119,13 @@ def test_boundary_flux_operator_DE2(self): #domain.print_statistics() for t in domain.evolve(yieldstep=1.0,finaltime=5.0): if verbose: domain.print_timestepping_statistics() - if verbose: print domain.get_water_volume(), domain.get_boundary_flux_integral() + if verbose: print(domain.get_water_volume(), domain.get_boundary_flux_integral()) pass # The domain was initially dry vol=domain.get_water_volume() boundaryFluxInt=domain.get_boundary_flux_integral() - if verbose: print flowalg, vol, boundaryFluxInt + if verbose: print(flowalg, vol, boundaryFluxInt) assert(numpy.allclose(vol,boundaryFluxInt)) diff --git a/anuga/operators/tests/test_rate_operators.py b/anuga/operators/tests/test_rate_operators.py index 9be630c21..f88f80f89 100644 --- a/anuga/operators/tests/test_rate_operators.py +++ b/anuga/operators/tests/test_rate_operators.py @@ -1,5 +1,7 @@ """ Test environmental forcing - rain, wind, etc. """ +from __future__ import print_function +from future.utils import raise_ import operator import unittest, os @@ -455,10 +457,10 @@ def test_rate_operator_functions_rate_default_rate(self): verbose = False if verbose: - print domain.quantities['elevation'].centroid_values - print domain.quantities['stage'].centroid_values - print domain.quantities['xmomentum'].centroid_values - print domain.quantities['ymomentum'].centroid_values + print(domain.quantities['elevation'].centroid_values) + print(domain.quantities['stage'].centroid_values) + print(domain.quantities['xmomentum'].centroid_values) + print(domain.quantities['ymomentum'].centroid_values) # Apply operator to these triangles indices = [0,1,3] @@ -468,7 +470,7 @@ def test_rate_operator_functions_rate_default_rate(self): def main_rate(t): if t > 20: msg = 'Model time exceeded.' - raise Modeltime_too_late, msg + raise_(Modeltime_too_late, msg) else: return 3.0 * t + 7.0 @@ -488,10 +490,10 @@ def main_rate(t): stage_ex = [ d, d, 1., d] if verbose: - print domain.quantities['elevation'].centroid_values - print domain.quantities['stage'].centroid_values - print domain.quantities['xmomentum'].centroid_values - print domain.quantities['ymomentum'].centroid_values + print(domain.quantities['elevation'].centroid_values) + print(domain.quantities['stage'].centroid_values) + print(domain.quantities['xmomentum'].centroid_values) + print(domain.quantities['ymomentum'].centroid_values) assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) @@ -507,10 +509,10 @@ def main_rate(t): stage_ex = [ d, d, 1., d] if verbose: - print domain.quantities['elevation'].centroid_values - print domain.quantities['stage'].centroid_values - print domain.quantities['xmomentum'].centroid_values - print domain.quantities['ymomentum'].centroid_values + print(domain.quantities['elevation'].centroid_values) + print(domain.quantities['stage'].centroid_values) + print(domain.quantities['xmomentum'].centroid_values) + print(domain.quantities['ymomentum'].centroid_values) assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) @@ -556,10 +558,10 @@ def test_rate_operator_functions_spatial(self): verbose = False if verbose: - print domain.quantities['elevation'].centroid_values - print domain.quantities['stage'].centroid_values - print domain.quantities['xmomentum'].centroid_values - print domain.quantities['ymomentum'].centroid_values + print(domain.quantities['elevation'].centroid_values) + print(domain.quantities['stage'].centroid_values) + print(domain.quantities['xmomentum'].centroid_values) + print(domain.quantities['ymomentum'].centroid_values) # Apply operator to these triangles factor = 10.0 @@ -596,10 +598,10 @@ def main_spatial_rate(x,y,t): stage_ex[:] = d if verbose: - print domain.quantities['elevation'].centroid_values - print domain.quantities['stage'].centroid_values - print domain.quantities['xmomentum'].centroid_values - print domain.quantities['ymomentum'].centroid_values + print(domain.quantities['elevation'].centroid_values) + print(domain.quantities['stage'].centroid_values) + print(domain.quantities['xmomentum'].centroid_values) + print(domain.quantities['ymomentum'].centroid_values) assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) @@ -649,10 +651,10 @@ def test_rate_operator_functions_spatial_with_ghost(self): verbose = False if verbose: - print domain.quantities['elevation'].centroid_values - print domain.quantities['stage'].centroid_values - print domain.quantities['xmomentum'].centroid_values - print domain.quantities['ymomentum'].centroid_values + print(domain.quantities['elevation'].centroid_values) + print(domain.quantities['stage'].centroid_values) + print(domain.quantities['xmomentum'].centroid_values) + print(domain.quantities['ymomentum'].centroid_values) # Apply operator to these triangles factor = 10.0 @@ -693,10 +695,10 @@ def main_spatial_rate(x,y,t): stage_ex[:] = d if verbose: - print domain.quantities['elevation'].centroid_values - print domain.quantities['stage'].centroid_values - print domain.quantities['xmomentum'].centroid_values - print domain.quantities['ymomentum'].centroid_values + print(domain.quantities['elevation'].centroid_values) + print(domain.quantities['stage'].centroid_values) + print(domain.quantities['xmomentum'].centroid_values) + print(domain.quantities['ymomentum'].centroid_values) assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) @@ -742,10 +744,10 @@ def test_rate_operator_functions_spatial_indices(self): verbose = False if verbose: - print domain.quantities['elevation'].centroid_values - print domain.quantities['stage'].centroid_values - print domain.quantities['xmomentum'].centroid_values - print domain.quantities['ymomentum'].centroid_values + print(domain.quantities['elevation'].centroid_values) + print(domain.quantities['stage'].centroid_values) + print(domain.quantities['xmomentum'].centroid_values) + print(domain.quantities['ymomentum'].centroid_values) # Apply operator to these triangles indices = [0,1,3] @@ -782,10 +784,10 @@ def main_spatial_rate(x,y,t): stage_ex[indices] = d if verbose: - print domain.quantities['elevation'].centroid_values - print domain.quantities['stage'].centroid_values - print domain.quantities['xmomentum'].centroid_values - print domain.quantities['ymomentum'].centroid_values + print(domain.quantities['elevation'].centroid_values) + print(domain.quantities['stage'].centroid_values) + print(domain.quantities['xmomentum'].centroid_values) + print(domain.quantities['ymomentum'].centroid_values) assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) @@ -832,10 +834,10 @@ def test_rate_operator_rate_quantity(self): verbose = False if verbose: - print domain.quantities['elevation'].centroid_values - print domain.quantities['stage'].centroid_values - print domain.quantities['xmomentum'].centroid_values - print domain.quantities['ymomentum'].centroid_values + print(domain.quantities['elevation'].centroid_values) + print(domain.quantities['stage'].centroid_values) + print(domain.quantities['xmomentum'].centroid_values) + print(domain.quantities['ymomentum'].centroid_values) # Apply operator to these triangles indices = [0,1,3] @@ -872,10 +874,10 @@ def test_rate_operator_rate_quantity(self): verbose = False if verbose: - print domain.quantities['elevation'].centroid_values - print domain.quantities['stage'].centroid_values - print domain.quantities['xmomentum'].centroid_values - print domain.quantities['ymomentum'].centroid_values + print(domain.quantities['elevation'].centroid_values) + print(domain.quantities['stage'].centroid_values) + print(domain.quantities['xmomentum'].centroid_values) + print(domain.quantities['ymomentum'].centroid_values) assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) @@ -920,10 +922,10 @@ def test_rate_operator_functions_empty_indices(self): verbose = False if verbose: - print domain.quantities['elevation'].centroid_values - print domain.quantities['stage'].centroid_values - print domain.quantities['xmomentum'].centroid_values - print domain.quantities['ymomentum'].centroid_values + print(domain.quantities['elevation'].centroid_values) + print(domain.quantities['stage'].centroid_values) + print(domain.quantities['xmomentum'].centroid_values) + print(domain.quantities['ymomentum'].centroid_values) # Apply operator to these triangles indices = [] @@ -961,10 +963,10 @@ def main_spatial_rate(x,y,t): stage_ex[indices] = d if verbose: - print domain.quantities['elevation'].centroid_values - print domain.quantities['stage'].centroid_values - print domain.quantities['xmomentum'].centroid_values - print domain.quantities['ymomentum'].centroid_values + print(domain.quantities['elevation'].centroid_values) + print(domain.quantities['stage'].centroid_values) + print(domain.quantities['xmomentum'].centroid_values) + print(domain.quantities['ymomentum'].centroid_values) assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) diff --git a/anuga/operators/tests/test_set_quantity.py b/anuga/operators/tests/test_set_quantity.py index a8acc0f1a..1bc8ae717 100644 --- a/anuga/operators/tests/test_set_quantity.py +++ b/anuga/operators/tests/test_set_quantity.py @@ -1,5 +1,6 @@ """ Test set_quantity """ +from __future__ import print_function import unittest, os import anuga @@ -478,7 +479,7 @@ def stage(x,y, t): ratio = float(nrows)/float(ncols) - print ratio + print(ratio) #y = numpy.arange(nrows)*cellsize #x = numpy.arange(ncols)*cellsize diff --git a/anuga/operators/tests/test_set_stage_operator.py b/anuga/operators/tests/test_set_stage_operator.py index 43fb4e33b..8a504a1c1 100644 --- a/anuga/operators/tests/test_set_stage_operator.py +++ b/anuga/operators/tests/test_set_stage_operator.py @@ -1,5 +1,6 @@ """ Test set operators - stage elevation erosion. """ +from __future__ import print_function import unittest, os import anuga @@ -458,7 +459,7 @@ def stage(x,y, t): ratio = float(nrows)/float(ncols) - print ratio + print(ratio) #y = numpy.arange(nrows)*cellsize #x = numpy.arange(ncols)*cellsize diff --git a/anuga/parallel/__init__.py b/anuga/parallel/__init__.py index 5c931f8ee..7ea9f9bcc 100644 --- a/anuga/parallel/__init__.py +++ b/anuga/parallel/__init__.py @@ -4,6 +4,7 @@ Ideally, all tools needed to run parallel simulations should be imported from this module """ +from __future__ import absolute_import @@ -11,17 +12,17 @@ test = Tester().test -from parallel_api import distribute -from parallel_api import myid, numprocs, get_processor_name -from parallel_api import send, receive -from parallel_api import pypar_available, barrier, finalize +from .parallel_api import distribute +from .parallel_api import myid, numprocs, get_processor_name +from .parallel_api import send, receive +from .parallel_api import pypar_available, barrier, finalize if pypar_available: - from parallel_meshes import parallel_rectangle - from parallel_shallow_water import Parallel_domain as Parallel_shallow_water_domain - from parallel_advection import Parallel_domain as Parallel_advection_domain - from parallel_operator_factory import Inlet_operator, Boyd_box_operator, Boyd_pipe_operator - from parallel_operator_factory import Weir_orifice_trapezoid_operator + from .parallel_meshes import parallel_rectangle + from .parallel_shallow_water import Parallel_domain as Parallel_shallow_water_domain + from .parallel_advection import Parallel_domain as Parallel_advection_domain + from .parallel_operator_factory import Inlet_operator, Boyd_box_operator, Boyd_pipe_operator + from .parallel_operator_factory import Weir_orifice_trapezoid_operator else: from anuga import rectangular_cross as parallel_rectangle from anuga import Domain as Parallel_shallow_water_domain diff --git a/anuga/parallel/config.py b/anuga/parallel/config.py index f72e3f253..bde2cfbe0 100644 --- a/anuga/parallel/config.py +++ b/anuga/parallel/config.py @@ -3,6 +3,7 @@ # To change this template, choose Tools | Templates # and open the template in the editor. +from __future__ import print_function __author__="stephen" __date__ ="$27/08/2012 8:58:23 PM$" @@ -11,4 +12,4 @@ if __name__ == "__main__": - print "Hello World"; + print("Hello World"); diff --git a/anuga/parallel/distribute_mesh.py b/anuga/parallel/distribute_mesh.py index 3436170e2..1f3525395 100644 --- a/anuga/parallel/distribute_mesh.py +++ b/anuga/parallel/distribute_mesh.py @@ -1,3 +1,5 @@ +from __future__ import print_function +from __future__ import absolute_import ######################################################### # # @@ -31,7 +33,7 @@ try: import local_config as config except: - import config as config + from . import config as config verbose = False @@ -124,10 +126,10 @@ def reorder_new(quantities, epart_order, proc_sum): try: from anuga.pymetis.metis_ext import partMeshNodal except ImportError: - print "***************************************************" - print " Metis is probably not compiled." - print " Read anuga.pymetis README" - print "***************************************************" + print("***************************************************") + print(" Metis is probably not compiled.") + print(" Read anuga.pymetis README") + print("***************************************************") raise ImportError def pmesh_divide_metis(domain, n_procs): @@ -213,9 +215,9 @@ def pmesh_divide_metis_helper(domain, n_procs): if verbose: from pprint import pprint - print 'epart' + print('epart') pprint(epart) - print 'new_tri_index' + print('new_tri_index') pprint(new_tri_index) #print 50*'=' @@ -611,7 +613,7 @@ def ghost_bnd_layer_old(ghosttri, tlower, tupper, mesh, p): n = mesh.neighbours[t[0], 0] if not is_in_processor(ghost_list, tlower, tupper, n): - if boundary.has_key( (t[0], 0) ): + if (t[0], 0) in boundary: subboundary[t[0], 0] = boundary[t[0],0] else: subboundary[t[0], 0] = 'ghost' @@ -619,7 +621,7 @@ def ghost_bnd_layer_old(ghosttri, tlower, tupper, mesh, p): n = mesh.neighbours[t[0], 1] if not is_in_processor(ghost_list, tlower, tupper, n): - if boundary.has_key( (t[0], 1) ): + if (t[0], 1) in boundary: subboundary[t[0], 1] = boundary[t[0],1] else: subboundary[t[0], 1] = 'ghost' @@ -627,7 +629,7 @@ def ghost_bnd_layer_old(ghosttri, tlower, tupper, mesh, p): n = mesh.neighbours[t[0], 2] if not is_in_processor(ghost_list, tlower, tupper, n): - if boundary.has_key( (t[0], 2) ): + if (t[0], 2) in boundary: subboundary[t[0], 2] = boundary[t[0],2] else: subboundary[t[0], 2] = 'ghost' @@ -1144,7 +1146,7 @@ def build_local_commun(tri_map, ghostc, fullc, nproc): for global_id in fullc: for i in xrange(len(fullc[global_id])): neigh = fullc[global_id][i] - if not tmp_send.has_key(neigh): + if neigh not in tmp_send: tmp_send[neigh] = [] tmp_send[neigh].append([global_id, \ tri_map[global_id]]) @@ -1287,7 +1289,7 @@ def send_submesh(submesh, triangles_per_proc, p, verbose=True): myid = pypar.rank() nprocs = pypar.size() - if verbose: print 'P%d: Sending submesh to P%d' %(myid, p) + if verbose: print('P%d: Sending submesh to P%d' %(myid, p)) # build and send the tagmap for the boundary conditions @@ -1295,12 +1297,12 @@ def send_submesh(submesh, triangles_per_proc, p, verbose=True): counter = 1 for b in submesh["full_boundary"][p]: bkey = submesh["full_boundary"][p][b] - if not tagmap.has_key(bkey): + if bkey not in tagmap: tagmap[bkey] = counter counter = counter+1 for b in submesh["ghost_boundary"][p]: bkey = submesh["ghost_boundary"][p][b] - if not tagmap.has_key(bkey): + if bkey not in tagmap: tagmap[bkey] = counter counter = counter+1 @@ -1424,7 +1426,7 @@ def rec_submesh_flat(p, verbose=True): submesh_cell = {} - if verbose: print indent+'P%d: Receiving submesh from P%d' %(myid, p) + if verbose: print(indent+'P%d: Receiving submesh from P%d' %(myid, p)) # receive the tagmap for the boundary conditions diff --git a/anuga/parallel/parallel_advection.py b/anuga/parallel/parallel_advection.py index 053e77a35..45de16fae 100644 --- a/anuga/parallel/parallel_advection.py +++ b/anuga/parallel/parallel_advection.py @@ -1,3 +1,4 @@ +from __future__ import print_function import sys @@ -49,8 +50,8 @@ def __init__(self, self.communication_reduce_time = 0.0 - print 'processor',self.processor - print 'numproc',self.numproc + print('processor',self.processor) + print('numproc',self.numproc) def check_integrity(self): Domain.check_integrity(self) @@ -133,7 +134,7 @@ def update_ghosts(self): else: #Receive data from the iproc processor - if self.ghost_recv_dict.has_key(iproc): + if iproc in self.ghost_recv_dict: # LINDA: # now store ghost as local id, global id, value @@ -150,7 +151,7 @@ def update_ghosts(self): #local update of ghost cells iproc = self.processor - if self.full_send_dict.has_key(iproc): + if iproc in self.full_send_dict: # LINDA: # now store full as local id, global id, value diff --git a/anuga/parallel/parallel_api.py b/anuga/parallel/parallel_api.py index b6eab24a1..4d3db7134 100644 --- a/anuga/parallel/parallel_api.py +++ b/anuga/parallel/parallel_api.py @@ -2,6 +2,8 @@ """ +from __future__ import print_function +from __future__ import absolute_import import numpy as num @@ -77,7 +79,7 @@ def distribute(domain, verbose=False, debug=False, parameters = None): if myid == 0: - from sequential_distribute import Sequential_distribute + from .sequential_distribute import Sequential_distribute partition = Sequential_distribute(domain, verbose, debug, parameters) partition.distribute(numprocs) @@ -204,7 +206,7 @@ def old_distribute(domain, verbose=False, debug=False, parameters = None): domain_minimum_allowed_height, georef, \ number_of_global_triangles, number_of_global_nodes), p) else: - if verbose: print 'P%d: Receiving domain attributes' %(myid) + if verbose: print('P%d: Receiving domain attributes' %(myid)) domain_name, domain_dir, domain_store, \ domain_store_centroids, domain_smooth, domain_reduction, \ @@ -224,7 +226,7 @@ def old_distribute(domain, verbose=False, debug=False, parameters = None): # FIXME SR: Creates cPickle dump send(boundary_map, p) else: - if verbose: print 'P%d: Receiving boundary map' %(myid) + if verbose: print('P%d: Receiving boundary map' %(myid)) boundary_map = receive(0) @@ -250,20 +252,20 @@ def old_distribute(domain, verbose=False, debug=False, parameters = None): #tri_l2g = p2s_map[tri_l2g] if debug: - print 'P%d' %myid - print 'tri_map ',tri_map - print 'node_map',node_map - print 'tri_l2g', tri_l2g - print 'node_l2g', node_l2g - print 's2p_map', s2p_map - print 'p2s_map', p2s_map + print('P%d' %myid) + print('tri_map ',tri_map) + print('node_map',node_map) + print('tri_l2g', tri_l2g) + print('node_l2g', node_l2g) + print('s2p_map', s2p_map) + print('p2s_map', p2s_map) def protocol(x): vanilla=False from anuga.utilities import parallel_abstraction as pypar control_info, x = pypar.create_control_info(x, vanilla, return_object=True) - print 'protocol', control_info[0] + print('protocol', control_info[0]) # Send serial to parallel (s2p) and parallel to serial (p2s) triangle mapping to proc 1 .. numprocs @@ -287,16 +289,16 @@ def protocol(x): #print p2s_map send(p2s_map_flat, p) else: - if verbose: print 'Not sending s2p_map and p2s_map' + if verbose: print('Not sending s2p_map and p2s_map') s2p_map = None p2s_map = None - if verbose: print 'Communication done' + if verbose: print('Communication done') else: # Read in the mesh partition that belongs to this # processor - if verbose: print 'P%d: Receiving submeshes' %(myid) + if verbose: print('P%d: Receiving submeshes' %(myid)) points, vertices, boundary, quantities,\ ghost_recv_dict, full_send_dict,\ number_of_full_nodes, number_of_full_triangles, \ @@ -327,7 +329,7 @@ def protocol(x): # Build the domain for this processor using partion structures #------------------------------------------------------------------------ - if verbose: print 'myid = %g, no_full_nodes = %g, no_full_triangles = %g' % (myid, number_of_full_nodes, number_of_full_triangles) + if verbose: print('myid = %g, no_full_nodes = %g, no_full_triangles = %g' % (myid, number_of_full_nodes, number_of_full_triangles)) domain = Parallel_domain(points, vertices, boundary, @@ -396,7 +398,7 @@ def distribute_mesh(domain, verbose=False, debug=False, parameters=None): # Subdivide the mesh - if verbose: print 'Subdivide mesh' + if verbose: print('Subdivide mesh') new_nodes, new_triangles, new_boundary, triangles_per_proc, quantities, \ s2p_map, p2s_map = \ pmesh_divide_metis_with_map(domain, numprocs) @@ -408,15 +410,15 @@ def distribute_mesh(domain, verbose=False, debug=False, parameters=None): # Build the mesh that should be assigned to each processor, # this includes ghost nodes and the communication pattern - if verbose: print 'Build submeshes' + if verbose: print('Build submeshes') submesh = build_submesh(new_nodes, new_triangles, new_boundary, quantities, triangles_per_proc, parameters) if verbose: for p in range(numprocs): N = len(submesh['ghost_nodes'][p]) M = len(submesh['ghost_triangles'][p]) - print 'There are %d ghost nodes and %d ghost triangles on proc %d'\ - %(N, M, p) + print('There are %d ghost nodes and %d ghost triangles on proc %d'\ + %(N, M, p)) #if debug: # from pprint import pprint @@ -424,7 +426,7 @@ def distribute_mesh(domain, verbose=False, debug=False, parameters=None): # Send the mesh partition to the appropriate processor - if verbose: print 'Distribute submeshes' + if verbose: print('Distribute submeshes') for p in range(1, numprocs): send_submesh(submesh, triangles_per_proc, p2s_map, p, verbose) diff --git a/anuga/parallel/parallel_boyd_box_operator.py b/anuga/parallel/parallel_boyd_box_operator.py index e25197d5a..f8dd52523 100644 --- a/anuga/parallel/parallel_boyd_box_operator.py +++ b/anuga/parallel/parallel_boyd_box_operator.py @@ -1,11 +1,12 @@ +from __future__ import absolute_import import anuga import math import numpy from anuga.structures.boyd_box_operator import boyd_box_function -from parallel_inlet_operator import Parallel_Inlet_operator -from parallel_structure_operator import Parallel_Structure_operator +from .parallel_inlet_operator import Parallel_Inlet_operator +from .parallel_structure_operator import Parallel_Structure_operator class Parallel_Boyd_box_operator(Parallel_Structure_operator): """Culvert flow - transfer water from one rectangular box to another. diff --git a/anuga/parallel/parallel_boyd_pipe_operator.py b/anuga/parallel/parallel_boyd_pipe_operator.py index b36f1f43d..1999ae354 100644 --- a/anuga/parallel/parallel_boyd_pipe_operator.py +++ b/anuga/parallel/parallel_boyd_pipe_operator.py @@ -1,11 +1,13 @@ +from __future__ import print_function +from __future__ import absolute_import import anuga import math import numpy from anuga.structures.boyd_pipe_operator import boyd_pipe_function -from parallel_inlet_operator import Parallel_Inlet_operator -from parallel_structure_operator import Parallel_Structure_operator +from .parallel_inlet_operator import Parallel_Inlet_operator +from .parallel_structure_operator import Parallel_Structure_operator class Parallel_Boyd_pipe_operator(Parallel_Structure_operator): """Culvert flow - transfer water from one rectangular box to another. @@ -112,8 +114,8 @@ def __init__(self, self.case = 'N/A' - print 80*'=' - print "DON'T USE BOYD PIPES AS ALGORITHM NOT VERIFIED YET" + print(80*'=') + print("DON'T USE BOYD PIPES AS ALGORITHM NOT VERIFIED YET") # May/June 2014 -- allow 'smoothing ' of driving_energy, delta total energy, and outflow_enq_depth diff --git a/anuga/parallel/parallel_generic_communications.py b/anuga/parallel/parallel_generic_communications.py index a341ed8ea..a84a511a8 100644 --- a/anuga/parallel/parallel_generic_communications.py +++ b/anuga/parallel/parallel_generic_communications.py @@ -100,7 +100,7 @@ def communicate_ghosts_blocking(domain): else: #Receive data from the iproc processor - if domain.ghost_recv_dict.has_key(iproc): + if iproc in domain.ghost_recv_dict: Idg = domain.ghost_recv_dict[iproc][0] X = domain.ghost_recv_dict[iproc][2] @@ -114,7 +114,7 @@ def communicate_ghosts_blocking(domain): #local update of ghost cells iproc = domain.processor - if domain.full_send_dict.has_key(iproc): + if iproc in domain.full_send_dict: # LINDA: # now store full as local id, global id, value diff --git a/anuga/parallel/parallel_inlet.py b/anuga/parallel/parallel_inlet.py index c507ae1bd..71918946a 100644 --- a/anuga/parallel/parallel_inlet.py +++ b/anuga/parallel/parallel_inlet.py @@ -1,3 +1,4 @@ +from __future__ import print_function # To change this template, choose Tools | Templates # and open the template in the editor. @@ -79,7 +80,7 @@ def compute_area(self): if len(self.triangle_indices) == 0: region = 'Inlet line=%s' % (self.line) msg = 'No triangles have been identified in region ' - print "WARNING: " + msg + print("WARNING: " + msg) self.area = 0.0 for j in self.triangle_indices: @@ -619,4 +620,4 @@ def statistics(self): __date__ ="$16/08/2011 6:49:42 PM$" if __name__ == "__main__": - print "Hello World" + print("Hello World") diff --git a/anuga/parallel/parallel_inlet_enquiry.py b/anuga/parallel/parallel_inlet_enquiry.py index 7a87bd5f1..b386a8649 100644 --- a/anuga/parallel/parallel_inlet_enquiry.py +++ b/anuga/parallel/parallel_inlet_enquiry.py @@ -1,10 +1,11 @@ +from __future__ import absolute_import from anuga.geometry.polygon import inside_polygon, is_inside_polygon, line_intersect from anuga.config import velocity_protection, g import math import numpy as num -import parallel_inlet +from . import parallel_inlet class Parallel_Inlet_enquiry(parallel_inlet.Parallel_Inlet): """Contains information associated with each inlet plus an enquiry point diff --git a/anuga/parallel/parallel_inlet_operator.py b/anuga/parallel/parallel_inlet_operator.py index 1eac9741e..bc55827ee 100644 --- a/anuga/parallel/parallel_inlet_operator.py +++ b/anuga/parallel/parallel_inlet_operator.py @@ -1,3 +1,5 @@ +from __future__ import print_function +from __future__ import absolute_import # To change this template, choose Tools | Templates # and open the template in the editor. @@ -8,7 +10,7 @@ from anuga.utilities.system_tools import log_to_file from anuga.structures.inlet_operator import Inlet_operator -import parallel_inlet +from . import parallel_inlet class Parallel_Inlet_operator(Inlet_operator): @@ -167,9 +169,9 @@ def update_Q(self, t): if callable(self.Q): try: Q = self.Q(t) - except Modeltime_too_early, e: + except Modeltime_too_early as e: Q = self.get_default(t) - except Modeltime_too_late, e: + except Modeltime_too_late as e: Q = self.get_default(t) else: Q = self.Q @@ -206,7 +208,7 @@ def print_statistics(self): # WARNING: requires synchronization, must be called by all procs associated # with this inlet - print self.statistics() + print(self.statistics()) def print_timestepping_statistics(self): @@ -218,7 +220,7 @@ def print_timestepping_statistics(self): message += '--------------------------------------------\n' message += 'Q [m^3/s]: %.2f\n' % self.applied_Q - print message + print(message) def set_logging(self, flag=True): diff --git a/anuga/parallel/parallel_internal_boundary_operator.py b/anuga/parallel/parallel_internal_boundary_operator.py index 8843cea7d..bdf52ffe8 100644 --- a/anuga/parallel/parallel_internal_boundary_operator.py +++ b/anuga/parallel/parallel_internal_boundary_operator.py @@ -1,3 +1,5 @@ +from __future__ import print_function +from __future__ import absolute_import import anuga import math import numpy @@ -7,8 +9,8 @@ #from anuga.structures.boyd_box_operator import boyd_box_function -from parallel_inlet_operator import Parallel_Inlet_operator -from parallel_structure_operator import Parallel_Structure_operator +from .parallel_inlet_operator import Parallel_Inlet_operator +from .parallel_structure_operator import Parallel_Structure_operator class Parallel_Internal_boundary_operator(Parallel_Structure_operator): """ @@ -43,11 +45,11 @@ def __init__(self, enquiry_proc = [0,0]): if verbose: - print '########################################' - print 'PARALLEL INTERNAL BOUNDARY OPERATOR' - print 'THIS IS EXPERIMENTAL' - print 'SUBJECT TO CHANGE WITHOUT NOTICE' - print '########################################' + print('########################################') + print('PARALLEL INTERNAL BOUNDARY OPERATOR') + print('THIS IS EXPERIMENTAL') + print('SUBJECT TO CHANGE WITHOUT NOTICE') + print('########################################') # Since no barrel_velocity is computed we cannot use_momentum_jet use_momentum_jet = False diff --git a/anuga/parallel/parallel_meshes.py b/anuga/parallel/parallel_meshes.py index 58d217edf..e9d4e0fe6 100644 --- a/anuga/parallel/parallel_meshes.py +++ b/anuga/parallel/parallel_meshes.py @@ -11,6 +11,7 @@ Modified by Linda Stals, March 2006, to include ghost boundaries """ +from __future__ import absolute_import import sys @@ -19,10 +20,10 @@ from anuga.config import epsilon -from parallel_api import distribute -from parallel_api import myid, numprocs, get_processor_name -from parallel_api import send, receive -from parallel_api import pypar_available, barrier, finalize +from .parallel_api import distribute +from .parallel_api import myid, numprocs, get_processor_name +from .parallel_api import send, receive +from .parallel_api import pypar_available, barrier, finalize diff --git a/anuga/parallel/parallel_operator_factory.py b/anuga/parallel/parallel_operator_factory.py index 9c183945a..d11a8fd59 100644 --- a/anuga/parallel/parallel_operator_factory.py +++ b/anuga/parallel/parallel_operator_factory.py @@ -1,3 +1,5 @@ +from __future__ import print_function +from __future__ import absolute_import # To change this template, choose Tools | Templates # and open the template in the editor. @@ -12,12 +14,12 @@ import anuga from math import pi, pow, sqrt import numpy as num -from parallel_inlet_operator import Parallel_Inlet_operator -from parallel_structure_operator import Parallel_Structure_operator -from parallel_boyd_box_operator import Parallel_Boyd_box_operator -from parallel_boyd_pipe_operator import Parallel_Boyd_pipe_operator -from parallel_weir_orifice_trapezoid_operator import Parallel_Weir_orifice_trapezoid_operator -from parallel_internal_boundary_operator import Parallel_Internal_boundary_operator +from .parallel_inlet_operator import Parallel_Inlet_operator +from .parallel_structure_operator import Parallel_Structure_operator +from .parallel_boyd_box_operator import Parallel_Boyd_box_operator +from .parallel_boyd_pipe_operator import Parallel_Boyd_pipe_operator +from .parallel_weir_orifice_trapezoid_operator import Parallel_Weir_orifice_trapezoid_operator +from .parallel_internal_boundary_operator import Parallel_Internal_boundary_operator from . import distribute, myid, numprocs, finalize from anuga.geometry.polygon import inside_polygon, is_inside_polygon, line_intersect @@ -57,7 +59,7 @@ def Inlet_operator(domain, # If not parallel domain then allocate serial Inlet operator if isinstance(domain, Parallel_domain) is False: - if verbose: print "Allocating non parallel inlet operator ....." + if verbose: print("Allocating non parallel inlet operator .....") return anuga.structures.inlet_operator.Inlet_operator(domain, poly, Q, @@ -86,11 +88,11 @@ def Inlet_operator(domain, if alloc: if verbose and myid == inlet_master_proc: - print "Parallel Inlet Operator =================" - print "Poly = " + str(poly) - print "Master Processor is P%d" %(inlet_master_proc) - print "Processors are P%s" %(inlet_procs) - print "=========================================" + print("Parallel Inlet Operator =================") + print("Poly = " + str(poly)) + print("Master Processor is P%d" %(inlet_master_proc)) + print("Processors are P%s" %(inlet_procs)) + print("=========================================") return Parallel_Inlet_operator(domain, poly, @@ -143,7 +145,7 @@ def Boyd_box_operator(domain, # If not parallel domain then allocate serial Boyd box operator if isinstance(domain, Parallel_domain) is False: - if verbose: print "Allocating non parallel boyd box operator ....." + if verbose: print("Allocating non parallel boyd box operator .....") return anuga.structures.boyd_box_operator.Boyd_box_operator(domain=domain, losses=losses, width=width, @@ -209,7 +211,7 @@ def Boyd_box_operator(domain, pypar.send(exchange_lines_tmp, i) pypar.send(enquiry_points_tmp, i) else: - raise Exception, 'Define either exchange_lines or end_points' + raise Exception('Define either exchange_lines or end_points') else: if exchange_lines is not None: @@ -241,15 +243,15 @@ def Boyd_box_operator(domain, enquiry_proc = [enquiry0_proc, enquiry1_proc] if myid == master_proc and verbose: - print "Parallel Boyd Box Operator =============================" - print "Structure Master Proc is P" + str(inlet0_master_proc) - print "Structure Procs are P" + str(structure_procs) - print "Inlet Master Procs are P" + str(inlet_master_proc) - print "Inlet Procs are P" + str(inlet_procs[0]) + " and " + str(inlet_procs[1]) - print "Inlet Enquiry Procs are P" + str(enquiry_proc) - print "Enquiry Points are " + str(enquiry_point0) + " and " + str(enquiry_point1) - print "Inlet Exchange Lines are " + str(line0) + " and " + str(line1) - print "========================================================" + print("Parallel Boyd Box Operator =============================") + print("Structure Master Proc is P" + str(inlet0_master_proc)) + print("Structure Procs are P" + str(structure_procs)) + print("Inlet Master Procs are P" + str(inlet_master_proc)) + print("Inlet Procs are P" + str(inlet_procs[0]) + " and " + str(inlet_procs[1])) + print("Inlet Enquiry Procs are P" + str(enquiry_proc)) + print("Enquiry Points are " + str(enquiry_point0) + " and " + str(enquiry_point1)) + print("Inlet Exchange Lines are " + str(line0) + " and " + str(line1)) + print("========================================================") if alloc0 or alloc1: return Parallel_Boyd_box_operator(domain=domain, @@ -317,7 +319,7 @@ def Boyd_pipe_operator(domain, # If not parallel domain then allocate serial Boyd box operator if isinstance(domain, Parallel_domain) is False: - if verbose: print "Allocating non parallel boyd pipe operator ....." + if verbose: print("Allocating non parallel boyd pipe operator .....") return anuga.structures.boyd_pipe_operator.Boyd_pipe_operator(domain=domain, losses=losses, diameter=diameter, @@ -374,7 +376,7 @@ def Boyd_pipe_operator(domain, pypar.send(exchange_lines_tmp, i) pypar.send(enquiry_points_tmp, i) else: - raise Exception, 'Define either exchange_lines or end_points' + raise Exception('Define either exchange_lines or end_points') else: if exchange_lines is not None: @@ -406,15 +408,15 @@ def Boyd_pipe_operator(domain, enquiry_proc = [enquiry0_proc, enquiry1_proc] if myid == master_proc and verbose: - print "Parallel Boyd Pipe Operator =============================" - print "Structure Master Proc is P" + str(inlet0_master_proc) - print "Structure Procs are P" + str(structure_procs) - print "Inlet Master Procs are P" + str(inlet_master_proc) - print "Inlet Procs are P" + str(inlet_procs[0]) + " and " + str(inlet_procs[1]) - print "Inlet Enquiry Procs are P" + str(enquiry_proc) - print "Enquiry Points are " + str(enquiry_point0) + " and " + str(enquiry_point1) - print "Inlet Exchange Lines are " + str(line0) + " and " + str(line1) - print "========================================================" + print("Parallel Boyd Pipe Operator =============================") + print("Structure Master Proc is P" + str(inlet0_master_proc)) + print("Structure Procs are P" + str(structure_procs)) + print("Inlet Master Procs are P" + str(inlet_master_proc)) + print("Inlet Procs are P" + str(inlet_procs[0]) + " and " + str(inlet_procs[1])) + print("Inlet Enquiry Procs are P" + str(enquiry_proc)) + print("Enquiry Points are " + str(enquiry_point0) + " and " + str(enquiry_point1)) + print("Inlet Exchange Lines are " + str(line0) + " and " + str(line1)) + print("========================================================") if alloc0 or alloc1: return Parallel_Boyd_pipe_operator(domain=domain, @@ -488,7 +490,7 @@ def Weir_orifice_trapezoid_operator(domain, # If not parallel domain then allocate serial Weir orifice trapezoid operator if isinstance(domain, Parallel_domain) is False: - if verbose: print "Allocating non parallel weir orifice trapzezoid operator ....." + if verbose: print("Allocating non parallel weir orifice trapzezoid operator .....") return anuga.structures.weir_orifice_trapezoid_operator.Weir_orifice_trapezoid_operator(domain=domain, losses=losses, width=width, @@ -555,7 +557,7 @@ def Weir_orifice_trapezoid_operator(domain, pypar.send(exchange_lines_tmp, i) pypar.send(enquiry_points_tmp, i) else: - raise Exception, 'Define either exchange_lines or end_points' + raise Exception('Define either exchange_lines or end_points') else: if exchange_lines is not None: @@ -587,15 +589,15 @@ def Weir_orifice_trapezoid_operator(domain, enquiry_proc = [enquiry0_proc, enquiry1_proc] if myid == master_proc and verbose: - print "Parallel Weir Orifice Trapezoid Operator =============================" - print "Structure Master Proc is P" + str(inlet0_master_proc) - print "Structure Procs are P" + str(structure_procs) - print "Inlet Master Procs are P" + str(inlet_master_proc) - print "Inlet Procs are P" + str(inlet_procs[0]) + " and " + str(inlet_procs[1]) - print "Inlet Enquiry Procs are P" + str(enquiry_proc) - print "Enquiry Points are " + str(enquiry_point0) + " and " + str(enquiry_point1) - print "Inlet Exchange Lines are " + str(line0) + " and " + str(line1) - print "========================================================" + print("Parallel Weir Orifice Trapezoid Operator =============================") + print("Structure Master Proc is P" + str(inlet0_master_proc)) + print("Structure Procs are P" + str(structure_procs)) + print("Inlet Master Procs are P" + str(inlet_master_proc)) + print("Inlet Procs are P" + str(inlet_procs[0]) + " and " + str(inlet_procs[1])) + print("Inlet Enquiry Procs are P" + str(enquiry_proc)) + print("Enquiry Points are " + str(enquiry_point0) + " and " + str(enquiry_point1)) + print("Inlet Exchange Lines are " + str(line0) + " and " + str(line1)) + print("========================================================") if alloc0 or alloc1: return Parallel_Weir_orifice_trapezoid_operator(domain=domain, @@ -668,7 +670,7 @@ def Internal_boundary_operator(domain, # If not parallel domain then allocate serial Internal boundary operator if isinstance(domain, Parallel_domain) is False: - if verbose: print "Allocating non parallel internal_boundary operator ....." + if verbose: print("Allocating non parallel internal_boundary operator .....") return anuga.structures.internal_boundary_operator.Internal_boundary_operator(domain=domain, internal_boundary_function=internal_boundary_function, width=width, @@ -727,7 +729,7 @@ def Internal_boundary_operator(domain, pypar.send(exchange_lines_tmp, i) pypar.send(enquiry_points_tmp, i) else: - raise Exception, 'Define either exchange_lines or end_points' + raise Exception('Define either exchange_lines or end_points') else: if exchange_lines is not None: @@ -759,15 +761,15 @@ def Internal_boundary_operator(domain, enquiry_proc = [enquiry0_proc, enquiry1_proc] if myid == master_proc and verbose: - print "Parallel Internal boundary Operator =============================" - print "Structure Master Proc is P" + str(inlet0_master_proc) - print "Structure Procs are P" + str(structure_procs) - print "Inlet Master Procs are P" + str(inlet_master_proc) - print "Inlet Procs are P" + str(inlet_procs[0]) + " and " + str(inlet_procs[1]) - print "Inlet Enquiry Procs are P" + str(enquiry_proc) - print "Enquiry Points are " + str(enquiry_point0) + " and " + str(enquiry_point1) - print "Inlet Exchange Lines are " + str(line0) + " and " + str(line1) - print "========================================================" + print("Parallel Internal boundary Operator =============================") + print("Structure Master Proc is P" + str(inlet0_master_proc)) + print("Structure Procs are P" + str(structure_procs)) + print("Inlet Master Procs are P" + str(inlet_master_proc)) + print("Inlet Procs are P" + str(inlet_procs[0]) + " and " + str(inlet_procs[1])) + print("Inlet Enquiry Procs are P" + str(enquiry_proc)) + print("Enquiry Points are " + str(enquiry_point0) + " and " + str(enquiry_point1)) + print("Inlet Exchange Lines are " + str(line0) + " and " + str(line1)) + print("========================================================") if alloc0 or alloc1: return Parallel_Internal_boundary_operator(domain=domain, @@ -894,17 +896,17 @@ def allocate_inlet_procs(domain, poly, enquiry_point = None, master_proc = 0, pr #tri_id = line_intersect(vertex_coordinates, poly) if len(poly) == 2: # poly is a line - if verbose : print "======================" + if verbose : print("======================") tri_id = line_intersect(vertex_coordinates, poly) else: # poly is a polygon - if verbose : print "+++++++++++++++++++++++" + if verbose : print("+++++++++++++++++++++++") tris_0 = line_intersect(vertex_coordinates, [poly[0],poly[1]]) tris_1 = inside_polygon(domain_centroids, poly) tri_id = num.union1d(tris_0, tris_1) if verbose: - print "P%d has %d triangles in poly %s" %(myid, len(tri_id), poly) + print("P%d has %d triangles in poly %s" %(myid, len(tri_id), poly)) size = len(tri_id) @@ -915,12 +917,12 @@ def allocate_inlet_procs(domain, poly, enquiry_point = None, master_proc = 0, pr if domain.tri_full_flag[k] == 1: size = size + 1 has_enq_point = True - if verbose: print "P%d has enq point %s" %(myid, enquiry_point) + if verbose: print("P%d has enq point %s" %(myid, enquiry_point)) else: - if verbose: print "P%d contains ghost copy of enq point %s" %(myid, enquiry_point) + if verbose: print("P%d contains ghost copy of enq point %s" %(myid, enquiry_point)) has_enq_point = False except: - if verbose: print "P%d does not contain enq point %s" %(myid, enquiry_point) + if verbose: print("P%d does not contain enq point %s" %(myid, enquiry_point)) has_enq_point = False if myid == master_proc: @@ -986,4 +988,4 @@ def allocate_inlet_procs(domain, poly, enquiry_point = None, master_proc = 0, pr __date__ ="$06/09/2011 1:17:57 PM$" if __name__ == "__main__": - print "Parallel operator factory" + print("Parallel operator factory") diff --git a/anuga/parallel/parallel_shallow_water.py b/anuga/parallel/parallel_shallow_water.py index 174fc463e..b3cfbd5df 100644 --- a/anuga/parallel/parallel_shallow_water.py +++ b/anuga/parallel/parallel_shallow_water.py @@ -10,10 +10,12 @@ Geoscience Australia, 2004-2005 """ +from __future__ import print_function +from __future__ import absolute_import from anuga import Domain -import parallel_generic_communications as generic_comms +from . import parallel_generic_communications as generic_comms import anuga.utilities.parallel_abstraction as pypar @@ -223,7 +225,7 @@ def dump_triangulation(self, filename="domain.png"): import matplotlib.pyplot as plt import matplotlib.tri as tri except: - print "Couldn't import module from matplotlib, probably you need to update matplotlib" + print("Couldn't import module from matplotlib, probably you need to update matplotlib") raise vertices = self.get_vertex_coordinates() diff --git a/anuga/parallel/parallel_structure_operator.py b/anuga/parallel/parallel_structure_operator.py index cd45f74cc..8fd599732 100644 --- a/anuga/parallel/parallel_structure_operator.py +++ b/anuga/parallel/parallel_structure_operator.py @@ -1,7 +1,9 @@ +from __future__ import print_function +from __future__ import absolute_import import anuga import numpy as num import math -import parallel_inlet_enquiry +from . import parallel_inlet_enquiry from anuga.utilities import parallel_abstraction as pypar from anuga.utilities.system_tools import log_to_file @@ -166,7 +168,7 @@ def __init__(self, elif end_points is not None: self.__process_non_skew_culvert() else: - raise Exception, 'Define either exchange_lines or end_points' + raise Exception('Define either exchange_lines or end_points') self.inlets = [] @@ -536,7 +538,7 @@ def __process_skew_culvert(self): self.culvert_vector = centre_point1 - centre_point0 else: - raise Exception, 'n_exchange_0 != 2 or 4' + raise Exception('n_exchange_0 != 2 or 4') self.culvert_length = math.sqrt(num.sum(self.culvert_vector**2)) assert self.culvert_length > 0.0, 'The length of culvert is less than 0' @@ -636,7 +638,7 @@ def print_statistics(self): # Warning: requires synchronization, must be called by all procs associated # with this structure - print self.statistics() + print(self.statistics()) def print_timestepping_statistics(self): @@ -657,7 +659,7 @@ def print_timestepping_statistics(self): message += 'Delta Total Energy %.2f\n' % self.delta_total_energy message += 'Control at this instant: %s\n' % self.case - print message + print(message) def set_parallel_logging(self, flag=True): diff --git a/anuga/parallel/parallel_weir_orifice_trapezoid_operator.py b/anuga/parallel/parallel_weir_orifice_trapezoid_operator.py index 315ccfc7b..80d622bab 100644 --- a/anuga/parallel/parallel_weir_orifice_trapezoid_operator.py +++ b/anuga/parallel/parallel_weir_orifice_trapezoid_operator.py @@ -1,11 +1,12 @@ +from __future__ import absolute_import import anuga import math import numpy from anuga.structures.weir_orifice_trapezoid_operator import weir_orifice_trapezoid_function -from parallel_inlet_operator import Parallel_Inlet_operator -from parallel_structure_operator import Parallel_Structure_operator +from .parallel_inlet_operator import Parallel_Inlet_operator +from .parallel_structure_operator import Parallel_Structure_operator class Parallel_Weir_orifice_trapezoid_operator(Parallel_Structure_operator): """Culvert flow - transfer water from one trapezoid section to another. diff --git a/anuga/parallel/print_stats.py b/anuga/parallel/print_stats.py index 83ed3bc8a..d427f11ac 100644 --- a/anuga/parallel/print_stats.py +++ b/anuga/parallel/print_stats.py @@ -16,6 +16,7 @@ # ######################################################### +from __future__ import print_function import sys from anuga.utilities import parallel_abstraction as pypar @@ -85,7 +86,7 @@ def print_l1_stats(full_edge): tri_norm[0] = tri_norm[0]+recv_norm[0] tri_norm[1] = tri_norm[1]+recv_norm[1] tri_norm[2] = tri_norm[2]+recv_norm[2] - print 'l1_norm along each axis : [', tri_norm[0],', ', tri_norm[1], ', ', tri_norm[2], ']' + print('l1_norm along each axis : [', tri_norm[0],', ', tri_norm[1], ', ', tri_norm[2], ']') else: pypar.send(tri_norm, 0) @@ -122,8 +123,8 @@ def print_l2_stats(full_edge): tri_norm[0] = tri_norm[0]+recv_norm[0] tri_norm[1] = tri_norm[1]+recv_norm[1] tri_norm[2] = tri_norm[2]+recv_norm[2] - print 'l2_norm along each axis : [', pow(tri_norm[0], 0.5),', ', pow(tri_norm[1], 0.5), \ - ', ', pow(tri_norm[2], 0.5), ']' + print('l2_norm along each axis : [', pow(tri_norm[0], 0.5),', ', pow(tri_norm[1], 0.5), \ + ', ', pow(tri_norm[2], 0.5), ']') else: pypar.send(tri_norm, 0) @@ -161,7 +162,7 @@ def print_linf_stats(full_edge): tri_norm[0] = max(tri_norm[0], recv_norm[0]) tri_norm[1] = max(tri_norm[1], recv_norm[1]) tri_norm[2] = max(tri_norm[2], recv_norm[2]) - print 'linf_norm along each axis : [', tri_norm[0],', ', tri_norm[1], ', ', tri_norm[2], ']' + print('linf_norm along each axis : [', tri_norm[0],', ', tri_norm[1], ', ', tri_norm[2], ']') else: pypar.send(tri_norm, 0) @@ -197,7 +198,7 @@ def print_test_stats(domain, tri_full_flag): for k in domain.quantities.keys(): TestStage = domain.quantities[k] if myid == 0: - print " ===== ", k, " ===== " + print(" ===== ", k, " ===== ") full_edge = take(TestStage.edge_values, nonzero(tri_full_flag)) print_l1_stats(full_edge) print_l2_stats(full_edge) diff --git a/anuga/parallel/sequential_distribute.py b/anuga/parallel/sequential_distribute.py index cf39f4999..ef62d3ac2 100644 --- a/anuga/parallel/sequential_distribute.py +++ b/anuga/parallel/sequential_distribute.py @@ -2,6 +2,7 @@ """ +from __future__ import print_function import numpy as num @@ -67,7 +68,7 @@ def distribute(self, numprocs=1): # Subdivide the mesh - if verbose: print 'sequential_distribute: Subdivide mesh' + if verbose: print('sequential_distribute: Subdivide mesh') new_nodes, new_triangles, new_boundary, triangles_per_proc, quantities, \ s2p_map, p2s_map = \ @@ -76,8 +77,8 @@ def distribute(self, numprocs=1): # Build the mesh that should be assigned to each processor, # this includes ghost nodes and the communication pattern - if verbose: print 'sequential_distribute: Build submeshes' - if verbose: print 'sequential_distribute: parameters = ',parameters + if verbose: print('sequential_distribute: Build submeshes') + if verbose: print('sequential_distribute: parameters = ',parameters) submesh = build_submesh(new_nodes, new_triangles, new_boundary, \ quantities, triangles_per_proc, parameters=parameters) @@ -86,8 +87,8 @@ def distribute(self, numprocs=1): for p in range(numprocs): N = len(submesh['ghost_nodes'][p]) M = len(submesh['ghost_triangles'][p]) - print 'There are %d ghost nodes and %d ghost triangles on proc %d'\ - %(N, M, p) + print('There are %d ghost nodes and %d ghost triangles on proc %d'\ + %(N, M, p)) self.submesh = submesh @@ -121,13 +122,13 @@ def extract_submesh(self, p=0): if debug: import pprint - print 50*"=" - print 'NODE_L2G' + print(50*"=") + print('NODE_L2G') pprint.pprint(node_l2g) pprint.pprint(node_l2g[vertices[:,0]]) - print 'VERTICES' + print('VERTICES') pprint.pprint(vertices[:,0]) pprint.pprint(new_triangles[tri_l2g,0]) @@ -136,14 +137,14 @@ def extract_submesh(self, p=0): assert num.allclose(node_l2g[vertices[:,2]], new_triangles[tri_l2g,2]) - print 'POINTS' + print('POINTS') pprint.pprint(points) assert num.allclose(points[:,0], new_nodes[node_l2g,0]) assert num.allclose(points[:,1], new_nodes[node_l2g,1]) - print 'TRI' + print('TRI') pprint.pprint(tri_l2g) pprint.pprint(p2s_map[tri_l2g]) @@ -152,7 +153,7 @@ def extract_submesh(self, p=0): assert num.allclose(original_triangles[tri_l2orig,1],node_l2g[vertices[:,1]]) assert num.allclose(original_triangles[tri_l2orig,2],node_l2g[vertices[:,2]]) - print 'NODES' + print('NODES') pprint.pprint(node_map) pprint.pprint(node_l2g) @@ -166,7 +167,7 @@ def extract_submesh(self, p=0): #------------------------------------------------------------------------ if verbose: - print 'sequential_distribute: P%g, no_full_nodes = %g, no_full_triangles = %g' % (p, number_of_full_nodes, number_of_full_triangles) + print('sequential_distribute: P%g, no_full_nodes = %g, no_full_triangles = %g' % (p, number_of_full_nodes, number_of_full_triangles)) kwargs = {'full_send_dict': full_send_dict, diff --git a/anuga/parallel/tests/skip_parallel_boyd_box_op_apron.py b/anuga/parallel/tests/skip_parallel_boyd_box_op_apron.py index c5948c551..c53050ce2 100644 --- a/anuga/parallel/tests/skip_parallel_boyd_box_op_apron.py +++ b/anuga/parallel/tests/skip_parallel_boyd_box_op_apron.py @@ -1,3 +1,5 @@ +from __future__ import print_function +from future.utils import raise_ import os.path import sys @@ -155,7 +157,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve except: tri_ids.append(-2) - if verbose: print 'P%d has points = %s' %(myid, tri_ids) + if verbose: print('P%d has points = %s' %(myid, tri_ids)) if not parallel: control_data = [] @@ -185,7 +187,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve #if inlet1 is not None and verbose: inlet1.print_statistics() if boyd_box0 is not None and verbose: - print "++++", myid + print("++++", myid) boyd_box0.print_statistics() # if parallel: @@ -235,7 +237,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve if boyd_box0 is not None and verbose : if myid == boyd_box0.master_proc: - print 'master_proc ',myid + print('master_proc ',myid) boyd_box0.print_timestepping_statistics() #for i in range(samples): @@ -268,7 +270,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve control_data.append(inlet0.inlet.get_total_water_volume()) control_data.append(inlet0.inlet.get_average_depth()) - if verbose: print 'P%d control_data = %s' %(myid, control_data) + if verbose: print('P%d control_data = %s' %(myid, control_data)) else: stage = domain.get_quantity('stage') @@ -277,7 +279,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve local_success = num.allclose(control_data[i], stage.centroid_values[tri_ids[i]]) success = success and local_success if verbose: - print 'P%d tri %d, control = %s, actual = %s, Success = %s' %(myid, i, control_data[i], stage.centroid_values[tri_ids[i]], local_success) + print('P%d tri %d, control = %s, actual = %s, Success = %s' %(myid, i, control_data[i], stage.centroid_values[tri_ids[i]], local_success)) if inlet0 is not None: @@ -290,15 +292,15 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve if myid == inlet_master_proc: if verbose: - print 'P%d average stage, control = %s, actual = %s' %(myid, control_data[samples], average_stage) + print('P%d average stage, control = %s, actual = %s' %(myid, control_data[samples], average_stage)) - print 'P%d average xmom, control = %s, actual = %s' %(myid, control_data[samples+1], average_xmom) + print('P%d average xmom, control = %s, actual = %s' %(myid, control_data[samples+1], average_xmom)) - print 'P%d average ymom, control = %s, actual = %s' %(myid, control_data[samples+2], average_ymom) + print('P%d average ymom, control = %s, actual = %s' %(myid, control_data[samples+2], average_ymom)) - print 'P%d average volume, control = %s, actual = %s' %(myid, control_data[samples+3], average_volume) + print('P%d average volume, control = %s, actual = %s' %(myid, control_data[samples+3], average_volume)) - print 'P%d average depth, control = %s, actual = %s' %(myid, control_data[samples+4], average_depth) + print('P%d average depth, control = %s, actual = %s' %(myid, control_data[samples+4], average_depth)) return control_data, success @@ -325,7 +327,7 @@ def test_parallel_operator(self): def assert_(condition, msg="Assertion Failed"): if condition == False: #pypar.finalize() - raise AssertionError, msg + raise_(AssertionError, msg) if __name__=="__main__": if numprocs == 1: @@ -342,7 +344,7 @@ def assert_(condition, msg="Assertion Failed"): test_points = [] if myid == 0: - if verbose: print 'PARALLEL START' + if verbose: print('PARALLEL START') for i in range(samples): x = random.randrange(0,1000)/1000.0 * length y = random.randrange(0,1000)/1000.0 * width @@ -382,7 +384,7 @@ def assert_(condition, msg="Assertion Failed"): else: all_success= pypar.receive(0) - if verbose: print 'myid ',myid, 'all_success ',all_success + if verbose: print('myid ',myid, 'all_success ',all_success) if myid == 0: diff --git a/anuga/parallel/tests/skip_parallel_boyd_box_operator.py b/anuga/parallel/tests/skip_parallel_boyd_box_operator.py index 0c307ae1c..5b7359b97 100644 --- a/anuga/parallel/tests/skip_parallel_boyd_box_operator.py +++ b/anuga/parallel/tests/skip_parallel_boyd_box_operator.py @@ -1,3 +1,5 @@ +from __future__ import print_function +from future.utils import raise_ import os.path import sys @@ -162,7 +164,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve except: tri_ids.append(-2) - if verbose: print 'P%d has points = %s' %(myid, tri_ids) + if verbose: print('P%d has points = %s' %(myid, tri_ids)) if not parallel: control_data = [] @@ -192,7 +194,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve #if inlet1 is not None and verbose: inlet1.print_statistics() if boyd_box0 is not None and verbose: - print "++++", myid + print("++++", myid) boyd_box0.print_statistics() @@ -211,7 +213,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve if boyd_box0 is not None and verbose : if myid == boyd_box0.master_proc: - print 'master_proc ',myid + print('master_proc ',myid) boyd_box0.print_timestepping_statistics() @@ -241,7 +243,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve control_data.append(inlet0.inlet.get_total_water_volume()) control_data.append(inlet0.inlet.get_average_depth()) - if verbose: print 'P%d control_data = %s' %(myid, control_data) + if verbose: print('P%d control_data = %s' %(myid, control_data)) else: # parallel stage = domain.get_quantity('stage') @@ -250,7 +252,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve local_success = num.allclose(control_data[i], stage.centroid_values[tri_ids[i]]) success = success and local_success if verbose: - print 'P%d tri %d, control = %s, actual = %s, Success = %s' %(myid, i, control_data[i], stage.centroid_values[tri_ids[i]], local_success) + print('P%d tri %d, control = %s, actual = %s, Success = %s' %(myid, i, control_data[i], stage.centroid_values[tri_ids[i]], local_success)) if inlet0 is not None: @@ -263,11 +265,11 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve if myid == inlet_master_proc: if verbose: - print 'P%d average stage, control = %s, actual = %s' %(myid, control_data[samples], average_stage) - print 'P%d average xmom, control = %s, actual = %s' %(myid, control_data[samples+1], average_xmom) - print 'P%d average ymom, control = %s, actual = %s' %(myid, control_data[samples+2], average_ymom) - print 'P%d average volume, control = %s, actual = %s' %(myid, control_data[samples+3], average_volume) - print 'P%d average depth, control = %s, actual = %s' %(myid, control_data[samples+4], average_depth) + print('P%d average stage, control = %s, actual = %s' %(myid, control_data[samples], average_stage)) + print('P%d average xmom, control = %s, actual = %s' %(myid, control_data[samples+1], average_xmom)) + print('P%d average ymom, control = %s, actual = %s' %(myid, control_data[samples+2], average_ymom)) + print('P%d average volume, control = %s, actual = %s' %(myid, control_data[samples+3], average_volume)) + print('P%d average depth, control = %s, actual = %s' %(myid, control_data[samples+4], average_depth)) #assert(success) @@ -318,7 +320,7 @@ def test_parallel_operator(self): def assert_(condition, msg="Assertion Failed"): if condition == False: #pypar.finalize() - raise AssertionError, msg + raise_(AssertionError, msg) if __name__=="__main__": if numprocs == 1: @@ -336,7 +338,7 @@ def assert_(condition, msg="Assertion Failed"): test_points = [] if myid == 0: - if verbose: print 'PARALLEL START' + if verbose: print('PARALLEL START') random.seed(1001) for i in range(samples): x = random.randrange(0,1000)/1000.0 * length @@ -377,7 +379,7 @@ def assert_(condition, msg="Assertion Failed"): else: all_success= pypar.receive(0) - if verbose: print 'myid ',myid, 'all_success ',all_success + if verbose: print('myid ',myid, 'all_success ',all_success) #finalize() diff --git a/anuga/parallel/tests/skip_parallel_boyd_pipe_operator.py b/anuga/parallel/tests/skip_parallel_boyd_pipe_operator.py index d3162c697..541c868e7 100644 --- a/anuga/parallel/tests/skip_parallel_boyd_pipe_operator.py +++ b/anuga/parallel/tests/skip_parallel_boyd_pipe_operator.py @@ -1,3 +1,5 @@ +from __future__ import print_function +from future.utils import raise_ import os.path import sys @@ -156,7 +158,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve except: tri_ids.append(-2) - if verbose: print 'P%d has points = %s' %(myid, tri_ids) + if verbose: print('P%d has points = %s' %(myid, tri_ids)) if not parallel: control_data = [] @@ -260,7 +262,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve control_data.append(inlet0.inlet.get_total_water_volume()) control_data.append(inlet0.inlet.get_average_depth()) - if verbose: print 'P%d control_data = %s' %(myid, control_data) + if verbose: print('P%d control_data = %s' %(myid, control_data)) else: stage = domain.get_quantity('stage') @@ -269,7 +271,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve local_success = num.allclose(control_data[i], stage.centroid_values[tri_ids[i]]) success = success and local_success if verbose: - print 'P%d tri %d, control = %s, actual = %s, Success = %s' %(myid, i, control_data[i], stage.centroid_values[tri_ids[i]], local_success) + print('P%d tri %d, control = %s, actual = %s, Success = %s' %(myid, i, control_data[i], stage.centroid_values[tri_ids[i]], local_success)) if inlet0 is not None: @@ -282,15 +284,15 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve if myid == inlet_master_proc: if verbose: - print 'P%d average stage, control = %s, actual = %s' %(myid, control_data[samples], average_stage) + print('P%d average stage, control = %s, actual = %s' %(myid, control_data[samples], average_stage)) - print 'P%d average xmom, control = %s, actual = %s' %(myid, control_data[samples+1], average_xmom) + print('P%d average xmom, control = %s, actual = %s' %(myid, control_data[samples+1], average_xmom)) - print 'P%d average ymom, control = %s, actual = %s' %(myid, control_data[samples+2], average_ymom) + print('P%d average ymom, control = %s, actual = %s' %(myid, control_data[samples+2], average_ymom)) - print 'P%d average volume, control = %s, actual = %s' %(myid, control_data[samples+3], average_volume) + print('P%d average volume, control = %s, actual = %s' %(myid, control_data[samples+3], average_volume)) - print 'P%d average depth, control = %s, actual = %s' %(myid, control_data[samples+4], average_depth) + print('P%d average depth, control = %s, actual = %s' %(myid, control_data[samples+4], average_depth)) assert(success) @@ -316,7 +318,7 @@ def test_parallel_operator(self): def assert_(condition, msg="Assertion Failed"): if condition == False: #pypar.finalize() - raise AssertionError, msg + raise_(AssertionError, msg) if __name__=="__main__": if numprocs == 1: @@ -330,7 +332,7 @@ def assert_(condition, msg="Assertion Failed"): test_points = [] if myid == 0: - if verbose: print 'PARALLEL START' + if verbose: print('PARALLEL START') for i in range(samples): x = random.randrange(0,1000)/1000.0 * length y = random.randrange(0,1000)/1000.0 * width diff --git a/anuga/parallel/tests/test_all.py b/anuga/parallel/tests/test_all.py index fd89449a0..4b7e19144 100644 --- a/anuga/parallel/tests/test_all.py +++ b/anuga/parallel/tests/test_all.py @@ -4,10 +4,12 @@ module through PyUnit. This script will aggregate all found test suites into one big test suite and run them all at once. """ +from __future__ import print_function # Author: Mark Pilgrim # Modified by Ole Nielsen +from future.utils import raise_ import unittest import os import sys @@ -59,15 +61,15 @@ def list_names(names, func=None, col_width=None, page_width=None): for name in names: if func: name = func(name) - print '%-*s' % (c_width-1, name), + print('%-*s' % (c_width-1, name), end=' ') column += 1 if column >= max_columns: column = 0 - print + print() # if last line not finished, end it here if column > 0: - print + print() ## @@ -102,15 +104,15 @@ def get_unittestfiles(path): def regressionTest(test_verbose=False): # start off with where we are path = os.getcwd() - print - print 'Testing path: %s' % path + print() + print('Testing path: %s' % path) # get the terminal width term_width = terminal_width() # explain what we are doing - print - print "The following directories will be skipped over:" + print() + print("The following directories will be skipped over:") exclude_dirs.sort() list_names(exclude_dirs, page_width=term_width) @@ -121,14 +123,14 @@ def regressionTest(test_verbose=False): files = [x for x in test_files if not x == 'test_all.py'] files.sort() # Ensure same order on all platforms - print - print 'Paths searched:' + print() + print('Paths searched:') list_names(path_files, os.path.basename, page_width=term_width) - print - print 'Files tested:' + print() + print('Files tested:') list_names(files, page_width=term_width) - print + print() # update system path with found paths for path in path_files: @@ -136,14 +138,14 @@ def regressionTest(test_verbose=False): # exclude files that we can't handle for file in exclude_files: - print 'WARNING: File '+ file + ' to be excluded from testing' + print('WARNING: File '+ file + ' to be excluded from testing') try: files.remove(file) - except ValueError, e: + except ValueError as e: msg = 'File "%s" was not found in test suite.\n' % file msg += 'Original error is "%s"\n' % e msg += 'Perhaps it should be removed from exclude list?' - raise Exception, msg + raise_(Exception, msg) # import all test_*.py files # NOTE: This implies that test_*.py files MUST HAVE UNIQUE NAMES! @@ -185,8 +187,8 @@ def check_anuga_import(): # importing something that loads quickly import anuga.anuga_exceptions except ImportError: - print "Python cannot import ANUGA module." - print "Check you have followed all steps of its installation." + print("Python cannot import ANUGA module.") + print("Check you have followed all steps of its installation.") import sys sys.exit() @@ -209,8 +211,8 @@ def check_anuga_import(): # timestamp at the end timestamp = time.asctime() version = aust.get_revision_number() - print - print 'Finished at %s, version %s' % (timestamp, version) + print() + print('Finished at %s, version %s' % (timestamp, version)) # Cleaning up if len(sys.argv) > 1 and sys.argv[1][0].upper() == 'V': diff --git a/anuga/parallel/tests/test_distribute_settings.py b/anuga/parallel/tests/test_distribute_settings.py index 2c202e049..288762e22 100644 --- a/anuga/parallel/tests/test_distribute_settings.py +++ b/anuga/parallel/tests/test_distribute_settings.py @@ -6,11 +6,13 @@ This is a very simple test of the parallel algorithm using the simplified parallel API """ +from __future__ import print_function #------------------------------------------------------------------------------ # Import necessary modules #------------------------------------------------------------------------------ +from future.utils import raise_ import unittest import os import sys @@ -86,7 +88,7 @@ def run_simulation(parallel=False, verbose=False): # Create pickled partition #-------------------------------------------------------------------------- if myid == 0: - if verbose: print 'DUMPING PARTITION DATA' + if verbose: print('DUMPING PARTITION DATA') sequential_distribute_dump(domain, numprocs, verbose=verbose, parameters=new_parameters) #-------------------------------------------------------------------------- @@ -94,11 +96,11 @@ def run_simulation(parallel=False, verbose=False): #-------------------------------------------------------------------------- if parallel: - if myid == 0 and verbose : print 'DISTRIBUTING TO PARALLEL DOMAIN' + if myid == 0 and verbose : print('DISTRIBUTING TO PARALLEL DOMAIN') pdomain = distribute(domain, verbose=verbose, parameters=new_parameters) pdomain.set_name('pdomain') - if myid == 0 and verbose : print 'LOADING IN PARALLEL DOMAIN' + if myid == 0 and verbose : print('LOADING IN PARALLEL DOMAIN') sdomain = sequential_distribute_load(filename='odomain', verbose = verbose) sdomain.set_name('sdomain') @@ -132,7 +134,7 @@ def run_simulation(parallel=False, verbose=False): class Test_parallel_sw_flow(unittest.TestCase): def test_parallel_sw_flow(self): - if verbose : print "Expect this test to fail if not run from the parallel directory." + if verbose : print("Expect this test to fail if not run from the parallel directory.") abs_script_name = os.path.abspath(__file__) cmd = "mpiexec -np %d python %s" % (nprocs, abs_script_name) @@ -145,7 +147,7 @@ def test_parallel_sw_flow(self): def assert_(condition, msg="Assertion Failed"): if condition == False: #pypar.finalize() - raise AssertionError, msg + raise_(AssertionError, msg) if __name__=="__main__": if numprocs == 1: @@ -160,7 +162,7 @@ def assert_(condition, msg="Assertion Failed"): # Run the codel and compare sequential # results at 4 gauge stations #------------------------------------------ - if myid ==0 and verbose: print 'PARALLEL START' + if myid ==0 and verbose: print('PARALLEL START') from anuga.utilities.parallel_abstraction import global_except_hook import sys diff --git a/anuga/parallel/tests/test_failure.py b/anuga/parallel/tests/test_failure.py index 0842dafdb..1e137dd36 100644 --- a/anuga/parallel/tests/test_failure.py +++ b/anuga/parallel/tests/test_failure.py @@ -1,3 +1,4 @@ +from __future__ import print_function import os.path import sys @@ -145,7 +146,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve except: tri_ids.append(-2) - if verbose: print 'P%d has points = %s' %(myid, tri_ids) + if verbose: print('P%d has points = %s' %(myid, tri_ids)) if not parallel: control_data = [] @@ -254,7 +255,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve control_data.append(inlet0.inlet.get_total_water_volume()) control_data.append(inlet0.inlet.get_average_depth()) - if verbose: print 'P%d control_data = %s' %(myid, control_data) + if verbose: print('P%d control_data = %s' %(myid, control_data)) else: stage = domain.get_quantity('stage') @@ -263,7 +264,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve local_success = num.allclose(control_data[i], stage.centroid_values[tri_ids[i]]) success = success and local_success if verbose and not local_success: - print 'P%d tri %d, control = %s, actual = %s, Success = %s' %(myid, i, control_data[i], stage.centroid_values[tri_ids[i]], local_success) + print('P%d tri %d, control = %s, actual = %s, Success = %s' %(myid, i, control_data[i], stage.centroid_values[tri_ids[i]], local_success)) sys.stdout.flush() @@ -278,15 +279,15 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve if myid == inlet_master_proc: if verbose: - print 'P%d average stage, control = %s, actual = %s' %(myid, control_data[samples], average_stage) + print('P%d average stage, control = %s, actual = %s' %(myid, control_data[samples], average_stage)) - print 'P%d average xmom, control = %s, actual = %s' %(myid, control_data[samples+1], average_xmom) + print('P%d average xmom, control = %s, actual = %s' %(myid, control_data[samples+1], average_xmom)) - print 'P%d average ymom, control = %s, actual = %s' %(myid, control_data[samples+2], average_ymom) + print('P%d average ymom, control = %s, actual = %s' %(myid, control_data[samples+2], average_ymom)) - print 'P%d average volume, control = %s, actual = %s' %(myid, control_data[samples+3], average_volume) + print('P%d average volume, control = %s, actual = %s' %(myid, control_data[samples+3], average_volume)) - print 'P%d average depth, control = %s, actual = %s' %(myid, control_data[samples+4], average_depth) + print('P%d average depth, control = %s, actual = %s' %(myid, control_data[samples+4], average_depth)) #assert(success) @@ -311,8 +312,8 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve else: test_points = pypar.receive(0) - print "Test Points::" - print test_points + print("Test Points::") + print(test_points) if myid == 0: control_data = run_simulation(parallel=False, test_points = test_points, verbose = True) diff --git a/anuga/parallel/tests/test_parallel_boyd_box_operator.py b/anuga/parallel/tests/test_parallel_boyd_box_operator.py index 2d0042291..c9d523afb 100644 --- a/anuga/parallel/tests/test_parallel_boyd_box_operator.py +++ b/anuga/parallel/tests/test_parallel_boyd_box_operator.py @@ -1,3 +1,5 @@ +from __future__ import print_function +from future.utils import raise_ import os.path import sys @@ -152,7 +154,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve except: tri_ids.append(-2) - if verbose: print 'P%d has points = %s' %(myid, tri_ids) + if verbose: print('P%d has points = %s' %(myid, tri_ids)) if not parallel: control_data = [] @@ -182,7 +184,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve #if inlet1 is not None and verbose: inlet1.print_statistics() if boyd_box0 is not None and verbose: - print "++++", myid + print("++++", myid) boyd_box0.print_statistics() @@ -201,7 +203,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve if boyd_box0 is not None and verbose : if myid == boyd_box0.master_proc: - print 'master_proc ',myid + print('master_proc ',myid) boyd_box0.print_timestepping_statistics() @@ -231,7 +233,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve control_data.append(inlet0.inlet.get_total_water_volume()) control_data.append(inlet0.inlet.get_average_depth()) - if verbose: print 'P%d control_data = %s' %(myid, control_data) + if verbose: print('P%d control_data = %s' %(myid, control_data)) else: # parallel stage = domain.get_quantity('stage') @@ -240,7 +242,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve local_success = num.allclose(control_data[i], stage.centroid_values[tri_ids[i]]) success = success and local_success if verbose: - print 'P%d tri %d, control = %s, actual = %s, Success = %s' %(myid, i, control_data[i], stage.centroid_values[tri_ids[i]], local_success) + print('P%d tri %d, control = %s, actual = %s, Success = %s' %(myid, i, control_data[i], stage.centroid_values[tri_ids[i]], local_success)) if inlet0 is not None: @@ -253,11 +255,11 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve if myid == inlet_master_proc: if verbose: - print 'P%d average stage, control = %s, actual = %s' %(myid, control_data[samples], average_stage) - print 'P%d average xmom, control = %s, actual = %s' %(myid, control_data[samples+1], average_xmom) - print 'P%d average ymom, control = %s, actual = %s' %(myid, control_data[samples+2], average_ymom) - print 'P%d average volume, control = %s, actual = %s' %(myid, control_data[samples+3], average_volume) - print 'P%d average depth, control = %s, actual = %s' %(myid, control_data[samples+4], average_depth) + print('P%d average stage, control = %s, actual = %s' %(myid, control_data[samples], average_stage)) + print('P%d average xmom, control = %s, actual = %s' %(myid, control_data[samples+1], average_xmom)) + print('P%d average ymom, control = %s, actual = %s' %(myid, control_data[samples+2], average_ymom)) + print('P%d average volume, control = %s, actual = %s' %(myid, control_data[samples+3], average_volume)) + print('P%d average depth, control = %s, actual = %s' %(myid, control_data[samples+4], average_depth)) #assert(success) @@ -306,7 +308,7 @@ def test_parallel_operator(self): def assert_(condition, msg="Assertion Failed"): if condition == False: #pypar.finalize() - raise AssertionError, msg + raise_(AssertionError, msg) if __name__=="__main__": if numprocs == 1: @@ -325,7 +327,7 @@ def assert_(condition, msg="Assertion Failed"): test_points = [] if myid == 0: - if verbose: print 'PARALLEL START' + if verbose: print('PARALLEL START') random.seed(1001) for i in range(samples): x = random.randrange(0,1000)/1000.0 * length @@ -366,7 +368,7 @@ def assert_(condition, msg="Assertion Failed"): else: all_success= pypar.receive(0) - if verbose: print 'myid ',myid, 'all_success ',all_success + if verbose: print('myid ',myid, 'all_success ',all_success) finalize() diff --git a/anuga/parallel/tests/test_parallel_distribute_domain.py b/anuga/parallel/tests/test_parallel_distribute_domain.py index f490a47f5..ebd550836 100644 --- a/anuga/parallel/tests/test_parallel_distribute_domain.py +++ b/anuga/parallel/tests/test_parallel_distribute_domain.py @@ -5,11 +5,13 @@ WARNING: This assumes that the command to run jobs is mpiexec. Tested with MPICH and LAM (Ole) """ +from __future__ import print_function #------------------------------------------------------------------------------ # Import necessary modules #------------------------------------------------------------------------------ +from future.utils import raise_ import unittest import os import sys @@ -84,7 +86,7 @@ def run_simulation(parallel=False): #-------------------------------------------------------------------------- if parallel: - if myid == 0 and verbose: print 'DISTRIBUTING PARALLEL DOMAIN' + if myid == 0 and verbose: print('DISTRIBUTING PARALLEL DOMAIN') domain = distribute(domain) #------------------------------------------------------------------------------ @@ -111,9 +113,9 @@ def run_simulation(parallel=False): # Evolution #------------------------------------------------------------------------------ if parallel: - if myid == 0 and verbose: print 'PARALLEL EVOLVE' + if myid == 0 and verbose: print('PARALLEL EVOLVE') else: - if verbose: print 'SEQUENTIAL EVOLVE' + if verbose: print('SEQUENTIAL EVOLVE') for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): edges = domain.quantities[quantity].edge_values.take(num.flatnonzero(domain.tri_full_flag),axis=0) @@ -183,7 +185,7 @@ def test_parallel_distribute_domain(self): def assert_(condition, msg="Assertion Failed"): if condition == False: #pypar.finalize() - raise AssertionError, msg + raise_(AssertionError, msg) if __name__=="__main__": if numprocs == 1: @@ -198,12 +200,12 @@ def assert_(condition, msg="Assertion Failed"): pypar.barrier() if myid == 0: - if verbose: print 'SEQUENTIAL START' + if verbose: print('SEQUENTIAL START') l1norm_seq, l2norm_seq, linfnorm_seq = run_simulation(parallel=False) pypar.barrier() if myid ==0: - if verbose: print 'PARALLEL START' + if verbose: print('PARALLEL START') l1norm_par, l2norm_par, linfnorm_par = run_simulation(parallel=True) @@ -232,7 +234,7 @@ def assert_(condition, msg="Assertion Failed"): assert_(abs(l2norm_par[x][y] - l2norm_par[x-1][y]) < tol) assert_(abs(linfnorm_par[x][y] - linfnorm_par[x-1][y]) < tol) - if verbose: print 'Parallel test OK' + if verbose: print('Parallel test OK') diff --git a/anuga/parallel/tests/test_parallel_distribute_mesh.py b/anuga/parallel/tests/test_parallel_distribute_mesh.py index 8bed0076c..68b446a81 100644 --- a/anuga/parallel/tests/test_parallel_distribute_mesh.py +++ b/anuga/parallel/tests/test_parallel_distribute_mesh.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +from __future__ import print_function +from future.utils import raise_ import unittest import sys import os @@ -91,7 +93,7 @@ def distibute_three_processors(): true_seq_values = get_true_seq_values() if False: - print "True Seq Values = \\" + print("True Seq Values = \\") pprint(true_seq_values) assert_(num.allclose(vertices, true_seq_values['vertices'] )) @@ -106,7 +108,7 @@ def distibute_three_processors(): quantities, triangles_per_proc) if False: - print 'submesh_values = \\' + print('submesh_values = \\') print_submesh_values(submesh) true_values = get_true_submesh_values() @@ -168,14 +170,14 @@ def distibute_three_processors(): if myid == 0: if False: - print 'extract_values = \\' + print('extract_values = \\') print_extract_submesh(points, triangles, ghost_recv_dict, \ full_send_dict, tri_map, node_map, ghost_layer_width) true_values = get_true_extract_submesh() if False: - print 'true_extract_values = \\' + print('true_extract_values = \\') pprint(true_values) @@ -193,7 +195,7 @@ def distibute_three_processors(): if myid == 1: if False: - print "rec_submesh_1 = \\" + print("rec_submesh_1 = \\") print_rec_submesh_1(points, triangles, ghost_recv_dict, full_send_dict, \ tri_map, node_map, ghost_layer_width) @@ -201,7 +203,7 @@ def distibute_three_processors(): true_values = get_true_rec_submesh_1() if False: - print 'true_rec_values_1 = \\' + print('true_rec_values_1 = \\') pprint(true_values) assert_(num.allclose(points, true_values['points'])) @@ -218,14 +220,14 @@ def distibute_three_processors(): if myid == 2: if False: - print "rec_submesh_2 = \\" + print("rec_submesh_2 = \\") print_rec_submesh_2(points, triangles, ghost_recv_dict, full_send_dict, \ tri_map, node_map, ghost_layer_width) true_values = get_true_rec_submesh_2() if False: - print 'true_rec_values_2 = \\' + print('true_rec_values_2 = \\') pprint(true_values) assert_(num.allclose(points, true_values['points'])) @@ -318,7 +320,7 @@ def get_true_seq_values(): def print_seq_values(vertices, triangles, triangles_per_proc): values = dict(vertices = vertices, triangles = triangles, triangles_per_proc = triangles_per_proc) - print "seq_values" + print("seq_values") pprint(values) @@ -335,10 +337,10 @@ def print_submesh_values(submesh): name = "submesh['"+parm+"']["+str(i)+"]" value = eval(name) msg = parm + '_'+str(i)+'='+ pformat(value) + ',' - print msg + print(msg) value = submesh['full_commun'] msg = 'full_commun='+ pformat(value) - print msg + print(msg) def get_true_submesh_values(): metis_version = 4 @@ -912,7 +914,6 @@ def get_true_rec_submesh_2(): return true_values - metis_version = 4 if metis_version == 4: @@ -969,7 +970,7 @@ def test_distribute_three_processors(self): # the PyUnit defined assert_ function can't be used. def assert_(condition, msg="Assertion Failed"): if condition == False: - raise AssertionError, msg + raise_(AssertionError, msg) #------------------------------------------------------------- if __name__ == "__main__": diff --git a/anuga/parallel/tests/test_parallel_file_boundary.py b/anuga/parallel/tests/test_parallel_file_boundary.py index 2d5bb00c5..e7739b003 100644 --- a/anuga/parallel/tests/test_parallel_file_boundary.py +++ b/anuga/parallel/tests/test_parallel_file_boundary.py @@ -10,11 +10,13 @@ Will produce sww files with names domain_Pn_m.sww where m is number of processors and n in [0, m-1] refers to specific processor that owned this part of the partitioned mesh. """ +from __future__ import print_function #------------------------------------------------------------------------------ # Import necessary modules #------------------------------------------------------------------------------ +from future.utils import raise_ import os import sys import time @@ -174,7 +176,7 @@ def sequential_time_varying_file_boundary_sts(self): domain_fbound = Domain(meshname) domain_fbound.set_quantities_to_be_stored(None) domain_fbound.set_quantity('stage', tide) - if verbose: print "Creating file boundary condition" + if verbose: print("Creating file boundary condition") Bf = File_boundary(sts_file+'.sts', domain_fbound, boundary_polygon=boundary_polygon) @@ -183,7 +185,7 @@ def sequential_time_varying_file_boundary_sts(self): domain_fbound.set_boundary({'ocean': Bf,'otherocean': Br}) temp_fbound=num.zeros(int(finaltime/yieldstep)+1,num.float) - if verbose: print "Evolving domain with file boundary condition" + if verbose: print("Evolving domain with file boundary condition") for i, t in enumerate(domain_fbound.evolve(yieldstep=yieldstep, finaltime=finaltime, skip_initial_step = False)): @@ -352,7 +354,7 @@ def parallel_time_varying_file_boundary_sts(self): barrier() if ( verbose and myid == 0 ): - print 'DISTRIBUTING PARALLEL DOMAIN' + print('DISTRIBUTING PARALLEL DOMAIN') domain_fbound = distribute(domain_fbound) #-------------------------------------------------------------------- @@ -390,7 +392,7 @@ def parallel_time_varying_file_boundary_sts(self): fbound_proc_tri_ids.append(-2) - if verbose: print 'P%d has points = %s' %(myid, fbound_proc_tri_ids) + if verbose: print('P%d has points = %s' %(myid, fbound_proc_tri_ids)) #------------------------------------------------------------ # Set boundary conditions @@ -442,7 +444,7 @@ def parallel_time_varying_file_boundary_sts(self): drchlt_proc_tri_ids.append(-2) - if verbose: print 'P%d has points = %s' %(myid, drchlt_proc_tri_ids) + if verbose: print('P%d has points = %s' %(myid, drchlt_proc_tri_ids)) #------------------------------------------------------------ # Evolve entire domain on each processor @@ -481,7 +483,7 @@ def parallel_time_varying_file_boundary_sts(self): def assert_(condition, msg="Assertion Failed"): if condition == False: #pypar.finalize() - raise AssertionError, msg + raise_(AssertionError, msg) # Test an nprocs-way run of the shallow water equations @@ -490,7 +492,7 @@ def assert_(condition, msg="Assertion Failed"): if __name__=="__main__": #verbose=False if myid ==0 and verbose: - print 'PARALLEL START' + print('PARALLEL START') suite = unittest.makeSuite(Test_urs2sts_parallel,'parallel_test') #suite = unittest.makeSuite(Test_urs2sts_parallel,'sequential_test') runner = unittest.TextTestRunner() diff --git a/anuga/parallel/tests/test_parallel_frac_op.py b/anuga/parallel/tests/test_parallel_frac_op.py index 28e23c3a8..c962c23e8 100644 --- a/anuga/parallel/tests/test_parallel_frac_op.py +++ b/anuga/parallel/tests/test_parallel_frac_op.py @@ -1,3 +1,5 @@ +from __future__ import print_function +from future.utils import raise_ import os.path import sys @@ -146,7 +148,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve except: tri_ids.append(-2) - if verbose: print 'P%d has points = %s' %(myid, tri_ids) + if verbose: print('P%d has points = %s' %(myid, tri_ids)) if not parallel: control_data = [] @@ -223,7 +225,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve control_data.append(inlet0.inlet.get_total_water_volume()) control_data.append(inlet0.inlet.get_average_depth()) - if verbose: print 'P%d control_data = %s' %(myid, control_data) + if verbose: print('P%d control_data = %s' %(myid, control_data)) else: stage = domain.get_quantity('stage') @@ -232,9 +234,9 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve local_success = num.allclose(control_data[i], stage.centroid_values[tri_ids[i]]) success = success and local_success if verbose: - print 'P%d tri %d, control = %s, actual = %s, Success = %s' %(myid, i, control_data[i], stage.centroid_values[tri_ids[i]], local_success) + print('P%d tri %d, control = %s, actual = %s, Success = %s' %(myid, i, control_data[i], stage.centroid_values[tri_ids[i]], local_success)) if not local_success: - print 'Ouput P%d tri %d, control = %s, actual = %s, Success = %s' %(myid, i, control_data[i], stage.centroid_values[tri_ids[i]], local_success) + print('Ouput P%d tri %d, control = %s, actual = %s, Success = %s' %(myid, i, control_data[i], stage.centroid_values[tri_ids[i]], local_success)) #assert success @@ -249,15 +251,15 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve if myid == inlet_master_proc: if verbose: - print 'P%d average stage, control = %s, actual = %s' %(myid, control_data[samples], average_stage) + print('P%d average stage, control = %s, actual = %s' %(myid, control_data[samples], average_stage)) - print 'P%d average xmom, control = %s, actual = %s' %(myid, control_data[samples+1], average_xmom) + print('P%d average xmom, control = %s, actual = %s' %(myid, control_data[samples+1], average_xmom)) - print 'P%d average ymom, control = %s, actual = %s' %(myid, control_data[samples+2], average_ymom) + print('P%d average ymom, control = %s, actual = %s' %(myid, control_data[samples+2], average_ymom)) - print 'P%d average volume, control = %s, actual = %s' %(myid, control_data[samples+3], average_volume) + print('P%d average volume, control = %s, actual = %s' %(myid, control_data[samples+3], average_volume)) - print 'P%d average depth, control = %s, actual = %s' %(myid, control_data[samples+4], average_depth) + print('P%d average depth, control = %s, actual = %s' %(myid, control_data[samples+4], average_depth)) #assert(success) @@ -284,7 +286,7 @@ def test_parallel_frac_op(self): def assert_(condition, msg="Assertion Failed"): if condition == False: #pypar.finalize() - raise AssertionError, msg + raise_(AssertionError, msg) if __name__=="__main__": @@ -304,7 +306,7 @@ def assert_(condition, msg="Assertion Failed"): test_points = [] if myid == 0: - if verbose: print 'PARALLEL START' + if verbose: print('PARALLEL START') random.seed(2) for i in range(samples): x = random.randrange(0,1000)/1000.0 * length diff --git a/anuga/parallel/tests/test_parallel_inlet_operator.py b/anuga/parallel/tests/test_parallel_inlet_operator.py index 9b83e9a6f..2bed2bb6d 100644 --- a/anuga/parallel/tests/test_parallel_inlet_operator.py +++ b/anuga/parallel/tests/test_parallel_inlet_operator.py @@ -1,3 +1,5 @@ +from __future__ import print_function +from future.utils import raise_ import os.path import sys @@ -152,7 +154,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve except: tri_ids.append(-2) - if verbose: print 'P%d has points = %s' %(myid, tri_ids) + if verbose: print('P%d has points = %s' %(myid, tri_ids)) if not parallel: control_data = [] @@ -216,7 +218,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve control_data.append(inlet0.inlet.get_total_water_volume()) control_data.append(inlet0.inlet.get_average_depth()) - if verbose: print 'P%d control_data = %s' %(myid, control_data) + if verbose: print('P%d control_data = %s' %(myid, control_data)) else: stage = domain.get_quantity('stage') @@ -225,7 +227,7 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve local_success = num.allclose(control_data[i], stage.centroid_values[tri_ids[i]]) success = success and local_success if verbose: - print 'P%d tri %d, control = %s, actual = %s, Success = %s' %(myid, i, control_data[i], stage.centroid_values[tri_ids[i]], local_success) + print('P%d tri %d, control = %s, actual = %s, Success = %s' %(myid, i, control_data[i], stage.centroid_values[tri_ids[i]], local_success)) if inlet0 is not None: @@ -238,15 +240,15 @@ def run_simulation(parallel = False, control_data = None, test_points = None, ve if myid == inlet_master_proc: if verbose: - print 'P%d average stage, control = %s, actual = %s' %(myid, control_data[samples], average_stage) + print('P%d average stage, control = %s, actual = %s' %(myid, control_data[samples], average_stage)) - print 'P%d average xmom, control = %s, actual = %s' %(myid, control_data[samples+1], average_xmom) + print('P%d average xmom, control = %s, actual = %s' %(myid, control_data[samples+1], average_xmom)) - print 'P%d average ymom, control = %s, actual = %s' %(myid, control_data[samples+2], average_ymom) + print('P%d average ymom, control = %s, actual = %s' %(myid, control_data[samples+2], average_ymom)) - print 'P%d average volume, control = %s, actual = %s' %(myid, control_data[samples+3], average_volume) + print('P%d average volume, control = %s, actual = %s' %(myid, control_data[samples+3], average_volume)) - print 'P%d average depth, control = %s, actual = %s' %(myid, control_data[samples+4], average_depth) + print('P%d average depth, control = %s, actual = %s' %(myid, control_data[samples+4], average_depth)) assert(success) @@ -273,7 +275,7 @@ def test_parallel_frac_op(self): def assert_(condition, msg="Assertion Failed"): if condition == False: #pypar.finalize() - raise AssertionError, msg + raise_(AssertionError, msg) if __name__=="__main__": if numprocs == 1: @@ -292,7 +294,7 @@ def assert_(condition, msg="Assertion Failed"): test_points = [] if myid == 0: - if verbose: print 'PARALLEL START' + if verbose: print('PARALLEL START') random.seed(1001) for i in range(samples): x = random.randrange(0,1000)/1000.0 * length diff --git a/anuga/parallel/tests/test_parallel_riverwall.py b/anuga/parallel/tests/test_parallel_riverwall.py index ccca0bb71..d40e699d8 100644 --- a/anuga/parallel/tests/test_parallel_riverwall.py +++ b/anuga/parallel/tests/test_parallel_riverwall.py @@ -1,11 +1,13 @@ """ Test parallel and sequential results of riverwall procedure """ +from __future__ import print_function #------------------------------------------------------------------------------ # Import necessary modules #------------------------------------------------------------------------------ +from future.utils import raise_ import unittest import os import sys @@ -114,16 +116,16 @@ def stagefun(x,y): #-------------------------------------------------------------------------- if parallel: - if myid == 0 and verbose : print 'DISTRIBUTING TO PARALLEL DOMAIN' + if myid == 0 and verbose : print('DISTRIBUTING TO PARALLEL DOMAIN') pdomain = distribute(sdomain, verbose=verbose) pdomain.set_name('p_riverwall') pdomain.set_store_vertices_uniquely() if myid == 0 and verbose: - print 60*'=' - print 'EVOLVING pdomain' - print 60*'=' + print(60*'=') + print('EVOLVING pdomain') + print(60*'=') setup_and_evolve(pdomain, verbose=verbose) @@ -131,9 +133,9 @@ def stagefun(x,y): if myid == 0: if verbose: - print 60*'=' - print 'EVOLVING sdomain' - print 60*'=' + print(60*'=') + print('EVOLVING sdomain') + print(60*'=') setup_and_evolve(sdomain, verbose=verbose) barrier() @@ -142,7 +144,7 @@ def stagefun(x,y): # Now compare the merged sww files #--------------------------------- if myid == 0: - if verbose: print 'COMPARING SWW FILES' + if verbose: print('COMPARING SWW FILES') sdomain_v = util.get_output('s_riverwall.sww') sdomain_c = util.get_centroids(sdomain_v) @@ -156,14 +158,14 @@ def stagefun(x,y): if verbose: order = 0 - print 'PDOMAIN CENTROID VALUES' - print num.linalg.norm(sdomain_c.x-pdomain_c.x,ord=order) - print num.linalg.norm(sdomain_c.y-pdomain_c.y,ord=order) - print num.linalg.norm(sdomain_c.stage[-1]-pdomain_c.stage[-1],ord=order) - print num.linalg.norm(sdomain_c.xmom[-1]-pdomain_c.xmom[-1],ord=order) - print num.linalg.norm(sdomain_c.ymom[-1]-pdomain_c.ymom[-1],ord=order) - print num.linalg.norm(sdomain_c.xvel[-1]-pdomain_c.xvel[-1],ord=order) - print num.linalg.norm(sdomain_c.yvel[-1]-pdomain_c.yvel[-1],ord=order) + print('PDOMAIN CENTROID VALUES') + print(num.linalg.norm(sdomain_c.x-pdomain_c.x,ord=order)) + print(num.linalg.norm(sdomain_c.y-pdomain_c.y,ord=order)) + print(num.linalg.norm(sdomain_c.stage[-1]-pdomain_c.stage[-1],ord=order)) + print(num.linalg.norm(sdomain_c.xmom[-1]-pdomain_c.xmom[-1],ord=order)) + print(num.linalg.norm(sdomain_c.ymom[-1]-pdomain_c.ymom[-1],ord=order)) + print(num.linalg.norm(sdomain_c.xvel[-1]-pdomain_c.xvel[-1],ord=order)) + print(num.linalg.norm(sdomain_c.yvel[-1]-pdomain_c.yvel[-1],ord=order)) assert num.allclose(sdomain_c.stage,pdomain_c.stage) assert num.allclose(sdomain_c.xmom,pdomain_c.xmom) @@ -211,9 +213,9 @@ def boundaryFun(t): #------------------------------ #Evolve the system through time #------------------------------ - if verbose: print 'Evolve' + if verbose: print('Evolve') for t in domain.evolve(yieldstep=10.0,finaltime=150.0): - if myid == 0 and verbose: print domain.timestepping_statistics() + if myid == 0 and verbose: print(domain.timestepping_statistics()) domain.sww_merge(delete_old=True) @@ -224,7 +226,7 @@ def boundaryFun(t): class Test_parallel_riverwall(unittest.TestCase): def test_parallel_riverwall(self): - if verbose : print "Expect this test to fail if not run from the parallel directory." + if verbose : print("Expect this test to fail if not run from the parallel directory.") abs_script_name = os.path.abspath(__file__) cmd = "mpiexec -np %d python %s" % (3, abs_script_name) @@ -236,7 +238,7 @@ def test_parallel_riverwall(self): def assert_(condition, msg="Assertion Failed"): if condition == False: #pypar.finalize() - raise AssertionError, msg + raise_(AssertionError, msg) if __name__=="__main__": if numprocs == 1: @@ -249,7 +251,7 @@ def assert_(condition, msg="Assertion Failed"): # Run the code code and compare sequential # and parallel values #------------------------------------------ - if myid ==0 and verbose: print 'PARALLEL START' + if myid ==0 and verbose: print('PARALLEL START') from anuga.utilities.parallel_abstraction import global_except_hook import sys diff --git a/anuga/parallel/tests/test_parallel_shallow_domain.py b/anuga/parallel/tests/test_parallel_shallow_domain.py index 40e954322..b14a620ea 100644 --- a/anuga/parallel/tests/test_parallel_shallow_domain.py +++ b/anuga/parallel/tests/test_parallel_shallow_domain.py @@ -5,11 +5,13 @@ WARNING: This assumes that the command to run jobs is mpiexec. Tested with MPICH and LAM (Ole) """ +from __future__ import print_function #------------------------------------------------------------------------------ # Import necessary modules #------------------------------------------------------------------------------ +from future.utils import raise_ import unittest import os import sys @@ -85,7 +87,7 @@ def run_simulation(parallel=False): #-------------------------------------------------------------------------- if parallel: - if myid == 0 and verbose: print 'DISTRIBUTING PARALLEL DOMAIN' + if myid == 0 and verbose: print('DISTRIBUTING PARALLEL DOMAIN') domain = distribute(domain) #------------------------------------------------------------------------------ @@ -101,9 +103,9 @@ def run_simulation(parallel=False): # Evolution #------------------------------------------------------------------------------ if parallel: - if myid == 0 and verbose: print 'PARALLEL EVOLVE' + if myid == 0 and verbose: print('PARALLEL EVOLVE') else: - if verbose: print 'SEQUENTIAL EVOLVE' + if verbose: print('SEQUENTIAL EVOLVE') for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): pass @@ -129,7 +131,7 @@ def test_parallel_shallow_domain(self): def assert_(condition, msg="Assertion Failed"): if condition == False: #pypar.finalize() - raise AssertionError, msg + raise_(AssertionError, msg) if __name__=="__main__": if numprocs == 1: @@ -144,12 +146,12 @@ def assert_(condition, msg="Assertion Failed"): pypar.barrier() if myid ==0: - if verbose: print 'PARALLEL START' + if verbose: print('PARALLEL START') run_simulation(parallel=True) if myid == 0: - if verbose: print 'Parallel test OK' + if verbose: print('Parallel test OK') diff --git a/anuga/parallel/tests/test_parallel_sw_flow.py b/anuga/parallel/tests/test_parallel_sw_flow.py index 3fef7997a..897780d1b 100644 --- a/anuga/parallel/tests/test_parallel_sw_flow.py +++ b/anuga/parallel/tests/test_parallel_sw_flow.py @@ -6,11 +6,13 @@ This is a very simple test of the parallel algorithm using the simplified parallel API """ +from __future__ import print_function #------------------------------------------------------------------------------ # Import necessary modules #------------------------------------------------------------------------------ +from future.utils import raise_ import unittest import os import sys @@ -61,7 +63,7 @@ def run_simulation(parallel=False, G = None, seq_interpolation_points=None, verb # Create the parallel domain #-------------------------------------------------------------------------- if parallel: - if myid == 0 and verbose : print 'DISTRIBUTING PARALLEL DOMAIN' + if myid == 0 and verbose : print('DISTRIBUTING PARALLEL DOMAIN') domain = distribute(domain, verbose=False) #-------------------------------------------------------------------------- @@ -114,14 +116,14 @@ def run_simulation(parallel=False, G = None, seq_interpolation_points=None, verb #print " tri_ids ",myid, i, tri_ids[-1] - if verbose: print 'P%d has points = %s' %(myid, tri_ids) + if verbose: print('P%d has points = %s' %(myid, tri_ids)) c_coord = domain.get_centroid_coordinates() interpolation_points = [] for id in tri_ids: if id<1: - if verbose: print 'WARNING: Interpolation point not within the domain!' + if verbose: print('WARNING: Interpolation point not within the domain!') interpolation_points.append(c_coord[id,:]) #------------------------------------------------------------------------------ @@ -130,9 +132,9 @@ def run_simulation(parallel=False, G = None, seq_interpolation_points=None, verb time = [] if parallel: - if myid == 0 and verbose: print 'PARALLEL EVOLVE' + if myid == 0 and verbose: print('PARALLEL EVOLVE') else: - if myid == 0 and verbose: print 'SEQUENTIAL EVOLVE' + if myid == 0 and verbose: print('SEQUENTIAL EVOLVE') for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): @@ -172,7 +174,7 @@ def run_simulation(parallel=False, G = None, seq_interpolation_points=None, verb class Test_parallel_sw_flow(unittest.TestCase): def test_parallel_sw_flow(self): - if verbose : print "Expect this test to fail if not run from the parallel directory." + if verbose : print("Expect this test to fail if not run from the parallel directory.") abs_script_name = os.path.abspath(__file__) cmd = "mpiexec -np %d python %s" % (3, abs_script_name) result = os.system(cmd) @@ -184,7 +186,7 @@ def test_parallel_sw_flow(self): def assert_(condition, msg="Assertion Failed"): if condition == False: #pypar.finalize() - raise AssertionError, msg + raise_(AssertionError, msg) if __name__=="__main__": if numprocs == 1: @@ -203,7 +205,7 @@ def assert_(condition, msg="Assertion Failed"): # array G #------------------------------------------ barrier() - if myid == 0 and verbose: print 'SEQUENTIAL START' + if myid == 0 and verbose: print('SEQUENTIAL START') G , interpolation_points = run_simulation(parallel=False,verbose=verbose) G = num.array(G,num.float) @@ -214,7 +216,7 @@ def assert_(condition, msg="Assertion Failed"): # Run the code code and compare sequential # results at 4 gauge stations #------------------------------------------ - if myid ==0 and verbose: print 'PARALLEL START' + if myid ==0 and verbose: print('PARALLEL START') run_simulation(parallel=True, G=G, seq_interpolation_points = interpolation_points, verbose= verbose) diff --git a/anuga/parallel/tests/test_parallel_sw_flow_de0.py b/anuga/parallel/tests/test_parallel_sw_flow_de0.py index d7c5db39c..29725481f 100644 --- a/anuga/parallel/tests/test_parallel_sw_flow_de0.py +++ b/anuga/parallel/tests/test_parallel_sw_flow_de0.py @@ -6,11 +6,13 @@ This is a very simple test of the parallel algorithm using the simplified parallel API """ +from __future__ import print_function #------------------------------------------------------------------------------ # Import necessary modules #------------------------------------------------------------------------------ +from future.utils import raise_ import unittest import os import sys @@ -60,7 +62,7 @@ def run_simulation(parallel=False, G = None, seq_interpolation_points=None, verb # Create the parallel domain #-------------------------------------------------------------------------- if parallel: - if myid == 0 and verbose : print 'DISTRIBUTING PARALLEL DOMAIN' + if myid == 0 and verbose : print('DISTRIBUTING PARALLEL DOMAIN') domain = distribute(domain, verbose=False) #-------------------------------------------------------------------------- @@ -113,14 +115,14 @@ def run_simulation(parallel=False, G = None, seq_interpolation_points=None, verb #print " tri_ids ",myid, i, tri_ids[-1] - if verbose: print 'P%d has points = %s' %(myid, tri_ids) + if verbose: print('P%d has points = %s' %(myid, tri_ids)) c_coord = domain.get_centroid_coordinates() interpolation_points = [] for id in tri_ids: if id<1: - if verbose: print 'WARNING: Interpolation point not within the domain!' + if verbose: print('WARNING: Interpolation point not within the domain!') interpolation_points.append(c_coord[id,:]) #------------------------------------------------------------------------------ @@ -129,9 +131,9 @@ def run_simulation(parallel=False, G = None, seq_interpolation_points=None, verb time = [] if parallel: - if myid == 0 and verbose: print 'PARALLEL EVOLVE' + if myid == 0 and verbose: print('PARALLEL EVOLVE') else: - if myid == 0 and verbose: print 'SEQUENTIAL EVOLVE' + if myid == 0 and verbose: print('SEQUENTIAL EVOLVE') for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): @@ -171,7 +173,7 @@ def run_simulation(parallel=False, G = None, seq_interpolation_points=None, verb class Test_parallel_sw_flow(unittest.TestCase): def test_parallel_sw_flow(self): - if verbose : print "Expect this test to fail if not run from the parallel directory." + if verbose : print("Expect this test to fail if not run from the parallel directory.") abs_script_name = os.path.abspath(__file__) cmd = "mpiexec -np %d python %s" % (3, abs_script_name) @@ -184,7 +186,7 @@ def test_parallel_sw_flow(self): def assert_(condition, msg="Assertion Failed"): if condition == False: #pypar.finalize() - raise AssertionError, msg + raise_(AssertionError, msg) if __name__=="__main__": if numprocs == 1: @@ -199,7 +201,7 @@ def assert_(condition, msg="Assertion Failed"): # array G #------------------------------------------ barrier() - if myid == 0 and verbose: print 'SEQUENTIAL START' + if myid == 0 and verbose: print('SEQUENTIAL START') G , interpolation_points = run_simulation(parallel=False,verbose=verbose) G = num.array(G,num.float) @@ -210,7 +212,7 @@ def assert_(condition, msg="Assertion Failed"): # Run the code code and compare sequential # results at 4 gauge stations #------------------------------------------ - if myid ==0 and verbose: print 'PARALLEL START' + if myid ==0 and verbose: print('PARALLEL START') from anuga.utilities.parallel_abstraction import global_except_hook import sys diff --git a/anuga/parallel/tests/test_parallel_sw_flow_low_froude_0.py b/anuga/parallel/tests/test_parallel_sw_flow_low_froude_0.py index 21a8f3a88..6a4ae9787 100644 --- a/anuga/parallel/tests/test_parallel_sw_flow_low_froude_0.py +++ b/anuga/parallel/tests/test_parallel_sw_flow_low_froude_0.py @@ -6,11 +6,13 @@ This is a very simple test of the parallel algorithm using the simplified parallel API """ +from __future__ import print_function #------------------------------------------------------------------------------ # Import necessary modules #------------------------------------------------------------------------------ +from future.utils import raise_ import unittest import os import sys @@ -67,7 +69,7 @@ def run_simulation(parallel=False, G = None, seq_interpolation_points=None, verb # Create the parallel domain #-------------------------------------------------------------------------- if parallel: - if myid == 0 and verbose : print 'DISTRIBUTING PARALLEL DOMAIN' + if myid == 0 and verbose : print('DISTRIBUTING PARALLEL DOMAIN') domain = distribute(domain, verbose=False) #-------------------------------------------------------------------------- @@ -118,14 +120,14 @@ def run_simulation(parallel=False, G = None, seq_interpolation_points=None, verb #print " tri_ids ",myid, i, tri_ids[-1] - if verbose: print 'P%d has points = %s' %(myid, tri_ids) + if verbose: print('P%d has points = %s' %(myid, tri_ids)) c_coord = domain.get_centroid_coordinates() interpolation_points = [] for id in tri_ids: if id<1: - if verbose: print 'WARNING: Interpolation point not within the domain!' + if verbose: print('WARNING: Interpolation point not within the domain!') interpolation_points.append(c_coord[id,:]) #------------------------------------------------------------------------------ @@ -134,9 +136,9 @@ def run_simulation(parallel=False, G = None, seq_interpolation_points=None, verb time = [] if parallel: - if myid == 0 and verbose: print 'PARALLEL EVOLVE' + if myid == 0 and verbose: print('PARALLEL EVOLVE') else: - if myid == 0 and verbose: print 'SEQUENTIAL EVOLVE' + if myid == 0 and verbose: print('SEQUENTIAL EVOLVE') for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): @@ -176,7 +178,7 @@ def run_simulation(parallel=False, G = None, seq_interpolation_points=None, verb class Test_parallel_sw_flow(unittest.TestCase): def test_parallel_sw_flow(self): - if verbose : print "Expect this test to fail if not run from the parallel directory." + if verbose : print("Expect this test to fail if not run from the parallel directory.") abs_script_name = os.path.abspath(__file__) cmd = "mpiexec -np %d python %s" % (3, abs_script_name) @@ -189,7 +191,7 @@ def test_parallel_sw_flow(self): def assert_(condition, msg="Assertion Failed"): if condition == False: #pypar.finalize() - raise AssertionError, msg + raise_(AssertionError, msg) if __name__=="__main__": if numprocs == 1: @@ -204,7 +206,7 @@ def assert_(condition, msg="Assertion Failed"): # array G #------------------------------------------ barrier() - if myid == 0 and verbose: print 'SEQUENTIAL START' + if myid == 0 and verbose: print('SEQUENTIAL START') G , interpolation_points = run_simulation(parallel=False,verbose=verbose) G = num.array(G,num.float) @@ -215,7 +217,7 @@ def assert_(condition, msg="Assertion Failed"): # Run the code code and compare sequential # results at 4 gauge stations #------------------------------------------ - if myid ==0 and verbose: print 'PARALLEL START' + if myid ==0 and verbose: print('PARALLEL START') from anuga.utilities.parallel_abstraction import global_except_hook import sys diff --git a/anuga/parallel/tests/test_parallel_sw_flow_low_froude_1.py b/anuga/parallel/tests/test_parallel_sw_flow_low_froude_1.py index 4535865ca..17a9ef312 100644 --- a/anuga/parallel/tests/test_parallel_sw_flow_low_froude_1.py +++ b/anuga/parallel/tests/test_parallel_sw_flow_low_froude_1.py @@ -6,11 +6,13 @@ This is a very simple test of the parallel algorithm using the simplified parallel API """ +from __future__ import print_function #------------------------------------------------------------------------------ # Import necessary modules #------------------------------------------------------------------------------ +from future.utils import raise_ import unittest import os import sys @@ -68,7 +70,7 @@ def run_simulation(parallel=False, G = None, seq_interpolation_points=None, verb # Create the parallel domain #-------------------------------------------------------------------------- if parallel: - if myid == 0 and verbose : print 'DISTRIBUTING PARALLEL DOMAIN' + if myid == 0 and verbose : print('DISTRIBUTING PARALLEL DOMAIN') domain = distribute(domain, verbose=False) #-------------------------------------------------------------------------- @@ -119,14 +121,14 @@ def run_simulation(parallel=False, G = None, seq_interpolation_points=None, verb #print " tri_ids ",myid, i, tri_ids[-1] - if verbose: print 'P%d has points = %s' %(myid, tri_ids) + if verbose: print('P%d has points = %s' %(myid, tri_ids)) c_coord = domain.get_centroid_coordinates() interpolation_points = [] for id in tri_ids: if id<1: - if verbose: print 'WARNING: Interpolation point not within the domain!' + if verbose: print('WARNING: Interpolation point not within the domain!') interpolation_points.append(c_coord[id,:]) #------------------------------------------------------------------------------ @@ -135,9 +137,9 @@ def run_simulation(parallel=False, G = None, seq_interpolation_points=None, verb time = [] if parallel: - if myid == 0 and verbose: print 'PARALLEL EVOLVE' + if myid == 0 and verbose: print('PARALLEL EVOLVE') else: - if myid == 0 and verbose: print 'SEQUENTIAL EVOLVE' + if myid == 0 and verbose: print('SEQUENTIAL EVOLVE') for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): @@ -177,7 +179,7 @@ def run_simulation(parallel=False, G = None, seq_interpolation_points=None, verb class Test_parallel_sw_flow(unittest.TestCase): def test_parallel_sw_flow(self): - if verbose : print "Expect this test to fail if not run from the parallel directory." + if verbose : print("Expect this test to fail if not run from the parallel directory.") abs_script_name = os.path.abspath(__file__) cmd = "mpiexec -np %d python %s" % (3, abs_script_name) @@ -190,7 +192,7 @@ def test_parallel_sw_flow(self): def assert_(condition, msg="Assertion Failed"): if condition == False: #pypar.finalize() - raise AssertionError, msg + raise_(AssertionError, msg) if __name__=="__main__": if numprocs == 1: @@ -209,7 +211,7 @@ def assert_(condition, msg="Assertion Failed"): # array G #------------------------------------------ barrier() - if myid == 0 and verbose: print 'SEQUENTIAL START' + if myid == 0 and verbose: print('SEQUENTIAL START') G , interpolation_points = run_simulation(parallel=False,verbose=verbose) G = num.array(G,num.float) @@ -220,7 +222,7 @@ def assert_(condition, msg="Assertion Failed"): # Run the code code and compare sequential # results at 4 gauge stations #------------------------------------------ - if myid ==0 and verbose: print 'PARALLEL START' + if myid ==0 and verbose: print('PARALLEL START') run_simulation(parallel=True, G=G, seq_interpolation_points = interpolation_points, verbose= verbose) diff --git a/anuga/parallel/tests/test_sequential_dist_sw_flow.py b/anuga/parallel/tests/test_sequential_dist_sw_flow.py index e912722ec..b45658e4c 100644 --- a/anuga/parallel/tests/test_sequential_dist_sw_flow.py +++ b/anuga/parallel/tests/test_sequential_dist_sw_flow.py @@ -6,11 +6,13 @@ This is a very simple test of the parallel algorithm using the simplified parallel API """ +from __future__ import print_function #------------------------------------------------------------------------------ # Import necessary modules #------------------------------------------------------------------------------ +from future.utils import raise_ import unittest import os import sys @@ -78,7 +80,7 @@ def run_simulation(parallel=False, verbose=False): # Create pickled partition #-------------------------------------------------------------------------- if myid == 0: - if verbose: print 'DUMPING PARTITION DATA' + if verbose: print('DUMPING PARTITION DATA') sequential_distribute_dump(domain, numprocs, verbose=verbose, parameters=new_parameters) #-------------------------------------------------------------------------- @@ -86,22 +88,22 @@ def run_simulation(parallel=False, verbose=False): #-------------------------------------------------------------------------- if parallel: - if myid == 0 and verbose : print 'DISTRIBUTING TO PARALLEL DOMAIN' + if myid == 0 and verbose : print('DISTRIBUTING TO PARALLEL DOMAIN') pdomain = distribute(domain, verbose=verbose, parameters=new_parameters) pdomain.set_name('pdomain') - if myid == 0 and verbose : print 'LOADING IN PARALLEL DOMAIN' + if myid == 0 and verbose : print('LOADING IN PARALLEL DOMAIN') sdomain = sequential_distribute_load(filename='odomain', verbose = verbose) sdomain.set_name('sdomain') - if myid == 0 and verbose: print 'EVOLVING pdomain' + if myid == 0 and verbose: print('EVOLVING pdomain') setup_and_evolve(pdomain, verbose=verbose) - if myid == 0 and verbose: print 'EVOLVING sdomain' + if myid == 0 and verbose: print('EVOLVING sdomain') setup_and_evolve(sdomain, verbose=verbose) if myid == 0: - if verbose: print 'EVOLVING odomain' + if verbose: print('EVOLVING odomain') setup_and_evolve(domain, verbose=verbose) @@ -133,7 +135,7 @@ def run_simulation(parallel=False, verbose=False): # Now compare the merged sww files #--------------------------------- if myid == 0: - if verbose: print 'COMPARING SWW FILES' + if verbose: print('COMPARING SWW FILES') odomain_v = util.get_output('odomain.sww') odomain_c = util.get_centroids(odomain_v) @@ -149,38 +151,38 @@ def run_simulation(parallel=False, verbose=False): if verbose: order = 2 - print 'PDOMAIN CENTROID VALUES' - print num.linalg.norm(odomain_c.x-pdomain_c.x,ord=order) - print num.linalg.norm(odomain_c.y-pdomain_c.y,ord=order) - print num.linalg.norm(odomain_c.stage[-1]-pdomain_c.stage[-1],ord=order) - print num.linalg.norm(odomain_c.xmom[-1]-pdomain_c.xmom[-1],ord=order) - print num.linalg.norm(odomain_c.ymom[-1]-pdomain_c.ymom[-1],ord=order) - print num.linalg.norm(odomain_c.xvel[-1]-pdomain_c.xvel[-1],ord=order) - print num.linalg.norm(odomain_c.yvel[-1]-pdomain_c.yvel[-1],ord=order) + print('PDOMAIN CENTROID VALUES') + print(num.linalg.norm(odomain_c.x-pdomain_c.x,ord=order)) + print(num.linalg.norm(odomain_c.y-pdomain_c.y,ord=order)) + print(num.linalg.norm(odomain_c.stage[-1]-pdomain_c.stage[-1],ord=order)) + print(num.linalg.norm(odomain_c.xmom[-1]-pdomain_c.xmom[-1],ord=order)) + print(num.linalg.norm(odomain_c.ymom[-1]-pdomain_c.ymom[-1],ord=order)) + print(num.linalg.norm(odomain_c.xvel[-1]-pdomain_c.xvel[-1],ord=order)) + print(num.linalg.norm(odomain_c.yvel[-1]-pdomain_c.yvel[-1],ord=order)) - print 'SDOMAIN CENTROID VALUES' - print num.linalg.norm(odomain_c.x-sdomain_c.x,ord=order) - print num.linalg.norm(odomain_c.y-sdomain_c.y,ord=order) - print num.linalg.norm(odomain_c.stage[-1]-sdomain_c.stage[-1],ord=order) - print num.linalg.norm(odomain_c.xmom[-1]-sdomain_c.xmom[-1],ord=order) - print num.linalg.norm(odomain_c.ymom[-1]-sdomain_c.ymom[-1],ord=order) - print num.linalg.norm(odomain_c.xvel[-1]-sdomain_c.xvel[-1],ord=order) - print num.linalg.norm(odomain_c.yvel[-1]-sdomain_c.yvel[-1],ord=order) + print('SDOMAIN CENTROID VALUES') + print(num.linalg.norm(odomain_c.x-sdomain_c.x,ord=order)) + print(num.linalg.norm(odomain_c.y-sdomain_c.y,ord=order)) + print(num.linalg.norm(odomain_c.stage[-1]-sdomain_c.stage[-1],ord=order)) + print(num.linalg.norm(odomain_c.xmom[-1]-sdomain_c.xmom[-1],ord=order)) + print(num.linalg.norm(odomain_c.ymom[-1]-sdomain_c.ymom[-1],ord=order)) + print(num.linalg.norm(odomain_c.xvel[-1]-sdomain_c.xvel[-1],ord=order)) + print(num.linalg.norm(odomain_c.yvel[-1]-sdomain_c.yvel[-1],ord=order)) - print 'PDOMAIN VERTEX VALUES' - print num.linalg.norm(odomain_v.stage[-1]-pdomain_v.stage[-1],ord=order) - print num.linalg.norm(odomain_v.xmom[-1]-pdomain_v.xmom[-1],ord=order) - print num.linalg.norm(odomain_v.ymom[-1]-pdomain_v.ymom[-1],ord=order) - print num.linalg.norm(odomain_v.xvel[-1]-pdomain_v.xvel[-1],ord=order) - print num.linalg.norm(odomain_v.yvel[-1]-pdomain_v.yvel[-1],ord=order) + print('PDOMAIN VERTEX VALUES') + print(num.linalg.norm(odomain_v.stage[-1]-pdomain_v.stage[-1],ord=order)) + print(num.linalg.norm(odomain_v.xmom[-1]-pdomain_v.xmom[-1],ord=order)) + print(num.linalg.norm(odomain_v.ymom[-1]-pdomain_v.ymom[-1],ord=order)) + print(num.linalg.norm(odomain_v.xvel[-1]-pdomain_v.xvel[-1],ord=order)) + print(num.linalg.norm(odomain_v.yvel[-1]-pdomain_v.yvel[-1],ord=order)) - print 'SDOMAIN VERTEX VALUES' - print num.linalg.norm(odomain_v.stage[-1]-sdomain_v.stage[-1],ord=order) - print num.linalg.norm(odomain_v.xmom[-1]-sdomain_v.xmom[-1],ord=order) - print num.linalg.norm(odomain_v.ymom[-1]-sdomain_v.ymom[-1],ord=order) - print num.linalg.norm(odomain_v.xvel[-1]-sdomain_v.xvel[-1],ord=order) - print num.linalg.norm(odomain_v.yvel[-1]-sdomain_v.yvel[-1],ord=order) + print('SDOMAIN VERTEX VALUES') + print(num.linalg.norm(odomain_v.stage[-1]-sdomain_v.stage[-1],ord=order)) + print(num.linalg.norm(odomain_v.xmom[-1]-sdomain_v.xmom[-1],ord=order)) + print(num.linalg.norm(odomain_v.ymom[-1]-sdomain_v.ymom[-1],ord=order)) + print(num.linalg.norm(odomain_v.xvel[-1]-sdomain_v.xvel[-1],ord=order)) + print(num.linalg.norm(odomain_v.yvel[-1]-sdomain_v.yvel[-1],ord=order)) @@ -289,7 +291,7 @@ def setup_and_evolve(domain, verbose=False): class Test_parallel_sw_flow(unittest.TestCase): def test_parallel_sw_flow(self): - if verbose : print "Expect this test to fail if not run from the parallel directory." + if verbose : print("Expect this test to fail if not run from the parallel directory.") abs_script_name = os.path.abspath(__file__) cmd = "mpiexec -np %d python %s" % (3, abs_script_name) @@ -302,7 +304,7 @@ def test_parallel_sw_flow(self): def assert_(condition, msg="Assertion Failed"): if condition == False: #pypar.finalize() - raise AssertionError, msg + raise_(AssertionError, msg) if __name__=="__main__": if numprocs == 1: @@ -319,7 +321,7 @@ def assert_(condition, msg="Assertion Failed"): # Run the codel and compare sequential # results at 4 gauge stations #------------------------------------------ - if myid ==0 and verbose: print 'PARALLEL START' + if myid ==0 and verbose: print('PARALLEL START') run_simulation(parallel=True, verbose=verbose) diff --git a/anuga/pmesh/AppShell.py b/anuga/pmesh/AppShell.py index af4da7d51..122272393 100644 --- a/anuga/pmesh/AppShell.py +++ b/anuga/pmesh/AppShell.py @@ -1,345 +1,346 @@ -#! /usr/env/python - -""" -AppShell provides a GUI application framework. - -This is a streamlined adaptation of GuiAppD.py, originally -created by Doug Hellmann (doughellmann@mindspring.com). - -Pmw copyright - -Copyright 1997-1999 Telstra Corporation Limited, -Australia Copyright 2000-2002 Really Good Software Pty Ltd, Australia - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -""" - - -from Tkinter import * -import Pmw -import sys, string -import ProgressBar - -class AppShell(Pmw.MegaWidget): - appversion = '1.0' - appname = 'Generic Application Frame' - copyright = 'Copyright YYYY Your Company. All Rights Reserved' - contactname = 'Your Name' - contactphone = '(999) 555-1212' - contactemail = 'youremail@host.com' - - frameWidth = 450 - frameHeight = 320 - padx = 5 - pady = 5 - usecommandarea = 0 - balloonhelp = 1 - - busyCursor = 'watch' - - def __init__(self, **kw): - optiondefs = ( - ('padx', 1, Pmw.INITOPT), - ('pady', 1, Pmw.INITOPT), - ('framewidth', 1, Pmw.INITOPT), - ('frameheight', 1, Pmw.INITOPT), - ('usecommandarea', self.usecommandarea, Pmw.INITOPT)) - self.defineoptions(kw, optiondefs) - - self.root = Tk() - self.initializeTk(self.root) - Pmw.initialise(self.root) - self.root.title(self.appname) - self.root.geometry('%dx%d' % (self.frameWidth, self.frameHeight)) - - # Initialize the base class - Pmw.MegaWidget.__init__(self, parent=self.root) - - # initialize the application - self.appInit() - - # create the interface - self.__createInterface() - - # create a table to hold the cursors for - # widgets which get changed when we go busy - self.preBusyCursors = None - - # pack the container and set focus - # to ourselves - self._hull.pack(side=TOP, fill=BOTH, expand=YES) - self.focus_set() - - # initialize our options - self.initialiseoptions(AppShell) - - def appInit(self): - # Called before interface is created (should be overridden). - pass - - def initializeTk(self, root): - # Initialize platform-specific options - if sys.platform == 'mac': - self.__initializeTk_mac(root) - elif sys.platform == 'win32': - self.__initializeTk_win32(root) - else: - self.__initializeTk_unix(root) - - def __initializeTk_colors_common(self, root): - root.option_add('*background', 'grey') - root.option_add('*foreground', 'black') - root.option_add('*EntryField.Entry.background', 'white') - root.option_add('*Entry.background', 'white') - root.option_add('*MessageBar.Entry.background', 'gray85') - root.option_add('*Listbox*background', 'white') - root.option_add('*Listbox*selectBackground', 'dark slate blue') - root.option_add('*Listbox*selectForeground', 'white') - - def __initializeTk_win32(self, root): - self.__initializeTk_colors_common(root) - root.option_add('*Font', 'Verdana 10 bold') - root.option_add('*EntryField.Entry.Font', 'Courier 10') - root.option_add('*Listbox*Font', 'Courier 10') - - def __initializeTk_mac(self, root): - self.__initializeTk_colors_common(root) - - def __initializeTk_unix(self, root): - self.__initializeTk_colors_common(root) - - def busyStart(self, newcursor=None): - if not newcursor: - newcursor = self.busyCursor - newPreBusyCursors = {} - for component in self.busyWidgets: - newPreBusyCursors[component] = component['cursor'] - component.configure(cursor=newcursor) - component.update_idletasks() - self.preBusyCursors = (newPreBusyCursors, self.preBusyCursors) - - def busyEnd(self): - if not self.preBusyCursors: - return - oldPreBusyCursors = self.preBusyCursors[0] - self.preBusyCursors = self.preBusyCursors[1] - for component in self.busyWidgets: - try: - component.configure(cursor=oldPreBusyCursors[component]) - except KeyError: - pass - component.update_idletasks() - - def __createAboutBox(self): - Pmw.aboutversion(self.appversion) - Pmw.aboutcopyright(self.copyright) - Pmw.aboutcontact( - 'For more information, contact:\n %s\n Phone: %s\n Email: %s' %\ - (self.contactname, self.contactphone, - self.contactemail)) - self.about = Pmw.AboutDialog(self._hull, - applicationname=self.appname) - self.about.withdraw() - return None - - def showAbout(self): - # Create the dialog to display about and contact information. - self.about.show() - self.about.focus_set() - - def toggleBalloon(self): - if self.toggleBalloonVar.get(): - self.__balloon.configure(state = 'both') - else: - self.__balloon.configure(state = 'status') - - def __createMenuBar(self): - self.menuBar = self.createcomponent('menubar', (), None, - Pmw.MenuBar, - (self._hull,), - hull_relief=RAISED, - hull_borderwidth=1, - balloon=self.balloon()) - - self.menuBar.pack(fill=X) - self.menuBar.addmenu('Help', 'About %s' % self.appname, side='right') - self.menuBar.addmenu('File', 'File commands and Quit') - - def createMenuBar(self): - self.menuBar.addmenuitem('Help', 'command', - 'Get information on application', - label='About...', command=self.showAbout) - self.toggleBalloonVar = IntVar() - self.toggleBalloonVar.set(1) - self.menuBar.addmenuitem('Help', 'checkbutton', - 'Toggle balloon help', - label='Balloon help', - variable = self.toggleBalloonVar, - command=self.toggleBalloon) - - self.menuBar.addmenuitem('File', 'command', 'Quit this application', - label='Quit', - command=self.quit) - - def __createBalloon(self): - # Create the balloon help manager for the frame. - # Create the manager for the balloon help - self.__balloon = self.createcomponent('balloon', (), None, - Pmw.Balloon, (self._hull,)) - - def balloon(self): - return self.__balloon - - def __createDataArea(self): - # Create data area where data entry widgets are placed. - self.dataArea = self.createcomponent('dataarea', - (), None, - Frame, (self._hull,), - relief=GROOVE, - bd=1) - self.dataArea.pack(side=TOP, fill=BOTH, expand=YES, - padx=self['padx'], pady=self['pady']) - - def __createCommandArea(self): - # Create a command area for application-wide buttons. - self.__commandFrame = self.createcomponent('commandframe', (), None, - Frame, - (self._hull,), - relief=SUNKEN, - bd=1) - self.__buttonBox = self.createcomponent('buttonbox', (), None, - Pmw.ButtonBox, - (self.__commandFrame,), - padx=0, pady=0) - self.__buttonBox.pack(side=TOP, expand=NO, fill=X) - if self['usecommandarea']: - self.__commandFrame.pack(side=TOP, - expand=NO, - fill=X, - padx=self['padx'], - pady=self['pady']) - - - def __createMessageBar(self): - # Create the message bar area for help and status messages. - frame = self.createcomponent('bottomtray', (), None, - Frame,(self._hull,), relief=SUNKEN) - self.__messageBar = self.createcomponent('messagebar', - (), None, - Pmw.MessageBar, - (frame,), - #entry_width = 40, - entry_relief=SUNKEN, - entry_bd=1, - labelpos=None) - self.__messageBar.pack(side=LEFT, expand=YES, fill=X) - - self.__progressBar = ProgressBar.ProgressBar(frame, - fillColor='slateblue', - doLabel=1, - width=150) - self.__progressBar.frame.pack(side=LEFT, expand=NO, fill=NONE) - - self.updateProgress(0) - frame.pack(side=BOTTOM, expand=NO, fill=X) - - self.__balloon.configure(statuscommand = \ - self.__messageBar.helpmessage) - - def messageBar(self): - return self.__messageBar - - def updateProgress(self, newValue=0, newMax=0): - self.__progressBar.updateProgress(newValue, newMax) - - def bind(self, child, balloonHelpMsg, statusHelpMsg=None): - # Bind a help message and/or status message to a widget. - self.__balloon.bind(child, balloonHelpMsg, statusHelpMsg) - - def interior(self): - # Retrieve the interior site where widgets should go. - return self.dataArea - - def buttonBox(self): - # Retrieve the button box. - return self.__buttonBox - - def buttonAdd(self, buttonName, helpMessage=None, - statusMessage=None, **kw): - # Add a button to the button box. - newBtn = self.__buttonBox.add(buttonName) - newBtn.configure(kw) - if helpMessage: - self.bind(newBtn, helpMessage, statusMessage) - return newBtn - - def __createInterface(self): - self.__createBalloon() - self.__createMenuBar() - self.__createDataArea() - self.__createCommandArea() - self.__createMessageBar() - self.__createAboutBox() - # - # Create the parts of the interface - # which can be modified by subclasses - # - self.busyWidgets = ( self.root, ) - self.createMenuBar() - self.createInterface() - - def createInterface(self): - # Override this method to create the interface for the app. - pass - - def main(self): - # This method should be left intact! - self.pack() - self.mainloop() - - def run(self): - self.main() - -class TestAppShell(AppShell): - usecommandarea=1 - - def createButtons(self): - self.buttonAdd('Ok', - helpMessage='Exit', - statusMessage='Exit', - command=self.quit) - - def createMain(self): - self.label = self.createcomponent('label', (), None, - Label, - (self.interior(),), - text='Data Area') - self.label.pack() - self.bind(self.label, 'Space taker') - - def createInterface(self): - AppShell.createInterface(self) - self.createButtons() - self.createMain() - -if __name__ == '__main__': - test = TestAppShell(balloon_state='both') - test.run() +#! /usr/env/python + +""" +AppShell provides a GUI application framework. + +This is a streamlined adaptation of GuiAppD.py, originally +created by Doug Hellmann (doughellmann@mindspring.com). + +Pmw copyright + +Copyright 1997-1999 Telstra Corporation Limited, +Australia Copyright 2000-2002 Really Good Software Pty Ltd, Australia + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +""" +from __future__ import absolute_import + + +from Tkinter import * +from . import Pmw +import sys, string +from . import ProgressBar + +class AppShell(Pmw.MegaWidget): + appversion = '1.0' + appname = 'Generic Application Frame' + copyright = 'Copyright YYYY Your Company. All Rights Reserved' + contactname = 'Your Name' + contactphone = '(999) 555-1212' + contactemail = 'youremail@host.com' + + frameWidth = 450 + frameHeight = 320 + padx = 5 + pady = 5 + usecommandarea = 0 + balloonhelp = 1 + + busyCursor = 'watch' + + def __init__(self, **kw): + optiondefs = ( + ('padx', 1, Pmw.INITOPT), + ('pady', 1, Pmw.INITOPT), + ('framewidth', 1, Pmw.INITOPT), + ('frameheight', 1, Pmw.INITOPT), + ('usecommandarea', self.usecommandarea, Pmw.INITOPT)) + self.defineoptions(kw, optiondefs) + + self.root = Tk() + self.initializeTk(self.root) + Pmw.initialise(self.root) + self.root.title(self.appname) + self.root.geometry('%dx%d' % (self.frameWidth, self.frameHeight)) + + # Initialize the base class + Pmw.MegaWidget.__init__(self, parent=self.root) + + # initialize the application + self.appInit() + + # create the interface + self.__createInterface() + + # create a table to hold the cursors for + # widgets which get changed when we go busy + self.preBusyCursors = None + + # pack the container and set focus + # to ourselves + self._hull.pack(side=TOP, fill=BOTH, expand=YES) + self.focus_set() + + # initialize our options + self.initialiseoptions(AppShell) + + def appInit(self): + # Called before interface is created (should be overridden). + pass + + def initializeTk(self, root): + # Initialize platform-specific options + if sys.platform == 'mac': + self.__initializeTk_mac(root) + elif sys.platform == 'win32': + self.__initializeTk_win32(root) + else: + self.__initializeTk_unix(root) + + def __initializeTk_colors_common(self, root): + root.option_add('*background', 'grey') + root.option_add('*foreground', 'black') + root.option_add('*EntryField.Entry.background', 'white') + root.option_add('*Entry.background', 'white') + root.option_add('*MessageBar.Entry.background', 'gray85') + root.option_add('*Listbox*background', 'white') + root.option_add('*Listbox*selectBackground', 'dark slate blue') + root.option_add('*Listbox*selectForeground', 'white') + + def __initializeTk_win32(self, root): + self.__initializeTk_colors_common(root) + root.option_add('*Font', 'Verdana 10 bold') + root.option_add('*EntryField.Entry.Font', 'Courier 10') + root.option_add('*Listbox*Font', 'Courier 10') + + def __initializeTk_mac(self, root): + self.__initializeTk_colors_common(root) + + def __initializeTk_unix(self, root): + self.__initializeTk_colors_common(root) + + def busyStart(self, newcursor=None): + if not newcursor: + newcursor = self.busyCursor + newPreBusyCursors = {} + for component in self.busyWidgets: + newPreBusyCursors[component] = component['cursor'] + component.configure(cursor=newcursor) + component.update_idletasks() + self.preBusyCursors = (newPreBusyCursors, self.preBusyCursors) + + def busyEnd(self): + if not self.preBusyCursors: + return + oldPreBusyCursors = self.preBusyCursors[0] + self.preBusyCursors = self.preBusyCursors[1] + for component in self.busyWidgets: + try: + component.configure(cursor=oldPreBusyCursors[component]) + except KeyError: + pass + component.update_idletasks() + + def __createAboutBox(self): + Pmw.aboutversion(self.appversion) + Pmw.aboutcopyright(self.copyright) + Pmw.aboutcontact( + 'For more information, contact:\n %s\n Phone: %s\n Email: %s' %\ + (self.contactname, self.contactphone, + self.contactemail)) + self.about = Pmw.AboutDialog(self._hull, + applicationname=self.appname) + self.about.withdraw() + return None + + def showAbout(self): + # Create the dialog to display about and contact information. + self.about.show() + self.about.focus_set() + + def toggleBalloon(self): + if self.toggleBalloonVar.get(): + self.__balloon.configure(state = 'both') + else: + self.__balloon.configure(state = 'status') + + def __createMenuBar(self): + self.menuBar = self.createcomponent('menubar', (), None, + Pmw.MenuBar, + (self._hull,), + hull_relief=RAISED, + hull_borderwidth=1, + balloon=self.balloon()) + + self.menuBar.pack(fill=X) + self.menuBar.addmenu('Help', 'About %s' % self.appname, side='right') + self.menuBar.addmenu('File', 'File commands and Quit') + + def createMenuBar(self): + self.menuBar.addmenuitem('Help', 'command', + 'Get information on application', + label='About...', command=self.showAbout) + self.toggleBalloonVar = IntVar() + self.toggleBalloonVar.set(1) + self.menuBar.addmenuitem('Help', 'checkbutton', + 'Toggle balloon help', + label='Balloon help', + variable = self.toggleBalloonVar, + command=self.toggleBalloon) + + self.menuBar.addmenuitem('File', 'command', 'Quit this application', + label='Quit', + command=self.quit) + + def __createBalloon(self): + # Create the balloon help manager for the frame. + # Create the manager for the balloon help + self.__balloon = self.createcomponent('balloon', (), None, + Pmw.Balloon, (self._hull,)) + + def balloon(self): + return self.__balloon + + def __createDataArea(self): + # Create data area where data entry widgets are placed. + self.dataArea = self.createcomponent('dataarea', + (), None, + Frame, (self._hull,), + relief=GROOVE, + bd=1) + self.dataArea.pack(side=TOP, fill=BOTH, expand=YES, + padx=self['padx'], pady=self['pady']) + + def __createCommandArea(self): + # Create a command area for application-wide buttons. + self.__commandFrame = self.createcomponent('commandframe', (), None, + Frame, + (self._hull,), + relief=SUNKEN, + bd=1) + self.__buttonBox = self.createcomponent('buttonbox', (), None, + Pmw.ButtonBox, + (self.__commandFrame,), + padx=0, pady=0) + self.__buttonBox.pack(side=TOP, expand=NO, fill=X) + if self['usecommandarea']: + self.__commandFrame.pack(side=TOP, + expand=NO, + fill=X, + padx=self['padx'], + pady=self['pady']) + + + def __createMessageBar(self): + # Create the message bar area for help and status messages. + frame = self.createcomponent('bottomtray', (), None, + Frame,(self._hull,), relief=SUNKEN) + self.__messageBar = self.createcomponent('messagebar', + (), None, + Pmw.MessageBar, + (frame,), + #entry_width = 40, + entry_relief=SUNKEN, + entry_bd=1, + labelpos=None) + self.__messageBar.pack(side=LEFT, expand=YES, fill=X) + + self.__progressBar = ProgressBar.ProgressBar(frame, + fillColor='slateblue', + doLabel=1, + width=150) + self.__progressBar.frame.pack(side=LEFT, expand=NO, fill=NONE) + + self.updateProgress(0) + frame.pack(side=BOTTOM, expand=NO, fill=X) + + self.__balloon.configure(statuscommand = \ + self.__messageBar.helpmessage) + + def messageBar(self): + return self.__messageBar + + def updateProgress(self, newValue=0, newMax=0): + self.__progressBar.updateProgress(newValue, newMax) + + def bind(self, child, balloonHelpMsg, statusHelpMsg=None): + # Bind a help message and/or status message to a widget. + self.__balloon.bind(child, balloonHelpMsg, statusHelpMsg) + + def interior(self): + # Retrieve the interior site where widgets should go. + return self.dataArea + + def buttonBox(self): + # Retrieve the button box. + return self.__buttonBox + + def buttonAdd(self, buttonName, helpMessage=None, + statusMessage=None, **kw): + # Add a button to the button box. + newBtn = self.__buttonBox.add(buttonName) + newBtn.configure(kw) + if helpMessage: + self.bind(newBtn, helpMessage, statusMessage) + return newBtn + + def __createInterface(self): + self.__createBalloon() + self.__createMenuBar() + self.__createDataArea() + self.__createCommandArea() + self.__createMessageBar() + self.__createAboutBox() + # + # Create the parts of the interface + # which can be modified by subclasses + # + self.busyWidgets = ( self.root, ) + self.createMenuBar() + self.createInterface() + + def createInterface(self): + # Override this method to create the interface for the app. + pass + + def main(self): + # This method should be left intact! + self.pack() + self.mainloop() + + def run(self): + self.main() + +class TestAppShell(AppShell): + usecommandarea=1 + + def createButtons(self): + self.buttonAdd('Ok', + helpMessage='Exit', + statusMessage='Exit', + command=self.quit) + + def createMain(self): + self.label = self.createcomponent('label', (), None, + Label, + (self.interior(),), + text='Data Area') + self.label.pack() + self.bind(self.label, 'Space taker') + + def createInterface(self): + AppShell.createInterface(self) + self.createButtons() + self.createMain() + +if __name__ == '__main__': + test = TestAppShell(balloon_state='both') + test.run() diff --git a/anuga/pmesh/Pmw.py b/anuga/pmesh/Pmw.py index 54f9ec7af..ed4548d77 100644 --- a/anuga/pmesh/Pmw.py +++ b/anuga/pmesh/Pmw.py @@ -1,9253 +1,9252 @@ -""" -Pmw copyright - -Copyright 1997-1999 Telstra Corporation Limited, -Australia Copyright 2000-2002 Really Good Software Pty Ltd, Australia - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - - -""" - - -import PmwColor -Color = PmwColor -del PmwColor - -import PmwBlt -Blt = PmwBlt -del PmwBlt - - -### Loader functions: - -_VERSION = '1.2' - -def setversion(version): - if version != _VERSION: - raise ValueError('Dynamic versioning not available') - -def setalphaversions(*alpha_versions): - if alpha_versions != (): - raise ValueError('Dynamic versioning not available') - -def version(alpha = 0): - if alpha: - return () - else: - return _VERSION - -def installedversions(alpha = 0): - if alpha: - return () - else: - return (_VERSION,) - - -###################################################################### -### File: PmwBase.py -# Pmw megawidget base classes. - -# This module provides a foundation for building megawidgets. It -# contains the MegaArchetype class which manages component widgets and -# configuration options. Also provided are the MegaToplevel and -# MegaWidget classes, derived from the MegaArchetype class. The -# MegaToplevel class contains a Tkinter Toplevel widget to act as the -# container of the megawidget. This is used as the base class of all -# megawidgets that are contained in their own top level window, such -# as a Dialog window. The MegaWidget class contains a Tkinter Frame -# to act as the container of the megawidget. This is used as the base -# class of all other megawidgets, such as a ComboBox or ButtonBox. -# -# Megawidgets are built by creating a class that inherits from either -# the MegaToplevel or MegaWidget class. - -import os -import string -import sys -import traceback -import types -import Tkinter - -# Special values used in index() methods of several megawidgets. -END = ['end'] -SELECT = ['select'] -DEFAULT = ['default'] - -# Constant used to indicate that an option can only be set by a call -# to the constructor. -INITOPT = ['initopt'] -_DEFAULT_OPTION_VALUE = ['default_option_value'] -_useTkOptionDb = 0 - -# Symbolic constants for the indexes into an optionInfo list. -_OPT_DEFAULT = 0 -_OPT_VALUE = 1 -_OPT_FUNCTION = 2 - -# Stacks - -_busyStack = [] - # Stack which tracks nested calls to show/hidebusycursor (called - # either directly or from activate()/deactivate()). Each element - # is a dictionary containing: - # 'newBusyWindows' : List of windows which had busy_hold called - # on them during a call to showbusycursor(). - # The corresponding call to hidebusycursor() - # will call busy_release on these windows. - # 'busyFocus' : The blt _Busy window which showbusycursor() - # set the focus to. - # 'previousFocus' : The focus as it was when showbusycursor() - # was called. The corresponding call to - # hidebusycursor() will restore this focus if - # the focus has not been changed from busyFocus. - -_grabStack = [] - # Stack of grabbed windows. It tracks calls to push/popgrab() - # (called either directly or from activate()/deactivate()). The - # window on the top of the stack is the window currently with the - # grab. Each element is a dictionary containing: - # 'grabWindow' : The window grabbed by pushgrab(). The - # corresponding call to popgrab() will release - # the grab on this window and restore the grab - # on the next window in the stack (if there is one). - # 'globalMode' : True if the grabWindow was grabbed with a - # global grab, false if the grab was local - # and 'nograb' if no grab was performed. - # 'previousFocus' : The focus as it was when pushgrab() - # was called. The corresponding call to - # popgrab() will restore this focus. - # 'deactivateFunction' : - # The function to call (usually grabWindow.deactivate) if - # popgrab() is called (usually from a deactivate() method) - # on a window which is not at the top of the stack (that is, - # does not have the grab or focus). For example, if a modal - # dialog is deleted by the window manager or deactivated by - # a timer. In this case, all dialogs above and including - # this one are deactivated, starting at the top of the - # stack. - - # Note that when dealing with focus windows, the name of the Tk - # widget is used, since it may be the '_Busy' window, which has no - # python instance associated with it. - -#============================================================================= - -# Functions used to forward methods from a class to a component. - -# Fill in a flattened method resolution dictionary for a class (attributes are -# filtered out). Flattening honours the MI method resolution rules -# (depth-first search of bases in order). The dictionary has method names -# for keys and functions for values. -def __methodDict(cls, dict): - - # the strategy is to traverse the class in the _reverse_ of the normal - # order, and overwrite any duplicates. - baseList = list(cls.__bases__) - baseList.reverse() - - # do bases in reverse order, so first base overrides last base - for super in baseList: - __methodDict(super, dict) - - # do my methods last to override base classes - for key, value in cls.__dict__.items(): - # ignore class attributes - if type(value) == types.FunctionType: - dict[key] = value - -def __methods(cls): - # Return all method names for a class. - - # Return all method names for a class (attributes are filtered - # out). Base classes are searched recursively. - - dict = {} - __methodDict(cls, dict) - return dict.keys() - -# Function body to resolve a forwarding given the target method name and the -# attribute name. The resulting lambda requires only self, but will forward -# any other parameters. -__stringBody = ( - 'def %(method)s(this, *args, **kw): return ' + - 'apply(this.%(attribute)s.%(method)s, args, kw)') - -# Get a unique id -__counter = 0 -def __unique(): - global __counter - __counter = __counter + 1 - return str(__counter) - -# Function body to resolve a forwarding given the target method name and the -# index of the resolution function. The resulting lambda requires only self, -# but will forward any other parameters. The target instance is identified -# by invoking the resolution function. -__funcBody = ( - 'def %(method)s(this, *args, **kw): return ' + - 'apply(this.%(forwardFunc)s().%(method)s, args, kw)') - -def forwardmethods(fromClass, toClass, toPart, exclude = ()): - # Forward all methods from one class to another. - - # Forwarders will be created in fromClass to forward method - # invocations to toClass. The methods to be forwarded are - # identified by flattening the interface of toClass, and excluding - # methods identified in the exclude list. Methods already defined - # in fromClass, or special methods with one or more leading or - # trailing underscores will not be forwarded. - - # For a given object of class fromClass, the corresponding toClass - # object is identified using toPart. This can either be a String - # denoting an attribute of fromClass objects, or a function taking - # a fromClass object and returning a toClass object. - - # Example: - # class MyClass: - # ... - # def __init__(self): - # ... - # self.__target = TargetClass() - # ... - # def findtarget(self): - # return self.__target - # forwardmethods(MyClass, TargetClass, '__target', ['dangerous1', 'dangerous2']) - # # ...or... - # forwardmethods(MyClass, TargetClass, MyClass.findtarget, - # ['dangerous1', 'dangerous2']) - - # In both cases, all TargetClass methods will be forwarded from - # MyClass except for dangerous1, dangerous2, special methods like - # __str__, and pre-existing methods like findtarget. - - - # Allow an attribute name (String) or a function to determine the instance - if not isinstance(toPart, basestring): - - # check that it is something like a function - if callable(toPart): - - # If a method is passed, use the function within it - if hasattr(toPart, 'im_func'): - toPart = toPart.im_func - - # After this is set up, forwarders in this class will use - # the forwarding function. The forwarding function name is - # guaranteed to be unique, so that it can't be hidden by subclasses - forwardName = '__fwdfunc__' + __unique() - fromClass.__dict__[forwardName] = toPart - - # It's not a valid type - else: - raise TypeError('toPart must be attribute name, function or method') - - # get the full set of candidate methods - dict = {} - __methodDict(toClass, dict) - - # discard special methods - for ex in dict.keys(): - if ex[:1] == '_' or ex[-1:] == '_': - del dict[ex] - # discard dangerous methods supplied by the caller - for ex in exclude: - if dict.has_key(ex): - del dict[ex] - # discard methods already defined in fromClass - for ex in __methods(fromClass): - if dict.has_key(ex): - del dict[ex] - - for method, func in dict.items(): - d = {'method': method, 'func': func} - if isinstance(toPart, basestring): - execString = \ - __stringBody % {'method' : method, 'attribute' : toPart} - else: - execString = \ - __funcBody % {'forwardFunc' : forwardName, 'method' : method} - - exec execString in d - - # this creates a method - fromClass.__dict__[method] = d[method] - -#============================================================================= - -def setgeometryanddeiconify(window, geom): - # To avoid flashes on X and to position the window correctly on NT - # (caused by Tk bugs). - - if os.name == 'nt' or \ - (os.name == 'posix' and sys.platform[:6] == 'cygwin'): - # Require overrideredirect trick to stop window frame - # appearing momentarily. - redirect = window.overrideredirect() - if not redirect: - window.overrideredirect(1) - window.deiconify() - if geom is not None: - window.geometry(geom) - # Call update_idletasks to ensure NT moves the window to the - # correct position it is raised. - window.update_idletasks() - window.tkraise() - if not redirect: - window.overrideredirect(0) - else: - if geom is not None: - window.geometry(geom) - - # Problem!? Which way around should the following two calls - # go? If deiconify() is called first then I get complaints - # from people using the enlightenment or sawfish window - # managers that when a dialog is activated it takes about 2 - # seconds for the contents of the window to appear. But if - # tkraise() is called first then I get complaints from people - # using the twm window manager that when a dialog is activated - # it appears in the top right corner of the screen and also - # takes about 2 seconds to appear. - - #window.tkraise() - # Call update_idletasks to ensure certain window managers (eg: - # enlightenment and sawfish) do not cause Tk to delay for - # about two seconds before displaying window. - #window.update_idletasks() - #window.deiconify() - - window.deiconify() - if window.overrideredirect(): - # The window is not under the control of the window manager - # and so we need to raise it ourselves. - window.tkraise() - -#============================================================================= - -class MegaArchetype: - # Megawidget abstract root class. - - # This class provides methods which are inherited by classes - # implementing useful bases (this class doesn't provide a - # container widget inside which the megawidget can be built). - - def __init__(self, parent = None, hullClass = None): - - # Mapping from each megawidget option to a list of information - # about the option - # - default value - # - current value - # - function to call when the option is initialised in the - # call to initialiseoptions() in the constructor or - # modified via configure(). If this is INITOPT, the - # option is an initialisation option (an option that can - # be set by the call to the constructor but can not be - # used with configure). - # This mapping is not initialised here, but in the call to - # defineoptions() which precedes construction of this base class. - # - # self._optionInfo = {} - - # Mapping from each component name to a tuple of information - # about the component. - # - component widget instance - # - configure function of widget instance - # - the class of the widget (Frame, EntryField, etc) - # - cget function of widget instance - # - the name of the component group of this component, if any - self.__componentInfo = {} - - # Mapping from alias names to the names of components or - # sub-components. - self.__componentAliases = {} - - # Contains information about the keywords provided to the - # constructor. It is a mapping from the keyword to a tuple - # containing: - # - value of keyword - # - a boolean indicating if the keyword has been used. - # A keyword is used if, during the construction of a megawidget, - # - it is defined in a call to defineoptions() or addoptions(), or - # - it references, by name, a component of the megawidget, or - # - it references, by group, at least one component - # At the end of megawidget construction, a call is made to - # initialiseoptions() which reports an error if there are - # unused options given to the constructor. - # - # After megawidget construction, the dictionary contains - # keywords which refer to a dynamic component group, so that - # these components can be created after megawidget - # construction and still use the group options given to the - # constructor. - # - # self._constructorKeywords = {} - - # List of dynamic component groups. If a group is included in - # this list, then it not an error if a keyword argument for - # the group is given to the constructor or to configure(), but - # no components with this group have been created. - # self._dynamicGroups = () - - if hullClass is None: - self._hull = None - else: - if parent is None: - parent = Tkinter._default_root - - # Create the hull. - self._hull = self.createcomponent('hull', - (), None, - hullClass, (parent,)) - _hullToMegaWidget[self._hull] = self - - if _useTkOptionDb: - # Now that a widget has been created, query the Tk - # option database to get the default values for the - # options which have not been set in the call to the - # constructor. This assumes that defineoptions() is - # called before the __init__(). - option_get = self.option_get - _VALUE = _OPT_VALUE - _DEFAULT = _OPT_DEFAULT - for name, info in self._optionInfo.items(): - value = info[_VALUE] - if value is _DEFAULT_OPTION_VALUE: - resourceClass = string.upper(name[0]) + name[1:] - value = option_get(name, resourceClass) - if value != '': - try: - # Convert the string to int/float/tuple, etc - value = eval(value, {'__builtins__': {}}) - except: - pass - info[_VALUE] = value - else: - info[_VALUE] = info[_DEFAULT] - - def destroy(self): - # Clean up optionInfo in case it contains circular references - # in the function field, such as self._settitle in class - # MegaToplevel. - - self._optionInfo = {} - if self._hull is not None: - del _hullToMegaWidget[self._hull] - self._hull.destroy() - - #====================================================================== - # Methods used (mainly) during the construction of the megawidget. - - def defineoptions(self, keywords, optionDefs, dynamicGroups = ()): - # Create options, providing the default value and the method - # to call when the value is changed. If any option created by - # base classes has the same name as one in , the - # base class's value and function will be overriden. - - # This should be called before the constructor of the base - # class, so that default values defined in the derived class - # override those in the base class. - - if not hasattr(self, '_constructorKeywords'): - # First time defineoptions has been called. - tmp = {} - for option, value in keywords.items(): - tmp[option] = [value, 0] - self._constructorKeywords = tmp - self._optionInfo = {} - self._initialiseoptions_counter = 0 - self._initialiseoptions_counter = self._initialiseoptions_counter + 1 - - if not hasattr(self, '_dynamicGroups'): - self._dynamicGroups = () - self._dynamicGroups = self._dynamicGroups + tuple(dynamicGroups) - self.addoptions(optionDefs) - - def addoptions(self, optionDefs): - # Add additional options, providing the default value and the - # method to call when the value is changed. See - # "defineoptions" for more details - - # optimisations: - optionInfo = self._optionInfo - optionInfo_has_key = optionInfo.has_key - keywords = self._constructorKeywords - keywords_has_key = keywords.has_key - FUNCTION = _OPT_FUNCTION - - for name, default, function in optionDefs: - if '_' not in name: - # The option will already exist if it has been defined - # in a derived class. In this case, do not override the - # default value of the option or the callback function - # if it is not None. - if not optionInfo_has_key(name): - if keywords_has_key(name): - value = keywords[name][0] - optionInfo[name] = [default, value, function] - del keywords[name] - else: - if _useTkOptionDb: - optionInfo[name] = \ - [default, _DEFAULT_OPTION_VALUE, function] - else: - optionInfo[name] = [default, default, function] - elif optionInfo[name][FUNCTION] is None: - optionInfo[name][FUNCTION] = function - else: - # This option is of the form "component_option". If this is - # not already defined in self._constructorKeywords add it. - # This allows a derived class to override the default value - # of an option of a component of a base class. - if not keywords_has_key(name): - keywords[name] = [default, 0] - - def createcomponent(self, componentName, componentAliases, - componentGroup, widgetClass, *widgetArgs, **kw): - # Create a component (during construction or later). - - if self.__componentInfo.has_key(componentName): - raise ValueError('Component "%s" already exists' % componentName) - - if '_' in componentName: - raise ValueError('Component name "%s" must not contain "_"' - % componentName) - - if hasattr(self, '_constructorKeywords'): - keywords = self._constructorKeywords - else: - keywords = {} - for alias, component in componentAliases: - # Create aliases to the component and its sub-components. - index = string.find(component, '_') - if index < 0: - self.__componentAliases[alias] = (component, None) - else: - mainComponent = component[:index] - subComponent = component[(index + 1):] - self.__componentAliases[alias] = (mainComponent, subComponent) - - # Remove aliases from the constructor keyword arguments by - # replacing any keyword arguments that begin with *alias* - # with corresponding keys beginning with *component*. - - alias = alias + '_' - aliasLen = len(alias) - for option in keywords.keys(): - if len(option) > aliasLen and option[:aliasLen] == alias: - newkey = component + '_' + option[aliasLen:] - keywords[newkey] = keywords[option] - del keywords[option] - - componentPrefix = componentName + '_' - nameLen = len(componentPrefix) - for option in keywords.keys(): - if len(option) > nameLen and option[:nameLen] == componentPrefix: - # The keyword argument refers to this component, so add - # this to the options to use when constructing the widget. - kw[option[nameLen:]] = keywords[option][0] - del keywords[option] - else: - # Check if this keyword argument refers to the group - # of this component. If so, add this to the options - # to use when constructing the widget. Mark the - # keyword argument as being used, but do not remove it - # since it may be required when creating another - # component. - index = string.find(option, '_') - if index >= 0 and componentGroup == option[:index]: - rest = option[(index + 1):] - kw[rest] = keywords[option][0] - keywords[option][1] = 1 - - if kw.has_key('pyclass'): - widgetClass = kw['pyclass'] - del kw['pyclass'] - if widgetClass is None: - return None - if len(widgetArgs) == 1 and isinstance(widgetArgs[0], tuple): - # Arguments to the constructor can be specified as either - # multiple trailing arguments to createcomponent() or as a - # single tuple argument. - widgetArgs = widgetArgs[0] - widget = apply(widgetClass, widgetArgs, kw) - componentClass = widget.__class__.__name__ - self.__componentInfo[componentName] = (widget, widget.configure, - componentClass, widget.cget, componentGroup) - - return widget - - def destroycomponent(self, name): - # Remove a megawidget component. - - # This command is for use by megawidget designers to destroy a - # megawidget component. - - self.__componentInfo[name][0].destroy() - del self.__componentInfo[name] - - def createlabel(self, parent, childCols = 1, childRows = 1): - - labelpos = self['labelpos'] - labelmargin = self['labelmargin'] - if labelpos is None: - return - - label = self.createcomponent('label', - (), None, - Tkinter.Label, (parent,)) - - if labelpos[0] in 'ns': - # vertical layout - if labelpos[0] == 'n': - row = 0 - margin = 1 - else: - row = childRows + 3 - margin = row - 1 - label.grid(column=2, row=row, columnspan=childCols, sticky=labelpos) - parent.grid_rowconfigure(margin, minsize=labelmargin) - else: - # horizontal layout - if labelpos[0] == 'w': - col = 0 - margin = 1 - else: - col = childCols + 3 - margin = col - 1 - label.grid(column=col, row=2, rowspan=childRows, sticky=labelpos) - parent.grid_columnconfigure(margin, minsize=labelmargin) - - def initialiseoptions(self, dummy = None): - self._initialiseoptions_counter = self._initialiseoptions_counter - 1 - if self._initialiseoptions_counter == 0: - unusedOptions = [] - keywords = self._constructorKeywords - for name in keywords.keys(): - used = keywords[name][1] - if not used: - # This keyword argument has not been used. If it - # does not refer to a dynamic group, mark it as - # unused. - index = string.find(name, '_') - if index < 0 or name[:index] not in self._dynamicGroups: - unusedOptions.append(name) - if len(unusedOptions) > 0: - if len(unusedOptions) == 1: - text = 'Unknown option "' - else: - text = 'Unknown options "' - raise KeyError(text + string.join(unusedOptions, ', ') - + '" for ' + self.__class__.__name__) - - # Call the configuration callback function for every option. - FUNCTION = _OPT_FUNCTION - for info in self._optionInfo.values(): - func = info[FUNCTION] - if func is not None and func is not INITOPT: - func() - - #====================================================================== - # Method used to configure the megawidget. - - def configure(self, option=None, **kw): - # Query or configure the megawidget options. - # - # If not empty, *kw* is a dictionary giving new - # values for some of the options of this megawidget or its - # components. For options defined for this megawidget, set - # the value of the option to the new value and call the - # configuration callback function, if any. For options of the - # form _