def manual_genboundary(bspec,**kw): kwp = sd(kw,**bspec); getkw = mk_getkw(kwp,outletdefaults,prefer_passed = True); btype = getkw("type"); lims = getkw('lim'); di = dict(); for dim,lim in zip(all_lims,lims): di[dim] = lim; if btype == 'outlet': model = getkw('model'); di['label']= getkw('label'); di['phase_velocity'] = getkw('phase_velocity'); if model == 'none': ret = outlet_none_tmpl.format(**di); elif model == 'potential': di = sd(manbounds_defs,**di); if not test(di,'connection_rank'): di['connection_rank'] =''; else: di['connection_rank'] = 'connection_rank {}'.format( getkw('connection_rank')); if not test(kwp,'voltage_measurement'): di['voltage_measurement'] = ''; else: raise NotImplementedError("haven't got to this yet..."); di['circuit'] = di['circuit']; ret = outlet_pot_tmpl.format(**di); elif model == 'laser': raise NotImplementedError("need to implement the laser..."); else: raise ValueError("unknown outlet model {}".format(model)); else: raise NotImplementedError("yeah..."); return ret;
def mksim(pbsbase,**d): print("making {}".format(pbsbase)); myd = sd(lsp_d, **d); lsp=genlsp(**myd); #two color hack if test(myd, 'relative_phase'): relative_phases="phase {}".format(myd['relative_phase']) else: relative_phases='' lasers = ''' laser wavelength 780e-7 spotsize 2.17e-4 laser amplitude 0.36 wavelength 390e-7 {phase} end '''.format(phase=relative_phases); if test(myd, 'two_colors'): lsp = re.sub(r'type *19.*','type 85',lsp); lsp = re.sub(r' *; *\\lambda *spotsize','',lsp); lsp = re.sub('.*coefficients 7.80*e-03.*$', lasers,lsp,flags=re.MULTILINE); pbs=genpbs(pbsbase=pbsbase,domains=myd['domains']); pbs = re.sub("../scripts/autozipper","../../scripts/autozipper",pbs); pbs = re.sub("lsp-10-xy","lsp-10-xy-multilaser",pbs); output(lsp,pbs,pbsbase, dats=["sine700points.dat", myd['targetdat']], dir=pbsbase);
def genregions(**kw): if test(kw,'region_split'): regs = genregions_uniform([kw['region_split']],**kw); elif test(kw, 'region_splits'): regs = genregions_uniform(kw['region_splits'],**kw); else: regs = genregions_uniform([],**kw); return mkregion_str(regs);
def gendens(**kw): getkw = mk_getkw(kw, densdefaults); speciesl = getkw('speciesl'); outputfmt = "n_{}"; if test(kw,'target_density'): Q = kw['target_density']; fracs = getkw('fracs'); if test(kw, 'target_density_plainconst'): ret = { outputfmt.format(species) : plainconst_tmpl.format(data=Q*f) for species,f in zip(speciesl,fracs) }; kw.update(ret); return kw; if type(Q) == tuple: #the reason for tuple and not a general iterable #is when we pass a single scale, for example, which #we scale by fracs pass; else: if hasattr(Q[0], '__call__'): Q = [ lambda x: frac*iQf(x) for frac,iQf in zip(fracs,Q) ]; else: Q = [ frac*iQ for frac,iQ in zip(fracs,Q) ]; if hasattr(Q[0],'__call__'): #invariant: txmin,txmax are in cm. x = np.linspace(kw['txmin'][0],kw['txmax'][1],20); Q[:] = [iQ(x) for iQ in Q]; ret = { outputfmt.format(species) : densitypairs_tmpl.format(data = iQ) for species,iQ in zip(speciesl,Q) }; kw.update(ret) return kw; else: kw['dens_dat'] = getkw('dens_dat'); kw['dens_imul'] = getkw('dens_imul'); kw['dens_type'] = getkw('dens_type'); kw['fracs'] = getkw('fracs'); def copy_kw(l): if type(kw[l]) != tuple: kw[l] = (kw[l],)*len(speciesl) copy_kw('dens_dat'); copy_kw('dens_imul'); copy_kw('dens_type'); for i,species in enumerate(speciesl): kw['n_'+species] = densityfile_tmpl.format( targetdat = kw['dens_dat'][i], type = kw['dens_type'][i], imul = kw['dens_imul'][i], dmul = kw['fracs'][i]); return kw; pass;
def genregions(**kw): def getkw(l, scale=None): if test(kw, l): ret = kw[l] else: ret = region_defaults[l] if scale: return [scale * i for i in ret] return ret regsplit_dir, subdivs = getkw('region_split') nonsplits = [x for x in ['x', 'y', 'z'] if x != regsplit_dir] limkw = [x + lims for x in ['x', 'y', 'z'] for lims in ['min', 'max']] lims = {k: lim for k, lim in zip(limkw, getkw('lim', scale=1e-4))} total_doms = getkw('domains') doms = [total_doms // subdivs for i in range(subdivs)] doms[-1] += total_doms % subdivs lmn, lmx = regsplit_dir + 'min', regsplit_dir + 'max' mn, mx = lims[lmn], lims[lmx] edges = [mn + i * (mx - mn) / subdivs for i in range(subdivs)] + [mx] split_cells = getkw(getkw("region_dom_split") + "cells") mins = edges[:-1] maxs = edges[1:] if test(kw, "xcells") and test(kw, "ycells") and test(kw, "zcells"): zcells_per_region = [kw['zcells'] // subdivs for i in range(subdivs)] zcells_per_region[-1] += kw['zcells'] % subdivs cellses = [ kw['xcells'] * kw['ycells'] * zc for i, zc in enumerate(zcells_per_region) ] else: cellses = [None for i in range(subdivs)] reg = sd(lims, split=getkw("region_dom_split").upper() + "SPLIT") regions = [ sd(reg, **{ lmn: mn, lmx: mx, 'i': i + 1, 'domains': di, 'cells': cells }) for i, (mn, mx, di, cells) in enumerate(zip(mins, maxs, doms, cellses)) ] return mkregion_str(regions, split_cells=split_cells)
def gentemp(**kw): getkw = mk_getkw(kw, tempdefaults) speciesl = getkw('speciesl') otherfuncs = '' funcnum = getkw('funcnum') if test(kw, 'target_temps'): Q = kw['target_temps'] def process_temp(iq): if not iq: return None if hasattr(iq, '__call__'): xres = getkw('dat_xres') x = np.linspace(kw['txmin'][0], kw['txmax'][1], xres) iq = iq(x) raise ValueError("Not implemented yet!") elif type(iq) is dict: _getkw = mk_getkw(iq, species_tempdefault) ret = densityfile_tmpl.format(targetdat=_getkw('dat'), type=_getkw('type'), imul=_getkw('imul'), dmul=_getkw('frac')) return ret ss = [process_temp(iq) for iq in Q] else: Q = [dict()] * len(speciesl) ss = [None] * len(speciesl) for iq, species, s, e in zip(Q, speciesl, ss, getkw('thermal_energy')): cur = 'thermal_energy {}\n'.format(e) if s: otherfuncs += "function{}\n".format(funcnum) otherfuncs += s # if 'energy_flags' in iq: # energyflags = iq['energy_flags']; # else: # energyflags = getkw('energy_flags'); cur += 'spatial_function {}\n'.format(funcnum) # cur += 'energy_flags {}\n'.format( # joinspace([ # 1 if i else 0 # for i in getkw("dens_flags")])); funcnum += 1 kw['{}_thermalopts'.format(species)] = cur if otherfuncs != '': if not test(kw, 'other_funcs'): kw['other_funcs'] = '' kw['other_funcs'] += otherfuncs return kw
def gen_mov(**kw): getkw = mk_getkw(kw,mov_defaults); spec = dict(); if test(kw,'sclrq_path'): spec['sclrq_path'] = 'export PATH="{}:$PATH"\n'.format( sclrq_path); else: spec['sclrq_path'] =''; if getkw('cluster') == 'ramses': spec['sclrq_path']+='source ~/.bashrc\n'; if not test(kw,'plotI'): spec['plotI']=getkw('I')/10; if test(kw,'condafile'): spec['conda'] = kw['condafile']; else: spec['conda'] = clusters[getkw('cluster')]['condafile'] spec.update(dict( pbsbase=getkw('pbsbase'), lims=kw['clim'], n_c=getkw('n_c'))); if getkw('type')=='ni' or getkw('type')=='dq': Q,q = zip(*getkw(getkw('type')+'_species')); Q = ",".join(str(i) for i in Q); Q = "("+Q+")"; spec.update(dict( linthresh=getkw("linthresh"), quantity=Q, weights=str(q), averaging=getkw("averaging"), cmap=( getkw("cmap") if getkw('type') == 'ni' else getkw("dq_cmap")), type=getkw('type'), title="Ion Density" if getkw('type') == 'ni' else 'Charge Density', )); kw['movtmpl'] = 'movni_tmpl' elif getkw('type') == 'ne': spec['quantity'] = getkw('ne_species'); elif getkw('type') == 'rho': spec['quantity'] = getkw('ne_species'); spec['cmap'] = getkw('dq_cmap'); spec['linthresh'] = getkw('linthresh'); if test(kw, 'movtmpl'): fname = kw['movtmpl']; else: fname = 'mov{}_tmpl'.format(getkw('type')); with open(fname) as f: s=f.read(); return s.format(**spec);
def getkw(l,scale=None): if test(kw, l): if scale: return [scale*i for i in kw[l]]; return kw[l]; else: return defaults[l];
def gentemp(**kw): getkw = mk_getkw(kw, tempdefaults); speciesl = getkw('speciesl'); otherfuncs = ''; funcnum = getkw('funcnum'); if test(kw,'target_temps'): Q = kw['target_temps']; def process_temp(iq): if not iq: return None; if hasattr(iq, '__call__'): xres = getkw('dat_xres') x = np.linspace(kw['txmin'][0],kw['txmax'][1],xres); iq = iq(x); raise ValueError("Not implemented yet!"); elif type(iq) is dict: _getkw = mk_getkw(iq, species_tempdefault); ret = densityfile_tmpl.format( targetdat = _getkw('dat'), type = _getkw('type'), imul = _getkw('imul'), dmul = _getkw('frac')); return ret; ss = [ process_temp(iq) for iq in Q ]; else: Q = [dict()]*len(speciesl); ss= [None] *len(speciesl); for iq,species,s,e in zip(Q,speciesl,ss,getkw('thermal_energy')): cur = 'thermal_energy {}\n'.format(e); if s: otherfuncs += "function{}\n".format(funcnum); otherfuncs += s; # if 'energy_flags' in iq: # energyflags = iq['energy_flags']; # else: # energyflags = getkw('energy_flags'); cur += 'spatial_function {}\n'.format(funcnum); # cur += 'energy_flags {}\n'.format( # joinspace([ # 1 if i else 0 # for i in getkw("dens_flags")])); funcnum += 1; kw['{}_thermalopts'.format(species)] = cur; if otherfuncs != '': if not test(kw, 'other_funcs'): kw['other_funcs'] = '' kw['other_funcs'] += otherfuncs; return kw;
def gengrids(**kw): getkw = mk_getkw(kw,grid_defs,prefer_passed = True); outgrids = ['','','']; simple_gridtmpl = ''' {dim}min {min:e} {dim}max {max:e} {dim}-cells {cells}'''; vargrid_tmpl = ''' {dim}min {min:e} {dim}max {max:e} {dim}-cells {cells} {dim}-intervals d{dim}-start {dxstart:e} {intervals} end''' vargrid_intv_tmpl = " length {length:e} for {N}"; grids = [] for dim in 'xyz': if getkw('{}cells'.format(dim)) == 0: grids.append(""); continue; vgridlabel = '{}vargrid'.format(dim); if test(kw,vgridlabel): intv = '\n'.join([ vargrid_intv_tmpl.format(length=l, N=N) for l,N in getkw(vgridlabel)]); if test(kw, 'd{}start'.format(dim)): ddimstart = getkw('d{}start'.format(dim)); else: l = getkw(vgridlabel)[0][0]; N = getkw(vgridlabel)[0][1]; ddimstart = l/N; grid = vargrid_tmpl.format( dim=dim, min=getkw('{}min'.format(dim)), max=getkw('{}max'.format(dim)), cells=getkw('{}cells'.format(dim)), dxstart=ddimstart, intervals=intv) else: grid= simple_gridtmpl.format( dim=dim, min=getkw('{}min'.format(dim)), max=getkw('{}max'.format(dim)), cells=getkw('{}cells'.format(dim))); grids.append(grid); return grids;
def gen_mov(**kw): getkw = mk_getkw(kw, mov_defaults) spec = dict() if test(kw, 'sclrq_path'): spec['sclrq_path'] = 'export PATH="{}:$PATH"\n'.format(sclrq_path) else: spec['sclrq_path'] = '' if getkw('cluster') == 'ramses': spec['sclrq_path'] += 'source ~/.bashrc\n' if not test(kw, 'plotI'): spec['plotI'] = getkw('I') / 10 if test(kw, 'condafile'): spec['conda'] = kw['condafile'] else: spec['conda'] = clusters[getkw('cluster')]['condafile'] spec.update( dict(pbsbase=getkw('pbsbase'), lims=kw['clim'], n_c=getkw('n_c'))) if getkw('type') == 'ni' or getkw('type') == 'dq': Q, q = zip(*getkw(getkw('type') + '_species')) Q = ",".join(str(i) for i in Q) Q = "(" + Q + ")" spec.update( dict( linthresh=getkw("linthresh"), quantity=Q, weights=str(q), averaging=getkw("averaging"), cmap=(getkw("cmap") if getkw('type') == 'ni' else getkw("dq_cmap")), type=getkw('type'), title="Ion Density" if getkw('type') == 'ni' else 'Charge Density', )) kw['movtmpl'] = 'movni_tmpl' elif getkw('type') == 'ne': spec['quantity'] = getkw('ne_species') elif getkw('type') == 'rho': spec['quantity'] = getkw('rho_species') spec['cmap'] = getkw('dq_cmap') spec['linthresh'] = getkw('linthresh') if test(kw, 'movtmpl'): fname = kw['movtmpl'] else: fname = 'mov{}_tmpl'.format(getkw('type')) with open(fname) as f: s = f.read() return s.format(**spec)
def gendens(**kw): densityfile_tmpl = ''' type {type} data_file {targetdat} independent_variable_multiplier {imul} dependent_variable_multiplier {dmul} ''' densitypairs_tmpl = ''' type 0 data_pairs {data} end ''' getkw = mk_getkw(kw, densdefaults); speciesl = getkw('speciesl'); if test(kw,'target_density'): dens=kw['target_density']; if type(dens) == tuple: #the reason for tuple and not a general iterable #is when we pass a single scale, for example, which #we scale by fracs pass; else: if hasattr(dens, '__call__'): dens[:] = [ lambda x: frac*idensf(x) for frac,idensf in zip(fracs,dens) ]; else: dens[:] = [ frac*idens for frac,idens in zip(fracs,dens) ]; if hasattr(n_e[0],'__call__'): #invariant: txmin,txmax are in cm. x = np.linspace(kw['txmin'][0],kw['txmax'][1],20); dens[:] = [idens(x) for idens in dens]; for species,idens in zip(speciesl,dens): kw['n_' + species] = densitypairs_tmpl.format( data = idens) else: kw['dens_dat'] = getkw('dens_dat'); kw['dens_imul'] = getkw('dens_imul'); kw['dens_type'] = getkw('dens_type'); kw['fracs'] = getkw('fracs'); def copy_kw(l): if type(kw[l]) != tuple: kw[l] = (kw[l],)*len(speciesl) copy_kw('dens_dat'); copy_kw('dens_imul'); copy_kw('dens_type'); for i,species in enumerate(speciesl): kw['n_'+species] = densityfile_tmpl.format( targetdat = kw['dens_dat'][i], type = kw['dens_type'][i], imul = kw['dens_imul'][i], dmul = kw['fracs'][i]); pass; return kw;
def getkw(l, scale=None): if test(kw, l): ret = kw[l] else: ret = pext_defaults[l] if scale: return [scale * i for i in ret] return ret
def getkw(l,scale=None): if test(kw, l): ret = kw[l] else: ret = lspdefaults[l]; if scale: return [scale*i for i in ret]; return ret;
def getkw(l,scale=None): if test(kw, l): ret = kw[l] else: ret = pext_defaults[l]; if scale: return [scale*i for i in ret]; return ret;
def getkw(l, scale=None): if test(kw, l): ret = kw[l] else: ret = lspdefaults[l] if scale: return [scale * i for i in ret] return ret
def format_region(region): if not test(region, "cells"): region['cells'] = "" else: region['cells'] = "cells = {}".format(region['cells']) if float(split_cells) / float(region['domains']) < 4: print("warning: {} limits less than 4 cells thick".format( region['split'][0])) return region_tmpl.format(**region)
def setspeciesv(fmt,defaultl,join=False,scale=False): l = fmt.format(species); if test(kw,l): v = kw[l]; if scale: v=mt(v,getkw('ux')); if join: v=joinspace(v); else: v = fmtd[defaultl]; fmtd[l] = v;
def format_region(region): if not test(region,"cells"): region['cells'] = "" else: region['cells'] = "cells = {}".format(region['cells']) if split_cells is not None and split_cells/region['domains'] < 4: print("warning: {} limits less than 4 cells thick".format( region['split'][0])); return region_tmpl.format(**region);
def genoptions(**kw): if test(kw,"options"): kw = sd(kw,kw['options']); #quirks getkw_outer = mk_getkw(kw,lspdefaults); kw['maximum_restart_dump_time'] = getkw_outer('restart') def genopt(k,getkw=getkw_outer,flag=False): if not flag: flag = k; i = getkw(k); if re.search("_ns$",flag): if isvalue(i): i*=1e9; else: scaletuple(i,1e9); if i is None or i is (): return ""; elif i is True: return "{} ON\n".format(flag); elif i is False: return "{} OFF\n".format(flag); elif isvalue(i): return "{} {}\n".format(flag,i); else: return "{} {} end\n".format(flag,joinspace(i)); def genopts(opts): title,opts = opts; getkw = mk_getkw(kw,opts); tmpl=";;{}\n{}\n" all_opts = "".join([ genopt(k,getkw) for k in sorted(opts.keys()) ]); return tmpl.format(title,all_opts); out=''.join([ genopts(iopts) for iopts in [restart_opts, balance_opts,] ]); out+=options_dont_touch out+=''.join([ genopts(iopts) for iopts in [kinematic_opts,dump_opts,] ]); #now we do big dumps kw['interval_ns'] = getkw_outer("dumpinterval"); kw['times_ns'] = getkw_outer("dumptimes"); kw['steps'] = getkw_outer("dumpsteps"); def gen_dumpopt(dump,opt): label = "{}_dump_{}".format(dump,opt); if test(kw, label): return genopt(label); else: return genopt(opt,flag=label); for dump in bigdumps: if not getkw_outer("dump_{}".format(dump)) : continue; out += "dump_{}s_flag ON\n".format(dump); for iopt in ['interval_ns','times_ns','steps']: out += gen_dumpopt(dump,iopt); return out;
def genlsp(**kw): def getkw(l,scale=None): if test(kw, l): if scale: return [scale*i for i in kw[l]]; return kw[l]; else: return defaults[l]; E0 = np.sqrt(2*getkw('I')*1e4/(c*e0))*1e-5 xmin,xmax, ymin,ymax = getkw('lim',scale=1e-4) fp = joinspace(getkw("fp")); components = joinspace(getkw("components")); phases = joinspace(getkw("phases")); l = getkw('l')*100.0 if test(kw,'resd'): xres,yres = getkw("resd"); xcells = (xmax-xmin)/(l/xres); ycells = (ymax-ymin)/(l/yres); else: xcells, ycells = getkw("res"); w0 = getkw('w')*100.0; T = getkw('T')*1e9; targ_xmin,targ_xmax, targ_ymin,targ_ymax =getkw('tlim',scale=1e-4); domains=getkw('domains'); # we have that na~l/(pi*w), and the f-number~1/2na, thus # f-number ~ pi*w/2l fnum=np.pi*w0/2/l; totalt=getkw('totaltime')*1e9 timestep=getkw('timestep')*1e9; couraunt = min( ((xmax-xmin)/xcells/c)*1e9, ((ymax-ymin)/ycells/c)*1e9) if timestep > couraunt: import sys sys.stderr.write("warning: timestep exceeds couraunt limit\n"); targetdat = getkw('targetdat'); dumpinterval=getkw('dumpinterval')*1e9; description=getkw('description'); with open("hotwater2d_tmpl.lsp") as f: s=f.read(); s=s.format( xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax, xcells=xcells,ycells=ycells, l=l,w0=w0,E0=E0, fnum=fnum, targ_xmin=targ_xmin, targ_xmax=targ_xmax, targ_ymin=targ_ymin, targ_ymax=targ_ymax, fp=fp,pulse=T,components=components,phases=phases, intensity=getkw('I'), domains=domains,totalt=totalt, timestep=timestep, targetdat=targetdat, dumpinterval=dumpinterval, description=description ); return s;
def mkscale_sim(**d): datf = "water-{}.dat".format(d["dat"]) d = sd(scale_sim, **d) d["targetdat"] = datf d["description"] = "scale length sim with {}".format(d["dat"]) if not test(d, "name"): name = "longl_l={}_{}".format(d["dat"], d["I"]) else: name = d["name"] mksim(name, **d)
def mkscale_sim(**d): datf = "water-{}.dat".format(d['dat']) d = sd(scale_sim, **d) d['targetdat'] = datf d['description'] = "scale length sim with {}".format(d['dat']) if not test(d, "name"): name = "longl_l={}_{}".format(d['dat'], d['I']) else: name = d['name'] mksim(name, **d)
def genregions(**kw): def getkw(l,scale=None): if test(kw, l): ret = kw[l] else: ret = region_defaults[l]; if scale: return [scale*i for i in ret]; return ret; regsplit_dir, subdivs = getkw('region_split'); nonsplits = [x for x in ['x','y','z'] if x != regsplit_dir ]; limkw = [x+lims for x in ['x','y','z'] for lims in ['min','max']]; lims = {k:lim for k,lim in zip(limkw,getkw('lim',scale=1e-4))}; total_doms = getkw('domains'); doms = [total_doms//subdivs for i in range(subdivs)]; doms[-1] += total_doms % subdivs; lmn,lmx = regsplit_dir+'min', regsplit_dir+'max'; mn,mx = lims[lmn],lims[lmx] edges = [mn+i*(mx-mn)/subdivs for i in range(subdivs)] + [mx]; split_cells = getkw(getkw("region_dom_split")+"cells"); mins = edges[:-1]; maxs = edges[1:]; if test(kw,"xcells") and test(kw,"ycells") and test(kw,"zcells"): zcells_per_region = [kw['zcells']//subdivs for i in range(subdivs)] zcells_per_region[-1] += kw['zcells'] % subdivs; cellses = [ kw['xcells']*kw['ycells']*zc for i,zc in enumerate(zcells_per_region)]; else: cellses = [None for i in range(subdivs)]; reg = sd(lims,split=getkw("region_dom_split").upper()+"SPLIT") regions = [ sd(reg,**{lmn:mn,lmx:mx,'i':i+1,'domains':di,'cells':cells}) for i,(mn,mx,di,cells) in enumerate(zip(mins,maxs,doms,cellses)) ]; return mkregion_str(regions, split_cells=split_cells);
def genregions_uniform(subdivs,**kw): ''' genregions in a uniform fashion subdivs is a list of tuples, each of which specify a range to divide along an axis. There are two options, the first is (d, split) in which d is a string representing the dimension, and split is the number of subdivions along that axis. The section option is (d, split, dmin, dmax) or (d, split, (dmin, dmax)) where dmin is the minimum along the d axis and dmax is the max. The subdivisions are cartesian multiplied (or cartesian product). ''' getkw = mk_getkw(kw, unireg_defaults); lims = mt(getkw('lim'),getkw('ux')); dims = 'xyz'; out={} for subdiv in subdivs: if len(subdiv) == 3: subdiv = subdiv[:2] + subdiv[2]; elif len(subdiv) == 2: i=dims.index(subdiv[0]) subdiv = subdiv + tuple(lims[i*2:i*2+2]) ax, split, mn, mx = subdiv; if ax not in out: out[ax]=[]; out[ax].extend(list( np.linspace(mn,mx,split+1))); for i,dim in enumerate(dims): if dim not in out: out[dim] =lims[i*2:i*2+2]; regs = [ dict(xmin=xmin,xmax=xmax, ymin=ymin,ymax=ymax, zmin=zmin,zmax=zmax,) for xmin,xmax in zip(out['x'],out['x'][1:]) for ymin,ymax in zip(out['y'],out['y'][1:]) for zmin,zmax in zip(out['z'],out['z'][1:]) ]; for i,reg in enumerate(regs): reg['i'] = i+1; if test(kw,"domains_per_region"): for reg in regs: reg['domains'] = getkw("domains_per_region"); else: ndom = getkw("domains")//len(regs); ex = getkw("domains") %len(regs); for reg in regs: reg['domains'] = ndom; regs[-1]['domains'] += ex; for reg in regs: reg['split'] = "{}SPLIT".format( getkw("region_dom_split").upper()); return regs;
def genconductor_boundaries(**kw): getkw=mk_getkw(kw, condb_defaults); conductorss=''; for I,conductor in enumerate(getkw('conductors')): cd = sd(condb_objdef, **conductor); if test(cd,'from') and test(cd,'to'): cd['xmin'],cd['ymin'],cd['zmin'] = cd['from'] cd['xmax'],cd['ymax'],cd['zmax'] = cd['to'] pass; else: outlet = cd['outlet']; if outlet not in all_lims: raise ValueError('Unknown outlet "{}"'.format(outlet)); coords = outlet_coords(outlet,kw); cd['width']*=1e-4; cd['start']*=1e-4; sign = lambda outlet: 1.0 if outlet[-2:] == 'ax' else -1.0 coords[outlet] += sign(outlet)*(cd['width'] + cd['start']); coords[otherside(outlet)] += sign(outlet)*cd['start']; conductorss += condb_tmpl.format( i=I+1, **sd(cd,**coords)); return conductorss;
def highlight(ret, val, q=None, color='white', alpha=0.15, erase=False): ''' Highlight a pc. Essentially a wrapper of plt.contour Arguments: ret -- dict returned from pc. val -- value to highlight q -- quantity to highlight. If None, highlight ret's quantity Keyword Arguments: color -- color of highlight alpha -- alpha of highlight erase -- erases the highlights. Defaults to false (opposite of matplotlib!) Returns: ret but with stuff that plt.contour adds. ''' ax = ret['axes']; x,y=ret['x'],ret['y']; if q is None: q = ret['q']; if test(ret,'flip') or test(ret,'rotate'): x,y=y,x; #elif q is not ret['q'] and test(ret,'flip'): if not test(ret, 'cbar'): ret['cbar'] = plt.colorbar(ret['pc']); cbar = ret['cbar']; if not test(ret, 'cts'): ret['cts'] = []; ct = ax.contour(x,y,q, [val], colors=[color], alpha = alpha); ret['cts'].append(ct); if q is ret['q']: cbar.add_lines(ct,erase=erase); return ret;
def genconductors(**kw): getkw=mk_getkw(kw, condb_defaults); conductorss=''; for I,conductor in enumerate(getkw('conductors')): cd = sd(condb_objdef, **conductor); coords=dict(); if test(cd,'outlet'): outlet = cd['outlet']; if outlet not in all_lims: raise ValueError('Unknown outlet "{}"'.format(outlet)); coords = outlet_coords(outlet,kw); cd['width']*=1e-4; cd['start']*=1e-4; if outlet[-2:] == 'ax': sign = 1.0; else: sign =-1.0 cd['type']= 'BLOCK'; coords[outlet] += sign*(cd['width'] + cd['start']); coords[otherside(outlet)] += sign*cd['start']; cd['from'] = (coords['xmin'],coords['ymin'],coords['zmin']); cd['to'] = (coords['xmax'],coords['ymax'],coords['zmax']); conductorss += condf_tmpl.format( i=I+1, xf=cd['from'][0],yf=cd['from'][1],zf=cd['from'][2], **cd); def mk_to(cd): '''generate the to's''' return ''.join([ condt_tmpl.format(xt=xt,yt=yt,zt=zt) for xt,yt,zt in cd['to'] ]); if cd['type'] == 'BLOCK': if type(cd['to']) == list: cd['to'] = cd['to'][0] conductorss += condt_tmpl.format( xt=cd['to'][0],yt=cd['to'][1],zt=cd['to'][2]); elif cd['type'] == 'PARALLELPIPED': conductorss += mk_to(cd); elif cd['type'] == 'TRILATERAL': conductorss += mk_to(cd); conductorss += 'sweep_direction {sweep_direction}\n'.format( **cd); else: raise ValueError( "Unknown conductor type '{}'".format(cd['type'])); return conductorss;
def gendens(**kw): getkw = mk_getkw(kw, densdefaults) speciesl = getkw('speciesl') if test(kw, 'target_density'): Q = kw['target_density'] if type(Q) == tuple: #the reason for tuple and not a general iterable #is when we pass a single scale, for example, which #we scale by fracs pass else: if hasattr(Q[0], '__call__'): Q = [lambda x: frac * iQf(x) for frac, iQf in zip(fracs, Q)] else: Q = [frac * iQ for frac, iQ in zip(fracs, Q)] if hasattr(Q[0], '__call__'): #invariant: txmin,txmax are in cm. x = np.linspace(kw['txmin'][0], kw['txmax'][1], 20) Q[:] = [iQ(x) for iQ in Q] ret = { outputfmt.format(species): densitypairs_tmpl.format(data=iQ) for species, iQ in zip(speciesl, Q) } kw.update(ret) else: kw['dens_dat'] = getkw('dens_dat') kw['dens_imul'] = getkw('dens_imul') kw['dens_type'] = getkw('dens_type') kw['fracs'] = getkw('fracs') def copy_kw(l): if type(kw[l]) != tuple: kw[l] = (kw[l], ) * len(speciesl) copy_kw('dens_dat') copy_kw('dens_imul') copy_kw('dens_type') for i, species in enumerate(speciesl): kw['n_' + species] = densityfile_tmpl.format( targetdat=kw['dens_dat'][i], type=kw['dens_type'][i], imul=kw['dens_imul'][i], dmul=kw['fracs'][i]) pass return kw
def format_region(region): if not test(region, "cells"): region['cells'] = "" else: region['cells'] = "cells = {}".format(region['cells']) return region_tmpl.format(**region)
def genlsp(**kw): def getkw(l,scale=None): if test(kw, l): ret = kw[l] else: ret = lspdefaults[l]; if scale: return [scale*i for i in ret]; return ret; def mkdims(s=''): return [s+'x',s+'y',s+'z']; ux = getkw('ux'); fmtd = {}; fmtd['options'] = genoptions(**kw); fmtd['E0'] = np.sqrt(2*getkw('I')*1e4/(c*e0))*1e-5 spatial_lims = getkw('lim',scale=ux); for dim,ilim in zip(all_lims,spatial_lims): kw[dim] = fmtd[dim] = ilim; if not test(kw,'tlim'): target_lims=spatial_lims else: target_lims=getkw('tlim',scale=ux); for dim,ilim in zip(all_lims,target_lims): kw['t'+dim] = fmtd['t'+dim] = target_lims; kw['targ_'+dim] = fmtd['targ_'+dim] = target_lims; #SI l=fmtd['l'] = getkw('l')*100.0 if test(kw,'resd'): xres,yres,zres = getkw("resd"); kw['xcells'] = (xmax-xmin)/(l/xres) if xres > 0 else 0; kw['ycells'] = (ymax-ymin)/(l/yres) if yres > 0 else 0; kw['zcells'] = (zmax-zmin)/(l/zres) if zres > 0 else 0; else: kw['xcells'], kw['ycells'], kw['zcells'] = getkw("res"); xcells, ycells, zcells = kw['xcells'], kw['ycells'], kw['zcells']; #generating outlets fmtd['other_boundaries']=genboundaries(**kw); fmtd['objects']=genobjects(**kw); #dealing with conductors fmtd['cond_temp'] = getkw('cond_temp'); fmtd['cond_fraction'] = getkw('cond_fraction'); fmtd['cond_threshold']= getkw('cond_threshold'); #others w0=fmtd['w0'] = getkw('w')*100.0; fmtd['pulse'] = getkw('T')*1e9; #generating grid xgrid,ygrid,zgrid=gengrids(**kw); fmtd['domains']=getkw('domains'); # we have that na~l/(pi*w), and the f-number~1/2na, thus # f-number ~ pi*w/2l fmtd['fnum']=np.pi*w0/2/l; fmtd['totaltime']=getkw('totaltime')*1e9 fmtd['timestep']=getkw('timestep')*1e9; # calculate courant if test(kw, 'xvargrid') or test(kw, 'yvargrid') or test(kw, 'zvargrid'): print("warning: check couraunt condition of vargrids"); else: def f_(v,*arg): return kw[v.format(*arg)]; #f_ = format dim_couraunts = [ ( f_('{}max',dim) - f_('{}min',dim) ) / f_('{}cells',dim) / c_cgs * 1e9 if f_('{}cells',dim) > 0 else float('inf') for dim in 'xyz']; couraunt = min(*dim_couraunts); if fmtd['timestep'] > couraunt: print("warning: timestep exceeds couraunt limit\n"); #target kw = gendens(**kw); kw = gentemp(**kw); fmtd['discrete'] = joinspace(getkw("discrete")); fmtd['dens_flags']= joinspace([ 1 if i else 0 for i in getkw("dens_flags")]); fmtd['gb_flags']= joinspace([ 1 if i else 0 for i in getkw("gb_flags")]); for dim,v in zip('xyz',getkw('tref')): if v is None: v = kw['t'+dim+'min']; fmtd['targref'+dim] = v; fmtd['targref'] = joinspace(mt([ fmtd['targref'+dim] for dim in 'xyz'],getkw('ux'))); fmtd['gammabeta'] = joinspace(getkw('gammabeta')); for species in getkw('speciesl'): def setspeciesv(fmt,defaultl,join=False,scale=False): l = fmt.format(species); if test(kw,l): v = kw[l]; if scale: v=mt(v,getkw('ux')); if join: v=joinspace(v); else: v = fmtd[defaultl]; fmtd[l] = v; l = 'n_'+species; fmtd[l] = kw[l]; l = '{}_thermalopts'.format(species); fmtd[l] = kw[l]; setspeciesv('{}_discrete','discrete'); setspeciesv('{}_targref','targref'); setspeciesv('{}_gb','gammabeta',join=True); if test(kw, '{}_tlims'.format(species)): splim = getkw('{}_tlim'.format(species),scale=ux); else: splim = getkw('tlim',scale=ux); for dim,i in zip(all_lims,splim): fmtd['{}_{}'.format(species,dim)] = i; pass fmtd['dumpinterval']=getkw('dumpinterval')*1e9; description=getkw('description'); restart = getkw('restart'); pexts = genpext(**sd(kw,species=getkw('pext_species'))); if test(kw, "regions"): regions = kw['regions']; else: regions = genregions(**kw); if not test(kw,"no_pmovies"): pmovies =''' particle_movie_interval_ns {dumpinterval} particle_movie_components Q X Y Z VX VY VZ XI YI ZI '''.format(dumpinterval=fmtd['dumpinterval']); else: pmovies = ''; fmtd['other_funcs'] = getkw('other_funcs'); lsptemplate=getkw("lsptemplate"); with open(lsptemplate) as f: s=f.read(); s=s.format( xgrid=xgrid, ygrid=ygrid, zgrid=zgrid, intensity=getkw('I'), pmovies=pmovies, regions=regions, pexts=pexts, description=description, **fmtd ); return s;
def genpbs(**kw): getkw = mk_getkw(kw, defaults) domains = getkw('domains') lspexec = getkw('lspexec') cluster = getkw("cluster") clusterq = cluster if test(kw, "queue"): clusterq += "_" + kw['queue'] mycluster = clusters[clusterq] if test(kw, 'ppn'): ppn = kw['ppn'] else: ppn = mycluster['max_ppn'] nodes = int(domains / ppn) if domains % ppn > 0: nodes += 1 extra_headers = '' mpirun = mycluster['mpi'].format(domains) pre = ''' cd $PBS_O_WORKDIR ''' portions = normal_portion_tmpl.format(nodes=nodes, ppn=ppn) walltime = getkw("walltime") if walltime is inf: walltime = mycluster['max_walltime'] elif walltime > mycluster['max_walltime']: import sys sys.stderr.write("walltime exceedes allowed for {}".format(clusterq)) walltime = hours_to_walltime(walltime) if cluster == "ramses": pre = "module load openmpi-1.4.3-gnu-rpm\n\n" + pre if nodes == 1: pre = ''' D=/tmp/ngirmang.1-`mkdate`-$PBSBASE mkdir -p $D cd $PBS_O_WORKDIR cp autozipper {lspexec} {pbsbase}.lsp *.dat $D/ cd $D '''.format(lspexec=lspexec, pbsbase=pbsbase) pass else: mpirun += "-hostfile $PBS_NODEFILE" elif cluster == "garnet": if not test(kw, 'queue'): kw['queue'] = "standard_lw" if not test(kw, 'mpiprocs'): kw['mpiprocs'] = ppn portions = garnet_portion_tmpl.format(nodes=nodes, max_ppn=mycluster['max_ppn'], ppn=ppn) extra_headers = ''' #PBS -A __projectid__ #PBS -q {} '''.format(kw['queue']) if test(kw, 'autozipper'): pre += '''#autozipper ./autozipper > $PBS_O_WORKDIR/autozipper.log& ''' with open("hotwater3d_tmpl.pbs") as f: s = f.read() return s.format( nodes=nodes, mpirun_opts=mpirun, pre=pre, ppn=ppn, portions=portions, pbsbase=getkw('pbsbase'), walltime=walltime, mpirun=mpirun, extra_headers=extra_headers, lspexec=lspexec, )
def genoptions(**kw): if test(kw, "options"): kw = sd(kw, kw['options']) #quirks getkw_outer = mk_getkw(kw, lspdefaults, prefer_passed=True) kw['maximum_restart_dump_time'] = getkw_outer('restart') def genopt(k, getkw=getkw_outer, flag=False): if not flag: flag = k i = getkw(k) if re.search("_ns$", flag): if isvalue(i): i *= 1e9 else: scaletuple(i, 1e9) if i is None or i is (): return "" elif i is True: return "{} ON\n".format(flag) elif i is False: return "{} OFF\n".format(flag) elif isvalue(i): return "{} {}\n".format(flag, i) else: return "{} {} end\n".format(flag, joinspace(i)) def genopts(opts): title, opts = opts getkw = mk_getkw(kw, opts, prefer_passed=True) tmpl = ";;{}\n{}\n" all_opts = "".join([genopt(k, getkw) for k in sorted(opts.keys())]) return tmpl.format(title, all_opts) out = ''.join([genopts(iopts) for iopts in [ restart_opts, balance_opts, ]]) out += options_dont_touch out += ''.join([genopts(iopts) for iopts in [ kinematic_opts, dump_opts, ]]) #now we do big dumps kw['interval_ns'] = getkw_outer("dumpinterval") kw['times_ns'] = getkw_outer("dumptimes") kw['steps'] = getkw_outer("dumpsteps") def gen_dumpopt(dump, opt): label = "{}_dump_{}".format(dump, opt) if test(kw, label): return genopt(label) else: return genopt(opt, flag=label) for dump in bigdumps: if not getkw_outer("dump_{}".format(dump)): continue out += "dump_{}s_flag ON\n".format(dump) for iopt in ['interval_ns', 'times_ns', 'steps']: out += gen_dumpopt(dump, iopt) return out
def gen_dumpopt(dump,opt): label = "{}_dump_{}".format(dump,opt); if test(kw, label): return genopt(label); else: return genopt(opt,flag=label);
def genpext(**kw): def getkw(l,scale=None): if test(kw, l): ret = kw[l] else: ret = pext_defaults[l]; if scale: return [scale*i for i in ret]; return ret; tmpl=''' ; extract{i} species {species} direction {direction} maximum_number 1000000000 start_time {start_time} stop_time {stop_time} at {position} ''' lims = list(getkw('lim',scale=getkw('ux'))); lims[0] = [lims[0],0,0]; lims[1] = [lims[1],0,0]; lims[2] = [0,lims[2],0]; lims[3] = [0,lims[3],0]; lims[4] = [0,0,lims[4]]; lims[5] = [0,0,lims[5]]; dirs = ['X','X','Y','Y','Z','Z']; planes = list(zip(dirs,lims)); if kw['zcells'] == 0: del planes[4:6]; if kw['ycells'] == 0: del planes[2:4]; def formatsp(sp,i): return [ dict(i=i+j+1, species=sp, direction=d, start_time=getkw('start_time'), stop_time =getkw('stop_time'), position =joinspace(lim)) for j,(d,lim) in enumerate(planes)]; pextplanes = [ tmpl.format(**d) for i,sp in enumerate(getkw('species')) for d in formatsp(sp,len(planes)*i)]; ret = joinspace(pextplanes); if test(kw, 'extrapexts'): ppl = len(pextplanes); expext_def = dict( species=10, direction='X', start_time=0, stop_time=1, position=(0.0,0.0,0.0), ); expexts = []; for p in getkw('extrapexts'): if test(p,'species') and p['species'] != 'all': expexts.append(p); elif test(p,'species') and type(p['species']) != str: expexts += [ sd(p, species = sp) for sp in p['species']]; else: expexts += [ sd(p, species = sp) for sp in getkw('species') ]; for i,ep in enumerate(expexts): pgetkw = mk_getkw(p, expext_def, prefer_passed = True); ret+=tmpl.format( i=ppl+i+1, species = pgetkw('species'), direction = pgetkw('direction'), start_time = pgetkw('start_time'), stop_time = pgetkw('stop_time'), position = joinspace(pgetkw('position')),); return ret;
def gendat(**kw): getkw=mk_getkw(kw,datdefaults); res = getkw('dat_xres'); unit=getkw('unit'); tlim = mt(getkw('tlim'),m=unit); if test(kw,'f_1D') or test(kw, 'data1D'): dim = 1; elif (test(kw,'f_2D') or test(kw, 'data2D')) and test(kw, 'tlim'): dim = 2; elif (test(kw,'f_3D') or test(kw, 'data3D')) and test(kw, 'tlim'): dim = 3; else: raise ValueError("Cannot reckon data dimensionality"); if dim == 1: if test(kw,'f_1D'): x = np.linspace(tlim[0],tlim[1],res); d = getkw('f_1D')(x); elif test(kw,'data1D'): x,d = getkw('data1D'); s = StringIO(); np.savetxt(s,np.array([x,d]).T,fmt='%.8e',); return s.getvalue(); elif dim == 2: if test(kw,'f_2D'): x = np.linspace(tlim[0],tlim[1],res); if np.isclose(tlim[2],tlim[3]): y = np.linspace(tlim[4],tlim[5],res); else: y = np.linspace(tlim[2],tlim[3],res); X,Y = np.meshgrid(x,y,indexing='ij'); d = getkw('f_2D')(X,Y); elif test(kw,'data2D'): x,y,d = getkw('data2D'); s = StringIO(); np.savetxt(s,np.array(list(d.shape)).reshape(1,-1), fmt='%i'); np.savetxt(s,np.array(x).reshape(1,-1), fmt='%.8e'); np.savetxt(s,np.array(y).reshape(1,-1), fmt='%.8e'); np.savetxt(s,np.array(d).T,fmt='%.8e',); return s.getvalue(); else: if test(kw, 'f_3d'): x = np.linspace(tlim[0],tlim[1],res); y = np.linspace(tlim[2],tlim[3],res); z = np.linspace(tlim[4],tlim[5],res); X,Y,Z = np.meshgrid(x,y,z,indexing='ij'); d = getkw('f_3d')(X,Y,Z); elif test(kw,'data3D'): x,y,z,d = getkw('data3D'); s = StringIO(); s.write("{} {} {}\n".format( d.shape[0],d.shape[1],d.shape[2])); np.savetxt(s,np.array(x).reshape(1,-1),fmt='%.8e'); np.savetxt(s,np.array(y).reshape(1,-1),fmt='%.8e'); np.savetxt(s,np.array(z).reshape(1,-1),fmt='%.8e'); for sub in np.rollaxis(d,1): np.savetxt(s,np.array(sub).T,fmt='%.8e',); return s.getvalue(); pass;
def pextrect(d, xname, yname, **kw): xlim = kw['xlim'] if test(kw, 'xlim') else None ylim = kw['ylim'] if test(kw, 'ylim') else None x_spacing = kw['x_spacing'] if test(kw, 'x_spacing') else 100 y_spacing = kw['y_spacing'] if test(kw, 'y_spacing') else 100 xlabel = kw['xlabel'] if test(kw, 'xlabel') else xname ylabel = kw['ylabel'] if test(kw, 'ylabel') else yname x = d[xname] y = d[yname] F = kw['F'] if test(kw, 'F') else 1.0 good = np.ones(len(x)).astype(bool) if xlim and not test(kw, 'x_no_restrict'): good &= (x >= xlim[0]) & (x <= xlim[1]) else: xlim = (x.min(), x.max()) if ylim and not test(kw, 'y_no_restrict'): good &= (y >= ylim[0]) & (y <= ylim[1]) else: ylim = (x.min(), x.max()) s = -d['q'][good] * 1e6 * F x = x[good] y = y[good] maxQ = kw['maxQ'] if test(kw, 'maxQ') else None x_bins = np.linspace(xlim[0], xlim[1], x_spacing + 1) y_bins = np.linspace(ylim[0], ylim[1], y_spacing + 1) X, Y = np.mgrid[xlim[0]:xlim[1]:x_spacing * 1j, ylim[0]:ylim[1]:y_spacing * 1j] S, _, _ = np.histogram2d(x, y, bins=(x_bins, y_bins), weights=s) if test(kw, 'normalize'): S /= np.abs(xlim[1] - xlim[0]) / x_spacing S /= np.abs(ylim[1] - ylim[0]) / y_spacing fig = kw['fig'] if test(kw, 'fig') else plt.figure(1) ax = kw['ax'] if test(kw, 'ax') else plt.subplot() plt.xlim(xlim) plt.ylim(ylim) plt.xlabel(xlabel) plt.ylabel(ylabel) surf = ax.pcolormesh(X, Y, S, cmap=pastel, vmax=maxQ) c = fig.colorbar(surf, pad=0.075) if test(kw, 'clabel'): c.set_label(kw['clabel']) if test(kw, 'title'): ax.set_title(kw['title'], fontdict={'size': 28}) pass
def gen_dumpopt(dump, opt): label = "{}_dump_{}".format(dump, opt) if test(kw, label): return genopt(label) else: return genopt(opt, flag=label)
def genboundaries(**kw): retboundaries = '' if test(kw,'manual_boundaries'): return '\n'.join([ manual_genboundary(bspec,**kw) for bspec in kw['manual_boundaries'] ]); laserkw = kw['laseroutlet'] if test(kw, 'laseroutlet') else dict(); laserkw = sd(kw, **laserkw); getkw = mk_getkw(laserkw,outletdefaults,prefer_passed = True); lset = set(); side_label = dict( xmin='front', xmax='back', ymin='left', ymax='right', zmin='bottom', zmax='top'); ls = []; if not test(laserkw, 'multilaser') and not (test(laserkw,'nolaser') or test(laserkw,'nolaser_outlet')): ls = [ sd(laserkw, outlet='xmin') ]; elif test(laserkw,'multilaser'): ls = kw['multilaser']; print("experimental multi-lasers"); print("if you put more than one laser on a single outlet,"); print("be sure to be using my modifications to lsp for it."); ls = kw['multilaser']; #lasers for l in ls: l = sd(laserkw, **l); lgetkw = mk_getkw(l, laserdefaults, prefer_passed = True); outlet = lgetkw('outlet'); if outlet not in all_lims: raise ValueError('Unknown outlet "{}"'.format(outlet)); lset.add(outlet); retboundaries += laser10_tmpl.format( fp = joinspace(mt(lgetkw("fp"),getkw('ux'))), components = joinspace(lgetkw("components")), phase_velocity = lgetkw('phase_velocity'), phases = joinspace(lgetkw("phases")), lasertfunc = lgetkw('lasertfunc'), laserafunc = lgetkw('laserafunc'), time_delay = lgetkw('laser_time_delay'), **outlet_coords(outlet, l) ); just_outlets = [i for i in all_lims if i not in lset]; if test(kw,'freespace'): getkwfr = mk_getkw( sd(kw,**kw['freespace']),frsp_defs,prefer_passed=True); di=dict(); di['model_type'] = getkwfr('model_type'); if di['model_type'] not in ["WAVEABC","UNIAXIAL","CFSPML"]: raise ValueError("unrecognized model_type for freespace"); if di['model_type']=="WAVEABC": di['num_of_cells'] = ""; else: di['num_of_cells'] = "number_of_cells {}".format( getkwfr('num_of_cells')); keeps = getkwfr('keep_outlets'); if keeps is None: keeps = []; just_outlets = [ i for i in just_outlets if i in keeps ]; if test(kw['freespace'],'frlim'): for dim,lim in zip(getkwfr('frlim'),all_lims): di[lim] = dim; else: dx = getkwfr('freesp_delta'); keepset = lset.union(just_outlets); for lim in all_lims: di[lim] = getkw(lim); if lim in keepset: if 'min' in lim: di[lim] -= dx; else: di[lim] += dx; di['refp'] = joinspace(getkwfr('freesp_refp')); di['label']= getkwfr('freesp_label'); retboundaries += freespace_tmpl.format(**di); #outlet boundaries which and are not removed by freespace for side in just_outlets: if laserkw[side[0]+'cells'] > 0: retboundaries += outlet_none_tmpl.format( label = side_label[side], phase_velocity=getkw('phase_velocity'), **outlet_coords(side, laserkw)); pwbtmpl=''' planewave from {xmin:e} {ymin:e} {zmin:e} to {xmax:e} {ymax:e} {zmax:e} reference_point {refp} polar_angle {polar} azimuthal_angle {azimuth} rotation_angle {rotation} frequency {freq} temporal_function {pwfunc} ''' pwbdefs = dict( polar=90, azimuth=45, rotation=-45, freq=1e3, pwfunc=3, pw_refp=[0.0,0.0,0.0], ) if test(kw, 'planewave_boundary'): di=dict(); pwkw = sd(kw,**kw['planewave_boundary']); getkwpw = mk_getkw( pwkw,pwbdefs,prefer_passed=True); if not test(pwkw, 'pwblim'): raise ValueError("This requires pwblim for now"); pwlims = getkwpw('pwblim'); for dim,lim in zip(pwlims,all_lims): di[lim] = dim; for qi in ['polar','azimuth','rotation','freq','pwfunc']: di[qi] = getkwpw(qi); di['refp'] = joinspace(getkwpw('pw_refp')); retboundaries+=pwbtmpl.format(**di); return retboundaries;
def genlsp(**kw): def getkw(l, scale=None): if test(kw, l): ret = kw[l] else: ret = lspdefaults[l] if scale: return [scale * i for i in ret] return ret def mkdims(s=''): return [s + 'x', s + 'y', s + 'z'] fmtd = {} fmtd['options'] = genoptions(**kw) fmtd['E0'] = np.sqrt(2 * getkw('I') * 1e4 / (c * e0)) * 1e-5 xmin, xmax, ymin, ymax, zmin, zmax = getkw('lim', scale=1e-4) kw['xmin'], kw['xmax'] = xmin, xmax kw['ymin'], kw['ymax'] = ymin, ymax kw['zmin'], kw['zmax'] = zmin, zmax txmin, txmax, tymin, tymax, tzmin, tzmax = getkw('tlim', scale=1e-4) kw['txmin'], kw['txmax'] = txmin, txmax kw['tymin'], kw['tymax'] = tymin, tymax kw['tzmin'], kw['tzmax'] = tzmin, tzmax l = fmtd['l'] = getkw('l') * 100.0 if test(kw, 'resd'): xres, yres, zres = getkw("resd") kw['xcells'] = (xmax - xmin) / (l / xres) if xres > 0 else 0 kw['ycells'] = (ymax - ymin) / (l / yres) if yres > 0 else 0 kw['zcells'] = (zmax - zmin) / (l / zres) if zres > 0 else 0 else: kw['xcells'], kw['ycells'], kw['zcells'] = getkw("res") xcells, ycells, zcells = kw['xcells'], kw['ycells'], kw['zcells'] #generating non x outlets other_outlets = genoutlets(**kw) w0 = fmtd['w0'] = getkw('w') * 100.0 fmtd['pulse'] = getkw('T') * 1e9 #generating grid ygrid = zgrid = '' if ycells > 0: ygrid = '''; ymin {ymin:e} ymax {ymax:e} y-cells {ycells}'''.format(ymin=ymin, ymax=ymax, ycells=ycells) if zcells > 0: zgrid = '''; zmin {zmin:e} zmax {zmax:e} z-cells {zcells}'''.format(zmin=zmin, zmax=zmax, zcells=zcells) fmtd['domains'] = getkw('domains') # we have that na~l/(pi*w), and the f-number~1/2na, thus # f-number ~ pi*w/2l fmtd['fnum'] = np.pi * w0 / 2 / l fmtd['totaltime'] = getkw('totaltime') * 1e9 fmtd['timestep'] = getkw('timestep') * 1e9 # calculate courant couraunt = min( ((xmax - xmin) / xcells / c_cgs) * 1e9 if xcells > 0 else float('inf'), ((ymax - ymin) / ycells / c_cgs) * 1e9 if ycells > 0 else float('inf'), ((zmax - zmin) / zcells / c_cgs) * 1e9 if zcells > 0 else float('inf'), ) if fmtd['timestep'] > couraunt: print("warning: timestep exceeds couraunt limit\n") #target kw = gendens(**kw) kw = gentemp(**kw) for species in getkw('speciesl'): l = 'n_' + species fmtd[l] = kw[l] l = '{}_thermalopts'.format(species) fmtd[l] = kw[l] fmtd['discrete'] = joinspace(getkw("discrete")) fmtd['dens_flags'] = joinspace( [1 if i else 0 for i in getkw("dens_flags")]) for idim, v in zip(mkdims(), getkw('tref')): if v is None: v = kw['t' + idim + 'min'] fmtd['targref' + idim] = v fmtd['dumpinterval'] = getkw('dumpinterval') * 1e9 description = getkw('description') restart = getkw('restart') pexts = genpext(**sd(kw, species=getkw('pext_species'))) if test(kw, "regions"): regions = kw['regions'] else: regions = genregions(**kw) if not test(kw, "no_pmovies"): pmovies = ''' particle_movie_interval_ns {dumpinterval} particle_movie_components Q X Y Z VX VY VZ XI YI ZI '''.format(dumpinterval=fmtd['dumpinterval']) else: pmovies = '' fmtd['other_funcs'] = getkw('other_funcs') lsptemplate = getkw("lsptemplate") with open(lsptemplate) as f: s = f.read() s = s.format(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, xcells=xcells, ycells=ycells, zcells=zcells, ygrid=ygrid, zgrid=zgrid, other_outlets=other_outlets, targ_xmin=txmin, targ_xmax=txmax, targ_ymin=tymin, targ_ymax=tymax, targ_zmin=tzmin, targ_zmax=tzmax, intensity=getkw('I'), pmovies=pmovies, regions=regions, pexts=pexts, description=description, **fmtd) return s
def genlsp(**kw): def getkw(l, scale=None): if test(kw, l): ret = kw[l] else: ret = defaults[l] if scale: return [scale * i for i in ret] return ret intensity = getkw("I") E0 = np.sqrt(2 * getkw("I") * 1e4 / (c * e0)) * 1e-5 xmin, xmax, ymin, ymax = getkw("lim", scale=1e-4) fp = joinspace(getkw("fp", scale=1e-4)) components = joinspace(getkw("components")) phases = joinspace(getkw("phases")) l = getkw("l") * 100.0 if test(kw, "resd"): xres, yres = getkw("resd") xcells = (xmax - xmin) / (l / xres) ycells = (ymax - ymin) / (l / yres) else: xcells, ycells = getkw("res") w0 = getkw("w") * 100.0 T = getkw("T") * 1e9 targ_xmin, targ_xmax, targ_ymin, targ_ymax = getkw("tlim", scale=1e-4) domains = getkw("domains") # we have that na~l/(pi*w), and the f-number~1/2na, thus # f-number ~ pi*w/2l fnum = np.pi * w0 / 2 / l totalt = getkw("totaltime") * 1e9 timestep = getkw("timestep") * 1e9 couraunt = min(((xmax - xmin) / xcells / c_cgs) * 1e9, ((ymax - ymin) / ycells / c_cgs) * 1e9) if timestep > couraunt: import sys sys.stderr.write("warning: timestep exceeds couraunt limit\n") pexts = genpext(**sd(kw, species=getkw("pext_species"))) targetdat = getkw("targetdat") dumpinterval = getkw("dumpinterval") * 1e9 description = getkw("description") restart = getkw("restart") if not test(kw, "no_pmovies"): pmovies = """ particle_movie_interval_ns {dumpinterval} particle_movie_components Q X Y VX VY XI YI """.format( dumpinterval=dumpinterval ) else: pmovies = "" restarts = "maximum_restart_dump_time {}".format(restart) if restart else "" with open("hotwater2d_tmpl.lsp") as f: s = f.read() s = s.format( xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, xcells=xcells, ycells=ycells, l=l, w0=w0, E0=E0, fnum=fnum, targ_xmin=targ_xmin, targ_xmax=targ_xmax, targ_ymin=targ_ymin, targ_ymax=targ_ymax, fp=fp, pulse=T, components=components, phases=phases, intensity=getkw("I"), pmovies=pmovies, pexts=pexts, domains=domains, totalt=totalt, timestep=timestep, targetdat=targetdat, dumpinterval=dumpinterval, description=description, restarts=restarts, ) return s
def genpbs(**kw): getkw = mk_getkw(kw, pbsdefaults) domains = getkw('domains') lspexec = getkw('lspexec') pbsbase = getkw('pbsbase') label = getkw('label') if not label: label = pbsbase cluster = getkw("cluster") clusterq = cluster concurrents = getkw('concurrents') if not concurrents: concurrents = [] if 'autozipper' not in kw or kw['autozipper']: concurrents = [('zipper', './zipper -a >$PBS_O_WORKDIR')] + concurrents movne = False if test(kw, "queue"): clusterq += "_" + kw['queue'] mycluster = clusters[clusterq] if test(kw, 'ppn'): ppn = kw['ppn'] else: ppn = mycluster['max_ppn'] nodes = int(domains / ppn) if domains % ppn > 0: nodes += 1 mpiformat = mycluster['mpi'] extra_headers = '' pre = ''' cd "$PBS_O_WORKDIR" ''' portions = normal_portion_tmpl.format(nodes=nodes, ppn=ppn) walltime = getkw("walltime") if walltime is inf: walltime = mycluster['max_walltime'] elif walltime > mycluster['max_walltime']: import sys sys.stderr.write("walltime exceedes allowed for {}".format(clusterq)) walltime = hours_to_walltime(walltime) post = '' # # server quirks # if cluster == "ramses": if nodes == 1: pre += ''' D=/tmp/ngirmang.1-`mkdate`-$PBSBASE mkdir -p $D cd "$PBS_O_WORKDIR" cp {lspexec} {pbsbase}.lsp *.dat $D/ '''.format(lspexec=lspexec, pbsbase=pbsbase) for concurrent in concurrents: pre += 'cp {} $D/\n'.format(concurrent[0]) if len(concurrents) > 0: pre += 'cp loopscript $D/\n' pre += 'cd $D\n' pre = "module load openmpi-1.4.3-gnu-rpm\n\n" + pre if dodcluster(cluster): if test(kw, 'mpiprocs') and kw['mpiprocs'] != ppn: mpiformat = 'aprun -n {{}} -N {}'.format(kw['mpiprocs']) nodes = int(domains / kw['mpiprocs']) if domains % kw['mpiprocs'] > 0: nodes += 1 else: kw['mpiprocs'] = ppn portions = garnet_portion_tmpl.format(nodes=nodes, ppn=ppn, mpiprocs=kw['mpiprocs']) extra_headers = "#PBS -A __projectid__\n#PBS -q {}\n".format( kw['queue']) if test(kw, "mkrundir"): pre += ('export RUNDIR="${{PBS_JOBNAME}}_${{PBS_JOBID}}"\n' 'mkdir -p $RUNDIR' 'rcp {lspexec} {pbsbase}.lsp *.dat $RUNDIR' 'cd $RUNDIR') if cluster == "garnet": #truncate name because life sucks label = label[:14] if not test(kw, 'queue'): kw['queue'] = "standard_lw" #handling conncurrent scripts if not dodcluster(cluster): for concurrent in concurrents: script = concurrent[0] pre += '''#{script} ./loopscript {script} &> $PBS_O_WORKDIR/{script}.log& {script}_PID=$! '''.format(script=script) post += "kill ${script}_PID\n".format(script=script) if len(concurrent) > 1: post += "{}\n".format(concurrent[1]) mpirun = mpiformat.format(domains) #finally outputting with open("hotwater3d_tmpl.pbs") as f: s = f.read() return s.format( label=label, nodes=nodes, mpirun_opts=mpirun, pre=pre, post=post, ppn=ppn, portions=portions, pbsbase=getkw('pbsbase'), walltime=walltime, mpirun=mpirun, extra_headers=extra_headers, lspexec=lspexec, )
def angular(d, phi=None, e=None, **kw): ''' Make the angular plot. Call form 1: angular(s, phi, e, kw...) Arguments: s -- the charges. phi -- the angles of ejection. e -- energies of each charge. Call form 2 angular(d, kw...) Arguments: d -- pext data, a structured array from the lspreader. Keyword Arugments: phi -- If a string, read this array of recs as the angles. If an array, these are the angles. e -- If not None, use these as energies over d['KE'] energy_units -- Set the energy units. Options are eV, KeV, MeV, GeV, and auto. auto chooses the unit based on the max energy. energy_scale -- Set the energy scale. Not required, but you can hack it from the default due to energy_unit if you wish. toMeV -- Scale to the MeV scale from energy scale. Not required, but you can hack it from energy_unit if you wish. max_e -- Maximum energy, if 'auto', bin automatically. e_step -- Set the steps of the radius contours. min_q -- Minimum charge. max_q -- Maximum charge. angle_range -- Only bin and plot these angles. Does not affect angle binning, bins are still over (-pi,pi) angle_bins -- Set the number of angle bins. energy_bins -- Set the number of energy bins. colorbar -- If true, plot the colorbar. cmap -- use the colormap cmap. clabel -- Set the colorbar label. labels -- Set the angular labels. If not a list, if 'default', use default. If 'tdefault', use default for theta. (See defaults dict); normalize -- Subdivide charges by the bin weights. fig -- If set, use this figure, Otherwise, make a new figure. ax -- If set, use this axis. Otherwise, make a new axis. ltitle -- Make a plot on the top left. rtitle -- Make a plot on the top right. log_q -- log10 the charges. rgridopts -- pass a dictionary that sets details for the rgrid labels. Options for this dict are: angle -- angle of labels. size -- text side. color -- grid color. invert -- invert the rgrid colors. oap -- Plot this apex angle if not None as the oap collection angle. efficiency -- calculate and display the conversion efficiency in the oap angle. A dict of options passed to totalKE and laserE (I only, not E_0). See help for totalKE and laserE. F -- Multiply charges by a factor. dict_return -- Have the return value be a convenient dictionary instead of god knows what it currently is (a tuple of tuple of stuff). ''' #reckon the call form getkw = mk_getkw(kw, defaults) if type(d) == np.ndarray and len(d.dtype) > 0: structd = True e = np.copy(d['KE']) if not phi: phi = 'phi' phi = np.copy(d[phi]) s = np.abs(d['q']) * 1e6 else: structd = False if phi is None or e is None: raise ValueError("Either phi and s were not passed. See help.") s = d eunits = getkw('energy_units') if eunits == 'auto': pw = np.log10(np.max(e)) if pw < 3: eunits = 'eV' elif pw <= 5: eunits = 'KeV' elif pw <= 9: eunits = 'MeV' else: eunits = 'GeV' if test(kw, 'angle_range'): mnang, mxang = kw['angle_range'] if mxang > np.pi: good = np.logical_and(phi >= mnang, phi <= np.pi) good |= np.logical_and(phi >= -np.pi, phi <= -(mxang - np.pi)) else: good = np.logical_and(phi >= mnang, phi <= mxang) phi = phi[good] e = e[good] s = s[good] if structd: d = d[good] getunitkw = mk_getkw(kw, unit_defaults[eunits]) if test(kw, 'F'): s *= kw['F'] phi_spacing = getkw('angle_bins') E_spacing = getkw('energy_bins') e /= getunitkw('energy_scale') maxE = getunitkw('max_e') Estep = getunitkw('e_step') if maxE == 'max': maxE = np.max(e) elif maxE == 'round' or maxE == 'auto': mxe = np.max(e) tenpow = np.floor(np.log10(mxe)) mantissa = np.floor(mxe / (10**tenpow)) maxE = 10**tenpow * (int(mxe / (10**tenpow)) + 1) Estep = 10**tenpow if mantissa > 6: Estep = 6 * 10**tenpow if test(kw, 'e_step'): Estep = kw['e_step'] maxQ = getkw('max_q') minQ = getkw('min_q') if test(kw, "normalize"): s /= maxE / E_spacing * 2 * np.pi / phi_spacing s *= getunitkw('toMeV') clabel = getkw('clabel') cmap = getkw('cmap') phi_bins = np.linspace(-np.pi, np.pi, phi_spacing + 1) E_bins = np.linspace(0, maxE, E_spacing + 1) PHI, E = np.mgrid[-np.pi:np.pi:phi_spacing * 1j, 0:maxE:E_spacing * 1j] S, _, _ = np.histogram2d(phi, e, bins=(phi_bins, E_bins), weights=s) if test(kw, 'fig'): fig = kw['fig'] else: fig = plt.figure(1, facecolor=(1, 1, 1)) if test(kw, 'ax'): ax = kw['ax'] else: ax = plt.subplot(projection='polar', facecolor='white') norm = matplotlib.colors.LogNorm() if test(kw, 'log_q') else None ax.set_rmax(maxE) surf = plt.pcolormesh(PHI, E, S, norm=norm, cmap=cmap, vmin=minQ, vmax=maxQ) if test(kw, 'colorbar'): c = fig.colorbar(surf, pad=0.1) c.set_label(clabel) #making radial guides. rgrids only works for plt.polar calls #making rgrid if test(kw, 'rgridopts'): ropts = kw['rgridopts'] else: ropts = dict() getrkw = mk_getkw(ropts, rgrid_defaults) if test(ropts, 'unit'): runit = ropts['unit'] else: runit = eunits rangle = getrkw('angle') rsize = getrkw('size') gridc = getrkw('color') if test(ropts, 'invert'): c1, c2 = "w", "black" else: c1, c2 = "black", "w" full_phi = np.linspace(0.0, 2 * np.pi, 100) rlabels = np.arange(0.0, maxE, Estep)[1:] for i in rlabels: plt.plot(full_phi, np.ones(full_phi.shape) * i, c=gridc, alpha=0.9, lw=1, ls='--') ax.set_theta_zero_location('N') ax.patch.set_alpha(0.0) ax.set_facecolor('red') rlabel_str = '{} ' + runit #text outlines. _, ts = plt.rgrids(rlabels, labels=map(rlabel_str.format, rlabels), angle=rangle) for t in ts: t.set_path_effects( [pe.Stroke(linewidth=1.5, foreground=c2), pe.Normal()]) t.set_size(rsize) t.set_color(c1) if test(kw, 'oap'): oap = kw['oap'] / 2 * np.pi / 180 maxt = oap + np.pi mint = np.pi - oap maxr = maxE * .99 if test(kw, 'efficiency') and structd: defeff = dict(I=3e18, w=None, T=None, l=None, ecut=0, anglecut=None) effd = sd(defeff, **kw['efficiency']) if effd['ecut'] == 'wilks': effd['ecut'] = ( np.sqrt(1 + a0(effd['I'], l=effd['l'] * 1e2)**2 / 2.0) - 1.0) * effd['massE'] dim = effd['dim'] LE = laserE(I=effd['I'], w=effd['w'], T=effd['T'], dim=dim) KE, good = totalKE(d, ecut=effd['ecut'], anglecut=(oap / np.pi * 180, dim), return_bools=True) minr = effd['ecut'] / getunitkw('energy_scale') totalq = np.abs(d['q'][good]).sum() * 1e6 def texs(f, l=2): tenpow = int(np.floor(np.log10(f))) nfmt = "{{:0.{}f}}".format(l) + "\\cdot 10^{{{}}}" return nfmt.format(f / 10**tenpow, tenpow) fig.text(0.01, 0.04, "Efficiency:\n$\\frac{{{}J}}{{{}J}}$=${}$".format( texs(KE, l=1), texs(LE, l=1), texs(KE / LE)), fontdict=dict(fontsize=20)) fig.text(0.65, 0.05, "$Q_{{tot}}={} ${}".format( texs(totalq), "pC" if dim == "3D" else "pC/cm"), fontdict=dict(fontsize=20)) fig.text(0.6, 0.92, "I = ${}$ W/cm$^2$".format(texs(effd['I'], l=1)), fontdict=dict(fontsize=20)) else: minr = 0.12 / getunitkw('toMeV') ths = np.linspace(mint, maxt, 20) rs = np.linspace(minr, maxr, 20) mkline = lambda a, b: plt.plot( a, b, c=(0.2, 0.2, 0.2), ls='-', alpha=0.5) mkline(ths, np.ones(ths.shape) * minr) mkline(mint * np.ones(ths.shape), rs) mkline(maxt * np.ones(ths.shape), rs) if test(kw, 'labels'): if kw['labels'] == 'default': labels = defaults['labels'] elif kw['labels'] == 'tdefault': labels = defaults['tlabels'] else: labels = kw['labels'] ax.set_xticks(np.pi / 180 * np.linspace(0, 360, len(labels), endpoint=False)) ax.set_xticklabels(labels) if test(kw, 'ltitle'): if len(kw['ltitle']) <= 4: ax.set_title(kw['ltitle'], loc='left', fontdict={'fontsize': 24}) else: ax.text(np.pi / 4 + 0.145, maxE + Estep * 2.4, kw['ltitle'], fontdict={'fontsize': 24}) if test(kw, 'rtitle'): if '\n' in kw['rtitle']: fig.text(0.60, 0.875, kw['rtitle'], fontdict={'fontsize': 22}) else: plt.title(kw['rtitle'], loc='right', fontdict={'fontsize': 22}) if test(kw, 'dict_return'): return dict( surf=surf, ax=ax, fig=fig, phi_bins=phi_bins, E_bins=E_bins, phi=phi, e=e, s=s, eunits=eunits, ) else: return (surf, ax, fig, (phi_bins, E_bins), (phi, e, s))
def genlsp(**kw): def getkw(l, scale=None): if test(kw, l): ret = kw[l] else: ret = defaults[l] if scale: return [scale * i for i in ret] return ret E0 = np.sqrt(2 * getkw('I') * 1e4 / (c * e0)) * 1e-5 xmin, xmax, ymin, ymax, zmin, zmax = getkw('lim', scale=1e-4) fp = joinspace(getkw("fp", scale=1e-4)) components = joinspace(getkw("components")) phases = joinspace(getkw("phases")) l = getkw('l') * 100.0 if test(kw, 'resd'): xres, yres, zres = getkw("resd") kw['xcells'] = (xmax - xmin) / (l / xres) kw['ycells'] = (ymax - ymin) / (l / yres) kw['zcells'] = (zmax - zmin) / (l / zres) else: kw['xcells'], kw['ycells'], kw['zcells'] = getkw("res") xcells, ycells, zcells = kw['xcells'], kw['ycells'], kw['zcells'] w0 = getkw('w') * 100.0 T = getkw('T') * 1e9 txmin, txmax, tymin, tymax, tzmin, tzmax = getkw('tlim', scale=1e-4) domains = getkw('domains') # we have that na~l/(pi*w), and the f-number~1/2na, thus # f-number ~ pi*w/2l fnum = np.pi * w0 / 2 / l totaltime = getkw('totaltime') * 1e9 timestep = getkw('timestep') * 1e9 couraunt = min( ((xmax - xmin) / xcells / c_cgs) * 1e9, ((ymax - ymin) / ycells / c_cgs) * 1e9, ((zmax - zmin) / zcells / c_cgs) * 1e9, ) if timestep > couraunt: import sys sys.stderr.write("warning: timestep exceeds couraunt limit\n") targetdat = getkw('targetdat') dumpinterval = getkw('dumpinterval') * 1e9 description = getkw('description') restart = getkw('restart') pexts = genpext(**sd(kw, species=getkw('pext_species'))) if test(kw, "regions"): regions = kw['regions'] else: regions = genregions(**kw) if not test(kw, "no_pmovies"): pmovies = ''' particle_movie_interval_ns {dumpinterval} particle_movie_components Q X Y Z VX VY VZ XI YI ZI '''.format(dumpinterval=dumpinterval) else: pmovies = '' restarts = "maximum_restart_dump_time {}".format( restart) if restart else "" with open("hotwater3d_tmpl.lsp") as f: s = f.read() s = s.format( xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, xcells=kw['xcells'], ycells=kw['ycells'], zcells=kw['zcells'], l=l, w0=w0, E0=E0, fnum=fnum, targ_xmin=txmin, targ_xmax=txmax, targ_ymin=tymin, targ_ymax=tymax, targ_zmin=tzmin, targ_zmax=tzmax, fp=fp, pulse=T, components=components, phases=phases, intensity=getkw('I'), pmovies=pmovies, regions=regions, pexts=pexts, domains=domains, totaltime=totaltime, timestep=timestep, targetdat=targetdat, dumpinterval=dumpinterval, description=description, restarts=restarts, ) return s
def genobjects(**kw): getkw=mk_getkw(kw, condb_defaults); ux = getkw('ux'); objss=''; for I,objspec in enumerate(getkw('objects')): coords=dict(); #objd = sd(condb_defaults, **kw); objd = sd(condb_objdef, **kw); objd = sd(objd, **objspec); objd['i'] = I + 1; if test(objspec,'outlet'): outlet = objd['outlet']; if outlet not in all_lims: raise ValueError('Unknown outlet "{}"'.format(outlet)); coords = outlet_coords(outlet,kw); objd['width']*=ux; objd['start']*=ux; if outlet[-2:] == 'ax': sign = 1.0; else: sign =-1.0 objd['type']= 'BLOCK'; coords[outlet] += sign*(objd['width'] + objd['start']); coords[otherside(outlet)] += sign*objd['start']; objd['crossstart']*=ux; otherdims = [i for i in 'xyz' if i != outlet[:-3]]; for dim in otherdims: if getkw('{}cells'.format(dim)) > 0: coords['{}min'.format(dim)] += objd['crossstart']; coords['{}max'.format(dim)] -= objd['crossstart']; objd['from'] = (coords['xmin'],coords['ymin'],coords['zmin']); objd['to'] = (coords['xmax'],coords['ymax'],coords['zmax']); #objss += condf_tmpl.format( # i=I+1, # condon=objd['condon'], # xf=objd['from'][0],yf=objd['from'][1],zf=objd['from'][2], # **objd); def mk_to(objd): '''generate the to's''' return ''.join([ condt_tmpl.format(xt=xt,yt=yt,zt=zt) for xt,yt,zt in objd['to'] ]); frms = ['BLOCK', 'PARALLELPIPED', 'TRILATERAL']; if objd['type'] == 'SOLID': objss += obj_solid_tmpl.format(**objd); elif objd['type'] in frms: objss += condf_tmpl.format( xf=objd['from'][0],yf=objd['from'][1],zf=objd['from'][2], **objd); objd = sd(condb_objdef, **objd); if objd['type'] == 'BLOCK': if type(objd['to']) != list: objd['to'] = [objd['to']]; objss += mk_to(objd); if objd['type'] == 'TRILATERAL': objss += 'sweep_direction {sweep_direction}\n'.format( **objd); elif objd['type'] == 'CONE': objd = sd(obj_cone_defs, **objd); coordtokeysl(objd, 'base', '{}b'); coordtokeysl(objd, 'apex', '{}ap'); coordtokeysl(objd, 'edge', '{}ed'); objss += obj_cone_tmpl.format(**objd); elif objd['type'] == 'CYLINDER' or objd['type'] == 'TORUS': objd = sd(obj_cyl_defs, **objd); objd['axis'],objd['pitch'] = objd['axis_pitch']; objd['azaxis'],objd['azpitch'] = objd['azimuthal_axis_pitch']; objd['start_angle'],objd['sweep_angle'] = objd['azimuth_range']; if objd['type'] == 'CYLINDER': coordtokeysl(objd, 'base', '{}b'); objss += obj_cyl_tmpl.format(**objd); elif objd['type'] == 'TORUS': objd = sd(obj_torus_defs,**objd); coordtokeysl(objd, 'center', '{}c'); objss += obj_torus_tmpl.format(**objd); else: raise Exception("WTFdiosjf03y2q8qencdq"); else: raise ValueError( "Unknown object type '{}'".format(objd['type'])); pass #end for return objss;
def genoutlets(**kw): outlet_tmpl=''' ;{label} outlet from {xmin:e} {ymin:e} {zmin:e} to {xmax:e} {ymax:e} {zmax:e} phase_velocity 1.0 drive_model NONE'''; laser10_tmpl=''' ;laser outlet from {xmin:e} {ymin:e} {zmin:e} to {xmax:e} {ymax:e} {zmax:e} phase_velocity 1.0 drive_model LASER reference_point {fp} components {components} phases {phases} temporal_function {lasertfunc} analytic_function {laserafunc} time_delay {time_delay} ''' retoutlets = '' laserkw = kw['laseroutlet'] if test(kw, 'laseroutlet') else dict(); laserkw = sd(kw, **laserkw); getkw = mk_getkw(laserkw,outletdefaults,prefer_passed = True); lset = set(); side_label = dict( xmin='front', xmax='back', ymin='left', ymax='right', zmin='bottom', zmax='top'); ls = []; if not test(laserkw, 'multilaser') and not test(laserkw,'nolaser'): ls = [ sd(laserkw, outlet='xmin') ]; elif test(laserkw,'multilaser'): ls = kw['multilaser']; print("experimental multi-lasers"); print("if you put more than one laser on a single outlet,"); print("be sure to be using my modifications to lsp for it."); ls = kw['multilaser']; #lasers for l in ls: l = sd(laserkw, **l); lgetkw = mk_getkw(l, laserdefaults, prefer_passed = True); outlet = lgetkw('outlet'); if outlet not in all_lims: raise ValueError('Unknown outlet "{}"'.format(outlet)); lset.add(outlet); retoutlets += laser10_tmpl.format( fp = joinspace(scaletuple(lgetkw("fp"))), components = joinspace(lgetkw("components")), phases = joinspace(lgetkw("phases")), lasertfunc = lgetkw('lasertfunc'), laserafunc = lgetkw('laserafunc'), time_delay = lgetkw('laser_time_delay'), **outlet_coords(outlet, l) ); #outlet boundaries for side in [i for i in all_lims if i not in lset] : if laserkw[side[0]+'cells'] > 0: retoutlets += outlet_tmpl.format( label = side_label[side], **outlet_coords(side, laserkw)); return retoutlets;
t = d['t']; tbins = np.arange(ti,t[-1]+tstep,tstep); #f*****g c like loop shit mother f****r. i=0; Is=[]; for j,ct in enumerate(t): if ct > tbins[i]: Is.append(j); i+=1; #do first surf,ax,fig,bins,data = angular(d,**kw); kw['fig'] = fig; rmax=ax.get_rmax() t=fig.text(tx,ty,'t = {:4.1f} fs'.format(tbins[0]*1e6), fontdict={'fontsize':22}); if not test(kw, 'phi'): kw['phi']='phi' def animate(ii): j,i = ii; #plt.clf(); #_,ax,_,_ = angular(d[:i],**kw); #ax.set_rmax(rmax); S,_,_ = np.histogram2d( data[0][:i], data[1][:i], bins=bins, weights=data[2][:i]); surf.set_array(S[::,:-1].ravel()); #t=fig.text(tx,ty,'t = {:3.2f}e-4 ns'.format((tbins[j]-mt)*1e4), # fontdict={'fontsize':22});
def genlsp(**kw): def getkw(l,scale=None): if test(kw, l): ret = kw[l] else: ret = lspdefaults[l]; if scale: return [scale*i for i in ret]; return ret; def mkdims(s=''): return [s+'x',s+'y',s+'z'] fmtd = {}; fmtd['options'] = genoptions(**kw); fmtd['E0'] = np.sqrt(2*getkw('I')*1e4/(c*e0))*1e-5 xmin,xmax, ymin,ymax, zmin,zmax = getkw('lim',scale=1e-4) kw['xmin'],kw['xmax']=xmin,xmax kw['ymin'],kw['ymax']=ymin,ymax kw['zmin'],kw['zmax']=zmin,zmax txmin,txmax,tymin,tymax,tzmin,tzmax=getkw('tlim',scale=1e-4); kw['txmin'],kw['txmax']=txmin,txmax kw['tymin'],kw['tymax']=tymin,tymax kw['tzmin'],kw['tzmax']=tzmin,tzmax l=fmtd['l'] = getkw('l')*100.0 if test(kw,'resd'): xres,yres,zres = getkw("resd"); kw['xcells'] = (xmax-xmin)/(l/xres) if xres > 0 else 0; kw['ycells'] = (ymax-ymin)/(l/yres) if yres > 0 else 0; kw['zcells'] = (zmax-zmin)/(l/zres) if zres > 0 else 0; else: kw['xcells'], kw['ycells'], kw['zcells'] = getkw("res"); xcells, ycells, zcells = kw['xcells'], kw['ycells'], kw['zcells']; #generating outlets other_outlets=genoutlets(**kw); conductors=genconductor_boundaries(**kw); #dealing with conductors fmtd['cond_temp'] = getkw('cond_temp'); fmtd['cond_fraction'] = getkw('cond_fraction'); w0=fmtd['w0'] = getkw('w')*100.0; fmtd['pulse'] = getkw('T')*1e9; #generating grid ygrid=zgrid=''; if ycells > 0: ygrid='''; ymin {ymin:e} ymax {ymax:e} y-cells {ycells}'''.format(ymin=ymin,ymax=ymax,ycells=ycells); if zcells > 0: zgrid='''; zmin {zmin:e} zmax {zmax:e} z-cells {zcells}'''.format(zmin=zmin,zmax=zmax,zcells=zcells); fmtd['domains']=getkw('domains'); # we have that na~l/(pi*w), and the f-number~1/2na, thus # f-number ~ pi*w/2l fmtd['fnum']=np.pi*w0/2/l; fmtd['totaltime']=getkw('totaltime')*1e9 fmtd['timestep']=getkw('timestep')*1e9; # calculate courant couraunt = min( ((xmax-xmin)/xcells/c_cgs)*1e9 if xcells > 0 else float('inf'), ((ymax-ymin)/ycells/c_cgs)*1e9 if ycells > 0 else float('inf'), ((zmax-zmin)/zcells/c_cgs)*1e9 if zcells > 0 else float('inf'),) if fmtd['timestep'] > couraunt: print("warning: timestep exceeds couraunt limit\n"); #target kw = gendens(**kw); kw = gentemp(**kw); for species in getkw('speciesl'): l = 'n_'+species; fmtd[l] = kw[l]; l = '{}_thermalopts'.format(species); fmtd[l] = kw[l]; fmtd['discrete'] = joinspace(getkw("discrete")); fmtd['dens_flags']= joinspace([ 1 if i else 0 for i in getkw("dens_flags")]); for idim,v in zip(mkdims(),getkw('tref')): if v is None: v = kw['t'+idim+'min']; fmtd['targref'+idim] = v; fmtd['dumpinterval']=getkw('dumpinterval')*1e9; description=getkw('description'); restart = getkw('restart'); pexts = genpext(**sd(kw,species=getkw('pext_species'))); if test(kw, "regions"): regions = kw['regions']; else: regions = genregions(**kw); if not test(kw,"no_pmovies"): pmovies =''' particle_movie_interval_ns {dumpinterval} particle_movie_components Q X Y Z VX VY VZ XI YI ZI '''.format(dumpinterval=fmtd['dumpinterval']); else: pmovies = ''; fmtd['other_funcs'] = getkw('other_funcs'); lsptemplate=getkw("lsptemplate"); with open(lsptemplate) as f: s=f.read(); s=s.format( xmin=xmin,xmax=xmax, ymin=ymin,ymax=ymax, zmin=zmin,zmax=zmax, xcells=xcells,ycells=ycells,zcells=zcells, ygrid=ygrid, zgrid=zgrid, other_outlets=other_outlets, objects=conductors, targ_xmin=txmin, targ_xmax=txmax, targ_ymin=tymin, targ_ymax=tymax, targ_zmin=tzmin, targ_zmax=tzmax, intensity=getkw('I'), pmovies=pmovies, regions=regions, pexts=pexts, description=description, **fmtd ); return s;
def genlsp(**kw): def getkw(l,scale=None): if test(kw, l): ret = kw[l] else: ret = defaults[l]; if scale: return [scale*i for i in ret]; return ret; intensity=getkw('I'); E0 = np.sqrt(2*getkw('I')*1e4/(c*e0))*1e-5 xmin,xmax, ymin,ymax = getkw('lim',scale=1e-4) fp = joinspace(getkw("fp",scale=1e-4)); components = joinspace(getkw("components")); phases = joinspace(getkw("phases")); l = getkw('l')*100.0 if test(kw,'resd'): xres,yres = getkw("resd"); xcells = (xmax-xmin)/(l/xres); ycells = (ymax-ymin)/(l/yres); else: xcells, ycells = getkw("res"); w0 = getkw('w')*100.0; T = getkw('T')*1e9; targ_xmin,targ_xmax, targ_ymin,targ_ymax =getkw('tlim',scale=1e-4); domains=getkw('domains'); # we have that na~l/(pi*w), and the f-number~1/2na, thus # f-number ~ pi*w/2l fnum=np.pi*w0/2/l; totalt=getkw('totaltime')*1e9 timestep=getkw('timestep')*1e9; couraunt = min( ((xmax-xmin)/xcells/c_cgs)*1e9, ((ymax-ymin)/ycells/c_cgs)*1e9) if timestep > couraunt: import sys sys.stderr.write("warning: timestep exceeds couraunt limit\n"); pexts = genpext(**sd(kw,species=getkw('pext_species'))); targetdat = getkw('targetdat'); dumpinterval=getkw('dumpinterval')*1e9; description=getkw('description'); restart = getkw('restart'); if not test(kw,"no_pmovies"): pmovies =''' particle_movie_interval_ns {dumpinterval} particle_movie_components Q X Y VX VY XI YI '''.format(dumpinterval=dumpinterval); else: pmovies = ''; restarts = "maximum_restart_dump_time {}".format(restart) if restart else ""; with open("hotwater2d_tmpl.lsp") as f: s=f.read(); s=s.format( xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax, xcells=xcells,ycells=ycells, l=l,w0=w0,E0=E0, fnum=fnum, targ_xmin=targ_xmin, targ_xmax=targ_xmax, targ_ymin=targ_ymin, targ_ymax=targ_ymax, fp=fp,pulse=T,components=components,phases=phases, intensity=getkw('I'), pmovies=pmovies, pexts=pexts, domains=domains, totalt=totalt, timestep=timestep, targetdat=targetdat, dumpinterval=dumpinterval, description=description, restarts=restarts ); return s;
def genoutlets(**kw): outlet_tmpl = ''' ;{side} outlet from {xf:e} {yf:e} {zf:e} to {xt:e} {yt:e} {zt:e} phase_velocity 1.0 drive_model NONE''' laser10_tmpl = ''' ;laser outlet from {xf:e} {yf:e} {zf:e} to {xt:e} {yt:e} {zt:e} phase_velocity 1.0 drive_model LASER reference_point {fp} components {components} phases {phases} temporal_function {lasertfunc} analytic_function {laserafunc} time_delay {time_delay} ''' retoutlets = '' laserkw = kw['laseroutlet'] if test(kw, 'laseroutlet') else dict() laserkw = sd(kw, **laserkw) getkw = mk_getkw(laserkw, outletdefaults, prefer_passed=True) if not test(laserkw, 'nolaser'): retoutlets += laser10_tmpl.format( xf=kw['xmin'], xt=kw['xmin'], yf=kw['ymin'], yt=kw['ymax'], zf=kw['zmin'], zt=kw['zmax'], fp=joinspace(scaletuple(getkw("fp"))), components=joinspace(getkw("components")), phases=joinspace(getkw("phases")), lasertfunc=getkw('lasertfunc'), laserafunc=getkw('laserafunc'), time_delay=getkw('laser_time_delay'), ) else: retoutlets += outlet_tmpl.format( side='front', xf=kw['xmin'], xt=kw['xmin'], yf=kw['ymin'], yt=kw['ymax'], zf=kw['zmin'], zt=kw['zmax'], ) retoutlets += outlet_tmpl.format( side='back', xf=kw['xmax'], xt=kw['xmax'], yf=kw['ymin'], yt=kw['ymax'], zf=kw['zmin'], zt=kw['zmax'], ) if kw['ycells'] > 0: retoutlets += outlet_tmpl.format( side='left', xf=kw['xmin'], xt=kw['xmax'], yf=kw['ymin'], yt=kw['ymin'], zf=kw['zmin'], zt=kw['zmax'], ) retoutlets += outlet_tmpl.format( side='right', xf=kw['xmin'], xt=kw['xmax'], yf=kw['ymax'], yt=kw['ymax'], zf=kw['zmin'], zt=kw['zmax'], ) if kw['zcells'] > 0: retoutlets += outlet_tmpl.format( side='bottom', xf=kw['xmin'], xt=kw['xmax'], yf=kw['ymin'], yt=kw['ymax'], zf=kw['zmin'], zt=kw['zmin'], ) retoutlets += outlet_tmpl.format( side='top', xf=kw['xmin'], xt=kw['xmax'], yf=kw['ymin'], yt=kw['ymax'], zf=kw['zmax'], zt=kw['zmax'], ) return retoutlets