_id
stringlengths
2
7
title
stringlengths
1
88
partition
stringclasses
3 values
text
stringlengths
31
13.1k
language
stringclasses
1 value
meta_information
dict
q14600
set_event_mgr
train
def set_event_mgr(dinfo, event_mgr, context=None): """Wrapper for openjpeg library function opj_set_event_mgr. """ argtypes = [ctypes.POINTER(CommonStructType), ctypes.POINTER(EventMgrType), ctypes.c_void_p]
python
{ "resource": "" }
q14601
check_load
train
def check_load(grid, mode): """ Checks for over-loading of branches and transformers for MV or LV grid. Parameters ---------- grid : GridDing0 Grid identifier. mode : str Kind of grid ('MV' or 'LV'). Returns ------- :obj:`dict` Dict of critical branches with max. relative overloading, and the following format:: { branch_1: rel_overloading_1, ..., branch_n: rel_overloading_n } :any:`list` of :obj:`GridDing0` List of critical transformers with the following format:: [trafo_1, ..., trafo_m] Notes ----- Lines'/cables' max. capacity (load case and feed-in case) are taken from [#]_. References ---------- .. [#] dena VNS See Also -------- ding0.flexopt.reinforce_measures.reinforce_branches_current : ding0.flexopt.reinforce_measures.reinforce_branches_voltage : """ crit_branches = {} crit_stations = [] if mode == 'MV': # load load factors (conditions) for cables, lines and trafos for load- and feedin case # load_factor_mv_trans_lc_normal = float(cfg_ding0.get('assumptions', # 'load_factor_mv_trans_lc_normal')) load_factor_mv_line_lc_normal = float(cfg_ding0.get('assumptions', 'load_factor_mv_line_lc_normal')) load_factor_mv_cable_lc_normal = float(cfg_ding0.get('assumptions', 'load_factor_mv_cable_lc_normal')) #load_factor_mv_trans_fc_normal = float(cfg_ding0.get('assumptions', # 'load_factor_mv_trans_fc_normal')) load_factor_mv_line_fc_normal = float(cfg_ding0.get('assumptions',
python
{ "resource": "" }
q14602
check_voltage
train
def check_voltage(grid, mode): """ Checks for voltage stability issues at all nodes for MV or LV grid Parameters ---------- grid : GridDing0 Grid identifier. mode : str Kind of grid ('MV' or 'LV'). Returns ------- :any:`list` of :any:`GridDing0` List of critical nodes, sorted descending by voltage difference. Notes ----- The examination is done in two steps, according to [#]_ : 1. It is checked #TODO: what? 2. #TODO: what's next? References ---------- .. [#] dena VNS """ crit_nodes = {} if mode == 'MV': # load max. voltage difference for load and feedin case mv_max_v_level_lc_diff_normal = float(cfg_ding0.get('mv_routing_tech_constraints', 'mv_max_v_level_lc_diff_normal')) mv_max_v_level_fc_diff_normal = float(cfg_ding0.get('mv_routing_tech_constraints', 'mv_max_v_level_fc_diff_normal')) # check nodes' voltages
python
{ "resource": "" }
q14603
get_critical_line_loading
train
def get_critical_line_loading(grid): """ Assign line loading to each branch determined by peak load and peak generation of descendant branches The attribute `s_res` is a list of two elements 1. apparent power in load case 2. apparent power in feed-in case Parameters ---------- grid : ding0.core.network.grids.LVGridDing0 Ding0 LV grid object Returns ------- :any:`list` List of critical branches incl. its line loading :any:`list` List of critical stations incl. its transformer loading """ cos_phi_load = cfg_ding0.get('assumptions', 'cos_phi_load') cos_phi_feedin = cfg_ding0.get('assumptions', 'cos_phi_gen') lf_trafo_load = cfg_ding0.get('assumptions', "load_factor_lv_trans_lc_normal") lf_trafo_gen = cfg_ding0.get('assumptions', "load_factor_lv_trans_fc_normal") critical_branches = [] critical_stations = [] # Convert grid to a tree (is a directed graph) # based on this tree, descendants of each node are accessible station = grid._station tree = nx.dfs_tree(grid._graph, station) for node in tree.nodes(): # list of descendant nodes including the node itself descendants = list(nx.descendants(tree, node)) descendants.append(node) if isinstance(node, LVStationDing0): # determine cumulative peak load at node and assign to branch peak_load, peak_gen = peak_load_generation_at_node(descendants) if grid.id_db == 61107: if isinstance(node, LVStationDing0): print(node) # get trafos cumulative apparent power s_max_trafos = sum([_.s_max_a for _ in node._transformers]) # compare with load and generation connected to if (((peak_load / cos_phi_load) > s_max_trafos * lf_trafo_load) or ((peak_gen / cos_phi_feedin) > s_max_trafos * lf_trafo_gen)): critical_stations.append( {'station': node, 's_max': [
python
{ "resource": "" }
q14604
peak_load_generation_at_node
train
def peak_load_generation_at_node(nodes): """ Get maximum occuring load and generation at a certain node Summarizes peak loads and nominal generation power of descendant nodes of a branch Parameters ---------- nodes : :any:`list` Any LV grid Ding0 node object that is part of the grid topology Return ------ :any:`float` peak_load : Sum of peak loads of descendant nodes :any:`float` peak_generation : Sum of nominal power of generation at descendant nodes """ loads = [node.peak_load for node in nodes
python
{ "resource": "" }
q14605
voltage_delta_stub
train
def voltage_delta_stub(grid, tree, main_branch_node, stub_node, r_preceeding, x_preceedig): """ Determine voltage for stub branches Parameters ---------- grid : LVGridDing0 Ding0 grid object tree : :networkx:`NetworkX Graph Obj< >` Tree of grid topology main_branch_node : graph node Node of main branch that stub branch node in connected to main_branch : dict Nodes of main branch r_preceeding : float Resitance of preceeding grid x_preceeding : float Reactance of preceeding grid Return ------ :any:`float` Delta voltage for node """ cos_phi_load = cfg_ding0.get('assumptions', 'cos_phi_load') cos_phi_feedin = cfg_ding0.get('assumptions', 'cos_phi_gen') v_nom = cfg_ding0.get('assumptions', 'lv_nominal_voltage') omega = 2 * math.pi * 50 stub_branch = [_ for _ in grid.graph_branches_from_node(main_branch_node) if _[0] == stub_node][0][1] r_stub = stub_branch['branch'].type['R'] * stub_branch[ 'branch'].length / 1e3 x_stub = stub_branch['branch'].type['L'] / 1e3 * omega * \ stub_branch['branch'].length / 1e3 s_max_gen = [_.capacity / cos_phi_feedin for _ in tree.successors(stub_node) if isinstance(_, GeneratorDing0)]
python
{ "resource": "" }
q14606
get_voltage_at_bus_bar
train
def get_voltage_at_bus_bar(grid, tree): """ Determine voltage level at bus bar of MV-LV substation Parameters ---------- grid : LVGridDing0 Ding0 grid object tree : :networkx:`NetworkX Graph Obj< >` Tree of grid topology: Returns ------- :any:`list` Voltage at bus bar. First item refers to load case, second item refers to voltage in feedin (generation) case """ # voltage at substation bus bar r_mv_grid, x_mv_grid = get_mv_impedance(grid) r_trafo = sum([tr.r for tr in grid._station._transformers]) x_trafo = sum([tr.x for tr in grid._station._transformers]) cos_phi_load = cfg_ding0.get('assumptions', 'cos_phi_load') cos_phi_feedin = cfg_ding0.get('assumptions', 'cos_phi_gen') v_nom = cfg_ding0.get('assumptions', 'lv_nominal_voltage') # loads and generators connected to bus bar bus_bar_load = sum( [node.peak_load for node in tree.successors(grid._station) if isinstance(node, LVLoadDing0)]) / cos_phi_load bus_bar_generation = sum( [node.capacity for node in tree.successors(grid._station) if isinstance(node, GeneratorDing0)]) / cos_phi_feedin v_delta_load_case_bus_bar = voltage_delta_vde(v_nom,
python
{ "resource": "" }
q14607
LocalSearchSolver.operator_cross
train
def operator_cross(self, graph, solution, op_diff_round_digits): # TODO: check docstring """applies Cross inter-route operator to solution Takes every node from every route and calculates savings when inserted into all possible positions in other routes. Insertion is done at position with max. saving and procedure starts over again with newly created graph as input. Stops when no improvement is found. Args ---- graph: :networkx:`NetworkX Graph Obj< >` Descr solution: BaseSolution Descr op_diff_round_digits: float Precision (floating point digits) for rounding route length differences. *Details*: In some cases when an exchange is performed on two routes with one node each, the difference between the both solutions (before and after the exchange) is not zero. This is due to internal rounding errors of float type. So the loop won't break (alternating between these two solutions), we need an additional criterion to avoid this behaviour: A threshold to handle values very close to zero as if they
python
{ "resource": "" }
q14608
LocalSearchSolver.solve
train
def solve(self, graph, savings_solution, timeout, debug=False, anim=None): """Improve initial savings solution using local search Parameters ---------- graph: :networkx:`NetworkX Graph Obj< >` Graph instance savings_solution: SavingsSolution initial solution of CVRP problem (instance of `SavingsSolution` class) timeout: int max processing time in seconds debug: bool, defaults to False If True, information is printed while routing anim: AnimationDing0 AnimationDing0 object Returns ------- LocalSearchSolution A solution (LocalSearchSolution class) """ # TODO: If necessary, use timeout to set max processing time of local search # load threshold for operator (see exchange or relocate operator's description for more information) op_diff_round_digits = int(cfg_ding0.get('mv_routing', 'operator_diff_round_digits')) solution = LocalSearchSolution(graph, savings_solution) # FOR BENCHMARKING OF OPERATOR'S ORDER: #self.benchmark_operator_order(graph, savings_solution, op_diff_round_digits) for run in range(10): start = time.time() solution = self.operator_exchange(graph, solution, op_diff_round_digits, anim) time1 = time.time() if debug: logger.debug('Elapsed time (exchange, run {1}): {0}, '
python
{ "resource": "" }
q14609
get_branches
train
def get_branches(grid): """ Individual graphs of sectoral loads :param geid: :return: """ station = grid._station tree = nx.dfs_tree(grid._graph, station) # TODO: idea # 1. build tree from lv_grid station as root -> diretions should point to # descending leafs # 2. for analysis of current issues get list of descendants with # nx.descendants(tree, station). Sum peak load / gen capacity # 3. Extract nodes belonging to main route of a branch by checking all # successors if these are LVCalbleDistributors # notes and hints: # 1. associated edges can be accessed via grid._graph.in_edges(<node>) # respectively grid._graph.out_edges(<node>) # 2. when using nx.descendants(tree, station) make sure the order of nodes # is maintained as this is important to properly assess voltage and over- # loading issues
python
{ "resource": "" }
q14610
NetworkDing0.add_mv_grid_district
train
def add_mv_grid_district(self, mv_grid_district): """Adds a MV grid_district to _mv_grid_districts if not already existing""" # TODO: use setter method here (make attribute '_mv_grid_districts' private)
python
{ "resource": "" }
q14611
NetworkDing0.get_mvgd_lvla_lvgd_obj_from_id
train
def get_mvgd_lvla_lvgd_obj_from_id(self): """ Build dict with mapping from LVLoadAreaDing0 id to LVLoadAreaDing0 object, MVGridDistrictDing0 id to MVGridDistrictDing0 object, LVGridDistrictDing0 id to LVGridDistrictDing0 object and LVStationDing0 id to LVStationDing0 object Returns ------- :obj:`dict` mv_grid_districts_dict:: { mv_grid_district_id_1: mv_grid_district_obj_1, ..., mv_grid_district_id_n: mv_grid_district_obj_n } :obj:`dict` lv_load_areas_dict:: { lv_load_area_id_1: lv_load_area_obj_1, ..., lv_load_area_id_n: lv_load_area_obj_n } :obj:`dict` lv_grid_districts_dict:: { lv_grid_district_id_1: lv_grid_district_obj_1, ..., lv_grid_district_id_n: lv_grid_district_obj_n } :obj:`dict` lv_stations_dict:: { lv_station_id_1: lv_station_obj_1, ..., lv_station_id_n: lv_station_obj_n } """
python
{ "resource": "" }
q14612
NetworkDing0.build_mv_grid_district
train
def build_mv_grid_district(self, poly_id, subst_id, grid_district_geo_data, station_geo_data): """Initiates single MV grid_district including station and grid Parameters ---------- poly_id: int ID of grid_district according to database table. Also used as ID for created grid #TODO: check type subst_id: int ID of station according to database table #TODO: check type grid_district_geo_data: :shapely:`Shapely Polygon object<polygons>` Polygon of grid district station_geo_data: :shapely:`Shapely Point object<points>` Point of station Returns ------- :shapely:`Shapely Polygon object<polygons>` Description of return #TODO: check """ mv_station = MVStationDing0(id_db=subst_id, geo_data=station_geo_data) mv_grid = MVGridDing0(network=self,
python
{ "resource": "" }
q14613
NetworkDing0.build_lv_grid_district
train
def build_lv_grid_district(self, lv_load_area, lv_grid_districts, lv_stations): """Instantiates and associates lv_grid_district incl grid and station. The instantiation creates more or less empty objects including relevant data for transformer choice and grid creation Parameters ---------- lv_load_area: :shapely:`Shapely Polygon object<polygons>` load_area object lv_grid_districts: :pandas:`pandas.DataFrame<dataframe>` Table containing lv_grid_districts of according load_area lv_stations : :pandas:`pandas.DataFrame<dataframe>` Table containing lv_stations of according load_area """ # There's no LVGD for current LA # -> TEMP WORKAROUND: Create single LVGD from LA, replace unknown valuess by zero # TODO: Fix #155 (see also: data_processing #68) if len(lv_grid_districts) == 0: # raise ValueError( # 'Load Area {} has no LVGD - please re-open #155'.format( # repr(lv_load_area))) geom = wkt_dumps(lv_load_area.geo_area) lv_grid_districts = \ lv_grid_districts.append( pd.DataFrame( {'la_id': [lv_load_area.id_db], 'geom': [geom], 'population': [0], 'peak_load_residential': [lv_load_area.peak_load_residential], 'peak_load_retail': [lv_load_area.peak_load_retail], 'peak_load_industrial': [lv_load_area.peak_load_industrial], 'peak_load_agricultural': [lv_load_area.peak_load_agricultural], 'sector_count_residential': [0], 'sector_count_retail': [0], 'sector_count_industrial': [0], 'sector_count_agricultural': [0], 'sector_consumption_residential': [0],
python
{ "resource": "" }
q14614
NetworkDing0.import_mv_grid_districts
train
def import_mv_grid_districts(self, session, mv_grid_districts_no=None): """ Imports MV Grid Districts, HV-MV stations, Load Areas, LV Grid Districts and MV-LV stations, instantiates and initiates objects. Parameters ---------- session : sqlalchemy.orm.session.Session Database session mv_grid_districts : List of MV grid_districts/stations (int) to be imported (if empty, all grid_districts & stations are imported) See Also -------- build_mv_grid_district : used to instantiate MV grid_district objects import_lv_load_areas : used to import load_areas for every single MV grid_district ding0.core.structure.regions.MVGridDistrictDing0.add_peak_demand : used to summarize peak loads of underlying load_areas """ # check arguments if not all(isinstance(_, int) for _ in mv_grid_districts_no): raise TypeError('`mv_grid_districts` has to be a list of integers.') # get srid settings from config try: srid = str(int(cfg_ding0.get('geo', 'srid'))) except OSError: logger.exception('cannot open config file.') # build SQL query grid_districts = session.query(self.orm['orm_mv_grid_districts'].subst_id, func.ST_AsText(func.ST_Transform( self.orm['orm_mv_grid_districts'].geom, srid)). \ label('poly_geom'), func.ST_AsText(func.ST_Transform( self.orm['orm_mv_stations'].point, srid)). \ label('subs_geom')).\ join(self.orm['orm_mv_stations'], self.orm['orm_mv_grid_districts'].subst_id == self.orm['orm_mv_stations'].subst_id).\ filter(self.orm['orm_mv_grid_districts'].subst_id.in_(mv_grid_districts_no)). \ filter(self.orm['version_condition_mvgd']). \ filter(self.orm['version_condition_mv_stations']). \ distinct() # read MV data from db mv_data = pd.read_sql_query(grid_districts.statement, session.bind, index_col='subst_id') # iterate over grid_district/station datasets and initiate objects
python
{ "resource": "" }
q14615
NetworkDing0.import_config
train
def import_config(self): """ Loads parameters from config files Returns ------- int config object #TODO check type """ # load parameters from configs cfg_ding0.load_config('config_db_tables.cfg')
python
{ "resource": "" }
q14616
NetworkDing0.import_pf_config
train
def import_pf_config(self): """ Creates power flow config class and imports config from file Returns ------- PFConfigDing0 PFConfigDing0 object """ scenario = cfg_ding0.get("powerflow", "test_grid_stability_scenario") start_hour = int(cfg_ding0.get("powerflow", "start_hour")) end_hour = int(cfg_ding0.get("powerflow", "end_hour")) start_time = datetime(1970, 1, 1, 00, 00, 0)
python
{ "resource": "" }
q14617
NetworkDing0.mv_routing
train
def mv_routing(self, debug=False, animation=False): """ Performs routing on all MV grids. Parameters ---------- debug: bool, default to False If True, information is printed while routing animation: bool, default to False If True, images of route modification steps are exported during routing process. A new animation object is created. See Also -------- ding0.core.network.grids.MVGridDing0.routing : for details on MVGridDing0 objects routing ding0.tools.animation.AnimationDing0 : for details on animation function.
python
{ "resource": "" }
q14618
NetworkDing0.build_lv_grids
train
def build_lv_grids(self): """ Builds LV grids for every non-aggregated LA in every MV grid district using model grids. """ for mv_grid_district in self.mv_grid_districts(): for load_area in mv_grid_district.lv_load_areas(): if not load_area.is_aggregated: for lv_grid_district in load_area.lv_grid_districts(): lv_grid_district.lv_grid.build_grid()
python
{ "resource": "" }
q14619
NetworkDing0.control_circuit_breakers
train
def control_circuit_breakers(self, mode=None): """ Opens or closes all circuit breakers of all MV grids. Args ---- mode: str Set mode='open' to open, mode='close' to close """ for grid_district in self.mv_grid_districts(): if mode == 'open': grid_district.mv_grid.open_circuit_breakers() elif mode == 'close': grid_district.mv_grid.close_circuit_breakers() else:
python
{ "resource": "" }
q14620
NetworkDing0.metadata
train
def metadata(self, run_id=None): """Provide metadata on a Ding0 run Parameters ---------- run_id: str, (defaults to current date) Distinguish multiple versions of Ding0 data by a `run_id`. If not set it defaults to current date in the format YYYYMMDDhhmmss Returns ------- dict Metadata """ # Get latest version and/or git commit hash try: version = subprocess.check_output( ["git", "describe", "--tags", "--always"]).decode('utf8') except: version = None # Collect names of database table used to run Ding0 and data version if self.config['input_data_source']['input_data'] == 'versioned': data_version = self.config['versioned']['version'] database_tables = self.config['versioned'] elif self.config['input_data_source']['input_data'] == 'model_draft': data_version = 'model_draft' database_tables = self.config['model_draft'] else: data_version = 'unknown' database_tables = 'unknown' # Collect assumptions assumptions = {} assumptions.update(self.config['assumptions']) assumptions.update(self.config['mv_connect']) assumptions.update(self.config['mv_routing'])
python
{ "resource": "" }
q14621
create_dir
train
def create_dir(dirpath): """Create directory and report about it Parameters ---------- dirpath : str Directory including path """ if not os.path.isdir(dirpath):
python
{ "resource": "" }
q14622
get_default_home_dir
train
def get_default_home_dir(): """ Return default home directory of Ding0 Returns ------- :any:`str` Default home directory including its path """ ding0_dir = str(cfg_ding0.get('config',
python
{ "resource": "" }
q14623
SavingsSolution.is_complete
train
def is_complete(self): """Returns True if this is a complete solution, i.e, all nodes are allocated Todo ---- TO BE REVIEWED Returns ------- bool True if this is a complete solution. """ allocated = all( [node.route_allocation() is not None
python
{ "resource": "" }
q14624
SavingsSolution.process
train
def process(self, pair): # TODO: check docstring """Processes a pair of nodes into the current solution MUST CREATE A NEW INSTANCE, NOT CHANGE ANY INSTANCE ATTRIBUTES Returns a new instance (deep copy) of self object Args ---- pair : type description Returns ------- type Description (Copy of self?) """ a, b = pair new_solution = self.clone() i, j = new_solution.get_pair((a, b)) route_i = i.route_allocation() route_j = j.route_allocation() inserted = False if ((route_i is not None and route_j is not None) and (route_i != route_j)): if route_i._nodes.index(i) == 0 and route_j._nodes.index(j) == len(route_j._nodes) - 1: if route_j.can_allocate(route_i._nodes): route_j.allocate(route_i._nodes) if i.route_allocation() != j.route_allocation(): raise Exception('wtf')
python
{ "resource": "" }
q14625
SavingsSolution.can_process
train
def can_process(self, pairs): """Returns True if this solution can process `pairs` Parameters ---------- pairs: :any:`list` of pairs of Route List of pairs Returns ------- bool True if this solution can process `pairs`. """ i, j = pairs # Neither points are in a route if i.route_allocation() is None
python
{ "resource": "" }
q14626
ClarkeWrightSolver.compute_savings_list
train
def compute_savings_list(self, graph): """Compute Clarke and Wright savings list A saving list is a matrix containing the saving amount S between i and j S is calculated by S = d(0,i) + d(0,j) - d(i,j) (CLARKE; WRIGHT, 1964) Args ---- graph: :networkx:`NetworkX Graph Obj< >` A NetworkX graaph is used. Returns ------- :any:`list` of `Node` List of nodes sorted by its savings """ savings_list = {} for i, j in graph.edges(): # t = (i, j) if repr(i) < repr(j): t = (i, j) else: t = (j, i)
python
{ "resource": "" }
q14627
ClarkeWrightSolver.solve
train
def solve(self, graph, timeout, debug=False, anim=None): """Solves the CVRP problem using Clarke and Wright Savings methods Parameters ---------- graph: :networkx:`NetworkX Graph Obj< >` A NetworkX graaph is used. timeout: int max processing time in seconds debug: bool, defaults to False If True, information is printed while routing anim: AnimationDing0 Returns
python
{ "resource": "" }
q14628
export_to_dir
train
def export_to_dir(network, export_dir): """ Exports PyPSA network as CSV files to directory Args: network: pypsa.Network export_dir: str Sub-directory in output/debug/grid/ where csv Files of PyPSA network are exported to.
python
{ "resource": "" }
q14629
edges_to_dict_of_dataframes
train
def edges_to_dict_of_dataframes(grid, edges): """ Export edges to DataFrame Parameters ---------- grid: ding0.Network edges: list Edges of Ding0.Network graph Returns ------- edges_dict: dict """ omega = 2 * pi * 50 srid = int(cfg_ding0.get('geo', 'srid')) lines = {'line_id': [], 'bus0': [], 'bus1': [], 'x': [], 'r': [], 's_nom': [], 'length': [], 'cables': [], 'geom': [], 'grid_id': []} # iterate over edges and add them one by one for edge in edges: line_name = '_'.join(['MV', str(grid.id_db), 'lin', str(edge['branch'].id_db)]) # TODO: find the real cause for being L, C, I_th_max type of Series if (isinstance(edge['branch'].type['L'], Series) or isinstance(edge['branch'].type['C'], Series)): x = omega * edge['branch'].type['L'].values[0] * 1e-3 else: x = omega * edge['branch'].type['L'] * 1e-3 if isinstance(edge['branch'].type['R'], Series): r = edge['branch'].type['R'].values[0] else: r = edge['branch'].type['R'] if (isinstance(edge['branch'].type['I_max_th'], Series) or isinstance(edge['branch'].type['U_n'], Series)): s_nom = sqrt(3) *
python
{ "resource": "" }
q14630
run_powerflow_onthefly
train
def run_powerflow_onthefly(components, components_data, grid, export_pypsa_dir=None, debug=False): """ Run powerflow to test grid stability Two cases are defined to be tested here: i) load case ii) feed-in case Parameters ---------- components: dict of pandas.DataFrame components_data: dict of pandas.DataFrame export_pypsa_dir: str Sub-directory in output/debug/grid/ where csv Files of PyPSA network are exported to. Export is omitted if argument is empty. """ scenario = cfg_ding0.get("powerflow", "test_grid_stability_scenario") start_hour = cfg_ding0.get("powerflow", "start_hour") end_hour = cfg_ding0.get("powerflow", "end_hour") # choose temp_id temp_id_set = 1 timesteps = 2 start_time = datetime(1970, 1, 1, 00, 00, 0) resolution = 'H' # inspect grid data for integrity if debug: data_integrity(components, components_data) # define investigated time range timerange = DatetimeIndex(freq=resolution, periods=timesteps, start=start_time) # TODO: Instead of hard coding PF config, values from class PFConfigDing0 can be used here. # create PyPSA powerflow problem network, snapshots = create_powerflow_problem(timerange, components) # import pq-sets for key in ['Load', 'Generator']: for attr in ['p_set', 'q_set']:
python
{ "resource": "" }
q14631
data_integrity
train
def data_integrity(components, components_data): """ Check grid data for integrity Parameters ---------- components: dict Grid components components_data: dict Grid component data (such as p,q and v set points) Returns ------- """ data_check = {} for comp in ['Bus', 'Load']: # list(components_data.keys()): data_check[comp] = {} data_check[comp]['length_diff'] = len(components[comp]) - len(
python
{ "resource": "" }
q14632
parallel_run
train
def parallel_run(districts_list, n_of_processes, n_of_districts, run_id, base_path=None): '''Organize parallel runs of ding0. The function take all districts in a list and divide them into n_of_processes parallel processes. For each process, the assigned districts are given to the function process_runs() with the argument n_of_districts Parameters ---------- districts_list: list of int List with all districts to be run. n_of_processes: int Number of processes to run in parallel n_of_districts: int Number of districts to be run in each cluster given as argument to process_stats() run_id: str Identifier for a run of Ding0. For example it is used to create a subdirectory of os.path.join(`base_path`, 'results') base_path : str Base path for ding0 data (input, results and logs). Default is `None` which sets it to :code:`~/.ding0` (may deviate on windows systems). Specify your own but keep in mind that it a required a particular structure of subdirectories. See Also -------- ding0_runs ''' # define base path if base_path is None: base_path = BASEPATH if not os.path.exists(os.path.join(base_path, run_id)): os.makedirs(os.path.join(base_path, run_id)) start = time.time() ####################################################################### # Define an output queue output_info = mp.Queue() ####################################################################### # Setup a list of processes that we want to run max_dist = len(districts_list) threat_long = floor(max_dist / n_of_processes) if threat_long == 0:
python
{ "resource": "" }
q14633
process_metadata
train
def process_metadata(meta): """ Merge metadata of run on multiple grid districts Parameters ---------- meta: list of dict Metadata of run of each MV grid district Returns ------- dict Single metadata dict including merge metadata """ mvgds = [] metadata = meta[0] for mvgd in meta:
python
{ "resource": "" }
q14634
reinforce_grid
train
def reinforce_grid(grid, mode): #TODO: finish docstring """ Evaluates grid reinforcement needs and performs measures Grid reinforcement according to methods described in [VNSRP]_ supplemented by [DENA]_. Parameters ---------- grid: GridDing0 Grid instance mode: str Choose of: 'MV' or 'LV' Notes ----- Currently only MV branch reinforcement is implemented. HV-MV stations are not reinforced since not required for status-quo scenario. References ---------- .. [DENA] Deutsche Energie-Agentur GmbH (dena), "dena-Verteilnetzstudie. Ausbau- und Innovationsbedarf der Stromverteilnetze in Deutschland bis 2030.", 2012 .. [VNSRP] Ackermann, T., Untsch, S., Koch, M., & Rothfuchs, H. (2014). Verteilnetzstudie Rheinland-Pfalz. Hg. v. Ministerium für Wirtschaft, Klimaschutz, Energie und Landesplanung Rheinland-Pfalz (MWKEL). energynautics GmbH. """ # kind of grid to be evaluated (MV or LV) if mode == 'MV': crit_branches, crit_stations = check_load(grid, mode) # STEP 1: reinforce branches # do reinforcement reinforce_branches_current(grid, crit_branches) # if branches or stations have been reinforced: run PF again to check for voltage issues if crit_branches or crit_stations: grid.network.run_powerflow(conn=None, method='onthefly') crit_nodes = check_voltage(grid, mode) crit_nodes_count_prev_step = len(crit_nodes) # as long as there are voltage issues, do reinforcement while crit_nodes: # determine all branches on the way from HV-MV substation to crit. nodes crit_branches_v = grid.find_and_union_paths(grid.station(), crit_nodes) # do reinforcement reinforce_branches_voltage(grid, crit_branches_v) # run PF grid.network.run_powerflow(session=None, method='onthefly') crit_nodes = check_voltage(grid, mode) # if there are critical nodes left but no larger cable available, stop reinforcement if len(crit_nodes) == crit_nodes_count_prev_step: logger.warning('==> There are {0} branches that cannot be '
python
{ "resource": "" }
q14635
example_stats
train
def example_stats(filename): """ Obtain statistics from create grid topology Prints some statistical numbers and produces exemplary figures """ nd = results.load_nd_from_pickle(filename=filename) nodes_df, edges_df = nd.to_dataframe() # get statistical numbers about grid stats = results.calculate_mvgd_stats(nd) # plot distribution of load/generation of subjacent LV grids stations = nodes_df[nodes_df['type'] == 'LV Station'] f, axarr = plt.subplots(2, sharex=True) f.suptitle("Peak load (top) / peak generation capacity (bottom) at LV " "substations in kW") stations['peak_load'].hist(bins=20, alpha=0.5, ax=axarr[0]) axarr[0].set_title("Peak load in kW") stations['generation_capacity'].hist(bins=20, alpha=0.5, ax=axarr[1]) axarr[1].set_title("Peak generation capacity in kW") plt.show() # Introduction of report
python
{ "resource": "" }
q14636
MVStationDing0.peak_generation
train
def peak_generation(self, mode): """Calculates cumulative peak generation of generators connected to underlying grids This is done instantaneously using bottom-up approach. Parameters ---------- mode: str determines which generators are included:: 'MV': Only generation capacities of MV level are considered. 'MVLV': Generation capacities of MV and LV are considered (= cumulative generation capacities in entire MVGD).
python
{ "resource": "" }
q14637
MVStationDing0.set_operation_voltage_level
train
def set_operation_voltage_level(self): """Set operation voltage level """ mv_station_v_level_operation = float(cfg_ding0.get('mv_routing_tech_constraints',
python
{ "resource": "" }
q14638
MVStationDing0.select_transformers
train
def select_transformers(self): """ Selects appropriate transformers for the HV-MV substation. The transformers are chosen according to max. of load case and feedin-case considering load factors. The HV-MV transformer with the next higher available nominal apparent power is chosen. If one trafo is not sufficient, multiple trafos are used. Additionally, in a second step an redundant trafo is installed with max. capacity of the selected trafos of the first step according to general planning principles for MV distribution grids (n-1). Parameters ---------- transformers : dict Contains technical information of p hv/mv transformers **kwargs : dict Should contain a value behind the key 'peak_load' Notes ----- Parametrization of transformers bases on [#]_. Potential hv-mv-transformers are chosen according to [#]_. References ---------- .. [#] Deutsche Energie-Agentur GmbH (dena), "dena-Verteilnetzstudie. Ausbau- und Innovationsbedarf der Stromverteilnetze in Deutschland bis 2030.", 2012 .. [#] X. Tao, "Automatisierte Grundsatzplanung von Mittelspannungsnetzen", Dissertation, 2006 """ # get power factor for loads and generators cos_phi_load = cfg_ding0.get('assumptions', 'cos_phi_load') cos_phi_feedin = cfg_ding0.get('assumptions', 'cos_phi_gen') # get trafo load factors load_factor_mv_trans_lc_normal = float(cfg_ding0.get('assumptions', 'load_factor_mv_trans_lc_normal')) load_factor_mv_trans_fc_normal = float(cfg_ding0.get('assumptions', 'load_factor_mv_trans_fc_normal')) # get equipment parameters of MV transformers trafo_parameters = self.grid.network.static_data['MV_trafos'] # get peak load and peak generation cum_peak_load = self.peak_load / cos_phi_load cum_peak_generation = self.peak_generation(mode='MVLV') / cos_phi_feedin # check if load or generation is greater respecting corresponding load factor if (cum_peak_load / load_factor_mv_trans_lc_normal) > \ (cum_peak_generation / load_factor_mv_trans_fc_normal): # use peak load and load factor from load case load_factor_mv_trans = load_factor_mv_trans_lc_normal residual_apparent_power = cum_peak_load else: # use peak generation and load factor for feedin case load_factor_mv_trans = load_factor_mv_trans_fc_normal residual_apparent_power = cum_peak_generation # determine number and size of required transformers # get max. trafo transformer_max = trafo_parameters.iloc[trafo_parameters['S_nom'].idxmax()] while residual_apparent_power > 0:
python
{ "resource": "" }
q14639
create_results_dirs
train
def create_results_dirs(base_path): """Create base path dir and subdirectories Parameters ---------- base_path : str The base path has subdirectories for raw and processed results """ if not os.path.exists(base_path): print("Creating directory {} for results data.".format(base_path)) os.mkdir(base_path) if not os.path.exists(os.path.join(base_path, 'results')): os.mkdir(os.path.join(base_path, 'results')) if not os.path.exists(os.path.join(base_path, 'plots')):
python
{ "resource": "" }
q14640
run_multiple_grid_districts
train
def run_multiple_grid_districts(mv_grid_districts, run_id, failsafe=False, base_path=None): """ Perform ding0 run on given grid districts Parameters ---------- mv_grid_districs : list Integers describing grid districts run_id: str Identifier for a run of Ding0. For example it is used to create a subdirectory of os.path.join(`base_path`, 'results') failsafe : bool Setting to True enables failsafe mode where corrupt grid districts (mostly due to data issues) are reported and skipped. Report is to be found in the log dir under :code:`~/.ding0` . Default is False. base_path : str Base path for ding0 data (input, results and logs). Default is `None` which sets it to :code:`~/.ding0` (may deviate on windows systems). Specify your own but keep in mind that it a required a particular structure of subdirectories. Returns ------- msg : str Traceback of error computing corrupt MV grid district .. TODO: this is only true if try-except environment is moved into this fundion and traceback return is implemented Notes ----- Consider that a large amount of MV grid districts may take hours or up to days to compute. A computational run for a single grid district may consume around 30 secs. """ start = time.time() # define base path if base_path is None: base_path = BASEPATH # database connection/ session engine = db.connection(section='oedb') session = sessionmaker(bind=engine)() corrupt_grid_districts = pd.DataFrame(columns=['id', 'message']) for mvgd in mv_grid_districts: # instantiate ding0 network object nd = NetworkDing0(name='network', run_id=run_id) if not os.path.exists(os.path.join(base_path, "grids")): os.mkdir(os.path.join(base_path, "grids")) if not failsafe: # run DING0 on selected MV Grid District msg = nd.run_ding0(session=session, mv_grid_districts_no=[mvgd]) # save results results.save_nd_to_pickle(nd, os.path.join(base_path, "grids")) else: # try to perform ding0 run on grid district try: msg = nd.run_ding0(session=session, mv_grid_districts_no=[mvgd])
python
{ "resource": "" }
q14641
_parse_nodes_section
train
def _parse_nodes_section(f, current_section, nodes): """Parse TSPLIB NODE_COORD_SECTION or DEMAND_SECTION from file descript f Returns a dict containing the node as key """ section = {} dimensions = None if current_section == 'NODE_COORD_SECTION': dimensions = 3 # i: (i, j) elif current_section == 'DEMAND_SECTION': dimensions = 2 # i: q else: raise ParseException('Invalid section {}'.format(current_section)) n = 0 for line in f: line = strip(line) # Check dimensions definitions = re.split(r'\s*', line) if len(definitions) != dimensions: raise ParseException('Invalid dimensions
python
{ "resource": "" }
q14642
_parse_edge_weight
train
def _parse_edge_weight(f, nodes): """Parse TSPLIB EDGE_WEIGHT_SECTION from file f Supports only FULL_MATRIX for now """ matrix = [] n = 0 for line in f: line = strip(line) regex = re.compile(r'\s+') row = regex.split(line) matrix.append(row) n = n + 1 if n == nodes:
python
{ "resource": "" }
q14643
_post_process_specs
train
def _post_process_specs(specs): """Post-process specs after pure parsing Casts any number expected values into integers Args ---- specs : Notes -----
python
{ "resource": "" }
q14644
_create_node_matrix_from_coord_section
train
def _create_node_matrix_from_coord_section(specs): """Transformed parsed data from NODE_COORD_SECTION into an upper triangular matrix Calculates distances between nodes 'MATRIX' key added to `specs` """ distances = specs['NODE_COORD_SECTION'] specs['MATRIX'] = {} for i in distances:
python
{ "resource": "" }
q14645
_create_node_matrix_from_full_matrix
train
def _create_node_matrix_from_full_matrix(specs): """Transform parsed data from EDGE_WEIGHT_SECTION into an upper triangular matrix 'MATRIX' key added to `specs` """ old_matrix = specs['EDGE_WEIGHT_SECTION'] nodes = specs['DIMENSION'] specs['MATRIX'] = {} for i in range(nodes): specs['MATRIX'][i + 1] =
python
{ "resource": "" }
q14646
_parse_tsplib
train
def _parse_tsplib(f): """Parses a TSPLIB file descriptor and returns a dict containing the problem definition""" line = '' specs = {} used_specs = ['NAME', 'COMMENT', 'DIMENSION', 'CAPACITY', 'TYPE', 'EDGE_WEIGHT_TYPE'] used_data = ['DEMAND_SECTION', 'DEPOT_SECTION'] # Parse specs part for line in f: line = strip(line) # Arbitrary sort, so we test everything out s = None for s in used_specs: if line.startswith(s): specs[s] = line.split('{} :'.format(s))[-1].strip() # get value data part break if s == 'EDGE_WEIGHT_TYPE' and s in specs and specs[s] == 'EXPLICIT': used_specs.append('EDGE_WEIGHT_FORMAT') # All specs read if len(specs) == len(used_specs): break if len(specs) != len(used_specs): missing_specs = set(used_specs).symmetric_difference(set(specs)) raise ParseException('Error parsing TSPLIB data: specs {} missing'.format(missing_specs)) print(specs) if specs['EDGE_WEIGHT_TYPE'] == 'EUC_2D': used_data.append('NODE_COORD_SECTION') elif specs['EDGE_WEIGHT_FORMAT'] == 'FULL_MATRIX': used_data.append('EDGE_WEIGHT_SECTION') else: raise ParseException('EDGE_WEIGHT_TYPE or EDGE_WEIGHT_FORMAT not supported') _post_process_specs(specs) # Parse data part for line in f: line = strip(line) for d in used_data: if line.startswith(d):
python
{ "resource": "" }
q14647
MVGridDing0.add_circuit_breaker
train
def add_circuit_breaker(self, circ_breaker): """Creates circuit breaker object and ... Args ---- circ_breaker: CircuitBreakerDing0 Description #TODO """ if circ_breaker not in self._circuit_breakers and
python
{ "resource": "" }
q14648
MVGridDing0.add_station
train
def add_station(self, mv_station, force=False): """Adds MV station if not already existing Args ---- mv_station: MVStationDing0 Description #TODO force: bool If True, MV Station is set even though it's not empty (override) """
python
{ "resource": "" }
q14649
MVGridDing0.remove_cable_distributor
train
def remove_cable_distributor(self, cable_dist): """Removes a cable distributor from _cable_distributors if existing""" if cable_dist in self.cable_distributors() and isinstance(cable_dist, MVCableDistributorDing0):
python
{ "resource": "" }
q14650
MVGridDing0.add_ring
train
def add_ring(self, ring): """Adds a ring to _rings if not already existing""" if
python
{ "resource": "" }
q14651
MVGridDing0.rings_full_data
train
def rings_full_data(self): """ Returns a generator for iterating over each ring Yields ------ For each ring, tuple composed by ring ID, list of edges, list of nodes Notes ----- Circuit breakers must be closed to find rings, this is done automatically. """ #close circuit breakers for circ_breaker in self.circuit_breakers(): if not circ_breaker.status == 'closed': circ_breaker.close() logger.info('Circuit breakers were closed in order to find MV
python
{ "resource": "" }
q14652
MVGridDing0.routing
train
def routing(self, debug=False, anim=None): """ Performs routing on Load Area centres to build MV grid with ring topology. Args ---- debug: bool, defaults to False If True, information is printed while routing anim: type, defaults to None Descr #TODO """ # do the routing self._graph = mv_routing.solve(graph=self._graph, debug=debug, anim=anim) logger.info('==> MV Routing for {} done'.format(repr(self))) # connect satellites (step 1, with restrictions like max. string length, max peak load per string) self._graph = mv_connect.mv_connect_satellites(mv_grid=self,
python
{ "resource": "" }
q14653
MVGridDing0.set_voltage_level
train
def set_voltage_level(self, mode='distance'): """ Sets voltage level of MV grid according to load density of MV Grid District or max. distance between station and Load Area. Parameters ---------- mode: str method to determine voltage level * 'load_density': Decision on voltage level is determined by load density of the considered region. Urban areas (load density of >= 1 MW/km2 according to [#]_) usually got a voltage of 10 kV whereas rural areas mostly use 20 kV. * 'distance' (default): Decision on voltage level is determined by the max. distance between Grid District's HV-MV station and Load Areas (LA's centre is used). According to [#]_ a value of 1kV/kV can be assumed. The `voltage_per_km_threshold` defines the distance threshold for distinction. (default in config = (20km+10km)/2 = 15km) References ---------- .. [#] Falk Schaller et al., "Modellierung realitätsnaher zukünftiger Referenznetze im Verteilnetzsektor zur Überprüfung der Elektroenergiequalität", Internationaler ETG-Kongress Würzburg, 2011 .. [#] Klaus Heuck et al., "Elektrische Energieversorgung", Vieweg+Teubner, Wiesbaden, 2007 """ if mode == 'load_density': # get power factor for loads cos_phi_load = cfg_ding0.get('assumptions', 'cos_phi_load') # get load density load_density_threshold = float(cfg_ding0.get('assumptions', 'load_density_threshold')) # transform MVGD's area to epsg 3035 # to achieve correct area calculation projection = partial( pyproj.transform, pyproj.Proj(init='epsg:4326'), # source coordinate system pyproj.Proj(init='epsg:3035')) # destination coordinate system # calculate load density kw2mw = 1e-3 sqm2sqkm = 1e6 load_density = ((self.grid_district.peak_load * kw2mw / cos_phi_load) /
python
{ "resource": "" }
q14654
MVGridDing0.set_default_branch_type
train
def set_default_branch_type(self, debug=False): """ Determines default branch type according to grid district's peak load and standard equipment. Args ---- debug: bool, defaults to False If True, information is printed during process Returns ------- :pandas:`pandas.Series<series>` default branch type: pandas Series object. If no appropriate type is found, return largest possible one. :pandas:`pandas.Series<series>` default branch type max: pandas Series object. Largest available line/cable type Notes ----- Parameter values for cables and lines are taken from [#]_, [#]_ and [#]_. Lines are chosen to have 60 % load relative to their nominal capacity according to [#]_. Decision on usage of overhead lines vs. cables is determined by load density of the considered region. Urban areas usually are equipped with underground cables whereas rural areas often have overhead lines as MV distribution system [#]_. References ---------- .. [#] Klaus Heuck et al., "Elektrische Energieversorgung", Vieweg+Teubner, Wiesbaden, 2007 .. [#] René Flosdorff et al., "Elektrische Energieverteilung", Vieweg+Teubner, 2005 .. [#] Südkabel GmbH, "Einadrige VPE-isolierte Mittelspannungskabel", http://www.suedkabel.de/cms/upload/pdf/Garnituren/Einadrige_VPE-isolierte_Mittelspannungskabel.pdf, 2017 .. [#] Deutsche Energie-Agentur GmbH (dena), "dena-Verteilnetzstudie. Ausbau- und Innovationsbedarf der Stromverteilnetze in Deutschland bis 2030.", 2012 .. [#] Tao, X., "Automatisierte Grundsatzplanung von Mittelspannungsnetzen", Dissertation, RWTH Aachen, 2007 """ # decide whether cable or line is used (initially for entire grid) and set grid's attribute if self.v_level == 20: self.default_branch_kind = 'line' elif self.v_level == 10: self.default_branch_kind = 'cable' # get power factor for loads cos_phi_load = cfg_ding0.get('assumptions', 'cos_phi_load') # get max. count of half rings per MV grid district mv_half_ring_count_max = int(cfg_ding0.get('mv_routing_tech_constraints', 'mv_half_ring_count_max')) #mv_half_ring_count_max=20 # load cable/line assumptions, file_names and parameter if self.default_branch_kind == 'line': load_factor_normal = float(cfg_ding0.get('assumptions', 'load_factor_mv_line_lc_normal')) branch_parameters = self.network.static_data['MV_overhead_lines'] # load cables as well to use it within settlements branch_parameters_settle = self.network.static_data['MV_cables'] # select types with
python
{ "resource": "" }
q14655
MVGridDing0.set_nodes_aggregation_flag
train
def set_nodes_aggregation_flag(self, peak_current_branch_max): """ Set Load Areas with too high demand to aggregated type. Args ---- peak_current_branch_max: float Max. allowed current for line/cable """ for lv_load_area in self.grid_district.lv_load_areas(): peak_current_node = (lv_load_area.peak_load / (3**0.5) / self.v_level) # units: kVA / kV = A
python
{ "resource": "" }
q14656
MVGridDing0.export_to_pypsa
train
def export_to_pypsa(self, session, method='onthefly'): """Exports MVGridDing0 grid to PyPSA database tables Peculiarities of MV grids are implemented here. Derive general export method from this and adapt to needs of LVGridDing0 Parameters ---------- session: :sqlalchemy:`SQLAlchemy session object<orm/session_basics.html>` Description method: str Specify export method:: 'db': grid data will be exported to database 'onthefly': grid data will be passed to PyPSA directly (default) Notes ----- It has to be proven that this method works for LV grids as well! Ding0 treats two stationary case of powerflow: 1) Full load: We assume no generation and loads to be set to peak load 2) Generation worst case: """ # definitions for temp_resolution table temp_id = 1 timesteps = 2 start_time = datetime(1970, 1, 1, 00, 00, 0) resolution = 'H' nodes = self._graph.nodes() edges = [edge for edge in list(self.graph_edges()) if (edge['adj_nodes'][0] in nodes and not isinstance( edge['adj_nodes'][0], LVLoadAreaCentreDing0)) and (edge['adj_nodes'][1] in nodes and not isinstance( edge['adj_nodes'][1], LVLoadAreaCentreDing0))] if method == 'db': # Export node objects: Busses, Loads, Generators pypsa_io.export_nodes(self, session, nodes, temp_id,
python
{ "resource": "" }
q14657
MVGridDing0.import_powerflow_results
train
def import_powerflow_results(self, session): """Assign results from power flow analysis to edges and nodes Parameters ---------- session: :sqlalchemy:`SQLAlchemy session object<orm/session_basics.html>` Description """
python
{ "resource": "" }
q14658
LVGridDing0.add_station
train
def add_station(self, lv_station): """Adds a LV station to _station and grid graph if not already existing""" if not isinstance(lv_station, LVStationDing0): raise Exception('Given LV
python
{ "resource": "" }
q14659
LVGridDing0.build_grid
train
def build_grid(self): """Create LV grid graph """ # add required transformers build_grid.transformer(self) # add branches of sectors retail/industrial and agricultural
python
{ "resource": "" }
q14660
select_transformers
train
def select_transformers(grid, s_max=None): """Selects LV transformer according to peak load of LV grid district. The transformers are chosen according to max. of load case and feedin-case considering load factors and power factor. The MV-LV transformer with the next higher available nominal apparent power is chosen. Therefore, a max. allowed transformer loading of 100% is implicitly assumed. If the peak load exceeds the max. power of a single available transformer, multiple transformer are build. By default `peak_load` and `peak_generation` are taken from `grid` instance. The behavior can be overridden providing `s_max` as explained in ``Arguments``. Parameters ---------- grid: LVGridDing0 LV grid data Arguments --------- s_max : dict dict containing maximum apparent power of load or generation case and str describing the case. For example .. code-block:: python { 's_max': 480, 'case': 'load' } or .. code-block:: python { 's_max': 120, 'case': 'gen' } s_max passed overrides `grid.grid_district.peak_load` respectively `grid.station().peak_generation`. Returns ------- :pandas:`pandas.DataFrame<dataframe>` Parameters of chosen Transformer :obj:`int` Count of transformers Notes ----- The LV transformer with the next higher available nominal apparent power is chosen. Therefore, a max. allowed transformer loading of 100% is implicitly assumed. If the peak load exceeds the max. power of a single available transformer, use multiple trafos. """ load_factor_lv_trans_lc_normal = cfg_ding0.get('assumptions', 'load_factor_lv_trans_lc_normal') load_factor_lv_trans_fc_normal = cfg_ding0.get('assumptions', 'load_factor_lv_trans_fc_normal') cos_phi_load = cfg_ding0.get('assumptions', 'cos_phi_load') cos_phi_gen = cfg_ding0.get('assumptions', 'cos_phi_gen') # get equipment parameters of LV transformers trafo_parameters = grid.network.static_data['LV_trafos'] # determine s_max from grid object if not provided via arguments if s_max is None: # get maximum from peak load and peak generation s_max_load = grid.grid_district.peak_load / cos_phi_load s_max_gen = grid.station().peak_generation / cos_phi_gen # check if load or
python
{ "resource": "" }
q14661
transformer
train
def transformer(grid): """ Choose transformer and add to grid's station Parameters ---------- grid: LVGridDing0 LV grid data """ # choose size and amount of transformers transformer, transformer_cnt = select_transformers(grid) # create transformers and add them to station of LVGD for t in range(0, transformer_cnt): lv_transformer = TransformerDing0( grid=grid, id_db=id, v_level=0.4,
python
{ "resource": "" }
q14662
select_grid_model_residential
train
def select_grid_model_residential(lvgd): """Selects typified model grid based on population Parameters ---------- lvgd : LVGridDistrictDing0 Low-voltage grid district object Returns ------- :pandas:`pandas.DataFrame<dataframe>` Selected string of typified model grid :pandas:`pandas.DataFrame<dataframe>` Parameters of chosen Transformer Notes ----- In total 196 distinct LV grid topologies are available that are chosen by population in the LV grid district. Population is translated to number of house branches. Each grid model fits a number of house branches. If this number exceeds 196, still the grid topology of 196 house branches is used. The peak load of the LV grid district is uniformly distributed across house branches. """ # Load properties of LV typified model grids string_properties = lvgd.lv_grid.network.static_data['LV_model_grids_strings'] # Load relational table of apartment count and strings of model grid apartment_string = lvgd.lv_grid.network.static_data[ 'LV_model_grids_strings_per_grid'] # load assumtions apartment_house_branch_ratio = cfg_ding0.get("assumptions", "apartment_house_branch_ratio") population_per_apartment = cfg_ding0.get("assumptions",
python
{ "resource": "" }
q14663
build_residential_branches
train
def build_residential_branches(lvgd): """Based on population and identified peak load data, the according grid topology for residential sector is determined and attached to the grid graph Parameters ---------- lvgd : LVGridDistrictDing0 Low-voltage grid district object """ # Choice of typified lv model grid depends on population within lv # grid district. If no population is given, lv grid is omitted and # load is represented by lv station's peak load if lvgd.population > 0 \ and lvgd.peak_load_residential > 0: model_grid = select_grid_model_residential(lvgd)
python
{ "resource": "" }
q14664
lv_grid_generators_bus_bar
train
def lv_grid_generators_bus_bar(nd): """ Calculate statistics about generators at bus bar in LV grids Parameters ---------- nd : ding0.NetworkDing0 Network container object Returns ------- lv_stats : dict Dict with keys of LV grid repr() on first level. Each of the grids has a set of statistical information about its topology """ lv_stats = {} for la in nd._mv_grid_districts[0].lv_load_areas(): for lvgd in la.lv_grid_districts(): station_neighbors = list(lvgd.lv_grid._graph[
python
{ "resource": "" }
q14665
plot_cable_length
train
def plot_cable_length(stats, plotpath): """ Cable length per MV grid district """ # cable and line kilometer distribution f, axarr = plt.subplots(2, 2, sharex=True) stats.hist(column=['Length of MV overhead lines'], bins=5, alpha=0.5, ax=axarr[0, 0]) stats.hist(column=['Length of MV underground cables'], bins=5, alpha=0.5, ax=axarr[0, 1])
python
{ "resource": "" }
q14666
plot_generation_over_load
train
def plot_generation_over_load(stats, plotpath): """ Plot of generation over load """ # Generation capacity vs. peak load sns.set_context("paper", font_scale=1.1) sns.set_style("ticks") # reformat to MW gen_cap_indexes = ["Gen. Cap. of MV at v_level 4", "Gen. Cap. of MV at v_level 5", "Gen. Cap. of LV at v_level 6", "Gen. Cap. of LV at v_level 7"] peak_load_index = ["LA Total LV Peak Load total"] stats['generation_capacity'] = stats[gen_cap_indexes].sum(axis=1) / 1e3 stats['peak_load'] = stats[peak_load_index] / 1e3 sns.lmplot('generation_capacity', 'peak_load', data=stats, fit_reg=False,
python
{ "resource": "" }
q14667
concat_nd_pickles
train
def concat_nd_pickles(self, mv_grid_districts): """ Read multiple pickles, join nd objects and save to file Parameters ---------- mv_grid_districts : list Ints describing MV grid districts """ pickle_name = cfg_ding0.get('output', 'nd_pickle') # self.nd = self.read_pickles_from_files(pickle_name) # TODO: instead of passing a list of mvgd's, pass list of filenames plus optionally a basth_path for mvgd in mv_grid_districts[1:]: filename = os.path.join( self.base_path, 'results', pickle_name.format(mvgd)) if os.path.isfile(filename): mvgd_pickle = pickle.load(open(filename, 'rb')) if mvgd_pickle._mv_grid_districts: mvgd.add_mv_grid_district(mvgd_pickle._mv_grid_districts[0]) # save to concatenated pickle pickle.dump(mvgd, open(os.path.join( self.base_path, 'results', "ding0_grids_{0}-{1}.pkl".format(
python
{ "resource": "" }
q14668
calculate_mvgd_voltage_current_stats
train
def calculate_mvgd_voltage_current_stats(nw): """ MV Voltage and Current Statistics for an arbitrary network Parameters ---------- nw: :any:`list` of NetworkDing0 The MV grid(s) to be studied Returns ------- pandas.DataFrame nodes_df : Dataframe containing voltage statistics for every node in the MVGD pandas.DataFrame lines_df : Dataframe containing voltage statistics for every edge in the MVGD """ ############################## # close circuit breakers nw.control_circuit_breakers(mode='close') ############################## nodes_idx = 0 nodes_dict = {} branches_idx = 0 branches_dict = {} for district in nw.mv_grid_districts(): # nodes voltage for node in district.mv_grid.graph_nodes_sorted(): nodes_idx += 1 if hasattr(node, 'voltage_res'): Vres0 = node.voltage_res[0] Vres1 = node.voltage_res[1] else: Vres0 = 'Not available' Vres1 = 'Not available' nodes_dict[nodes_idx] = {'MV_grid_id': district.mv_grid.id_db, 'node id': node.__repr__(), 'V_res_0': Vres0, 'V_res_1': Vres1, 'V nominal': district.mv_grid.v_level} # branches currents for branch in district.mv_grid.graph_edges(): branches_idx += 1 if hasattr(branch['branch'], 's_res'): s_res0 = branch['branch'].s_res[0] s_res1 = branch['branch'].s_res[1] else: s_res0 = 'Not available'
python
{ "resource": "" }
q14669
calculate_lvgd_voltage_current_stats
train
def calculate_lvgd_voltage_current_stats(nw): """ LV Voltage and Current Statistics for an arbitrary network Note ---- Aggregated Load Areas are excluded. Parameters ---------- nw: :any:`list` of NetworkDing0 The MV grid(s) to be studied Returns ------- pandas.DataFrame nodes_df : Dataframe containing voltage, respectively current, statis for every critical node, resp. every critical station, in every LV grid in nw. pandas.DataFrame lines_df : Dataframe containing current statistics for every critical line, in every LV grid in nw. """ ############################## # close circuit breakers nw.control_circuit_breakers(mode='close') ############################## nodes_idx = 0 nodes_dict = {} branches_idx = 0 branches_dict = {} for mv_district in nw.mv_grid_districts(): for LA in mv_district.lv_load_areas(): if not LA.is_aggregated: for lv_district in LA.lv_grid_districts(): # nodes voltage crit_nodes = get_critical_voltage_at_nodes(lv_district.lv_grid) for node in crit_nodes: nodes_idx += 1 nodes_dict[nodes_idx] = { 'MV_grid_id': mv_district.mv_grid.id_db, 'LV_grid_id': lv_district.lv_grid.id_db, 'LA_id': LA.id_db, 'node id': node['node'].__repr__(), 'v_diff_0': node['v_diff'][0], 'v_diff_1': node['v_diff'][1], 's_max_0': 'NA', 's_max_1': 'NA', 'V nominal': lv_district.lv_grid.v_level, } # branches currents critical_branches, critical_stations = get_critical_line_loading(lv_district.lv_grid) for branch in critical_branches: branches_idx += 1 branches_dict[branches_idx] = { 'MV_grid_id': mv_district.mv_grid.id_db, 'LV_grid_id': lv_district.lv_grid.id_db, 'LA_id': LA.id_db, 'branch id': branch['branch'].__repr__(), 's_max_0': branch['s_max'][0], 's_max_1': branch['s_max'][1], } # stations for node in critical_stations: nodes_idx += 1
python
{ "resource": "" }
q14670
init_mv_grid
train
def init_mv_grid(mv_grid_districts=[3545], filename='ding0_tests_grids_1.pkl'): '''Runs ding0 over the districtis selected in mv_grid_districts It also writes the result in filename. If filename = False, then the network is not saved. Parameters ---------- mv_grid_districts: :any:`list` of :obj:`int` Districts IDs: Defaults to [3545] filename: str Defaults to 'ding0_tests_grids_1.pkl' If filename=False, then the network is not saved Returns ------- NetworkDing0 The created MV network. ''' print('\n########################################') print(' Running ding0 for district', mv_grid_districts) # database connection/ session engine = db.connection(section='oedb') session = sessionmaker(bind=engine)() # instantiate new ding0 network object
python
{ "resource": "" }
q14671
process_stats
train
def process_stats(mv_districts, n_of_districts, source, mode, critical, filename, output): '''Generates stats dataframes for districts in mv_districts. If source=='ding0', then runned districts are saved to a pickle named filename+str(n_of_districts[0])+'_to_'+str(n_of_districts[-1])+'.pkl' Parameters ---------- districts_list: list of int List with all districts to be run. n_of_districts: int Number of districts to be run in each cluster source: str If 'pkl', pickle files are read. If 'ding0', ding0 is run over the districts. mode: str If 'MV', medium voltage stats are calculated. If 'LV', low voltage stats are calculated. If empty, medium and low voltage stats are calculated. critical: bool If True, critical nodes and branches are returned filename: str filename prefix for saving pickles output: outer variable where the output is stored as a tuple of 6 lists:: * mv_stats: MV stats DataFrames. If mode=='LV', then DataFrame is empty. * lv_stats: LV stats DataFrames. If mode=='MV', then DataFrame is empty. * mv_crit_nodes: MV critical nodes stats DataFrames. If mode=='LV', then DataFrame is empty. If critical==False, then DataFrame is empty. * mv_crit_edges: MV critical edges stats DataFrames. If mode=='LV', then DataFrame is empty. If critical==False, then DataFrame is empty. * lv_crit_nodes: LV critical nodes stats DataFrames. If mode=='MV', then DataFrame is empty. If critical==False, then DataFrame is empty. * lv_crit_edges: LV critical edges stats DataFrames. If mode=='MV', then DataFrame is empty. If critical==False, then DataFrame is empty. ''' ####################################################################### # decide what exactly to do with MV LV if mode == 'MV': calc_mv = True calc_lv = False elif mode == 'LV': calc_mv = False calc_lv = True else: calc_mv = True calc_lv = True ####################################################################### clusters = [mv_districts[x:x + n_of_districts] for x in range(0, len(mv_districts), n_of_districts)] mv_stats = [] lv_stats = [] mv_crit_nodes = [] mv_crit_edges = [] lv_crit_nodes = [] lv_crit_edges = [] ####################################################################### for cl in clusters: nw_name = filename + str(cl[0]) if not cl[0] == cl[-1]: nw_name = nw_name + '_to_' + str(cl[-1]) nw = NetworkDing0(name=nw_name)
python
{ "resource": "" }
q14672
ding0_exemplary_plots
train
def ding0_exemplary_plots(stats, base_path=BASEPATH): """ Analyze multiple grid district data generated with Ding0. Parameters ---------- stats : pandas.DataFrame Statistics of each MV grid districts base_path : str Root directory of Ding0 data structure, i.e. '~/.ding0' (which is default).
python
{ "resource": "" }
q14673
nd_load_and_stats
train
def nd_load_and_stats(filenames, base_path=BASEPATH): """ Load multiple files from disk and generate stats Passes the list of files assuming the ding0 data structure as default in :code:`~/.ding0`. Data will be concatenated and key indicators for each grid district are returned in table and graphic format. Parameters ---------- filenames : list of str Provide list of files you want to analyze base_path : str Root directory of Ding0 data structure, i.e. '~/.ding0' (which is default). Returns ------- stats : pandas.DataFrame Statistics of each MV grid districts """ # load Ding0 data nds = [] for filename in filenames: try: nd_load = results.load_nd_from_pickle(filename= os.path.join(base_path,
python
{ "resource": "" }
q14674
extend_substation
train
def extend_substation(grid, critical_stations, grid_level): """ Reinforce MV or LV substation by exchanging the existing trafo and installing a parallel one if necessary. First, all available transformers in a `critical_stations` are extended to maximum power. If this does not solve all present issues, additional transformers are build. Parameters ---------- grid: GridDing0 Ding0 grid container critical_stations : :any:`list` List of stations with overloading grid_level : str Either "LV" or "MV". Basis to select right equipment. Notes ----- Curently straight forward implemented for LV stations Returns ------- type #TODO: Description of return. Change type in the previous line accordingly """ load_factor_lv_trans_lc_normal = cfg_ding0.get( 'assumptions', 'load_factor_lv_trans_lc_normal') load_factor_lv_trans_fc_normal = cfg_ding0.get( 'assumptions', 'load_factor_lv_trans_fc_normal') trafo_params = grid.network._static_data['{grid_level}_trafos'.format( grid_level=grid_level)] trafo_s_max_max = max(trafo_params['S_nom']) for station in critical_stations: # determine if load or generation case and apply load factor if station['s_max'][0] > station['s_max'][1]: case = 'load' lf_lv_trans_normal = load_factor_lv_trans_lc_normal else: case = 'gen' lf_lv_trans_normal = load_factor_lv_trans_fc_normal # cumulative maximum power of transformers installed s_max_trafos = sum([_.s_max_a for _ in station['station']._transformers]) # determine missing trafo power to solve overloading issue s_trafo_missing = max(station['s_max']) - ( s_max_trafos * lf_lv_trans_normal) # list of trafos with rated apparent power below `trafo_s_max_max` extendable_trafos = [_ for _ in station['station']._transformers if _.s_max_a < trafo_s_max_max] # try to extend power of existing trafos while (s_trafo_missing > 0) and extendable_trafos: # only work with first of potentially multiple trafos trafo = extendable_trafos[0] trafo_s_max_a_before = trafo.s_max_a # extend power of first trafo to next higher size available
python
{ "resource": "" }
q14675
extend_substation_voltage
train
def extend_substation_voltage(crit_stations, grid_level='LV'): """ Extend substation if voltage issues at the substation occur Follows a two-step procedure: i) Existing transformers are extended by replacement with large nominal apparent power ii) New additional transformers added to substation (see 'Notes') Parameters ---------- crit_stations : :any:`list` List of stations with overloading or voltage issues. grid_level : str Specifiy grid level: 'MV' or 'LV' Notes ----- At maximum 2 new of largest (currently 630 kVA) transformer are additionally built to resolve voltage issues at MV-LV substation bus bar. """ grid = crit_stations[0]['node'].grid trafo_params = grid.network._static_data['{grid_level}_trafos'.format( grid_level=grid_level)] trafo_s_max_max = max(trafo_params['S_nom']) trafo_min_size = trafo_params.loc[trafo_params['S_nom'].idxmin(), :] v_diff_max_fc = cfg_ding0.get('assumptions', 'lv_max_v_level_fc_diff_normal') v_diff_max_lc = cfg_ding0.get('assumptions', 'lv_max_v_level_lc_diff_normal') tree = nx.dfs_tree(grid._graph, grid._station) for station in crit_stations: v_delta = max(station['v_diff']) # get list of nodes of main branch in right order extendable_trafos = [_ for _ in station['node']._transformers if _.s_max_a < trafo_s_max_max] v_delta_initially_lc = v_delta[0] v_delta_initially_fc = v_delta[1] new_transformers_cnt = 0 # extend existing trafo power while voltage issues exist and larger trafos # are available while (v_delta[0] > v_diff_max_lc) or (v_delta[1] > v_diff_max_fc): if extendable_trafos: # extend power of first trafo to next higher size available extend_trafo_power(extendable_trafos, trafo_params) elif new_transformers_cnt < 2: # build a new transformer lv_transformer = TransformerDing0( grid=grid,
python
{ "resource": "" }
q14676
reinforce_lv_branches_overloading
train
def reinforce_lv_branches_overloading(grid, crit_branches): """ Choose appropriate cable type for branches with line overloading Parameters ---------- grid : LVGridDing0 Ding0 LV grid object crit_branches : :any:`list` List of critical branches incl. its line loading Notes ----- If maximum size cable is not capable to resolve issue due to line overloading largest available cable type is assigned to branch. Returns ------- :any:`list` unsolved_branches : List of braches no suitable cable could be found """ unsolved_branches = [] cable_lf = cfg_ding0.get('assumptions', 'load_factor_lv_cable_lc_normal') cables = grid.network.static_data['LV_cables'] # resolve overloading issues for each branch segment for branch in crit_branches: I_max_branch_load = branch['s_max'][0] I_max_branch_gen = branch['s_max'][1] I_max_branch = max([I_max_branch_load, I_max_branch_gen]) suitable_cables = cables[(cables['I_max_th'] * cable_lf) > I_max_branch] if not suitable_cables.empty: cable_type = suitable_cables.loc[suitable_cables['I_max_th'].idxmin(), :] branch['branch'].type = cable_type
python
{ "resource": "" }
q14677
extend_trafo_power
train
def extend_trafo_power(extendable_trafos, trafo_params): """ Extend power of first trafo in list of extendable trafos Parameters ---------- extendable_trafos : :any:`list` Trafos with rated power below maximum size available trafo trafo_params : :pandas:`pandas.DataFrame<dataframe>` Transformer parameters """ trafo = extendable_trafos[0] trafo_s_max_a_before = trafo.s_max_a trafo_nearest_larger = trafo_params.loc[
python
{ "resource": "" }
q14678
GridDing0.graph_nodes_from_branch
train
def graph_nodes_from_branch(self, branch): """ Returns nodes that are connected by `branch` Args ---- branch: BranchDing0 Description #TODO Returns ------- (:obj:`GridDing0`, :obj:`GridDing0`)
python
{ "resource": "" }
q14679
GridDing0.graph_branches_from_node
train
def graph_branches_from_node(self, node): """ Returns branches that are connected to `node` Args ---- node: GridDing0 Ding0 object (member of graph) Returns ------- :any:`list` List of tuples (node in :obj:`GridDing0`, branch in :obj:`BranchDing0`) :: (node , branch_0 ), ..., (node , branch_N
python
{ "resource": "" }
q14680
GridDing0.graph_edges
train
def graph_edges(self): """ Returns a generator for iterating over graph edges The edge of a graph is described by the two adjacent node and the branch object itself. Whereas the branch object is used to hold all relevant power system parameters. Yields ------ int Description #TODO check Note ---- There are generator functions for nodes (`Graph.nodes()`) and edges (`Graph.edges()`) in NetworkX but unlike graph nodes, which can be represented by objects, branch objects can only be accessed by using an edge attribute ('branch' is used here) To make access to attributes of the branch objects simpler and more intuitive for the user, this generator yields a dictionary for each edge that contains information about
python
{ "resource": "" }
q14681
GridDing0.find_path
train
def find_path(self, node_source, node_target, type='nodes'): """Determines shortest path Determines the shortest path from `node_source` to `node_target` in _graph using networkx' shortest path algorithm. Args ---- node_source: GridDing0 source node, member of _graph node_target: GridDing0 target node, member of _graph type : str Specify if nodes or edges should be returned. Default is `nodes` Returns ------- :any:`list` of :obj:`GridDing0` path: shortest path from `node_source` to `node_target` (list of nodes in _graph) Notes -----
python
{ "resource": "" }
q14682
MVGridDistrictDing0.add_lv_load_area
train
def add_lv_load_area(self, lv_load_area): """ Adds a Load Area `lv_load_area` to _lv_load_areas if not already existing Additionally, adds the associated centre object to MV grid's _graph as node. Args ---- lv_load_area: LVLoadAreaDing0 instance of
python
{ "resource": "" }
q14683
MVGridDistrictDing0.add_lv_load_area_group
train
def add_lv_load_area_group(self, lv_load_area_group): """Adds a LV load_area to _lv_load_areas if not already existing. """
python
{ "resource": "" }
q14684
MVGridDistrictDing0.add_aggregated_peak_demand
train
def add_aggregated_peak_demand(self): """Summarizes peak loads of underlying aggregated load_areas""" peak_load_aggregated = 0 for lv_load_area in self.lv_load_areas(): if lv_load_area.is_aggregated:
python
{ "resource": "" }
q14685
LVLoadAreaDing0.peak_generation
train
def peak_generation(self): """Cumulative peak generation of generators connected to LV grids of underlying LVGDs """ cum_peak_generation = 0 for lv_grid_district in self._lv_grid_districts:
python
{ "resource": "" }
q14686
calc_geo_branches_in_polygon
train
def calc_geo_branches_in_polygon(mv_grid, polygon, mode, proj): """ Calculate geographical branches in polygon. For a given `mv_grid` all branches (edges in the graph of the grid) are tested if they are in the given `polygon`. You can choose different modes and projections for this operation. Parameters ---------- mv_grid : MVGridDing0 MV Grid object. Edges contained in `mv_grid.graph_edges()` are taken for the test. polygon : :shapely:`Shapely Point object<points>` Polygon that contains edges. mode : str Choose between 'intersects' or 'contains'. proj : int EPSG code to specify projection Returns ------- :any:`list` of :any:`BranchDing0` objects List of branches """ branches = [] polygon_shp = transform(proj,
python
{ "resource": "" }
q14687
calc_geo_branches_in_buffer
train
def calc_geo_branches_in_buffer(node, mv_grid, radius, radius_inc, proj): """ Determines branches in nodes' associated graph that are at least partly within buffer of `radius` from `node`. If there are no nodes, the buffer is successively extended by `radius_inc` until nodes are found. Parameters ---------- node : LVStationDing0, GeneratorDing0, or CableDistributorDing0 origin node (e.g. LVStationDing0 object) with associated shapely object (attribute `geo_data`) in any CRS (e.g. WGS84) radius : float
python
{ "resource": "" }
q14688
disconnect_node
train
def disconnect_node(node, target_obj_result, graph, debug): """ Disconnects `node` from `target_obj` Args ---- node: LVLoadAreaCentreDing0, i.e. Origin node - Ding0 graph object (e.g. LVLoadAreaCentreDing0) target_obj_result: LVLoadAreaCentreDing0, i.e. Origin node - Ding0 graph object (e.g. LVLoadAreaCentreDing0) graph: :networkx:`NetworkX Graph Obj< >` NetworkX graph object with nodes and newly created branches debug: bool If True, information is printed during process """ # backup kind and type of branch branch_kind = graph.adj[node][target_obj_result]['branch'].kind branch_type = graph.adj[node][target_obj_result]['branch'].type branch_ring = graph.adj[node][target_obj_result]['branch'].ring graph.remove_edge(node, target_obj_result) if isinstance(target_obj_result, MVCableDistributorDing0): neighbor_nodes = list(graph.neighbors(target_obj_result)) if len(neighbor_nodes) == 2: node.grid.remove_cable_distributor(target_obj_result)
python
{ "resource": "" }
q14689
mv_connect_generators
train
def mv_connect_generators(mv_grid_district, graph, debug=False): """Connect MV generators to MV grid Args ---- mv_grid_district: MVGridDistrictDing0 MVGridDistrictDing0 object for which the connection process has to be done graph: :networkx:`NetworkX Graph Obj< >` NetworkX graph object with nodes debug: bool, defaults to False If True, information is printed during process. Returns ------- :networkx:`NetworkX Graph Obj< >` NetworkX graph object with nodes and newly created branches """ generator_buffer_radius = cfg_ding0.get('mv_connect', 'generator_buffer_radius') generator_buffer_radius_inc = cfg_ding0.get('mv_connect', 'generator_buffer_radius_inc') # WGS84 (conformal) to ETRS (equidistant) projection proj1 = partial( pyproj.transform, pyproj.Proj(init='epsg:4326'), # source coordinate system pyproj.Proj(init='epsg:3035')) # destination coordinate system # ETRS (equidistant) to WGS84 (conformal) projection proj2 = partial( pyproj.transform, pyproj.Proj(init='epsg:3035'), # source coordinate system pyproj.Proj(init='epsg:4326')) # destination coordinate system for generator in sorted(mv_grid_district.mv_grid.generators(), key=lambda x: repr(x)): # ===== voltage level 4: generator has to be connected to MV station ===== if generator.v_level == 4: mv_station = mv_grid_district.mv_grid.station() branch_length = calc_geo_dist_vincenty(generator, mv_station) # TODO: set branch type to something reasonable (to be calculated) branch_kind = mv_grid_district.mv_grid.default_branch_kind branch_type = mv_grid_district.mv_grid.default_branch_type branch = BranchDing0(length=branch_length, kind=branch_kind, type=branch_type, ring=None) graph.add_edge(generator, mv_station, branch=branch) if debug: logger.debug('Generator {0} was connected to {1}'.format( generator, mv_station)) # ===== voltage level 5: generator has to be connected to MV grid (next-neighbor) ===== elif generator.v_level == 5: generator_shp = transform(proj1, generator.geo_data) # get branches within a the predefined radius `generator_buffer_radius` branches = calc_geo_branches_in_buffer(generator, mv_grid_district.mv_grid, generator_buffer_radius, generator_buffer_radius_inc, proj1) # calc distance between generator and grid's lines -> find nearest line conn_objects_min_stack = find_nearest_conn_objects(generator_shp, branches, proj1, conn_dist_weight=1,
python
{ "resource": "" }
q14690
solve
train
def solve(graph, debug=False, anim=None): # TODO: check docstring """ Do MV routing for given nodes in `graph`. Translate data from node objects to appropriate format before. Args ---- graph: :networkx:`NetworkX Graph Obj< >` NetworkX graph object with nodes debug: bool, defaults to False If True, information is printed while routing anim: AnimationDing0 AnimationDing0 object Returns ------- :networkx:`NetworkX Graph Obj< >` NetworkX graph object with nodes and edges See Also -------- ding0.tools.animation.AnimationDing0 : for a more detailed description on anim parameter. """ # TODO: Implement debug mode (pass to solver) to get more information while routing (print routes, draw network, ..) # translate DING0 graph to routing specs specs = ding0_graph_to_routing_specs(graph) # create routing graph using specs RoutingGraph
python
{ "resource": "" }
q14691
cable_type
train
def cable_type(nom_power, nom_voltage, avail_cables): """Determine suitable type of cable for given nominal power Based on maximum occurring current which is derived from nominal power (either peak load or max. generation capacity) a suitable cable type is chosen. Thus, no line overloading issues should occur. Parameters ---------- nom_power : float Nominal power of generators or loads connected via a cable nom_voltage : float Nominal voltage in kV avail_cables : :pandas:`pandas.DataFrame<dataframe>` Available cable types including it's electrical parameters Returns ------- :pandas:`pandas.DataFrame<dataframe>` Parameters of cable type """
python
{ "resource": "" }
q14692
handle_duplications
train
def handle_duplications(file_path): """ Omits the duplications in the strings files. Keys that appear more than once, will be joined to one appearance and the omit will be documented. Args: file_path (str): The path to the strings file. """ logging.info('Handling duplications for "%s"', file_path) f = open_strings_file(file_path, "r+") header_comment_key_value_tuples = extract_header_comment_key_value_tuples_from_file(f) file_elements = [] section_file_elements = [] keys_to_objects = {} duplicates_found = [] for header_comment, comments, key, value in header_comment_key_value_tuples: if len(header_comment) > 0: # New section - Appending the last section entries, sorted by comment for elem in sorted(section_file_elements, key=lambda x: x.comments[0]): file_elements.append(elem) section_file_elements = [] file_elements.append(Comment(header_comment)) if key in keys_to_objects: keys_to_objects[key].add_comments(comments) duplicates_found.append(key) else: loc_obj = LocalizationEntry(comments, key, value)
python
{ "resource": "" }
q14693
Api._copy_resources
train
def _copy_resources(self): """ Copy all of the resources over to the toplevel client -return: populates self with a pointer to each ._client.Resource """ for resource in self._client._resources: # set the name param, the keys now have / in them potion_resource = self._client._resources[resource] try: oc_cls = _model_lookup[resource] oc_cls._api =
python
{ "resource": "" }
q14694
login_uname_pwd
train
def login_uname_pwd(server, api_key=None): """ Prompts user for username and password, gets API key from server if not provided. """ username = click.prompt("Please enter your One Codex (email)") if api_key is not None: return username, api_key password = click.prompt("Please enter your password (typing will be
python
{ "resource": "" }
q14695
login_required
train
def login_required(fn): """Requires login before proceeding, but does not prompt the user to login. Decorator should be used only on Click CLI commands. Notes ----- Different means of authentication will be attempted in this order: 1. An API key present in the Click context object from a previous successful authentication. 2. A bearer token (ONE_CODEX_BEARER_TOKEN) in the environment. 3. An API key (ONE_CODEX_API_KEY) in the environment. 4. An API key in the credentials file (~/.onecodex). """ @wraps(fn) def login_wrapper(ctx, *args, **kwargs): base_url = os.environ.get("ONE_CODEX_API_BASE", "https://app.onecodex.com") api_kwargs = {"telemetry": ctx.obj["TELEMETRY"]} api_key_prior_login = ctx.obj.get("API_KEY") bearer_token_env = os.environ.get("ONE_CODEX_BEARER_TOKEN") api_key_env = os.environ.get("ONE_CODEX_API_KEY") api_key_creds_file = _login(base_url, silent=True) if api_key_prior_login is not
python
{ "resource": "" }
q14696
Classifications.results
train
def results(self, json=True): """ Returns the complete results table for the classification. Parameters ---------- json : bool, optional Return result as JSON? Default True. Returns ------- table : dict | DataFrame
python
{ "resource": "" }
q14697
Classifications.abundances
train
def abundances(self, ids=None): """ Query the results table to get abundance data for all or some tax ids """ # TODO: Consider removing this method... since it's kind of trivial # May want to replace with something that actually gets genome-size adjusted #
python
{ "resource": "" }
q14698
LocalizationEntry.add_comments
train
def add_comments(self, comments): """ Add comments to the localization entry Args: comments (list of str): The comments to be added to the localization entry. """ for comment in comments: if comment
python
{ "resource": "" }
q14699
merge_translations
train
def merge_translations(localization_bundle_path): """ Merges the new translation with the old one. The translated files are saved as '.translated' file, and are merged with old translated file. Args: localization_bundle_path (str): The path to the localization bundle. """ logging.info("Merging translations") for lang_dir in os.listdir(localization_bundle_path):
python
{ "resource": "" }