Changeset 3269
- Timestamp:
- 01/26/12 21:06:53 (4 months ago)
- Location:
- branches/mcxtrace-1.0/src/mcrun2
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
branches/mcxtrace-1.0/src/mcrun2/main.py
r3268 r3269 1 1 #!/usr/bin/env python2.6 2 2 3 import os.path4 3 import logging 5 4 6 from os.path import isfile, isdir, abspath, dirname , basename5 from os.path import isfile, isdir, abspath, dirname 7 6 from optparse import OptionParser, OptionGroup, OptionValueError 8 7 … … 18 17 ''' value must be acceptable ''' 19 18 if not accept(value): 20 raise OptionValueError('option %s: %s (was: "%s")' % (option, msg, value)) 19 raise OptionValueError('option %s: %s (was: "%s")' % \ 20 (option, msg, value)) 21 21 # Update parser with accepted value 22 22 setattr(parser.values, option.dest, value) … … 120 120 # Data options 121 121 dir_exists = lambda path: isdir(abspath(path)) 122 check_dir = build_checker(dir_exists, 'invalid path')123 122 def check_file(exist=True): 124 123 ''' Validate the path to a file ''' … … 164 163 165 164 165 def expand_options(options): 166 ''' Add extra options based on previous choices ''' 167 if options.mpi > 0: 168 options.use_mpi = True 169 options.cc = 'mpicc' 170 else: 171 options.use_mpi = False 172 options.cc = 'gcc' 173 174 166 175 def main(): 167 176 ''' Main routine ''' … … 170 179 formatter = logging.Formatter('%(asctime)s - %(message)s') 171 180 172 ch= logging.StreamHandler()173 ch.setLevel(logging.INFO)174 ch.setFormatter(formatter)181 handler = logging.StreamHandler() 182 handler.setLevel(logging.INFO) 183 handler.setFormatter(formatter) 175 184 176 185 LOG.setLevel(logging.DEBUG) 177 LOG.addHandler( ch)186 LOG.addHandler(handler) 178 187 179 188 # Add options … … 188 197 (options, args) = parser.parse_args() 189 198 parser.destroy() 199 expand_options(options) 190 200 191 201 if options.verbose: 192 ch.setLevel(logging.DEBUG)202 handler.setLevel(logging.DEBUG) 193 203 194 204 # Extract instrument and parameters 195 205 if len(args) == 0: 196 206 raise OptionValueError('No instrument file specified.') 197 instr = args[0]198 params = args[1:]207 options.instr = args[0] 208 options.params = args[1:] 199 209 200 210 # Run McStas 201 mcstas = McStas( instr)211 mcstas = McStas(options.instr) 202 212 mcstas.prepare(options) 203 mcstas.run( options)213 mcstas.run() 204 214 205 215 -
branches/mcxtrace-1.0/src/mcrun2/mcstas.py
r3266 r3269 13 13 14 14 def modified(path): 15 ''' Get modification time of path in seconds ''' 15 16 return os.stat(path).st_mtime 16 17 17 18 18 19 class ProcessException(Exception): 20 ''' Exception/error in external process ''' 21 19 22 def __init__(self, executable, args, retval): 20 23 Exception.__init__(self) … … 29 32 30 33 class Process: 34 ''' An external process ''' 35 31 36 def __init__(self, executable): 32 37 self.executable = executable 33 38 34 39 def run(self, args=None): 40 ''' Run external process with args ''' 41 35 42 if args is None: 36 43 args = [] … … 38 45 # Run executable 39 46 LOG.debug('CMD: %s %s' % (self.executable, args)) 40 f d = Popen([self.executable] + args,41 stdout=None,42 stderr=None)43 stdout, stderr = f d.communicate()47 fid = Popen([self.executable] + args, 48 stdout=None, 49 stderr=None) 50 stdout, stderr = fid.communicate() 44 51 45 52 # Check if process terminated correctly 46 retval = f d.wait()53 retval = fid.wait() 47 54 if retval != 0: 48 55 raise ProcessException(self.executable, args, retval) … … 52 59 53 60 class McStas: 61 ''' McStas instrument ''' 62 54 63 def __init__(self, instrument_file): 55 64 if not isfile(instrument_file): … … 57 66 self.path = instrument_file 58 67 self.name = splitext(basename(self.path))[0] 68 self.options = None 59 69 60 70 # Setup paths … … 69 79 def prepare(self, options): 70 80 ''' Prepare for simultation run ''' 81 self.options = options 82 83 # Use mpi? 84 mpi = options.use_mpi 85 86 self.binpath += (mpi and '-mpi' or '') 87 71 88 # Check if instrument code has changed 72 89 if not options.force_compile and isfile(self.binpath) \ … … 74 91 LOG.info('Using existing binary: %s' % self.binpath) 75 92 return # skip 93 LOG.info('Recompiling: %s' % self.binpath) 76 94 77 # Proceed with generation and compilation 78 LOG.info('Recompiling: %s' % self.binpath) 95 # Generate C-code 79 96 Process('mcstas').run(['-o', self.cpath, self.path]) 80 97 81 cc = 'gcc' 98 # Setup cflags 99 cflags = [ 100 '-lm', # math library 101 options.no_cflags and '-O0' or '-O2', # optimizing flags 102 mpi and '-DUSE_MPI' or '-D', # MPI 103 ] 82 104 83 105 # Compiler optimisation 84 opt = options.no_cflags and '-O0' or '-O2' 85 args = ['-o', self.binpath, opt, '-lm', self.cpath] 106 args = ['-o', self.binpath] + cflags + [self.cpath] 86 107 87 Process( cc).run(args)108 Process(options.cc).run(args) 88 109 89 110 90 def run(self , options):111 def run(self): 91 112 ''' Run simulation ''' 92 113 args = [] 114 options = self.options 93 115 print repr(options) 94 116 95 # Handle proxy options, pass through 117 mpi = self.options.use_mpi 118 119 # Handle proxy options with values 96 120 proxy_opts_val = ['seed', 'ncount', 'dir', 'file', 'format'] 97 proxy_opts_flags = ['trace', 'gravitation', 'data-only',98 'no-output-files', 'info']99 100 121 for opt in proxy_opts_val: 101 122 val = getattr(options, opt.replace('-', '_')) … … 103 124 args.extend(['--%s' % opt, str(val)]) 104 125 126 # Handle proxy options without values (flags) 127 proxy_opts_flags = ['trace', 'gravitation', 'data-only', 128 'no-output-files', 'info'] 105 129 for opt in proxy_opts_flags: 106 130 val = getattr(options, opt.replace('-', '_')) … … 108 132 args.append('--%s' % opt) 109 133 134 # Add parameters last 135 args += options.params 136 110 137 # Run McStas 111 LOG.info('Running: %s' % self.binpath) 112 Process(self.binpath).run(args) 138 if not mpi: 139 LOG.info('Running: %s' % self.binpath) 140 Process(self.binpath).run(args) 141 else: 142 LOG.info('Running via MPI: %s' % self.binpath) 143 mpi_args = ['-np', str(options.mpi), self.binpath] 144 mpi_args += args 145 Process('mpirun').run(mpi_args) 113 146 114 147 115 148 def cleanup(self): 149 ''' Remove temporary files ''' 116 150 for path in (self.cpath,): 117 151 try: 118 152 os.remove(path) 119 153 except OSError: 120 pass # file not found154 pass # file not found 121 155 122 156 os.rmdir(self.dir) 123 157 124 125 if __name__ == '__main__':126 formatter = logging.Formatter('%(asctime)s - %(message)s')127 128 ch = logging.StreamHandler()129 ch.setLevel(logging.INFO)130 ch.setFormatter(formatter)131 132 LOG.setLevel(logging.DEBUG)133 LOG.addHandler(ch)134 135 m = McStas('/home/jos/diku/McCode/trunk/src/McStasTest/examples/ILL_D4.instr')136 m.prepare()137 m.run()
