def __init__(self,start,goal,g):
        self.running = False  # initial with running state
        self.done = False #for tell whether search is done
        if start == None:
            self.start = Node([0,0])
        else:
            self.start = Node(start)
        self.goal_list = goal.copy()
        #print(self.goal_list)
        #self.g_temp = goal
        self.current_node = self.start
        # Create lists for open nodes and closed nodes
        self.open = []
        self.closed = []
        self.open.append(self.start)
        #for statistics

        stt = pygame.time.get_ticks()
        tracemalloc.start()
        self.astar_search(start,goal,g)
        CurrentMem, self.Peak_mem = tracemalloc.get_traced_memory()
        tracemalloc.stop()
        self.time_consumption = pygame.time.get_ticks() - stt
        
        print(self.Peak_mem,self.time_consumption)
        self.g_temp = self.goal_list.copy()       
        self.goal = self.g_temp.pop(0)
Exemplo n.º 2
0
def assign_new_to_values(grid, q_new, q, tmp):
    i_gm, i_env, i_uds, i_sd = q.domain_idx()
    slice_all_c = grid.slice_all(Center())
    slice_all_n = grid.slice_all(Node())
    for i in i_uds:
        q_new['w', i][slice_all_n] = [
            q['w', i][k] for k in grid.over_elems(Node())
        ]
        q_new['q_tot', i][slice_all_c] = [
            q['q_tot', i][k] for k in grid.over_elems(Center())
        ]
        q_new['q_rai', i][slice_all_c] = [
            q['q_rai', i][k] for k in grid.over_elems(Center())
        ]
        q_new['θ_liq', i][slice_all_c] = [
            q['θ_liq', i][k] for k in grid.over_elems(Center())
        ]
    return
 def search(self,g):
     # Loop until the open list is empty
     if len(self.open) > 0:
         # Sort the open list to get the node with the lowest cost first
         self.open.sort()
         # Get the node with the lowest cost
         self.current_node = self.open.pop(0)
         # Add the current node to the closed list
         self.closed.append(self.current_node)
         
         # Check if we have reached the goal, return the path
         #print(self.current_node.position,self.goal)
         if self.current_node.position == self.goal:
             #print('A_star done',self.goal)
             path = []
             backtraverse = deepcopy(self.current_node)
             while backtraverse.position != self.start.position:
                 path.append(backtraverse.position)
                 backtraverse = backtraverse.parent
             if len(self.g_temp) > 0:
                 self.goal = self.g_temp.pop(0)
             else:
                 self.done = True
                 return 0
             self.open = []
             self.closed = []
             
         # Get neighbors
         neighbors = g.find_neighbors(self.current_node.position)
         # Loop neighbors
         for next in neighbors:
             # Create a neighbor node
             neighbor = Node(next, self.current_node)
             # Check if the neighbor is in the closed list
             if(neighbor in self.closed):
                 continue
             # Generate heuristics (Manhattan distance)
             neighbor.g = abs(neighbor.position[0] - self.start.position[0]) + abs(neighbor.position[1] - self.start.position[1])
             neighbor.h = abs(neighbor.position[0] - self.goal[0]) + abs(neighbor.position[1] - self.goal[1])
             neighbor.f = neighbor.g + neighbor.h
             # Check if neighbor is in open list and if it has a lower f value
             if self.check(neighbor):
                 # Everything is green, add neighbor to open list
                 self.open.append(neighbor)
