def recalc_case(case, newTable): cod_miller.replace_table(newTable) args = ['--power=?'] + case params, state, help = ballistics.parse_arguments(args) newstate, points = ballistics.find_unknown( state, params['unknown'], params.get('unknown_scan')) return newstate['power_factor']
def calculate_case(hashval, args, info, verbose): """ Process an individual case. Enter: hashval: hash value used to memoize results. args: arguments formulated for the ballistics routines. info: info that was used to construct the arguments. verbose: verbosity for the ballistics program Exit: hashval: the input hash value. state: final state from the ballistics routines. points: time series of trajectory. """ if verbose >= 3: pprint.pprint(info) if verbose >= 3: print(hashval) ballistics.Verbose = max(0, verbose - 2) params, state, help = ballistics.parse_arguments( args, allowUnknownParams=True) ballistics.Verbose = max(0, verbose - 2) if verbose >= 4: pprint.pprint(state) starttime = ballistics.get_cpu_time() newstate, points = ballistics.find_unknown(state, params['unknown'], params.get('unknown_scan')) newstate['computation_time'] = ballistics.get_cpu_time()-starttime for key, technique in [ ('power_factor', 'given'), ('initial_velocity', 'given_velocity'), ('pendulum_length', 'pendulum'), ('chamber_volume', 'pressure'), ('final_velocity', 'chronograph'), ('rising_height', 'trajectory'), ('range', 'range'), ('final_time', 'time'), ('max_height', 'height'), ('final_angle', 'final_angle'), ]: if newstate.get('technique') is None and key in state: newstate['technique'] = technique if verbose >= 3: pprint.pprint(newstate) if len(points) > 0: subset = 1 if len(points) < 50 else ( 2 if len(points) < 100 else (5 if len(points) < 250 else 10)) points = points[:-1:subset] + points[-1:] points = {key: FloatList([ point.get(key) for point in points], '%.6g') for key in points[0]} else: points = None if verbose >= 2: if newstate.get('power_factor') is None: print('%s --> FAILED' % (hashval, )) else: print('%s --> %3.1f' % (hashval, newstate.get('power_factor'))) return hashval, newstate, points
def testFindUnknown(): state = { 'final_height': 0, 'initial_angle': 45.0, 'charge': 0.0311034768, 'mass': 11.070488780312502, 'material': 'brass', 'range': 229.13035200000002, 'time_delta': 0.05, } unknown = 'power_factor' result, _points = ballistics.find_unknown(state, unknown) assert 414000 < result['power_factor'] < 416000
def testCombinations(): testDir = os.path.dirname(os.path.realpath(__file__)) combinations = json.load(open(os.path.join(testDir, 'combinations.json'))) for entry in combinations: args = ['--%s=%s' % (k, v if v is not None else '') for k, v in entry['conditions'].items()] args.append('--power=?') args.append('--time_delta=%g' % max(0.0001, min( 0.2, entry['time'] * 0.049 if entry['time'] else 100))) params, state, _ = ballistics.parse_arguments(args) print(args) result, _ = ballistics.find_unknown(state, params['unknown'], params.get('unknown_scan')) assert 0.99 < result['power_factor'] / entry['power_factor'] < 1.01
def recalc_groups(groups, newTable): cod_miller.replace_table(newTable) for groupkey in sorted(groups): entry = groups[groupkey] for idx, case in enumerate(entry['cases']): args = ['--power=?'] + case params, state, help = ballistics.parse_arguments(args) newstate, points = ballistics.find_unknown( state, params['unknown'], params.get('unknown_scan')) entry['pf'][idx] = newstate['power_factor'] sys.stdout.write('%s %d %3.1f %3.1f %3.1f \r' % ( entry['key'], idx, newstate['power_factor'], entry['entries'][idx]['power_factor'], newstate['power_factor'] - entry['entries'][idx]['power_factor'])) sys.stdout.flush()
def testCombinations(): testDir = os.path.dirname(os.path.realpath(__file__)) combinations = json.load(open(os.path.join(testDir, 'combinations.json'))) for entry in combinations: args = [ '--%s=%s' % (k, v if v is not None else '') for k, v in entry['conditions'].items() ] args.append('--power=?') args.append('--time_delta=%g' % max( 0.0001, min(0.2, entry['time'] * 0.049 if entry['time'] else 100))) params, state, _ = ballistics.parse_arguments(args) print(args) result, _ = ballistics.find_unknown(state, params['unknown'], params.get('unknown_scan')) assert 0.99 < result['power_factor'] / entry['power_factor'] < 1.01
def recalc_groups(groups, newTable): cod_miller.replace_table(newTable) for groupkey in sorted(groups): entry = groups[groupkey] for idx, case in enumerate(entry['cases']): args = ['--power=?'] + case params, state, help = ballistics.parse_arguments(args) newstate, points = ballistics.find_unknown( state, params['unknown'], params.get('unknown_scan')) entry['pf'][idx] = newstate['power_factor'] sys.stdout.write('%s %d %3.1f %3.1f %3.1f \r' % (entry['key'], idx, newstate['power_factor'], entry['entries'][idx]['power_factor'], newstate['power_factor'] - entry['entries'][idx]['power_factor'])) sys.stdout.flush()
def calc_error(groups): sum2err = 0 for entry in groups.values(): avg = sum(val for val in entry['pf']) / len(entry['pf']) err2 = sum(math.log10(val / avg)**2 for val in entry['pf']) / len(entry['pf']) sum2err += err2 err = 10**((sum2err / len(groups))**0.5) - 1 return err def recalc_case(case, newTable): cod_miller.replace_table(newTable) args = ['--power=?'] + case params, state, help = ballistics.parse_arguments(args) newstate, points = ballistics.find_unknown(state, params['unknown'], params.get('unknown_scan')) return newstate['power_factor'] def recalc_groups_pool(groups, newTable): with concurrent.futures.ProcessPoolExecutor() as executor: priorityLevel = (psutil.BELOW_NORMAL_PRIORITY_CLASS if sys.platform == 'win32' else 10) parent = psutil.Process() parent.nice(priorityLevel) for child in parent.children(): child.nice(priorityLevel) futures = [] for groupkey in sorted(groups): entry = groups[groupkey]