Skip to content

Commit

Permalink
Add sensitivity factor container
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkleiven committed Oct 9, 2023
1 parent f56ea7c commit ed063c3
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
55 changes: 55 additions & 0 deletions src/main/SensitivityFactorContainer.kt
Original file line number Diff line number Diff line change
@@ -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<AutoSerializableSensitivityFactor>

class SensitivityFactorContainer : FormItemLoadable {
var factors: List<SensitivityFactor> = listOf()

fun update(jsonString: String) {
this.factors = Json.decodeFromString<AutoSerializableSensitivityFactorList>(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}" }
}
}

}
6 changes: 6 additions & 0 deletions src/test/DataFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}]"
}
13 changes: 12 additions & 1 deletion src/test/SensitivityFactorContainerTest.kt
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
import com.github.statnett.loadflowservice.SensitivityFactorContainer
import kotlin.test.Test
import kotlin.test.assertEquals

class SensitivityFactorContainerTest {
}
@Test
fun `load basic json`() {
val container = SensitivityFactorContainer()
container.update(sensitivityFactorList())
assertEquals(1, container.factors.size)

}
}

0 comments on commit ed063c3

Please sign in to comment.