def try_io_operation(io_fn): if cfg.options.on_io_error in [1, 2]: max_tries = 100 else: max_tries = 0 wait_time = 6 tries = 0 done = False while not done: try: io_fn() break except RuntimeError: # e.g., generated by C++ code tries += 1 if tries > max_tries: logger.error("I/O operation failed, giving up after %s tries.", max_tries) raise logger.warn( "I/O operation failed, retrying in %s seconds. (Tries left: %s)", wait_time, max_tries - tries) time.sleep(wait_time) except: # any other exception bails out raise
def create_controller(run, params, *args, **kwargs): """ Create a controller object, depending on the environment in which the script was executed: TODO: Explain. """ # Case 1: Report to stdout, then exit, if requested by command line options. print_num_params = cfg.options.print_num_params_requested print_all_params = cfg.options.print_all_params_requested if print_num_params or print_all_params: return PrintParametersController(run, params, print_num_params, print_all_params, *args, **kwargs) # Case 2: Use environment variable to select parameter set env = kwargs.pop("env", None) env_offset = kwargs.pop("env_offset", 0) if env: if env in os.environ: return EnvironmentVariableController(run, params, env, env_offset, *args, **kwargs) else: logger.warn("Environment variable '%s' not found, ignoring 'env' parameter in controller creation" % env) # Case 3: Sun grid engine controller sge = kwargs.pop("sun_grid_engine", False) if sge: if "SGE_TASK_ID" in os.environ: return SunGridEngineController(run, params, *args, **kwargs) else: logger.warn("Environment variable 'SGE_TASK_ID' not found, ignoring 'sun_grid_engine' parameter in controller creation") # Case 4: This controller is used when the script was executed locally. It optionally uses the -p argument passed to the sim script. return LocalController(run, params, *args, **kwargs)
def cpu_count(): try: import multiprocessing return multiprocessing.cpu_count() except: from magnum.logger import logger logger.warn("Could not find out number of processors") return 1
def assign_VectorField(field, val, mask): mesh = field.mesh # initialize from lambda? from_lambda = hasattr(val, "__call__") # initialize from other VectorField ??? from_field = isinstance(val, VectorField) # initialize from a numerical value? from_value = not from_lambda and not from_field and isinstance( val, tuple) and len(val) == 3 # fill entire field? fill_everywhere = (mask is None) or (len(mask) == mesh.total_nodes) # Do the initialization (modify field) if from_value: if fill_everywhere: # Fill complete field with a single value field.fill(val) else: # Masked fill with a single value for c in mask: field.set(c, val) elif from_field: src_field = val # Automatic field interpolation if meshes do not match. # XXX Is this a good idea? if not field.mesh.isCompatible(src_field.mesh): src_field = src_field.interpolate(field.mesh) logger.warn( "Field initialized by linear interpolation of other field (their meshes were different sizes!)" ) if fill_everywhere: # Fill field by copying values from init field field.assign(src_field) else: # Fill some parts of the field using values from field for c in mask: field.set(c, src_field.get(c)) elif from_lambda: fn = val # Fill some parts of the field using an initialization function for c in mask or range(mesh.total_nodes): field.set(c, fn(field, mesh.getPosition(c))) else: raise ValueError( "Don't know how to assign to vector field using the expression %s" % val) return field
def __init__(self, run, params, env, offset=0, *args, **kwargs): super(EnvironmentVariableController, self).__init__(run, params, *args, **kwargs) try: task_id = int(os.environ[env]) - offset except: logger.error("Could not read environment variable '%s'." % env) raise if task_id >= len(self.all_params): logger.warn("SGE task id is greater than the number of parameter sets.") self.my_params = [] else: self.my_params = [(task_id, self.all_params[task_id])]
def __init__(self, run, params, env, offset=0, *args, **kwargs): super(EnvironmentVariableController, self).__init__(run, params, *args, **kwargs) try: task_id = int(os.environ[env]) - offset except: logger.error("Could not read environment variable '%s'." % env) raise if task_id >= len(self.all_params): logger.warn( "SGE task id is greater than the number of parameter sets.") self.my_params = [] else: self.my_params = [(task_id, self.all_params[task_id])]
def assign_VectorField(field, val, mask): mesh = field.mesh # initialize from lambda? from_lambda = hasattr(val, "__call__") # initialize from other VectorField ??? from_field = isinstance(val, VectorField) # initialize from a numerical value? from_value = not from_lambda and not from_field and isinstance(val, tuple) and len(val) == 3 # fill entire field? fill_everywhere = (mask is None) or (len(mask) == mesh.total_nodes) # Do the initialization (modify field) if from_value: if fill_everywhere: # Fill complete field with a single value field.fill(val) else: # Masked fill with a single value for c in mask: field.set(c, val) elif from_field: src_field = val # Automatic field interpolation if meshes do not match. # XXX Is this a good idea? if not field.mesh.isCompatible(src_field.mesh): src_field = src_field.interpolate(field.mesh) logger.warn("Field initialized by linear interpolation of other field (their meshes were different sizes!)") if fill_everywhere: # Fill field by copying values from init field field.assign(src_field) else: # Fill some parts of the field using values from field for c in mask: field.set(c, src_field.get(c)) elif from_lambda: fn = val # Fill some parts of the field using an initialization function for c in mask or range(mesh.total_nodes): field.set(c, fn(field, mesh.getPosition(c))) else: raise ValueError("Don't know how to assign to vector field using the expression %s" % val) return field
def __init__(self, run, params, **options): ControllerBase.__init__(self, run, params) if hasattr(cfg.options, 'prange'): prange = cfg.options.prange else: prange = range(len(self.getAllParameters())) self.__prange = [] for i in prange: if i < len(self.getAllParameters()): self.__prange.append(i) else: logger.warn("Ignoring parameter id %s (no such parameter set!)" % i) if len(self.__prange) == 0: logger.warn("Controller: No parameter sets selected!")
def __init__(self, run, params, *args, **kwargs): super(LocalController, self).__init__(run, params, *args, **kwargs) if hasattr(cfg.options, 'prange'): prange = cfg.options.prange else: prange = range(self.num_params) self.my_params = [] for i in prange: if i < self.num_params: self.my_params.append((i, self.all_params[i])) else: logger.warn("Ignoring parameter id %s (no such parameter set!)" % i) if len(self.my_params) == 0: logger.warn("Controller: No parameter sets selected!")
def __init__(self, run, params, *args, **kwargs): super(LocalController, self).__init__(run, params, *args, **kwargs) if hasattr(cfg.options, 'prange'): prange = cfg.options.prange else: prange = range(self.num_params) self.my_params = [] for i in prange: if i < self.num_params: self.my_params.append((i, self.all_params[i])) else: logger.warn( "Ignoring parameter id %s (no such parameter set!)" % i) if len(self.my_params) == 0: logger.warn("Controller: No parameter sets selected!")
def try_io_operation(io_fn): if cfg.options.on_io_error in [1, 2]: max_tries = 100 else: max_tries = 0 wait_time = 6 tries = 0 done = False while not done: try: io_fn() break except RuntimeError: # e.g., generated by C++ code tries += 1 if tries > max_tries: logger.error("I/O operation failed, giving up after %s tries.", max_tries) raise logger.warn("I/O operation failed, retrying in %s seconds. (Tries left: %s)", wait_time, max_tries - tries) time.sleep(wait_time) except: # any other exception is an immediate error raise
def createImage(*args, **kwargs): logger.warn(_msg)
def parseCommandLine(self, argv): from optparse import OptionParser, OptionValueError, OptionGroup parser = OptionParser(version="MagNum v.0.1") hw_group = OptionGroup(parser, "Hardware options", "Options that control which hardware is used.", ) hw_group.add_option("-g", type = "string", help = "enable GPU processing (using 32-bit accuracy) on cuda device GPU_ID. The simulator will fall back to CPU mode if it was not compiled with CUDA support or when no CUDA capable graphics cards were detected.", metavar = "GPU_ID", dest = "gpu32", default = None ) hw_group.add_option("-G", type = "string", help = "enable GPU processing (using 64-bit accuracy) on cuda device GPU_ID. TODO: Describe fallback behaviour.", metavar = "GPU_ID", dest = "gpu64", default = None ) hw_group.add_option("-t", "--threads", help = "enable CPU multithreading with NUM_THREADS (1..64) threads. This parameter instructs the fftw library to use NUM_THREADS threads for computing FFTs.", metavar = "NUM_THREADS", dest = "num_fftw_threads", type = "int", default = 1 ) #hw_group.add_option("-f", "--fftw-mode", # help = "set fftw plan creation mode (0:Estimate, 1:Measure, 2:Exhaustive), default is Measure (1).", # dest = "fftw_plan", # default = 1, # type = "int", # metavar = "FFTW_MODE" #) parser.add_option_group(hw_group) log_group = OptionGroup(parser, "Logging options", "Options related to logging and benchmarking." ) log_group.add_option("--loglevel", "-l", type = "int", help = "set log level (0:Debug, 1:Info, 2:Warn, 4:Error, 5:Critical), default is Debug (0).", metavar = "LEVEL", dest = "loglevel", default = 1 ) log_group.add_option("--prof", help = "Log profiling info at program exit.", dest = "profiling_enabled", action = "store_true", default = False ) parser.add_option_group(log_group) def prange_callback(option, opt_str, value, parser, *args, **kwargs): # e.g.: ./script.py --prange=0,64 -> xrange(0,64) object match = re.match("^(\d+),(\d+)$", value) if match: p0, p1 = int(match.group(1)), int(match.group(2)) else: match = re.match("^(\d+)$", value) if match: p0 = int(match.group(1)) p1 = p0 + 1 else: raise OptionValueError("Invalid --prange format.") if not p0 < p1: raise OptionValueError("Invalid --prange specified: second value must be greater than first value") setattr(parser.values, "prange", range(p0, p1)) ctrl_group = OptionGroup(parser, "Parameter sweep options", "These options have only an effect when the simulation script uses a Controller object to sweep through a parameter range." ) ctrl_group.add_option("--param-range", "-p", type = "string", help = "set parameter range to run, e.g. --prange=0,64. ", metavar = "RANGE", action = "callback", callback= prange_callback, default = None ) ctrl_group.add_option("--print-num-params", help = "print number of sweep parameters to stdout and exit.", action = "store_true", dest = "print_num_params_requested", default = False ) ctrl_group.add_option("--print-all-params", action = "store_true", help = "print all sweep parameters to stdout and exit.", dest = "print_all_params_requested", default = False, ) parser.add_option_group(ctrl_group) misc_group = OptionGroup(parser, "Miscellanous options" ) misc_group.add_option("--on_io_error", type = "int", help = "Specifies what to do when an i/o error occurs when writing an .omf/.vtk file. 0: Abort (default), 1: Retry a few times, then abort, 2: Retry a few times, then pause and ask for user intervention", metavar = "MODE", dest = "on_io_error", default = 0 ) parser.add_option_group(misc_group) options, rest_args = parser.parse_args(argv) self.options = options ### Process the options ### # --loglevel, -l import logging ll_map = [logging.DEBUG, logging.INFO, logging.WARN, logging.ERROR, logging.CRITICAL] logger.setLevel(ll_map[options.loglevel]) logger.info("MicroMagnum 0.2 Copyright (C) 2012 by the MicroMagnum team.") logger.info("This program comes with ABSOLUTELY NO WARRANTY.") logger.info("This is free software, and you are welcome to redistribute it") logger.info("under certain conditions; see the file COPYING in the distribution package.") logger.info("----------") # -g, -G def parse_gpu_id(arg): if arg == "auto": return -1 return int(arg) cuda_mode = MagnumConfig.CUDA_DISABLED cuda_dev = -1 if options.gpu32 is not None: cuda_mode = MagnumConfig.CUDA_32 cuda_dev = parse_gpu_id(options.gpu32) if options.gpu64 is not None: if options.gpu32 is not None: logger.warn("Ignoring -g argument because -G was specified") cuda_mode = MagnumConfig.CUDA_64 cuda_dev = parse_gpu_id(options.gpu64) self.enableCuda(cuda_mode, cuda_dev) # --prof if options.profiling_enabled: self.enableProfiling(True) # enable fftw threads... self.setFFTWThreads(options.num_fftw_threads)
def Co(**params): """ Return a material object for the material 'Cobalt'. """ logger.warn("Warning: The Co material parameters are not verified!") return Material.fromDB("Co", **params)
def Ni(**params): """ Return a material object for the material 'Nickel'. """ logger.warn("Warning: The Ni material parameters are not verified!") return Material.fromDB("Ni", **params)
def Fe(**params): """ Return a material object for the material 'Iron'. """ logger.warn("Warning: The Fe material parameters are not verified!") return Material.fromDB("Fe", **params)
from .shape import Shape from magnum.logger import logger from magnum.mesh import RectangularMesh try: import Image _found_image_lib = True except: _found_image_lib = False logger.warn("Python Imaging Library not found!") logger.warn("-> This means that the ImageShapeCreator and related classes are not available!") class ImageShape(Shape): """ This class can create a shape with an image. The image defines the shape between the x-y-axes and z is filled in the whole world. To use the ImageShape take the *ImageShapeCreator*. For example: .. code-block:: python isc = ImageShapeCreator("phi.png", mesh) world = World(mesh, Body("phi", Material.Py(), isc.pick("black"))) You can use the following colors: *black, green, blue, red, yellow, grey, white.* It is not necessary to use a color exactly. Every pixel is interpreted as the nearest color. """ def __init__(self, isc, color):
# # You should have received a copy of the GNU General Public License # along with magnum.fd. If not, see <http://www.gnu.org/licenses/>. from magnum.micromagnetics.world.shape import Shape from magnum.logger import logger from magnum.mesh import RectangularMesh from math import log, floor try: import gmshpy _found_gmsh_lib = True except: _found_gmsh_lib = False logger.warn("Python wrappers for Gmsh not found!") logger.warn("-> This means that the GmshShape class is not available!") class GmshShape(Shape): """ This class can create a shape from a number of gemetry/mesh files that are supported by Gmsh. The Python wrappers for Gmsh have to be installed on the system. """ def __init__(self, model, shift = (0.0, 0.0, 0.0), scale = 1.0): if not _found_gmsh_lib: raise NotImplementedError("GmshShape class can not be used because the Python wrappers for GMSH could not be loaded ('import gmshpy')") super(GmshShape, self).__init__() self.__model = model self.__shift = shift self.__scale = scale
# # You should have received a copy of the GNU General Public License # along with magnum.fd. If not, see <http://www.gnu.org/licenses/>. from magnum.micromagnetics.world.shape import Shape from magnum.logger import logger from magnum.mesh import RectangularMesh from math import log, floor try: import gmshpy _found_gmsh_lib = True except: _found_gmsh_lib = False logger.warn("Python wrappers for Gmsh not found!") logger.warn("-> This means that the GmshShape class is not available!") class GmshShape(Shape): """ This class can create a shape from a number of gemetry/mesh files that are supported by Gmsh. The Python wrappers for Gmsh have to be installed on the system. """ def __init__(self, model, shift=(0.0, 0.0, 0.0), scale=1.0): if not _found_gmsh_lib: raise NotImplementedError( "GmshShape class can not be used because the Python wrappers for GMSH could not be loaded ('import gmshpy')" ) super(GmshShape, self).__init__()
def writeImage(*args, **kwargs): logger.warn(_msg)
def Controller(run, params, *args, **kwargs): logger.warn("The 'Controller' function is deprecated, please use 'create_controller' instead.") return create_controller(run, params, *args, **kwargs)
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with magnum.fd. If not, see <http://www.gnu.org/licenses/>. from magnum.logger import logger from magnum.mesh import RectangularMesh from magnum.micromagnetics.world.shape import Shape try: import Image _found_image_lib = True except: _found_image_lib = False logger.warn("Python Imaging Library not found!") logger.warn( "-> This means that the ImageShapeCreator and related classes are not available!" ) class ImageShape(Shape): """ This class can create a shape from an graphical image using a color mask. In order to use this class, the Python Imaging Library has to be installed ('import Image'). ImageShape objects are usually created by the *pick* method of an *ImageShapeCreator* object: .. code-block:: python isc = ImageShapeCreator("image.png", mesh) # load the image, stretching it over the xy-axes of the mesh.
def parseCommandLine(self, argv): import magnum parser = OptionParser(version="MicroMagnum " + magnum.__version__) hw_group = OptionGroup( parser, "Hardware options", "Options that control which hardware is used.", ) hw_group.add_option( "-g", type="string", help= "enable GPU processing (using 32-bit accuracy) on cuda device GPU_ID. The simulator will fall back to CPU mode if it was not compiled with CUDA support or when no CUDA capable graphics cards were detected.", metavar="GPU_ID", dest="gpu32", default=None) hw_group.add_option( "-G", type="string", help= "enable GPU processing (using 64-bit accuracy) on cuda device GPU_ID. TODO: Describe fallback behaviour.", metavar="GPU_ID", dest="gpu64", default=None) hw_group.add_option( "-t", "--threads", help= "enable CPU multithreading with NUM_THREADS (1..64) threads. This parameter instructs the fftw library to use NUM_THREADS threads for computing FFTs.", metavar="NUM_THREADS", dest="num_fftw_threads", type="int", default=1) parser.add_option_group(hw_group) log_group = OptionGroup( parser, "Logging options", "Options related to logging and benchmarking.") log_group.add_option( "--loglevel", "-l", type="int", help= "set log level (0:Debug, 1:Info, 2:Warn, 4:Error, 5:Critical), default is Debug (0).", metavar="LEVEL", dest="loglevel", default=1) log_group.add_option("--prof", help="Log profiling info at program exit.", dest="profiling_enabled", action="store_true", default=False) parser.add_option_group(log_group) def prange_callback(option, opt_str, value, parser, *args, **kwargs): # e.g.: ./script.py --prange=0,64 -> xrange(0,64) object match = re.match("^(\d+),(\d+)$", value) if match: p0, p1 = int(match.group(1)), int(match.group(2)) else: match = re.match("^(\d+)$", value) if match: p0 = int(match.group(1)) p1 = p0 + 1 else: raise OptionValueError( "Invalid --param-range / -p format.") if not p0 < p1: raise OptionValueError( "Invalid --param-range / -p specified: second value must be greater than first value" ) setattr(parser.values, "prange", range(p0, p1)) ctrl_group = OptionGroup( parser, "Parameter sweep options", "These options have only an effect when the simulation script uses a Controller object to sweep through a parameter range." ) ctrl_group.add_option( "--param-range", "-p", type="string", help= "select parameter set to run, e.g. --param-range=0,64 to run sets 0 to 63.", metavar="RANGE", action="callback", callback=prange_callback, default=None) ctrl_group.add_option( "--print-num-params", help="print number of sweep parameters to stdout and exit.", action="store_true", dest="print_num_params_requested", default=False) ctrl_group.add_option( "--print-all-params", action="store_true", help="print all sweep parameters to stdout and exit.", dest="print_all_params_requested", default=False, ) parser.add_option_group(ctrl_group) misc_group = OptionGroup(parser, "Miscellanous options") misc_group.add_option( "--on_io_error", type="int", help= "Specifies what to do when an i/o error occurs when writing an .omf/.vtk file. 0: Abort (default), 1: Retry a few times, then abort, 2: Retry a few times, then pause and ask for user intervention", metavar="MODE", dest="on_io_error", default=0) parser.add_option_group(misc_group) options, rest_args = parser.parse_args(argv) self.options = options ### Process the options ### # --loglevel, -l ll_map = [ logging.DEBUG, logging.INFO, logging.WARN, logging.ERROR, logging.CRITICAL ] logger.setLevel(ll_map[options.loglevel]) logger.info( "----------------------------------------------------------------------" ) logger.info("MicroMagnum %s" % magnum.__version__) logger.info("Copyright (C) 2012 by the MicroMagnum team.") logger.info("This program comes with ABSOLUTELY NO WARRANTY.") logger.info( "This is free software, and you are welcome to redistribute it under" ) logger.info( "certain conditions; see the file COPYING in the distribution package." ) logger.info( "----------------------------------------------------------------------" ) # -g, -G if options.gpu32 and options.gpu64: logger.warn("Ignoring -g argument because -G was specified") def parse_gpu_id(arg): return -1 if arg == "auto" else int(arg) if options.gpu64: cuda_mode, cuda_dev = MagnumConfig.CUDA_64, parse_gpu_id( options.gpu64) elif options.gpu32: cuda_mode, cuda_dev = MagnumConfig.CUDA_32, parse_gpu_id( options.gpu32) else: cuda_mode, cuda_dev = MagnumConfig.CUDA_DISABLED, -1 self.enableCuda(cuda_mode, cuda_dev) # --prof if options.profiling_enabled: self.enableProfiling(True) # enable fftw threads... self.setFFTWThreads(options.num_fftw_threads)