def run(parameter_set_name, filenames, grp_pattern, systemsize): print 'submitting ...', parameter_set_name print dicttoinfo.dicttoinfo(getattr(parameterSetsO2, parameter_set_name)) print 'for files', filenames dirs = set() for fn in filenames: with h5py.File(fn, 'r') as f: d = myutils.walkh5(f, grp_pattern) assert len(d), 'you f****d up, pattern "%s" not found in "%s"!' % ( grp_pattern, fn) dirs = set.union(dirs, d) print 'and resolved groups therein: %s' % ','.join(dirs) o2params = getattr(parameterSetsO2, parameter_set_name) o2params['name'] = parameter_set_name if callable(o2params): o2paramsList = o2params(len(filenames)) else: o2paramsList = itertools.repeat(o2params) for (o2params, fn) in zip(o2paramsList, filenames): o2params, num_threads = prepareParametersWithNumThreads( copy.deepcopy(o2params), systemsize) qsub.submit( qsub.func(worker_on_client, fn, grp_pattern, o2params), name='job_o2_' + parameter_set_name + '_' + basename(fn), num_cpus=num_threads, days= 5, # about one day per thread, the idea being that number of threads is proportional to systems size and runtime is mem='%iMB' % (2000 * num_threads), change_cwd=True)
def run(deltax, dtscaling, filename): dim = 2 kdiff = deltax**2 # cancel out deltax**2 in the diffusion CFL condition -> max_dt_diffusion = 1/dim kstf = 3000. # make it large so the timestep restriction for the stf dominates dt_max_diff = 0.8 * 0.5 / kdiff * (deltax**2) / (dim) dt = dtscaling / (kdiff * dim * kstf) * ( deltax**3 ) # the assumed scaling with various factors. dtscaling is determined experimentally. params = dict( dt=dt, tend=dt * 20, out_intervall=dt * 10, stepper='euler', fn_out=filename, num_threads=2, kdiff=kdiff, kstf=kstf, levelset_reinit=False, ) print 'running with', dt, dt_max_diff assert dt < dt_max_diff # initial condition is a spherical region. pprint.pprint(params) size = (50, 50, 20) ld = krebsutils.LatticeDataQuad3d( (0, size[0] - 1, 0, size[1] - 1, 0, size[2] - 1), deltax) ld.SetCellCentering((True, True, False)) f_conc = lambda x, y, z: circle_distfunc(x, y, z, size[ 0] * 0.3 * deltax, (size[0] * 0.5 * deltax, size[1] * 0.5 * deltax, size[2] * 0.5 * deltax)) surfaceTensionForceTest(dicttoinfo(params), ld, dict(conc_profile=f_conc))
def run_iffsim(params): params = deepcopy(params) outfilename = params.pop('fn_out') measure_params = params.pop('ift_measure', dict()) krebsutils.set_num_threads(params.pop('num_threads', 1)) krebsutils.run_iffsim(dicttoinfo.dicttoinfo(params), str(outfilename.encode('utf-8')), Measure(outfilename, measure_params))
def run(c): c = deepcopy(c) # so it can be changed for the simulation without disturbing custom data name = c.pop('name') fn = join(dstdir, name) c['fn_out'] = fn if not os.path.isfile(name+'.h5'): mkdir(c["fn_out"]) res = os.system("ulimit -c unlimited; %s -pa '%s'" % (exe, dicttoinfo(c))) if res > 0: sys.exit(res)
def run(vessel_fn, name, paramSet, mem, days): name, c = krebsjobs.PrepareConfigurationForSubmission(vessel_fn, name, 'fakeTum', paramSet) configstr = dicttoinfo(c) config_file_name = '%s.info' % c['fn_out'] with open(config_file_name, 'w') as f: f.write(configstr) qsub.submit(qsub.func(krebs.tumors.run_faketum, config_file_name), name = 'job_'+name, mem = mem, days = days, num_cpus = c['num_threads'], change_cwd = True)
def runLevelsetRedistancingScheme(): name = 'zalesak_redistanceing' params = dict(fn_out='zalesak_redistanceing') ld = krebsutils.LatticeDataQuad3d((0, 99, 0, 99, 0, 0), 1. / 100.) ld.SetCellCentering((True, True, False)) f_distort = lambda w: (-1 if w < 0 else 1) * (abs(w)**(1. / 3.)) f_conc = lambda x, y, z: f_distort( zalesakDisk((x, y, z), 0.33, (0.5, 0.5, 0.))) # if not os.path.exists(name+'.h5'): krebsutils.set_num_threads(2) levelsetRedistancing(dicttoinfo(params), ld, dict(conc_profile=f_conc))
def run_with_vessels(vessel_fn, name, config_, mem, days): name, config_ = PrepareConfigurationForSubmission(vessel_fn, name, 'tumBulk', config_) config_ = krebs.tumors.set_lattice_size(config_, vessel_fn) sx, sy, sz = config_['lattice_size'] print('cont size: %i, %i, %i' % (sx, sy, sz)) #c = PrepConfig_new_python_dict(c) configstr = dicttoinfo(config_) qsub.submit(qsub.func(krebs.tumors.run_bulktissue_w_vessels, configstr), name='job_' + name, mem=mem, days=days, num_cpus=config_['num_threads'], change_cwd=True)
def run(vessel_fn, name, paramSet, mem, days): name, paramSet = PrepareConfigurationForSubmission(vessel_fn, name, 'fakeTumMTS', paramSet) configstr = dicttoinfo(paramSet) config_file_name = '%s.info' % paramSet['fn_out'] with open(config_file_name, 'w') as f: f.write(configstr) #o2params = getattr(parameterSetsO2, "breastv3") qsub.submit(qsub.func(krebs.tumors.run_faketum_mts, config_file_name), name = 'job_'+name, mem = mem, days = days, num_cpus = paramSet['num_threads'], change_cwd = True)
def runs_on_client(name, config): # import krebs.iffsim # krebs.iffsim.run_iffsim(config) from krebs.iff import iff_cpp import krebsutils ''' now config is asumed to be imported from a py dict ''' params = deepcopy(config) outfilename = params.pop('fn_out') measure_params = params.pop('ift_measure', dict()) krebsutils.set_num_threads(params.pop('num_threads', 1)) #krebsutils.run_iffsim(dicttoinfo.dicttoinfo(params), str(outfilename.encode('utf-8')), Measure(outfilename, measure_params)) iff_cpp.run_iffsim(dicttoinfo.dicttoinfo(params), str(outfilename.encode('utf-8')), Measure(outfilename, measure_params)) if __debug__: print('iff_cpp returned')
def generate_data(params, default_args, **kwargs): """ tensor product style sampling of source term """ arg_dict = default_args.copy() arg_dict.update(kwargs) param_string = dicttoinfo(params) args = tuple( np.atleast_1d(np.asarray(arg_dict[q])) for q in 'theta ncells nother nnecro o2'.split()) shape = tuple(len(a) for a in args) arglist = [] for ii in np.ndindex(shape): arglist.append(tuple(args[j][i] for j, i in enumerate(ii))) q = krebsutils.calcBulkTissueSourceTerm(arglist, param_string) for i, ii in enumerate(np.ndindex(shape)): q[i] /= args[1][ii[1]] return q
def worker_on_client(vessel_fn, tumor_parameters, o2_parameter_set_name, num_threads): krebsutils.set_num_threads(num_threads) tumor_fn = tumor_parameters['fn_out'] tend = tumor_parameters['tend'] pattern1 = 'out0000' pattern2 = 'out%04i' % int(round(tend / tumor_parameters['out_intervall'])) pattern = '|'.join([pattern1, pattern2]) print tumor_fn, pattern #os.system("%s -s '%s'" % (krebsjobs.submitTumOnlyVessels.exe,dicttoinfo(tumor_parameters))) os.system("%s -s '%s'" % (krebs.tumors.run_faketum, dicttoinfo(tumor_parameters))) o2_refs = detailedo2.doit( tumor_fn, pattern, (getattr(krebs.detailedo2Analysis.parameterSetsO2, o2_parameter_set_name), o2_parameter_set_name)) for ref in o2_refs: po2group = h5files.open(ref.fn)[ref.path] krebs.analyzeTissueOxygenDetailed.WriteSamplesToDisk(po2group) krebs.povrayRenderOxygenDetailed.doit(o2_refs[1].fn, o2_refs[1].path) h5files.closeall()
print('Warning: lattice_size not set properly') config_['lattice_size'] = Vec((20, 20, 20)) try: if not 'lattice_size' in config_.keys(): raise AssertionError('No lattice_size found in configuration %s' % getattr(config_)) if type(config_['lattice_size']) == str: if config_['lattice_size'] == 'set me to match the vessel domain': raise AssertionError('Find better lattice size') except Exception, e: print e.message sys.exit(-1) sx, sy, sz = config_['lattice_size'] print('cont size: %i, %i, %i' % (sx, sy, sz)) #c = PrepConfig_new_python_dict(c) configstr = dicttoinfo(config_) qsub.submit(qsub.func(krebs.tumors.run_bulktissue_no_vessels, configstr), name='job_' + name, mem=mem, days=days, num_cpus=config_['num_threads'], change_cwd=True) if not qsub.is_client and __name__ == '__main__': #parser = argparse.ArgumentParser(parents=[general_group]) #subparsers=parser.add_subparsers(dest='action') #subparsers.add_parser('Restart',parents=[general_group, second_group]) #subparsers.add_parser('Start', parents=[general_group]) import argparse parent_parser = argparse.ArgumentParser(
def runSTFTest(): params = dict( tend=0.01, out_intervall=0.0001, stepper='impeuler', fn_out='sfttest', num_threads=2, kdiff=10., kstf=0.5, ) ld = krebsutils.LatticeDataQuad3d((0, 49, 0, 49, 0, 0), 1. / 50.) ld.SetCellCentering((True, True, False)) extent = ld.worldBox[:4] #f_conc = lambda x,y,z: zalesakDisk((x,y,z), 0.3, (0.5,0.5,0.)) f_conc = lambda x, y, z: circle_distfunc(x, y, z, 0.3, (0.5, 0.5, 0.)) if not os.path.isfile('sfttest.h5'): surfaceTensionForceTest(dicttoinfo(params), ld, dict(conc_profile=f_conc)) rc = matplotlib.rc rc('font', size=8.) rc('axes', titlesize=10., labelsize=10.) rc('figure', **{'subplot.wspace': 0.15, 'subplot.hspace': 0.15}) rc('savefig', facecolor='white') dpi = 100. results = Results('sfttest') ldface = (krebsutils.read_lattice_data_from_hdf(results.f['face_ld_0']), krebsutils.read_lattice_data_from_hdf(results.f['face_ld_1'])) extface = tuple(q.worldBox[:4] for q in ldface) data = collections.defaultdict(list) for idx, group in enumerate(results): def show(a, ext): pyplot.imshow(np.asarray(a[..., 0]).transpose(), extent=ext, origin='bottom', interpolation='nearest') def contour(): return pyplot.contour(np.asarray(group['ls'][..., 0]).transpose(), levels=[0.], extent=extent, origin='lower') print "plotting %i" % idx if 1: pyplot.figure(figsize=(1200. / dpi, 1000. / dpi)) # pyplot.subplot(221) # show(group['ls'], extent) # pyplot.colorbar() # contour() # pyplot.subplot(222) # show(group['rho'], extent) # pyplot.colorbar() # contour() pyplot.subplot(221) pyplot.plot(np.linspace(extent[0], extent[1], 50), group['ls'][:, 25, 0]) pyplot.gca().set(xlabel='x', ylabel=r'$\phi$') pyplot.gca().grid(color=(0.5, ) * 3) pyplot.subplot(222) pyplot.plot(np.linspace(extent[0], extent[1], 50), group['rho'][:, 25, 0]) pyplot.gca().set(xlabel='x', ylabel=r'$\rho$') pyplot.gca().grid(color=(0.5, ) * 3) pyplot.subplot(223) show(group['force_0'], extface[0]) pyplot.colorbar() contour() pyplot.subplot(224) show(group['force_1'], extface[1]) pyplot.colorbar() contour() pyplot.savefig("sfttest%04i.png" % idx, dpi=dpi) data['time'].append(group.attrs['time']) data['mass'].append(group.attrs['mass']) data['area'].append(group.attrs['area']) pyplot.figure(figsize=(1200. / dpi, 600. / dpi)) pyplot.subplot(121) pyplot.plot(data['time'], data['mass'], marker='x') pyplot.gca().set(xlabel='t', ylabel='mass') pyplot.subplot(122) pyplot.plot(data['time'], data['area'], marker='+') pyplot.gca().set(xlabel='t', ylabel='area') pyplot.savefig('vstime.png')
def convectionDiffusionReaction(name, params, ld, **kwargs): params['fn_out'] = name libkrebs.convectionDiffusionReaction(dicttoinfo(params), ld, kwargs)
def runLevelsetAdvectionScheme(): gradient_approx_method = 'upwind' stepper = 'rk3' advection_method = "weno5" velocity_type = 'shear' #'rotation' name = 'zalesak_%s_%s_%s_%s' % (velocity_type, gradient_approx_method, stepper, advection_method) params = dict( tend=math.pi * 2., out_intervall=math.pi * 2. / 12, stepper=stepper, fn_out=name, advection_method=advection_method, gradient_approx_method=gradient_approx_method, num_threads=3, ) ld = krebsutils.LatticeDataQuad3d((0, 99, 0, 99, 0, 0), 1. / 100.) ld.SetCellCentering((True, True, False)) f_conc = lambda x, y, z: zalesakDisk((x, y, z), 1.) if velocity_type == 'shear': f_vel = lambda x, y, z: rotationalVelocityField((x, y, z), (0, 0, .1), (0.5, 0.5, 0.), True) else: f_vel = lambda x, y, z: rotationalVelocityField((x, y, z), (0, 0, 1), (0.5, 0.5, 0.), False) convectionLevelset(dicttoinfo(params), ld, dict(conc_profile=f_conc, velocity_profile=f_vel)) def levelsetshow(a): #t = np.max(np.abs(a)) pyplot.imshow(np.asarray(a), origin='bottom', cmap=cm_ls, vmin=-0.1, vmax=0.1, extent=ld.worldBox[:4]) rc = matplotlib.rc rc('font', size=8.) rc('axes', titlesize=10., labelsize=10.) rc('pdf', compression=6, fonttype=42) rc('figure', **{'subplot.wspace': 0.2, 'subplot.hspace': 0.25}) results = Results(name) pyplot.suptitle(name) pyplot.subplot(221, xlabel='x', ylabel='y', title='t=0') levelsetshow(results[0]['ls'][:, :, 0]) pyplot.colorbar() pyplot.subplot(222, title=r't=2$\pi$') levelsetshow(results[-1]['ls'][:, :, 0]) pyplot.colorbar() # t, m = zip(*[ (g.attrs['time'], g.attrs['mass']) for g in results ]) # pyplot.subplot(223, title='mass loss', xlabel='t', ylabel='m', ylim=(0, max(m)*1.1)) # pyplot.plot(t, m, 'x-k') t, gnorm = results.f['gradientmeasure/time'], results.f[ 'gradientmeasure/gradnorm'] pyplot.subplot(223, title=r'$||\nabla d| -1|_\inf$', xlabel='t', ylabel='', xlim=(-0.1 * params['tend'], params['tend'] * 1.1)) pyplot.plot(t, gnorm, '-k') pyplot.subplot(224, title=r'contours t=0 .. 2$\pi$', xlabel='x', ylabel='y') for i, g in enumerate(results[::2]): col = 'k' if i == 0 else 'r' pyplot.contour(np.asarray(g['ls'][:, :, 0]), [0.], extent=ld.worldBox[:4], colors=col) pyplot.savefig(name + '.pdf', dpi=180., papertype='a4') pyplot.show()
def compute1dAdvectionModel(name, params, conc_func, velocity_func): params['fn_out'] = name f_conc = lambda x, y, z: conc_func(x) f_vel = lambda x, y, z: (velocity_func(x), 0, 0) convection(dicttoinfo(params), make_ld(params), dict(conc_profile=f_conc, velocity_profile=f_vel))