def run_parallel(i, gdir, y_t): try: random_climate2 = RandomMassBalance(gdir, y0=1865, halfsize=14) experiment = gdir.read_pickle('synthetic_experiment') # ensure that not the same climate as in the experiments is used if experiment['climate'] == random_climate2: random_climate2 = RandomMassBalance(gdir, y0=1865, halfsize=14) res = minimize( objfunc, [0], args=(gdir, y_t, random_climate2), method='COBYLA', #constraints={'type':'ineq','fun':con}, tol=1e-04, options={ 'maxiter': 100, 'rhobeg': 1 }) result_model_t0, result_model_t = run_model(res.x, gdir, y_t, random_climate2) return result_model_t0, result_model_t except: return None, None
def _run_parallel_experiment(gdir): # read flowlines from pre-processing fls = gdir.read_pickle('model_flowlines') try: # construct searched glacier # TODO: y0 in random mass balance? random_climate1 = RandomMassBalance(gdir, y0=1850, bias=0, seed=[1]) random_climate1.temp_bias = -0.75 commit_model = FluxBasedModel(fls, mb_model=random_climate1, glen_a=cfg.A, y0=1850) commit_model.run_until_equilibrium() y_t0 = deepcopy(commit_model) # try different seed of mass balance, if equilibrium could not be found except: # construct searched glacier random_climate1 = RandomMassBalance(gdir, y0=1850, bias=0,seed=[1]) commit_model = FluxBasedModel(fls, mb_model=random_climate1, glen_a=cfg.A, y0=1850) commit_model.run_until_equilibrium() y_t0 = deepcopy(commit_model) # construct observed glacier past_climate = PastMassBalance(gdir) commit_model2 = FluxBasedModel(commit_model.fls, mb_model=past_climate, glen_a=cfg.A, y0=1850) commit_model2.run_until(2000) y_t = deepcopy(commit_model2) # save output in gdir_dir experiment = {'y_t0': y_t0, 'y_t': y_t, 'climate': random_climate1} gdir.write_pickle(experiment, 'synthetic_experiment')
def _run_parallel_experiment(gdir, t0, te): ''' :param gdir: :param t0: :param te: :return: ''' # read flowlines from pre-processing fls = gdir.read_pickle('model_flowlines') try: # construct searched glacier random_climate1 = RandomMassBalance(gdir, y0=t0, halfsize=14) #set temp bias negative to force a glacier retreat later random_climate1.temp_bias = -0.75 commit_model = FluxBasedModel(fls, mb_model=random_climate1, glen_a=cfg.A, y0=t0) commit_model.run_until_equilibrium() y_t0 = deepcopy(commit_model) # try different seed of mass balance, if equilibrium could not be found except: # construct searched glacier random_climate1 = RandomMassBalance(gdir, y0=t0, halfsize=14) commit_model = FluxBasedModel(fls, mb_model=random_climate1, glen_a=cfg.A, y0=t0) commit_model.run_until_equilibrium() y_t0 = deepcopy(commit_model) # construct observed glacier past_climate = PastMassBalance(gdir) commit_model2 = FluxBasedModel(commit_model.fls, mb_model=past_climate, glen_a=cfg.A, y0=t0) commit_model2.run_until(te) y_t = deepcopy(commit_model2) # save output in gdir_dir experiment = {'y_t0': y_t0, 'y_t': y_t, 'climate': random_climate1} gdir.write_pickle(experiment, 'synthetic_experiment')
def run_parallel(i, gdir, y_2000): try: random_climate2 = RandomMassBalance(gdir, y0=1875, halfsize=14) res = minimize(objfunc, [0], args=(gdir, y_2000.fls, random_climate2), method='COBYLA', tol=1e-04, options={ 'maxiter': 100, 'rhobeg': 1 }) # try: result_model_1880, result_model_2000 = run_model( res.x, gdir, y_2000.fls, random_climate2) return result_model_1880, result_model_2000 except: return None, None
def run_parallel(i, gdir, y_t, t0, te): try: random_climate2 = RandomMassBalance(gdir, y0=t0, halfsize=14) res = minimize( objfunc, [0], args=(gdir, y_t, random_climate2, t0, te), method='COBYLA', #constraints={'type':'ineq','fun':con}, tol=1e-04, options={ 'maxiter': 100, 'rhobeg': 1 }) result_model_t0, result_model_t = run_model(res.x, gdir, y_t, random_climate2, t0, te) return result_model_t0, result_model_t except: return None, None
def find_initial_state(gdir): global past_climate, random_climate2 global y_2000 global x global ax1, ax2, ax3 fls = gdir.read_pickle('model_flowlines') past_climate = PastMassBalance(gdir) random_climate1 = RandomMassBalance(gdir, y0=1865, halfsize=14) #construct searched glacier commit_model = FluxBasedModel(fls, mb_model=random_climate1, glen_a=cfg.A, y0=1850) commit_model.run_until_equilibrium() y_1880 = copy.deepcopy(commit_model) #construct observed glacier commit_model2 = FluxBasedModel(commit_model.fls, mb_model=past_climate, glen_a=cfg.A, y0=1880) commit_model2.run_until(2000) y_2000 = copy.deepcopy(commit_model2) results = pd.DataFrame(columns=['1880', '2000', 'length_1880']) pool = mp.Pool() result_list = pool.map(partial(run_parallel, gdir=gdir, y_2000=y_2000), range(300)) pool.close() pool.join() result_list = [x for x in result_list if x != [None, None]] # create plots for i in range(len(result_list[0][0].fls)): plt.figure(i, figsize=(20, 10)) #add subplot in the corner fig, ax1 = plt.subplots(figsize=(20, 10)) ax2 = fig.add_axes([0.55, 0.66, 0.3, 0.2]) ax1.set_title(gdir.rgi_id + ' flowline ' + str(i)) box = ax1.get_position() ax1.set_position([box.x0, box.y0, box.width * 0.95, box.height]) x = np.arange( y_2000.fls[i].nx) * y_2000.fls[i].dx * y_2000.fls[i].map_dx for j in range(len(result_list)): if result_list[j][0] != None: ax1.plot( x, result_list[j][0].fls[i].surface_h, alpha=0.8, ) ax2.plot( x, result_list[j][1].fls[i].surface_h, alpha=0.8, ) ax1.plot(x, y_1880.fls[i].surface_h, 'k:') ax2.plot(x, y_2000.fls[i].surface_h, 'k') ax1.plot(x, y_1880.fls[i].bed_h, 'k') ax2.plot(x, y_2000.fls[i].bed_h, 'k') plot_dir = os.path.join(cfg.PATHS['working_dir'], 'plots', 'Ben_idea_parallel_without_length') if not os.path.exists(plot_dir): os.makedirs(plot_dir) plt.savefig( os.path.join(plot_dir, gdir.rgi_id + '_fls' + str(i) + '.png')) pickle.dump(result_list, open(os.path.join(plot_dir, gdir.rgi_id + '.pkl'), 'wb')) solution = [y_1880, y_2000] pickle.dump( solution, open(os.path.join(plot_dir, gdir.rgi_id + '_solution.pkl'), 'wb'))
ice_thick[:, -1] = 0 return ice_thick # The model run function def run_task(gdir, grid=None, mb_model=None, glen_a=cfg.A, outf=None, ice_thick_filter=None, print_stdout=None): dmodel = Upstream2D(bed_topo, dx=grid.dx, mb_model=mb_model, y0=0., glen_a=glen_a, ice_thick_filter=ice_thick_filter) dmodel.run_until_and_store(800, run_path=outf, grid=grid, print_stdout=print_stdout) # Multiprocessing tasks = [] mbmod = RandomMassBalance(gdir, seed=0) outf = db_dir + 'out_def_100m.nc' tasks.append((run_task, {'grid': grid, 'mb_model': mbmod, 'outf': outf, 'ice_thick_filter':filter_ice_border, 'print_stdout':'Task1'})) mbmod = RandomMassBalance(gdir, seed=0) mbmod.temp_bias = 0.5 outf = db_dir + 'out_tbias_100m.nc' tasks.append((run_task, {'grid': grid, 'mb_model': mbmod, 'outf': outf, 'ice_thick_filter':filter_ice_border, 'print_stdout': 'Task2'})) mbmod = RandomMassBalance(gdir, seed=0) outf = db_dir + 'out_filter_100m.nc' tasks.append((run_task, {'grid': grid, 'mb_model': mbmod, 'outf': outf,
def find_initial_state(gdir): global past_climate global y_1900, y_1850 global y_start global x global ax1, ax2, ax3 fls = gdir.read_pickle('model_flowlines') past_climate = PastMassBalance(gdir) random_climate = RandomMassBalance(gdir) commit_model = FluxBasedModel(fls, mb_model=random_climate, glen_a=cfg.A, y0=1850) commit_model.run_until_equilibrium() y_1850 = copy.deepcopy(commit_model) commit_model = FluxBasedModel(commit_model.fls, mb_model=past_climate, glen_a=cfg.A, y0=1850) commit_model.run_until(2000) y_1900 = copy.deepcopy(commit_model) x = np.arange( y_1900.fls[-1].nx) * y_1900.fls[-1].dx * y_1900.fls[-1].map_dx plt.figure(figsize=(20, 10)) fig, ax1 = plt.subplots() ax2 = fig.add_axes([0.55, 0.66, 0.3, 0.2]) ax1.set_title(gdir.rgi_id) box = ax1.get_position() ax1.set_position([box.x0, box.y0, box.width * 0.95, box.height]) # Put a legend to the right of the current axis #plt.setp(ax1.get_xticklabels(), visible=False) #plt.plot(x, y_1850.fls[-1].surface_h, 'k:', label='solution') #plt.plot(x, y_1850.fls[-1].bed_h, 'k', label='bed') #plt.legend(loc='best') #ax2 = plt.subplot(412, sharex=ax1) #plt.setp(ax2.get_xticklabels(), visible=False) ''' ax3 = plt.subplot(413,sharex=ax1) ax3.plot(x, np.zeros(len(x)), 'k--') ax4 = plt.subplot(414, sharex=ax1) ax4.plot(x, np.zeros(len(x)), 'k--') ''' growing_model = FluxBasedModel(fls, mb_model=past_climate, glen_a=cfg.A, y0=1850) y_start = copy.deepcopy(growing_model) colors = [ pylab.cm.Blues(np.linspace(0.3, 1, 2)), pylab.cm.Reds(np.linspace(0.3, 1, 2)), pylab.cm.Greens(np.linspace(0.3, 1, 2)) ] #for i in [0,0.2,0.4,0.6,0.8,1,5,10,15,20,25,30,35,40,45,50]: j = 0 for i in [1]: k = 0 col = colors[j] j = j + 1 for t in [20, 40, 60, 80, 100, 120, 140, 150]: res = minimize(objfunc, [0], args=( gdir, y_1900.fls, t, i, ), method='COBYLA', tol=1e-04, options={ 'maxiter': 500, 'rhobeg': 2 }) try: result_model_1850, result_model_1900 = run_model( res.x, gdir, y_1900.fls, t, i) f = np.sum(abs(result_model_1900.fls[-1].surface_h-y_1900.fls[-1].surface_h) ** 2) + \ np.sum(abs(y_1900.fls[-1].widths - result_model_1900.fls[-1].widths) ** 2) dif_s = result_model_1900.fls[-1].surface_h - y_1900.fls[ -1].surface_h dif_w = result_model_1900.fls[-1].widths - y_1900.fls[-1].widths #if np.max(dif_s)<40 and np.max(dif_w)<15: ax1.plot(x, result_model_1850.fls[-1].surface_h, alpha=0.8, color=col[k], label='t=' + str(t)) ax2.plot(x, result_model_1900.fls[-1].surface_h, alpha=0.8, color=col[k]) except: pass k = k + 1 ax1.plot(x, y_1850.fls[-1].surface_h, 'k:') #, label='surface elevation (not known)') ax1.plot(x, y_1850.fls[-1].bed_h, 'k') #, label='bed topography') ax2.plot(x, y_1900.fls[-1].surface_h, 'k', label='surface elevation (observed)') ax2.plot(x, y_1900.fls[-1].bed_h, 'k', label='bed') #ax3.plot(x,np.zeros(len(x)),'k:') ax1.annotate('t = 1850', xy=(0.1, 0.95), xycoords='axes fraction', fontsize=13) ax2.annotate('t = 2000', xy=(0.1, 0.9), xycoords='axes fraction', fontsize=9) ax1.set_xlabel('Distance along the Flowline (m)') ax1.set_ylabel('Altitude (m)') ax2.set_xlabel('Distance along the Flowline (m)') ax2.set_ylabel('Altitude (m)') ax1.legend(loc='center left', bbox_to_anchor=(1, 0.5)) ax2.legend(loc='best') plot_dir = os.path.join(cfg.PATHS['working_dir'], 'plots') if not os.path.exists(plot_dir): os.makedirs(plot_dir) plt.savefig(os.path.join(plot_dir, gdir.rgi_id + '.png')) plt.show()
def find_initial_state(gdir): global past_climate,random_climate2 global y_2000 global x global ax1,ax2,ax3 fls = gdir.read_pickle('model_flowlines') past_climate = PastMassBalance(gdir) random_climate1 = RandomMassBalance(gdir,y0=1865,halfsize=14) #construct searched glacier commit_model = FluxBasedModel(fls, mb_model=random_climate1, glen_a=cfg.A, y0=1850) commit_model.run_until_equilibrium() y_1880 = copy.deepcopy(commit_model) #construct observed glacier commit_model2 = FluxBasedModel(commit_model.fls, mb_model=past_climate, glen_a=cfg.A, y0=1880) commit_model2.run_until(2000) y_2000 = copy.deepcopy(commit_model2) results = pd.DataFrame(columns=['1880','2000','length_1880']) for i in range(4): random_climate2 = RandomMassBalance(gdir, y0=1875, halfsize=14) res = minimize(objfunc, [0], args=(gdir, y_2000.fls), method='COBYLA', tol=1e-04, options={'maxiter': 100, 'rhobeg': 1}) #try: result_model_1880, result_model_2000 = run_model(res.x, gdir, y_2000.fls) results = results.append({'1880':result_model_1880,'2000':result_model_2000,'length_1880':result_model_1880.length_m,'length_2000':result_model_2000.length_m}, ignore_index=True) #except: # pass # create plots for i in range(len(fls)): plt.figure(i,figsize=(20,10)) #add subplot in the corner fig, ax1 = plt.subplots(figsize=(20, 10)) ax2 = fig.add_axes([0.55, 0.66, 0.3, 0.2]) ax1.set_title(gdir.rgi_id+' flowline '+str(i)) box = ax1.get_position() ax1.set_position([box.x0, box.y0, box.width * 0.95, box.height]) x = np.arange(y_2000.fls[i].nx) * y_2000.fls[i].dx * y_2000.fls[i].map_dx for j in range(len(results)): ax1.plot(x, results.get_value(j, '1880').fls[i].surface_h, alpha=0.8, ) ax2.plot(x, results.get_value(j, '2000').fls[i].surface_h, alpha=0.8, ) ax1.plot(x,y_1880.fls[i].surface_h,'k:') ax2.plot(x, y_2000.fls[i].surface_h, 'k') ax1.plot(x, y_1880.fls[i].bed_h, 'k') ax2.plot(x, y_2000.fls[i].bed_h, 'k') plot_dir = os.path.join(cfg.PATHS['working_dir'], 'plots') plt.savefig(os.path.join(plot_dir, gdir.rgi_id +'_fls'+str(i)+ '.png')) #plt.show() results.to_csv(os.path.join(plot_dir,str(gdir.rgi_id)+'.csv')) '''