├── README.md ├── examples ├── run_calc.py ├── element_dictionaries.py ├── results.py ├── assign_characteristics.py ├── vectors_matrices.py ├── loads_file.csv ├── project_folders.py └── create_time_characteristics.py ├── docs ├── results.md ├── running_calculations.md ├── characteristics.md ├── accessing_elements.md ├── vectors_and_matrices.md └── project_folders.md └── LICENSE /README.md: -------------------------------------------------------------------------------- 1 | # Python for PowerFactory 2 | 3 | Python scripting for PowerFactory 4 | 5 | Suitable for PowerFactory versions v15.1.6 and above 6 | -------------------------------------------------------------------------------- /examples/run_calc.py: -------------------------------------------------------------------------------- 1 | """ 2 | ############################ 3 | ## run_calc.py 4 | ############################ 5 | 6 | - Gets a short circuit object from the active study case 7 | - Set up the short circuit case (IEC 3-phase fault on all busbars) 8 | - Runs the short circuit calculation 9 | 10 | """ 11 | 12 | import powerfactory as pf 13 | 14 | # Get PowerFactory application 15 | app = pf.GetApplication() 16 | app.ClearOutputWindow() 17 | 18 | # Get short circuit calculation object 19 | shc = app.GetFromStudyCase('ComShc') 20 | 21 | # Set up short circuit calculation 22 | shc.iopt_mde = 1 # IEC fault mode 23 | shc.iopt_allbus = 2 # All busbars 24 | 25 | # Run short circuit calculation 26 | ierr = shc.Execute() 27 | -------------------------------------------------------------------------------- /examples/element_dictionaries.py: -------------------------------------------------------------------------------- 1 | """ 2 | ############################ 3 | ## element_dictionaries.py 4 | ############################ 5 | 6 | - Gets lists of buses, generators and lines 7 | - Creates dictionaries for each element type with the object name as the key 8 | 9 | """ 10 | 11 | import powerfactory as pf 12 | 13 | app = pf.GetApplication() 14 | app.ClearOutputWindow() 15 | 16 | # Create dictionary of buses 17 | bus_dict = {} 18 | buses = app.GetCalcRelevantObjects('*.ElmTerm') 19 | for bus in buses: 20 | bus_dict[bus.loc_name] = bus 21 | 22 | # Create dictionary of lines 23 | line_dict = {} 24 | lines = app.GetCalcRelevantObjects('*.ElmLne') 25 | for line in lines: 26 | line_dict[line.loc_name] = line 27 | 28 | # Create dictionary of synchronous generators 29 | gen_dict = {} 30 | gens = app.GetCalcRelevantObjects('*.ElmSym') 31 | for gen in gens: 32 | gen_dict[gen.loc_name] = gen 33 | 34 | # Loop through generator dictionary and print key and generator object 35 | for gen_key in gen_dict.keys(): 36 | app.PrintPlain(gen_key) 37 | app.PrintPlain(gen_dict[gen_key]) 38 | -------------------------------------------------------------------------------- /examples/results.py: -------------------------------------------------------------------------------- 1 | """ 2 | ############################ 3 | ## results.py 4 | ############################ 5 | 6 | - Runs a load flow 7 | - Gets basic results from buses (voltage) and lines (loading) 8 | 9 | """ 10 | 11 | import powerfactory as pf 12 | 13 | # Get PowerFactory application 14 | app = pf.GetApplication() 15 | app.ClearOutputWindow() 16 | 17 | # Run load flow 18 | ldf = app.GetFromStudyCase('ComLdf') 19 | ierr = ldf.Execute() 20 | 21 | # Get lists of buses and lines 22 | buses = app.GetCalcRelevantObjects('*.ElmTerm') 23 | lines = app.GetCalcRelevantObjects('*.ElmLne') 24 | 25 | # Print bus voltages 26 | for bus in buses: 27 | # Only consider busbars (iUsage = 0) and in-service buses 28 | if bus.iUsage == 0 and bus.outserv == 0: 29 | bus_v = round(bus.GetAttribute('m:u'),2) 30 | app.PrintPlain('Voltage on bus ' + str(bus) + ': ' + str(bus_v) + 'pu') 31 | 32 | # Print loading on lines 33 | for line in lines: 34 | if line.outserv == 0: 35 | loading = round(line.GetAttribute('m:loading'),2) 36 | app.PrintPlain('Loading on line ' + str(line) + ': ' + str(loading) + '%%') 37 | -------------------------------------------------------------------------------- /examples/assign_characteristics.py: -------------------------------------------------------------------------------- 1 | """ 2 | ################################ 3 | ## assign_characteristics.py 4 | ################################ 5 | - Gets all characteristics in the characteristics folder 6 | - Matches load names with characteristic names 7 | - If there is a match, assign the characteristic to the load 8 | - If there no match, print a warning 9 | """ 10 | 11 | import powerfactory as pf 12 | 13 | # Get PowerFactory application 14 | app = pf.GetApplication() 15 | app.ClearOutputWindow() 16 | 17 | # Operational library 18 | oplib = app.GetProjectFolder('oplib') 19 | # Characteristics 20 | opchar = oplib.GetContents('Characteristics')[0][0] 21 | 22 | # Create dictionary of loads classified by load local name 23 | load_dict = {} 24 | loads = app.GetCalcRelevantObjects('*.ElmLod') 25 | for load in loads: 26 | load_dict[load.loc_name] = load 27 | 28 | print_str('Assigning characteristics to loads...') 29 | 30 | # Get all characteristics in characteristics folder 31 | load_chars = opchar.GetContents('*.*')[0] 32 | 33 | # Assign characteristics to loads 34 | for char in load_chars: 35 | if char.loc_name in load_dict: 36 | load = load_dict[char.loc_name] 37 | pliniref = load.CreateObject('ChaRef','plini') 38 | pliniref[0].typ_id = char 39 | app.PrintPlain(load) 40 | else: 41 | app.PrintWarn('Load ' + str(char) + ' not found...') 42 | -------------------------------------------------------------------------------- /examples/vectors_matrices.py: -------------------------------------------------------------------------------- 1 | """ 2 | ############################ 3 | ## vectors_matrices.py 4 | ############################ 5 | 6 | - Creates a vector and matrix object in the active study case 7 | - Puts dummy data into the vector and matrix objects 8 | - Gets the data out of the matrix object, modifies an entry and puts it back into the matrix object 9 | 10 | """ 11 | 12 | import powerfactory as pf 13 | 14 | # Get PowerFactory application 15 | app = pf.GetApplication() 16 | app.ClearOutputWindow() 17 | 18 | # Get current study case 19 | study = app.GetActiveStudyCase() 20 | 21 | # Create PowerFactory vector and matrix objects 22 | vec = study.CreateObject('IntVec')[0] 23 | mat = study.CreateObject('IntMat')[0] 24 | 25 | # Print out objects 26 | app.PrintPlain(vec) 27 | app.PrintPlain(mat) 28 | 29 | # Generate dummy data for vector and matrix 30 | # Note that vector data is a list and matrix data is a list of lists 31 | vec_data = [1, 6, 8, 1, 5, 8] 32 | mat_data = [[5, 7, 3], [2, 4, 6], [6, 1, 0]] 33 | 34 | # Put dummy data into PowerFactory vector and matrix objects 35 | vec.SetAttribute('V', vec_data) 36 | mat.SetAttribute('M', mat_data) 37 | 38 | # Get data from matrix object and print it out 39 | data = mat.GetAttribute('M') 40 | app.PrintPlain(data) 41 | 42 | # Modify the (3,3) entry of the matrix 43 | data[2][2] = 10 44 | 45 | # Put modified data back into matrix 46 | mat.SetAttribute('M', data) 47 | -------------------------------------------------------------------------------- /examples/loads_file.csv: -------------------------------------------------------------------------------- 1 | Load_1,1171,1132,1117,1022,1047,1024,1016,993,974,979,994,1035,1036,1042,1000,1016,1027,1046,1065,1081,1093,1121,1116,1114,1129,1135,1160,1182,1191,1178,1160,1154,1138,1139,1154,1232,1354,1472,1475,1466,1478,1450,1433,1391,1359,1300,1260,1208 2 | Load_2,5372,5034,5018,5377,4761,4706,4690,4668,4634,4782,5059,5393,5428,5061,4837,4723,4699,4846,4801,4898,4958,5124,5335,5400,5391,5405,5401,5437,5372,5258,5119,4964,4967,5174,5455,6193,7309,8000,8119,7962,7932,7680,7393,6955,6574,6194,5909,5568 3 | Load_3,2815,2754,2496,2191,2353,2249,2197,2109,2079,2031,2021,2075,2102,2115,2154,2202,2302,2488,2553,2606,2619,2773,2824,2859,2953,5040,2960,2939,2975,2966,2910,2924,2919,2903,3077,3154,3258,3384,3340,3341,3361,3364,3345,3244,3091,3042,2969,2861 4 | Load_4,3834,2770,2621,2304,2397,2364,2294,2205,2191,2182,2174,2191,2166,2260,2405,2668,2946,3160,3331,3452,3743,3445,3598,3675,3707,3705,3766,3820,3811,3772,4163,4135,4127,4005,4034,4009,5341,5521,5401,6057,6345,6246,6236,6038,5194,4457,4226,3947 5 | Load_5,2446,2295,2328,2032,2207,2113,2100,2057,2037,2020,2038,2057,2172,2161,2227,2392,2652,2817,2931,3002,3014,3121,3165,3228,3288,3262,3302,3835,3323,3321,3292,3269,3120,3028,2904,2875,2964,3068,3098,3084,3120,3047,2963,2834,2772,2694,2585,2493 6 | Load_6,3958,3857,3782,3499,3622,3616,3532,3522,3447,3474,3506,3599,3639,3752,3785,3903,4041,4322,4398,4478,4523,4686,4765,4890,4935,4989,4948,5053,4980,4896,4810,4664,4619,4526,4474,4655,4962,5144,5123,5087,5029,4970,4886,4755,4471,4228,4181,4059 7 | -------------------------------------------------------------------------------- /examples/project_folders.py: -------------------------------------------------------------------------------- 1 | """ 2 | ############################ 3 | ## project_folders.py 4 | ############################ 5 | 6 | Gets common project folders 7 | 8 | """ 9 | import powerfactory as pf 10 | 11 | app = pf.GetApplication() 12 | 13 | ######### 14 | # MODEL # 15 | ######### 16 | 17 | # Network model 18 | netmod = app.GetProjectFolder('netmod') 19 | 20 | # Network data 21 | netdata = app.GetProjectFolder('netdat') 22 | 23 | # Diagrams 24 | diag_fold = app.GetProjectFolder('dia') 25 | 26 | # Variations 27 | var_fold = app.GetProjectFolder('scheme') 28 | 29 | ########### 30 | # LIBRARY # 31 | ########### 32 | 33 | # Equipment library 34 | equip = app.GetProjectFolder('equip') 35 | lines_fold = equip.GetContents('Lines')[0][0] 36 | 37 | # User defined models 38 | main_lib = equip.GetParent() 39 | udm = main_lib.GetContents('User Defined Models*')[0][0] 40 | 41 | # Script library 42 | script_lib = app.GetProjectFolder('script') 43 | 44 | # Template library 45 | templ_lib = app.GetProjectFolder('templ') 46 | 47 | # Operational library 48 | oplib = app.GetProjectFolder('oplib') 49 | 50 | # Characteristics 51 | opchar = oplib.GetContents('Characteristics')[0][0] 52 | 53 | # Thermal ratings 54 | therm_fold = app.GetProjectFolder('therm') 55 | 56 | # MVAr limit curves 57 | mvar_fold = app.GetProjectFolder('mvar') 58 | 59 | ############## 60 | # STUDY CASE # 61 | ############## 62 | 63 | # Study cases 64 | op_scen = app.GetProjectFolder('scen') 65 | 66 | # Operational scenarios 67 | sc_fold = app.GetProjectFolder('study') 68 | -------------------------------------------------------------------------------- /docs/results.md: -------------------------------------------------------------------------------- 1 | ## Getting calculation results 2 | 3 | Calculation results (such as load flow or short circuit results) can be extracted by using either the `GetAttribute` or `getattr` functions: 4 | 5 | 1) The `GetAttribute` function is a method that is included in every PowerFactory object. For example, consider the bus object (*.ElmTerm) called "bus". The GetAttribute function is called as follows: 6 | 7 | ```python 8 | value = bus.GetAttribute(parameter) 9 | ``` 10 | 11 | where `parameter` is a string with the result variable of interest, e.g. 'm:u' for bus voltage, 'm:phiu' for bus angle, etc. 12 | 13 | 2) The `getattr` function is a generic Python method that takes an object as an explicit parameter, i.e. 14 | 15 | ```python 16 | value = getattr(obj, parameter) 17 | ``` 18 | 19 | where `obj` is a PowerFactory data object (such as a bus) and `parameter` is a string with the result variable of interest. 20 | 21 | ## Example 22 | This example script runs a load flow and gets bus voltages and line loadings. 23 | 24 | [\[ Download example here \]](https://github.com/susantoj/powerfactory_python/blob/master/examples/results.py) 25 | 26 | ```python 27 | import powerfactory as pf 28 | 29 | # Get PowerFactory application 30 | app = pf.GetApplication() 31 | app.ClearOutputWindow() 32 | 33 | # Run load flow 34 | ldf = app.GetFromStudyCase('ComLdf') 35 | ierr = ldf.Execute() 36 | 37 | # Get lists of buses and lines 38 | buses = app.GetCalcRelevantObjects('*.ElmTerm') 39 | lines = app.GetCalcRelevantObjects('*.ElmLne') 40 | 41 | # Print bus voltages 42 | for bus in buses: 43 | # Only consider busbars (iUsage = 0) and in-service buses 44 | if bus.iUsage == 0 and bus.outserv == 0: 45 | bus_v = round(bus.GetAttribute('m:u'),2) 46 | app.PrintPlain('Voltage on bus ' + str(bus) + ': ' + str(bus_v) + 'pu') 47 | 48 | # Print loading on lines 49 | for line in lines: 50 | if line.outserv == 0: 51 | loading = round(line.GetAttribute('m:loading'),2) 52 | app.PrintPlain('Loading on line ' + str(line) + ': ' + str(loading) + '%%') 53 | ``` 54 | -------------------------------------------------------------------------------- /docs/running_calculations.md: -------------------------------------------------------------------------------- 1 | ## Running calculations 2 | 3 | Calculations in PowerFactory (such as load flows, short circuits, time-domain simulations, etc) are controlled by Command objects (*.Com*), which reside in the study case folders. The simplest way to create a command object or access an existing one in the active study case is to use the `GetFromStudyCase` function. The `Execute` function is then used to run the calculation. 4 | 5 | General usage is as follows (the snippet below gets a short circuit object and runs a fault calculation): 6 | 7 | ```python 8 | import powerfactory as pf 9 | 10 | app = pf.GetApplication() 11 | 12 | shc = app.GetFromStudyCase('ComShc') 13 | shc.Execute() 14 | ``` 15 | 16 | Note that the GetFromStudyCase function looks for an existing command object in the active study case. If a command object is not found, then it will create a new one. 17 | 18 | Commonly used command objects are as follows: 19 | - ComLdf (Load flow) 20 | - ComShc (Short Circuit) 21 | - ComStatsim (Quasi-dynamic simulation) 22 | - ComSimoutage (Contingency analysis) 23 | - ComNmink (Contingency definition) 24 | - ComInc (Initial conditions for time domain simulation) 25 | - ComSim (Run time domain simulation) 26 | 27 | The command objects also include the setup parameters for the calculation. For example, the ComShc object contains parameters such as the calculation method (IEC, ANSI, etc), the type of fault (3-Phase, 2-Phase, Earth Fault, etc) and the fault impedance (R and X). These parameters can also be modified in script. For example, to set the calculation method to IEC, the "iopt_mde" parameter needs to be modified as follows" 28 | ```python 29 | shc.iopt_mde = 1 30 | ``` 31 | 32 | ## Example 33 | 34 | This example script gets a short circuit calculation object, sets it up and runs a short circuit calculation. 35 | 36 | [\[ Download example here \]](https://github.com/susantoj/powerfactory_python/blob/master/examples/run_calc.py) 37 | 38 | ```python 39 | import powerfactory as pf 40 | 41 | # Get PowerFactory application 42 | app = pf.GetApplication() 43 | app.ClearOutputWindow() 44 | 45 | # Get short circuit calculation object 46 | shc = app.GetFromStudyCase('ComShc') 47 | 48 | # Set up short circuit calculation 49 | shc.iopt_mde = 1 # IEC fault mode 50 | shc.iopt_allbus = 2 # All busbars 51 | 52 | # Run short circuit calculation 53 | ierr = shc.Execute() 54 | ``` 55 | -------------------------------------------------------------------------------- /examples/create_time_characteristics.py: -------------------------------------------------------------------------------- 1 | 2 | """ 3 | ##################################### 4 | ## create_time_characteristics.py 5 | ##################################### 6 | - Open a CSV file of 30min daily load profiles using TKinter file dialog box (for example "load_file.csv") 7 | - Read CSV file and build list of loads 8 | - Loop through list of loads and create time characteristics 9 | 10 | Format of CSV file: 11 | - Column 1: Name of load 12 | - Column 2-49: Load demand (in kW) for each half hour interval from 0:00 to 23:30 13 | """ 14 | 15 | import tkinter as tk 16 | from tkinter import filedialog 17 | import sys 18 | import csv 19 | import powerfactory as pf 20 | 21 | # Load object class definition 22 | class csv_load(): 23 | def __init__ (self, input): 24 | self.name = input[0] 25 | self.P = [float(i)/1000 for i in input[1:49]] # Convert loads from kW to MW 26 | 27 | # Get PowerFactory application 28 | app = pf.GetApplication() 29 | app.ClearOutputWindow() 30 | 31 | # Operational library folder 32 | oplib = app.GetProjectFolder('oplib') 33 | # Characteristics folder 34 | opchar = oplib.GetContents('Characteristics')[0][0] 35 | 36 | # Select csv file 37 | if len(sys.argv)>1: 38 | file_name_ = str(sys.argv[1]) 39 | else: 40 | root = tk.Tk() 41 | root.withdraw() 42 | file_name_ = filedialog.askopenfilename(title='Select a CSV file to open',filetypes=[('Comma Separated Value file', '*.csv')]) 43 | # Handler for cancel button 44 | if not file_name_: 45 | print_str('Script execution cancelled...') 46 | exit() 47 | 48 | # Read in lines from CSV file and put them into list of load objects 49 | csv_loads = [] 50 | with open(file_name_, 'r') as csvfile: 51 | line_file = csv.reader (csvfile, delimiter=",", quoting=csv.QUOTE_NONE) 52 | for row in line_file: 53 | csv_loads.append(csv_load(row)) 54 | 55 | # Loop through each load in list 56 | for load in csv_loads: 57 | # Create time characteristic 58 | app.PrintPlain('Creating time characteristic ' + load.name + '...') 59 | 60 | char = opchar.CreateObject('ChaTime', load.name)[0] 61 | # Set up characteristic 62 | char.source = 0 # Data Source = Table 63 | char.repeat = 0 # Recurrence = Daily 64 | char.cunit = 0 # Resolution = Minutes 65 | char.stepSize = 30 # Step Size = Half hourly intervals 66 | char.usage = 2 # Usage = Absolute 67 | char.approx = 4 # Approximation = Hermite 68 | 69 | # Populate characteristic with load data 70 | char.SetAttribute('vector', load.P) 71 | 72 | -------------------------------------------------------------------------------- /docs/characteristics.md: -------------------------------------------------------------------------------- 1 | ## Characteristics 2 | 3 | In PowerFactory, characteristics are a way of assigning values to object parameters based on a "trigger" and "scale". The way characteristics are set up in PowerFactory is generic and quite abstract, so is best illustrated by examples. 4 | 5 | **Example 1**: a load profile with a series of active power demands (in MW) in hourly intervals is assigned to a load. In this case, the trigger is the study time and the scale is time (hours). When the study time is changed, the value of the load changes correspondingly according to the load profile. 6 | 7 | **Example 2**: a capacitor bank is switched on and off depending on an operating mode: Low Load, Medium Load and High Load. In this case, a characteristic is assigned to the "out of service" flag of the capacitor bank. The scale is the range of applicable operating modes (low, medium and high) and the trigger is the current operating mode. When the operating mode is changed, the capacitor bank is switched on or off depending on the characteristic. 8 | 9 | **Example 3**: a wind turbine's power coefficient (Cp) is calculated from a lookup table based on two inputs: tip-speed ratio and blade pitch. In this case, the characteristic is assigned to the power output of the wind turbine. The characteristic is a matrix with two triggers (tip-speed ratio and blade pitch) and two scales (range of tip-speed ratios and range of blade pitches). Changing either of the triggers will change the wind turbine output accordingly. 10 | 11 | ## Creating Characteristics 12 | 13 | Characteristics are simply PowerFactory objects and can be created with the **CreateObject()** function. However, the data still needs to be entered into the characteristic. An efficient way to create characteristics is to automatically import data from a CSV file and then enter it automatically into characteristics. 14 | 15 | [\[ Example for creating time characteristics from a CSV file \]](https://github.com/susantoj/powerfactory_python/blob/master/examples/create_time_characteristics.py) 16 | 17 | [\[ CSV file for example \]](https://github.com/susantoj/powerfactory_python/blob/master/examples/loads_file.csv) 18 | 19 | ## Assigning Characteristics 20 | 21 | Once characteristics have been created, they still need to be assigned to an object parameter, e.g. load active power (Plini) or out of service flag (outserv). This is done through characteristic reference objects, which link a characteristic in the library to parameters in the network model. This allows a single characteristic to be reused in multiple object parameters. 22 | 23 | [\[ Example for assigning characteristics \]](https://github.com/susantoj/powerfactory_python/blob/master/examples/assign_characteristics.py) 24 | -------------------------------------------------------------------------------- /docs/accessing_elements.md: -------------------------------------------------------------------------------- 1 | ## Accessing network elements 2 | 3 | The most convenient way to access elements in the network is to use the `GetCalcRelevantObjects` function, which allows you to filter network elements by name and type. This method is included in the PowerFactory `application` object and returns a list. 4 | General usage is as follows: 5 | 6 | ```python 7 | import powerfactory as pf 8 | app = pf.GetApplication() 9 | returned_list = app.GetCalcRelevantObjects(filter) 10 | ``` 11 | 12 | where `returned_list` is a list of objects that is returned by the function. If nothing is found, then an empty list [] is returned. 13 | `filter` is a string parameter that describes which network objects you want to return (more on this below) 14 | 15 | ### Filter Examples 16 | Filters are strings that describe which objects you want from the query. The asterisk (*) is a wildcard character that denotes the matching of zero or more characters. 17 | 18 | Here are some examples of filters: 19 | 20 | a) Get all network objects (including types) 21 | 22 | ```python 23 | all_objs = app.GetCalcRelevantObjects('*') 24 | ``` 25 | 26 | b) Get all line elements 27 | 28 | ```python 29 | lines = app.GetCalcRelevantObjects('*.ElmLne') 30 | ``` 31 | 32 | c) Get lines with a name starting with '86' 33 | 34 | ```python 35 | lines = app.GetCalcRelevantObjects('86*.ElmLne') 36 | ``` 37 | 38 | d) Get the specific line with name 'line_3a' 39 | 40 | ```python 41 | line_3a = app.GetCalcRelevantObjects('line_3a.ElmLne') 42 | ``` 43 | 44 | e) Get all line types (that are being used, i.e. does not include unused types in the library) 45 | 46 | ```python 47 | line_types = app.GetCalcRelevantObjects('*.TypLne') 48 | ``` 49 | 50 | ### Example 51 | This example script gets lists of buses, generators and lines in the network and then creates dictionaries for each element type (e.g. dictionary of buses, generators and lines). 52 | 53 | [\[Download example here\]](https://github.com/susantoj/powerfactory_python/blob/master/examples/element_dictionaries.py) 54 | 55 | ```python 56 | import powerfactory as pf 57 | 58 | app = pf.GetApplication() 59 | app.ClearOutputWindow() 60 | 61 | # Create dictionary of buses 62 | bus_dict = {} 63 | buses = app.GetCalcRelevantObjects('*.ElmTerm') 64 | for bus in buses: 65 | bus_dict[bus.loc_name] = bus 66 | 67 | # Create dictionary of lines 68 | line_dict = {} 69 | lines = app.GetCalcRelevantObjects('*.ElmLne') 70 | for line in lines: 71 | line_dict[line.loc_name] = line 72 | 73 | # Create dictionary of synchronous generators 74 | gen_dict = {} 75 | gens = app.GetCalcRelevantObjects('*.ElmSym') 76 | for gen in gens: 77 | gen_dict[gen.loc_name] = gen 78 | 79 | # Loop through generator dictionary and print key and generator object 80 | for gen_key in gen_dict.keys(): 81 | app.PrintPlain(gen_key) 82 | app.PrintPlain(gen_dict[gen_key]) 83 | ``` 84 | -------------------------------------------------------------------------------- /docs/vectors_and_matrices.md: -------------------------------------------------------------------------------- 1 | ## Accessing and manipulating vectors and matrices 2 | 3 | One-dimensional (vector) and two-dimensional (matrix) data can be used in a variety of ways in PowerFactory, for example: 4 | 5 | * Reactive power capability curves (matrix) 6 | * Characteristics (vector and matrix) 7 | * Thermal ratings (vector and matrix) 8 | * Tower types (matrix) 9 | 10 | Retrieving and setting vector and matrix data via Python is done by the `GetAttribute` and `SetAttribute` functions that is inherited from the base PowerFactory dataobject class. 11 | 12 | ***Important note***: PowerFactory vector and matrices are represented in Python as lists and lists of lists respectively. 13 | 14 | ## Retrieving Data 15 | 16 | The GetAttribute function is used to get the data and put it into a variable. The GetAttribute function is called as follows: 17 | 18 | ```python 19 | value = obj.GetAttribute(parameter) 20 | ``` 21 | 22 | where `parameter` is a string with the variable name from which data will be retrieved and stored in `value` 23 | 24 | For example, consider a thermal rating object called "therm". We can get the thermal ratings data "MyMatrix" as follows: 25 | 26 | ```python 27 | data = therm.GetAttribute('MyMatrix') 28 | ``` 29 | 30 | ## Setting Data 31 | 32 | The SetAttribute function is used to put data into an object variable. The SetAttribute function is called as follows: 33 | 34 | ```python 35 | obj.SetAttribute(parameter, data) 36 | ``` 37 | 38 | where `parameter` is a string with the variable name into which `data` will be stored 39 | 40 | For example, consider a thermal rating object called "therm". We can set the "MyMatrix" variable as a 2 x2 matrix as follows: 41 | 42 | ```python 43 | data = 110, 150], [120, 140 44 | therm.SetAttribute('MyMatrix', data) 45 | ``` 46 | 47 | ## Example 48 | 49 | This example script creates a vector and matrix object, enters in dummy data and manipulates the matrix data. 50 | 51 | [\[ Download example here \]](https://github.com/susantoj/powerfactory_python/blob/master/examples/vectors_matrices.py) 52 | 53 | ```python 54 | import powerfactory as pf 55 | 56 | # Get PowerFactory application 57 | app = pf.GetApplication() 58 | app.ClearOutputWindow() 59 | 60 | # Get current study case 61 | study = app.GetActiveStudyCase() 62 | 63 | # Create PowerFactory vector and matrix objects 64 | vec = study.CreateObject('IntVec')[0] 65 | mat = study.CreateObject('IntMat')[0] 66 | 67 | # Print out objects 68 | app.PrintPlain(vec) 69 | app.PrintPlain(mat) 70 | 71 | # Generate dummy data for vector and matrix 72 | # Note that vector data is a list and matrix data is a list of lists 73 | vec_data = [1, 6, 8, 1, 5, 8] 74 | mat_data = [[5, 7, 3], [2, 4, 6], [6, 1, 0]] 75 | 76 | # Put dummy data into PowerFactory vector and matrix objects 77 | vec.SetAttribute('V', vec_data) 78 | mat.SetAttribute('M', mat_data) 79 | 80 | # Get data from matrix object and print it out 81 | data = mat.GetAttribute('M') 82 | app.PrintPlain(data) 83 | 84 | # Modify the (3,3) entry of the matrix 85 | data[2][2] = 10 86 | 87 | # Put modified data back into matrix 88 | mat.SetAttribute('M', data) 89 | ``` 90 | -------------------------------------------------------------------------------- /docs/project_folders.md: -------------------------------------------------------------------------------- 1 | ## Accessing project folders 2 | 3 | Each folder in the PowerFactory project tree (e.g. Library, Network Model, Diagrams, etc) are objects that can be accessed by Python code. 4 | 5 | PowerFactory contains a method GetProjectFolder that returns common project folders. The general usage is as follows: 6 | 7 | ```python 8 | import powerfactory as pf 9 | app = pf.GetApplication() 10 | prj_folder = app.GetProjectFolder(folder_string) 11 | ``` 12 | 13 | where `folder_string` is a string code for a particular project folder. 14 | 15 | PowerFactory supports the following `folder_strings`: 16 | 17 | | folder_string | Folder description | 18 | | ------------- | ---------------------- | 19 | | equip | Equipment type library | 20 | | netmod | Network model | 21 | | oplib | Operational library | 22 | | scen | Operational scenario | 23 | | script | Script library (local) | 24 | | study | Study case | 25 | | templ | Template | 26 | | netdat | Network data | 27 | | dia | Diagram | 28 | | scheme | Variation | 29 | | cbrat | CB rating | 30 | | therm | Thermal rating | 31 | | ra | Running arrangement | 32 | | mvar | MVAr limit curve | 33 | | outage | Outages (operational library) | 34 | | fault | Faults (operational library) | 35 | 36 | You will notice that not all of the folders in the project tree are not accessible by the GetProjectFolder function. Other folders can be accessed by using the GetParent and GetContents functions to traverse up and down the hierarchy respectively. 37 | 38 | For example, to get to the "User Defined Models" folder, one can traverse up from the equipment library (to get the main library folder) and then down to get the User Defined Models folder. 39 | 40 | ```python 41 | equip = app.GetProjectFolder('equip') 42 | lib_fold = equip.GetParent() 43 | udm_fold = lib_fold.GetContents('User Defined Models.IntPrjFolder')[0][0] 44 | ``` 45 | 46 | ## Example 47 | 48 | This example script returns objects for most of the commonly used folders in the PowerFactory project tree. 49 | 50 | [\[Download example here\]](https://github.com/susantoj/powerfactory_python/blob/master/examples/project_folders.py) 51 | 52 | ```python 53 | import powerfactory as pf 54 | 55 | app = pf.GetApplication() 56 | 57 | ######### 58 | # MODEL # 59 | ######### 60 | 61 | # Network model 62 | netmod = app.GetProjectFolder('netmod') 63 | 64 | # Network data 65 | netdata = app.GetProjectFolder('netdat') 66 | 67 | # Diagrams 68 | diag_fold = app.GetProjectFolder('dia') 69 | 70 | # Variations 71 | var_fold = app.GetProjectFolder('scheme') 72 | 73 | ########### 74 | # LIBRARY # 75 | ########### 76 | 77 | # Equipment library 78 | equip = app.GetProjectFolder('equip') 79 | lines_fold = equip.GetContents('Lines')[0][0] 80 | 81 | # User defined models 82 | main_lib = equip.GetParent() 83 | udm = main_lib.GetContents('User Defined Models*')[0][0] 84 | 85 | # Script library 86 | script_lib = app.GetProjectFolder('script') 87 | 88 | # Template library 89 | templ_lib = app.GetProjectFolder('templ') 90 | 91 | # Operational library 92 | oplib = app.GetProjectFolder('oplib') 93 | 94 | # Characteristics 95 | opchar = oplib.GetContents('Characteristics')[0][0] 96 | 97 | # Thermal ratings 98 | therm_fold = app.GetProjectFolder('therm') 99 | 100 | # MVAr limit curves 101 | mvar_fold = app.GetProjectFolder('mvar') 102 | 103 | ############## 104 | # STUDY CASE # 105 | ############## 106 | 107 | # Study cases 108 | op_scen = app.GetProjectFolder('scen') 109 | 110 | # Operational scenarios 111 | sc_fold = app.GetProjectFolder('study') 112 | ``` 113 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | --------------------------------------------------------------------------------