Exemplo n.º 4
0
def update_sol_gm(grid, q_new, q, q_tendencies, TS, tmp, tri_diag):
    i_gm, i_env, i_uds, i_sd = q.domain_idx()
    ρ_0_half = tmp['ρ_0']
    ae = q['a', i_env]
    slice_real_n = grid.slice_real(Node())
    slice_all_c = grid.slice_all(Center())

    tri_diag.ρaK[slice_real_n] = [
        ae.Mid(k) * tmp['K_h'].Mid(k) * ρ_0_half.Mid(k)
        for k in grid.over_elems_real(Node())
    ]
    construct_tridiag_diffusion_O1(grid, TS.Δt, tri_diag, ρ_0_half, ae)
    tri_diag.f[slice_all_c] = [
        q['q_tot', i_gm][k] + TS.Δt * q_tendencies['q_tot', i_gm][k]
        for k in grid.over_elems(Center())
    ]
    solve_tridiag_wrapper(grid, q_new['q_tot', i_gm], tri_diag)
    tri_diag.f[slice_all_c] = [
        q['θ_liq', i_gm][k] + TS.Δt * q_tendencies['θ_liq', i_gm][k]
        for k in grid.over_elems(Center())
    ]
    solve_tridiag_wrapper(grid, q_new['θ_liq', i_gm], tri_diag)

    tri_diag.ρaK[slice_real_n] = [
        ae.Mid(k) * tmp['K_m'].Mid(k) * ρ_0_half.Mid(k)
        for k in grid.over_elems_real(Node())
    ]
    construct_tridiag_diffusion_O1(grid, TS.Δt, tri_diag, ρ_0_half, ae)
    tri_diag.f[slice_all_c] = [
        q['U', i_gm][k] + TS.Δt * q_tendencies['U', i_gm][k]
        for k in grid.over_elems(Center())
    ]
    solve_tridiag_wrapper(grid, q_new['U', i_gm], tri_diag)
    tri_diag.f[slice_all_c] = [
        q['V', i_gm][k] + TS.Δt * q_tendencies['V', i_gm][k]
        for k in grid.over_elems(Center())
    ]
    solve_tridiag_wrapper(grid, q_new['V', i_gm], tri_diag)
    return
 def compute_prognostic_updrafts(self, grid, q_new, q, q_tendencies, tmp,
                                 UpdVar, Case, TS):
     time_elapsed = 0.0
     i_gm, i_env, i_uds, i_sd = q.domain_idx()
     u_max = np.max(
         [q['w', i][k] for i in i_uds for k in grid.over_elems(Node())])
     TS.Δt_up = np.minimum(TS.Δt, 0.5 * grid.dz / np.fmax(u_max, 1e-10))
     while time_elapsed < TS.Δt:
         compute_entrainment_detrainment(grid, UpdVar, Case, tmp, q,
                                         self.entr_detr_fp, self.wstar,
                                         self.tke_ed_coeff,
                                         self.entrainment_factor,
                                         self.detrainment_factor)
         eos_update_SA_mean(grid, q, False, tmp, self.max_supersaturation)
         buoyancy(grid, q, tmp)
         compute_sources(grid, q, tmp, self.max_supersaturation)
         update_updraftvars(grid, q, tmp)
         solve_updraft_velocity_area(grid, q_new, q, q_tendencies, tmp,
                                     UpdVar, TS, self.params)
         solve_updraft_scalars(grid, q_new, q, q_tendencies, tmp, UpdVar,
                               TS, self.params)
         assign_values_to_new(grid, q, q_new, tmp)
         for i in i_sd:
             q['θ_liq', i].apply_bc(grid, 0.0)
             q['q_tot', i].apply_bc(grid, 0.0)
             q['q_rai', i].apply_bc(grid, 0.0)
         q['w', i_env].apply_bc(grid, 0.0)
         time_elapsed += TS.Δt_up
         u_max = np.max(
             [q['w', i][k] for i in i_uds for k in grid.over_elems(Node())])
         TS.Δt_up = np.minimum(TS.Δt - time_elapsed,
                               0.5 * grid.dz / np.fmax(u_max, 1e-10))
         diagnose_environment(grid, q)
     eos_update_SA_mean(grid, q, True, tmp, self.max_supersaturation)
     buoyancy(grid, q, tmp)
     return
    def astar_search(self,start, end,g):
        # Create lists for open nodes and closed nodes
        open = []
        closed = []
        # Create a start node and an goal node
        start_node = Node(start, None)
        goal_node = Node(end.pop(0), None)
        # Add the start node
        open.append(start_node)
        
        # Loop until the open list is empty
        while len(open) > 0:
            # Sort the open list to get the node with the lowest cost first
            open.sort()
            # Get the node with the lowest cost
            current_node = open.pop(0)
            # Add the current node to the closed list
            closed.append(current_node)
            
            # Check goal
            #print(closed)
            if current_node == goal_node:
                if len(end) > 0:
                    goal_node = Node(end.pop(0), None)
                else:
                    return 0
                open = []
                closed = []
            
            # Get neighbors
            neighbors = g.find_neighbors(current_node.position)
            # Loop neighbors
            for next in neighbors:
                neighbor = Node(next,current_node)
                # Check if the neighbor is in the closed list
                if(neighbor in closed):
                    continue
                # Generate heuristics (Manhattan distance)
                neighbor.g = abs(neighbor.position[0] - start_node.position[0]) + abs(neighbor.position[1] - start_node.position[1])
                neighbor.h = abs(neighbor.position[0] - goal_node.position[0]) + abs(neighbor.position[1] - goal_node.position[1])
                neighbor.f = neighbor.g + neighbor.h

                # Check if neighbor is in open list and if it has a lower f value
                if self.chc(neighbor,open):
                    open.append(neighbor)
