From ed063c336222282e2c56e2be00205aa0dc44ac6f Mon Sep 17 00:00:00 2001 From: David Kleiven Date: Sun, 8 Oct 2023 16:49:01 +0200 Subject: [PATCH] Add sensitivity factor container --- src/main/SensitivityFactorContainer.kt | 55 ++++++++++++++++++++++ src/test/DataFactory.kt | 6 +++ src/test/SensitivityFactorContainerTest.kt | 13 ++++- 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/main/SensitivityFactorContainer.kt diff --git a/src/main/SensitivityFactorContainer.kt b/src/main/SensitivityFactorContainer.kt new file mode 100644 index 0000000..34fcb97 --- /dev/null +++ b/src/main/SensitivityFactorContainer.kt @@ -0,0 +1,55 @@ +package com.github.statnett.loadflowservice + +import com.powsybl.contingency.ContingencyContext +import com.powsybl.contingency.ContingencyContextType +import com.powsybl.sensitivity.SensitivityFactor +import com.powsybl.sensitivity.SensitivityFunctionType +import com.powsybl.sensitivity.SensitivityVariableType +import io.github.oshai.kotlinlogging.KotlinLogging +import io.ktor.http.content.* +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.Json + +private val logger = KotlinLogging.logger {} + +@Serializable +class AutoSerializableSensitivityFactor( + private val functionType: String, + private val functionId: String, + private val variableType: String, + private val variableId: String, + private val variableSet: Boolean, + private val contingencyContextType: String +) { + fun asSensitivityFactor(): SensitivityFactor { + val ctgType = ContingencyContextType.valueOf(contingencyContextType) + return SensitivityFactor( + SensitivityFunctionType.valueOf(functionType), functionId, + SensitivityVariableType.valueOf(variableType), variableId, variableSet, + + // TODO: Check how contingencyId is passed when type is SPECIAL + ContingencyContext.create(null, ctgType) + ) + } +} + +typealias AutoSerializableSensitivityFactorList = List + +class SensitivityFactorContainer : FormItemLoadable { + var factors: List = listOf() + + fun update(jsonString: String) { + this.factors = Json.decodeFromString(jsonString).map { item -> + item.asSensitivityFactor() + } + } + + override fun formItemHandler(part: PartData.FormItem) { + val name = part.name ?: "" + if (name == "sensitivity-factors") { + this.update(part.value) + logger.info { "Received sensitivity factors parameters: ${part.value}" } + } + } + +} \ No newline at end of file diff --git a/src/test/DataFactory.kt b/src/test/DataFactory.kt index a2cb05a..13e684c 100644 --- a/src/test/DataFactory.kt +++ b/src/test/DataFactory.kt @@ -106,4 +106,10 @@ fun basicContingencyJson(): String { "\"contingencies\":[{\"id\":\"contingency\",\"elements\":[{\"id\":\"L7-8-0\",\"type\":\"BRANCH\"}," + "{\"id\":\"L9-8-0\",\"type\":\"BRANCH\"}]}," + "{\"id\":\"contingency2\",\"elements\":[{\"id\":\"B1-G\",\"type\":\"GENERATOR\"}]}]}") +} + +fun sensitivityFactorList(): String { + return "[{\"functionType\":\"BRANCH_ACTIVE_POWER_2\",\"functionId\":\"l\"," + + "\"variableType\":\"INJECTION_ACTIVE_POWER\",\"variableId\":\"g\",\"variableSet\":false," + + "\"contingencyContextType\":\"ALL\"}]" } \ No newline at end of file diff --git a/src/test/SensitivityFactorContainerTest.kt b/src/test/SensitivityFactorContainerTest.kt index 20ff6c6..724dd0f 100644 --- a/src/test/SensitivityFactorContainerTest.kt +++ b/src/test/SensitivityFactorContainerTest.kt @@ -1,2 +1,13 @@ +import com.github.statnett.loadflowservice.SensitivityFactorContainer +import kotlin.test.Test +import kotlin.test.assertEquals + class SensitivityFactorContainerTest { -} \ No newline at end of file + @Test + fun `load basic json`() { + val container = SensitivityFactorContainer() + container.update(sensitivityFactorList()) + assertEquals(1, container.factors.size) + + } +}