{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Parameter studies are organized in study cases, operation scenarios and variations in PowerFactory. This tutorials shows how to create parameter studies with ease and fully automated using the study cases interface of *powfacpy*.  \n",
    "\n",
    "First, we activate the PowerFactory project as in the *getting started* tutorial."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# If you use IPython/Jupyter:\n",
    "import sys\n",
    "sys.path.append(r'C:\\Program Files\\DIgSILENT\\PowerFactory 2023 SP3\\Python\\3.11') # you may use a different directory\n",
    "# Get the PF app\n",
    "import powerfactory\n",
    "app = powerfactory.GetApplication()\n",
    "\n",
    "import powfacpy\n",
    "app.Show()\n",
    "app.ActivateProject(r\"powfacpy\\powfacpy_tests_copy_where_tests_are_run\") # You may change the project path."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Simple Parameter Study\n",
    "We use the class `PFStudyCases`. This class inherits from `PFActiveProject`, so all methods from that class are available. We want to create study case with various values for the active and reactive power of a load. \n",
    "\n",
    "First we set the attribute `parameter_values` and define a dictionary with parameter names and a list of values. Each element in the lists is used in one study case. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "pfsc = powfacpy.PFStudyCases(app)\n",
    "pfsc.parameter_values = {\n",
    "    \"p HV load\":[ 1,  2, 1, 2],\n",
    "    \"q HV load\":[-1, -1, 1, 1],\n",
    "}"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we have defined four study cases and in the first case `p HV load` equals 1 and `q HV load` equals -1. The parameter names must not contain *?=\",\\~|()! as these characters cannot be used for names of objects in PF.\n",
    "\n",
    "Next we  connect the variables to the PF database by defining a dictionary with paths in the attribute `parameter_paths` using the same parameter names as keys. The paths include the attribute name (e.g. `plini` which is the active power of the load). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "pfsc.parameter_paths = {\n",
    "    \"p HV load\":r\"Network Model\\Network Data\"\n",
    "        r\"\\test_case_studies\\Grid 2\\General Load HV\\plini\", # This is one string split over several lines\n",
    "    \"q HV load\":r\"Network Model\\Network Data\"\n",
    "        r\"\\test_case_studies\\Grid 2\\General Load HV\\qlini\",\n",
    "}"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally we define the active grids. For now, we assume that for all cases exactly one grid is active (we look at more complicated cases later). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "pfsc.active_grids = r\"Network Model\\Network Data\\test_case_studies\\Grid 2\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's create the study cases (and corresponding operation scenarios) in a new folder named 'Autocreated':"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "pfsc.parent_folder_study_cases = pfsc.create_in_folder(\"Autocreated.IntFolder\", folder=pfsc.study_cases_folder)\n",
    "pfsc.parent_folder_scenarios = pfsc.create_in_folder(\"Autocreated.IntFolder\", folder=pfsc.operation_scenarios_folder)\n",
    "# Alternatively, just use\n",
    "pfsc.set_parent_folders_for_cases_scenarios_variations(\"Autocreated\")\n",
    "\n",
    "pfsc.create_cases()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This creates the following study cases in the 'Autocreated' folder:\n",
    "\n",
    "![](figures/cases_1.png)\n",
    "\n",
    "The names of the study cases are created according to the parameter names and their values (\"_\" is used instead of \"=\" which is not allowed in PF object names). Furthermore, the corresponding operation scenarios are created: \n",
    "\n",
    "![](figures/scenarios_1.png)\n",
    "\n",
    "For more complicated case studies with further parameters, it is advisable to structure the cases in folders. We define the attribute `hierarchy` using a list (in this case with onl yone item) of parameter names that corresponds to folder directories."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "pfsc.hierarchy = [\"q HV load\"]\n",
    "pfsc.create_cases()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now `q HV load` is used for the folder names and not in the study case name.\n",
    "\n",
    "![](figures/cases_2.png)\n",
    "\n",
    "The structure of the operation scenarios is similar."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Advanced Parameter Study\n",
    "Let's take a look at a more complex parameter study."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "pfsc = powfacpy.PFStudyCases(app)\n",
    "pfsc.parameter_values = {\n",
    "    \"p HV load\":[1, 2, 1, 2, 1, 2, 1, 2, ],\n",
    "    \"q HV load\":[-1, -1, 1, 1, -1, -1, 1, 1, ],\n",
    "    \"control\": [\"A\", \"A\", \"A\", \"A\", \"B\", \"B\", \"B\", \"B\", ]\n",
    "}\n",
    "pfsc.parameter_paths = {\n",
    "    \"p HV load\":r\"Network Model\\Network Data\"\n",
    "        r\"\\test_case_studies\\Grid 2\\General Load HV\\plini\", \n",
    "\n",
    "    \"q HV load\":r\"Network Model\\Network Data\"\n",
    "        r\"\\test_case_studies\\Grid 2\\General Load HV\\qlini\", \n",
    "\n",
    "}\n",
    "pfsc.active_grids = r\"Network Model\\Network Data\\test_case_studies\\Grid 2\"\n",
    "pfsc.set_parent_folders_for_cases_scenarios_variations(\"Autocreated\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An additional parameter `control` is defined. Note that there is no parameter path defined for this parameter. The parameter can be seen as a placeholder for the study cases structure and to do more complicated things than simply setting an attribute of a PF object later (we'll see below).\n",
    "\n",
    "By default, study cases and operation scenarios are created. If variations should be created in addition, use:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "pfsc.add_variation_to_each_case = True"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This can be necessary because some parameters, like attributes of DSL objects, are stored in variants and not in operation scenarios.\n",
    "Let's again create the study cases."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "pfsc.hierarchy = [\"control\", \"q HV load\"]\n",
    "pfsc.create_cases()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This creates the following structure in the network variations (as well as in the scenarios and study cases):\n",
    "\n",
    "![](figures/variations_3.png)\n",
    "\n",
    "Let's set further parameters, add plots (using plot interface) and simulate (using dyn. simulation interface) for each study case. The study case objects are stored in the `study_cases` attribute. We can iterate through the cases and set parameters depending on the previously defined `control` parameter.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "pfpi = powfacpy.PFPlotInterface(app)\n",
    "pfds = powfacpy.PFDynSimInterface(app)\n",
    "dsl_controller_obj = pfsc.get_unique_obj(r\"Network Model\\Network Data\"\n",
    "    r\"\\test_case_studies\\Grid 2\\WECC WT Control System Type 4A\\REEC_A Electrical Control Model\")\n",
    "for case_num, study_case_obj in enumerate(pfsc.study_cases):\n",
    "    study_case_obj.Activate()\n",
    "    # Set controller parameters\n",
    "    if pfsc.get_value_of_parameter_for_case(\"control\", case_num) == \"A\":\n",
    "        pfsc.set_attr(dsl_controller_obj, {\"PfFlag\":0, \"VFlag\":1}) \n",
    "    elif pfsc.get_value_of_parameter_for_case(\"control\", case_num) == \"B\":\n",
    "        pfsc.set_attr(dsl_controller_obj, {\"PfFlag\":1, \"VFlag\":0}) \n",
    "    # Prepare plots\n",
    "    pfpi.clear_plot_pages()\n",
    "    pfpi.set_active_plot(\"Reactive current\", \"WPP\")\n",
    "    pfpi.plot(dsl_controller_obj, \"s:Iqcmd\")\n",
    "    # Simulate   \n",
    "    pfds.initialize_and_run_sim() "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's walk through this piece of code. The plotting and simulation interfaces are instantiated and a DSL object is assigned to be used later. \n",
    "\n",
    "It is iterated through the study cases objects (and case numbers) and each study case is activated. Then the controller parameters are set. The value of the `control` parameter is obtained using the method `get_value_of_parameter_for_case` and the controller parameters are set depending on the controller type (`A` or `B`). This provides the flexibility to adjust the study cases depending on previously defined parameters in any way (e.g. set parameters, activate grids, ...). And all settings are stored in the previously defined operation scenarios/variations.\n",
    "\n",
    "Finally, a plot is created for every case and the dynamic simulation is executed.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Compare Study Cases\n",
    "It is often required to compare the results of cases. In the following, we use the previously defined cases to compare the simulation results for cases where the parameter `p HV load` equals 1 (note that you may have to \"Rebuild\" ![](figures/rebuild_button.png) the plot in the upper left for PF to show it correctly):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a study case where results from other cases are compared in a plot\n",
    "study_case_comparison = pfsc.create_by_path(r\"Study Cases\\Autocreated\\Comparison.IntCase\")\n",
    "study_case_comparison.Activate()\n",
    "pfpi.clear_plot_pages()\n",
    "pfpi.set_active_plot(\"Reactive current controller setpoint for p HV load = 1\", \"Comparison \")\n",
    "dsl_controller_obj = (r\"Network Model\\Network Data\\test_case_studies\" \n",
    "    r\"\\Grid 2\\WECC WT Control System Type 4A\\REEC_A Electrical Control Model\")\n",
    "for case_num, study_case_obj in enumerate(pfsc.study_cases):  \n",
    "    if pfsc.get_value_of_parameter_for_case(\"p HV load\", case_num) == 1:\n",
    "        # Get results object from respective study case \n",
    "        results_obj = pfsc.get_unique_obj(\n",
    "            \"*.ElmRes\", parent_folder=study_case_obj)\n",
    "        # Create label for plot\n",
    "        case_label = pfsc.get_case_params_value_string(case_num,\n",
    "            omitted_parameters=\"p HV load\",\n",
    "            delimiter=\" | \",\n",
    "            equals_sign=\"=\") \n",
    "        label = \"Iq setpoint (\" + case_label + \")\"\n",
    "        # Plot\n",
    "        pfpi.plot(dsl_controller_obj, \"s:Iqcmd\",\n",
    "            results_obj=results_obj,\n",
    "            label=label)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This creates a plot with legend:\n",
    "\n",
    "![](figures/legend_1.png)\n",
    "\n",
    "Let's walk through the code. After creating a study case where the results are compared in a plot, it is again iterated through all cases. For cases where `p HV load` has a certain value, the result object is fetched. Next we want to create a label for the curve in the plot. We use the method `get_case_params_value_string` with certain options for the delimiter and the equals symbol (here we have more options than for PF object names, i.e. `=` and `|` are allowed). Moreover, the parameter `p HV load` is omitted because it is the same for all compared cases (i.e. 1, see condition). Further information on the variable is added to the label and the curve is finally added to the plot.\n",
    "\n",
    "An alternative to iterating through all study cases is to get certain cases using the method `get_study_cases`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "study_cases_p_HV_load_equals_1 = pfsc.get_study_cases({\"p HV load\":lambda x: x==1})\n",
    "for study_case_obj in study_cases_p_HV_load_equals_1:\n",
    "    pass\n",
    "    # do something with the specific cases\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`get_study_cases` takes a dictionary as input, where lambda functions with conditions for parameters are defined. Note that the methods `get_value_of_parameter_for_case` and `get_case_params_value_string` also work with study case objects as inputs instead of case numbers."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A very convenient yet powerful alternative to 'get_study_cases' especially for complicated conditionals is 'get_study_cases_from_string'. This method simply accepts a string which can contain complicated conditionals:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "study_cases_from_conditionals = pfsc.get_study_cases_from_string(\n",
    "    \"p HV load >= 2 and (control == 'A' or q HV load != 1)\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That makes it very easy to plot and compare certain cases."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Permutation\n",
    "It is also possible to create study cases for the permutation of the parameters (i.e. all combinations).\n",
    "This is achieved with the method `apply_permutation`. Define the parameter values first (and also the hierarchy if applicable) and then call the method. Make sure that the PF application is hidden to improve the performance (if not hidden, this can take minutes and otherwise only seconds - the 'try-finally' block ensures that the app is always shown in the end even if an exception is thrown): "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    pfsc = powfacpy.PFStudyCases(app)\n",
    "    pfsc.app.Hide()\n",
    "    pfsc.parameter_values = {\n",
    "            \"p HV load\":[1, 2, 3],\n",
    "            \"q HV load\":[-1, 1],\n",
    "            \"control 1\": [\"A\", \"B\"],\n",
    "            \"control 2\": [\"R\", \"S\"],\n",
    "        }\n",
    "    pfsc.hierarchy = [\"p HV load\", \"control 1\", ]\n",
    "    pfsc.apply_permutation()\n",
    "    pfsc.set_parent_folders_for_cases_scenarios_variations(\"Autocreated\")\n",
    "    pfsc.clear_parent_folders()\n",
    "    pfsc.create_cases()\n",
    "finally:     \n",
    "    pfsc.app.Show()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`apply_permutation` irreversibly replaces the values in `parameter_values` with the permutation. \n",
    "\n",
    "It may be desirable to use the permutation but omit certain combinations. These omitted combinations can be defined in a list of dictionaries and used as a keyword argument as follows (note that we clear the parent folders from the formerly created objects first): "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    app.Hide()\n",
    "    omitted_combinations = [\n",
    "        {\"q HV load\": [1], \"control 2\": [\"R\"]},\n",
    "        {\"p HV load\": [2, 3], \"control 1\": \"all\"},\n",
    "    ]\n",
    "    pfsc.apply_permutation(omitted_combinations=omitted_combinations)\n",
    "    pfsc.clear_parent_folders()\n",
    "    pfsc.create_cases()\n",
    "finally:\n",
    "    app.Show()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The first dictionary causes any combinations where \"q HV load\" is 1  and \"control 2\" is \"R\" to be omitted (these cases will not be created when `create_cases` is called). In the second dictionary, the keyword \"all\" is used. This means that all combinations with this parameter are omitted."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Further functionality\n",
    "The attributes of `PFStudyCases` instances allow to set further options. \n",
    "\n",
    "The study case names can be numbered consecutively setting `consecutively_number_case_names`. \n",
    "\n",
    "Sometimes it is convenient to use only parameter values without the parameter names for the folder/study case names (imagine the value of a parameter is expressive enough and you don't want to use \"Controller_GridCode2012Controller\" but only \"GridCode2012Controller\" instead). This can be achieved by adding parameters to the list of `anonymous_parameters` (e.g. `pfsc.anonymous_parameters=[\"Controller\",\"FurtherParName\"]`).\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.4 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "20b4f2ae9a71ebfc0af8cf0167d05f9c8f5c94fe23811db744e66ae1fdb328c7"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