Exemplo n.º 7
0
    def __init__(self, namelist, paramlist, root_dir):
        z_min = 0
        n_elems_real = namelist['grid']['nz']
        z_max = namelist['grid']['dz'] * namelist['grid']['nz']
        n_ghost = namelist['grid']['gw']
        N_subdomains = namelist['turbulence']['EDMF_PrognosticTKE'][
            'updraft_number'] + 2

        N_sd = N_subdomains

        unkowns = (
            ('a', Center(), Neumann(), N_sd),
            ('w', Node(), Dirichlet(), N_sd),
            ('q_tot', Center(), Neumann(), N_sd),
            ('q_rai', Center(), Neumann(), N_sd),
            ('θ_liq', Center(), Neumann(), N_sd),
            ('tke', Center(), Neumann(), N_sd),
            ('cv_q_tot', Center(), Neumann(), N_sd),
            ('cv_θ_liq', Center(), Neumann(), N_sd),
            ('cv_θ_liq_q_tot', Center(), Neumann(), N_sd),
            ('U', Center(), Neumann(), N_sd),
            ('V', Center(), Neumann(), N_sd),
        )

        temp_vars = (
            ('mean_entr_sc', Center(), Neumann(), 1),
            ('mean_detr_sc', Center(), Neumann(), 1),
            ('massflux_half', Center(), Neumann(), 1),
            ('mf_q_tot_half', Center(), Neumann(), 1),
            ('mf_θ_liq_half', Center(), Neumann(), 1),
            ('temp_C', Center(), Neumann(), 1),
            ('ρ_0', Center(), Neumann(), 1),
            ('α_0', Center(), Neumann(), 1),
            ('p_0', Center(), Neumann(), 1),
            ('K_m', Center(), Neumann(), 1),
            ('K_h', Center(), Neumann(), 1),
            ('l_mix', Center(), Neumann(), 1),
            ('q_tot_dry', Center(), Neumann(), 1),
            ('θ_dry', Center(), Neumann(), 1),
            ('t_cloudy', Center(), Neumann(), 1),
            ('q_vap_cloudy', Center(), Neumann(), 1),
            ('q_tot_cloudy', Center(), Neumann(), 1),
            ('θ_cloudy', Center(), Neumann(), 1),
            ('cv_θ_liq_rain_dt', Center(), Neumann(), 1),
            ('cv_q_tot_rain_dt', Center(), Neumann(), 1),
            ('cv_θ_liq_q_tot_rain_dt', Center(), Neumann(), 1),
            ('CF', Center(), Neumann(), 1),
            ('entr_sc', Center(), Neumann(),
             N_sd),  # Entrainment/Detrainment rates
            ('detr_sc', Center(), Neumann(),
             N_sd),  # Entrainment/Detrainment rates
            ('δ_src_a', Center(), Neumann(), N_sd),
            ('δ_src_w', Center(), Neumann(), N_sd),
            ('δ_src_q_tot', Center(), Neumann(), N_sd),
            ('δ_src_θ_liq', Center(), Neumann(), N_sd),
            ('δ_src_a_model', Center(), Neumann(), N_sd),
            ('δ_src_w_model', Center(), Neumann(), N_sd),
            ('δ_src_q_tot_model', Center(), Neumann(), N_sd),
            ('δ_src_θ_liq_model', Center(), Neumann(), N_sd),
            ('q_liq', Center(), Neumann(), N_sd),
            ('prec_src_θ_liq', Center(), Neumann(), N_sd),
            ('prec_src_q_tot', Center(), Neumann(), N_sd),
            ('T', Center(), Neumann(), N_sd),
            ('B', Center(), Neumann(), N_sd),
            ('mf_θ_liq', Center(), Neumann(), N_sd),
            ('mf_q_tot', Center(), Neumann(), N_sd),
            ('mf_tend_θ_liq', Center(), Neumann(), N_sd),
            ('mf_tend_q_tot', Center(), Neumann(), N_sd),
            ('mf_tmp', Center(), Neumann(), N_sd),
        )

        q_2MO = (
            ('values', Center(), Neumann(), 1),
            ('dissipation', Center(), Neumann(), 1),
            ('entr_gain', Center(), Neumann(), 1),
            ('detr_loss', Center(), Neumann(), 1),
            ('buoy', Center(), Neumann(), 1),
            ('press', Center(), Neumann(), 1),
            ('shear', Center(), Neumann(), 1),
            ('interdomain', Center(), Neumann(), 1),
            ('rain_src', Center(), Neumann(), 1),
        )

        self.grid = Grid(z_min, z_max, n_elems_real, n_ghost)
        self.q = StateVec(unkowns, self.grid)
        self.q_new = copy.deepcopy(self.q)
        self.q_old = copy.deepcopy(self.q)
        self.q_tendencies = copy.deepcopy(self.q)
        self.tmp = StateVec(temp_vars, self.grid)

        self.tmp_O2 = {}
        self.tmp_O2['tke'] = StateVec(q_2MO, self.grid)
        self.tmp_O2['cv_q_tot'] = copy.deepcopy(self.tmp_O2['tke'])
        self.tmp_O2['cv_θ_liq'] = copy.deepcopy(self.tmp_O2['tke'])
        self.tmp_O2['cv_θ_liq_q_tot'] = copy.deepcopy(self.tmp_O2['tke'])
        self.tmp_O2['tke'] = copy.deepcopy(self.tmp_O2['tke'])

        self.n_updrafts = namelist['turbulence']['EDMF_PrognosticTKE'][
            'updraft_number']

        self.Ref = ReferenceState(self.grid)
        self.Case = CasesFactory(namelist, paramlist)
        self.Turb = EDMF_PrognosticTKE(namelist, paramlist, self.grid)
        self.UpdVar = [
            UpdraftVariables(i, self.Turb.surface_area, self.n_updrafts)
            for i in range(self.n_updrafts)
        ]
        self.TS = TimeStepping(namelist)
        self.Stats = NetCDFIO_Stats(namelist, paramlist, self.grid, root_dir)
        self.tri_diag = type('', (), {})()
        self.tri_diag.a = Half(self.grid)
        self.tri_diag.b = Half(self.grid)
        self.tri_diag.c = Half(self.grid)
        self.tri_diag.f = Half(self.grid)
        self.tri_diag.β = Half(self.grid)
        self.tri_diag.γ = Half(self.grid)
        self.tri_diag.xtemp = Half(self.grid)
        self.tri_diag.ρaK = Full(self.grid)
        return
Exemplo n.º 8
0
 def __init__(self, grid, bc=None):
     super(Full, self).__init__(grid.nzg, Node(), bc)
     return
Exemplo n.º 9
0
 def extrap(self, grid):
     for k in reversed(grid.over_elems_ghost(Node(), Zmin())):
         self.values[k] = 2.0 * self.values[k + 1] - self.values[k + 2]
     for k in grid.over_elems_ghost(Node(), Zmax()):
         self.values[k] = 2.0 * self.values[k - 1] - self.values[k - 2]
Exemplo n.º 10
0
def initialize_ref_state(grid, Stats, p_0, ρ_0, α_0, loc, sg, Pg, Tg, qtg):
    sg = t_to_entropy_c(Pg, Tg, qtg, 0.0, 0.0)

    # Form a right hand side for integrating the hydrostatic equation to
    # determine the reference pressure
    def rhs(p, z):
        T, q_l = eos_entropy(np.exp(p), qtg, sg)
        q_i = 0.0
        R_m = Rd * (1.0 - qtg + eps_vi * (qtg - q_l - q_i))
        return -g / (R_m * T)

    # Construct arrays for integration points
    z_full = [grid.z[k] for k in grid.over_elems_real(Node())]
    z_half = [grid.z_half[k] for k in grid.over_elems_real(Center())]
    z = z_full if isinstance(loc, Node) else z_half

    # We are integrating the log pressure so need to take the log of the
    # surface pressure
    q_liq = Field.field(grid, loc)
    q_ice = Field.field(grid, loc)
    q_vap = Field.field(grid, loc)
    temperature = Field.field(grid, loc)

    p0 = np.log(Pg)
    p_0[grid.slice_real(loc)] = odeint(rhs, p0, z, hmax=1.0)[:, 0]
    p_0.apply_Neumann(grid, 0.0)
    p_0[:] = np.exp(p_0[:])

    # Compute reference state thermodynamic profiles
    for k in grid.over_elems_real(loc):
        temperature[k], q_liq[k] = eos_entropy(p_0[k], qtg, sg)
        q_vap[k] = qtg - (q_liq[k] + q_ice[k])
        α_0[k] = alpha_c(p_0[k], temperature[k], qtg, q_vap[k])
        ρ_0[k] = 1.0 / α_0[k]

    # Sanity check: make sure Reference State entropy is uniform
    for k in grid.over_elems(loc):
        s = t_to_entropy_c(p_0[k], temperature[k], qtg, q_liq[k], q_ice[k])
        if np.abs(s - sg) / sg > 0.01:
            print('Error in reference profiles entropy not constant !')
            print('Likely error in saturation adjustment')

    α_0.extrap(grid)
    p_0.extrap(grid)
    ρ_0.extrap(grid)

    p_0_name = nice_name('p_0') + str(loc.__class__.__name__)
    ρ_0_name = nice_name('ρ_0') + str(loc.__class__.__name__)
    α_0_name = nice_name('α_0') + str(loc.__class__.__name__)

    plt.plot(p_0.values, grid.z)
    plt.title(p_0_name + ' vs z')
    plt.xlabel(p_0_name)
    plt.ylabel('z')
    plt.savefig(Stats.figpath + p_0_name + '.png')
    plt.close()

    plt.plot(ρ_0.values, grid.z)
    plt.title(ρ_0_name + ' vs z')
    plt.xlabel(ρ_0_name)
    plt.ylabel('z')
    plt.savefig(Stats.figpath + ρ_0_name + '.png')
    plt.close()

    plt.plot(α_0.values, grid.z)
    plt.title(α_0_name + ' vs z')
    plt.xlabel(α_0_name)
    plt.ylabel('z')
    plt.savefig(Stats.figpath + α_0_name + '.png')
    plt.close()

    p_0.export_data(grid, Stats.outpath + p_0_name + '.dat')
    ρ_0.export_data(grid, Stats.outpath + ρ_0_name + '.dat')
    α_0.export_data(grid, Stats.outpath + α_0_name + '.dat')

    k_1 = grid.boundary(Zmin())
    k_2 = grid.boundary(Zmax())

    Stats.add_reference_profile(p_0_name)
    Stats.write_reference_profile(p_0_name, α_0[k_1:k_2])
    Stats.add_reference_profile(ρ_0_name)
    Stats.write_reference_profile(ρ_0_name, p_0[k_1:k_2])
    Stats.add_reference_profile(α_0_name)
    Stats.write_reference_profile(α_0_name, ρ_0[k_1:k_2])
    return