Skip to content

Commit

Permalink
Add option to receive default parameters for sensitivity analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkleiven committed Nov 18, 2023
1 parent 1a1a1b5 commit 47f3672
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
30 changes: 30 additions & 0 deletions src/main/ApiUtil.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.github.statnett.loadflowservice

import com.fasterxml.jackson.databind.ObjectMapper
import com.powsybl.iidm.network.Network
import com.powsybl.nad.NetworkAreaDiagram
import com.powsybl.sensitivity.SensitivityAnalysisParameters
import com.powsybl.sensitivity.json.SensitivityJsonModule
import com.powsybl.sld.SingleLineDiagram
import com.powsybl.sld.SldParameters
import io.github.oshai.kotlinlogging.KotlinLogging
Expand Down Expand Up @@ -105,4 +108,31 @@ fun loadNames(network: Network): List<String> {

fun branchNames(network: Network): List<String> {
return network.lines.map { line -> line.nameOrId }
}

fun defaultSensitivityAnalysisParameters(): String {
val mapper = ObjectMapper()
mapper.registerModule(SensitivityJsonModule())
return mapper.writeValueAsString(SensitivityAnalysisParameters())
}

class InvalidParameterSet(message: String) : Exception(message)

fun defaultParameterSet(name: String): String {
val loadParams = "load-params"
val sensitivityAnalysisParams = "sensitivity-analysis-params"
return when (name) {
loadParams -> {
defaultLoadFlowParameters()
}

sensitivityAnalysisParams -> {
defaultSensitivityAnalysisParameters()
}

else -> {
val allowed = listOf(loadParams, sensitivityAnalysisParams)
throw InvalidParameterSet("Unknown parameters set $name. Must be one of $allowed")
}
}
}
5 changes: 3 additions & 2 deletions src/main/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ fun Application.module() {
call.respond(branchNames(network))
}

get("/default-load-parameters") {
call.respondText(defaultLoadFlowParameters(), ContentType.Application.Json, HttpStatusCode.OK)
get("/default-values/{parameter-set}") {
val parameterSet = call.parameters["parameter-set"] ?: ""
call.respondText(defaultParameterSet(parameterSet))
}

post("/substation-names") {
Expand Down
11 changes: 9 additions & 2 deletions src/main/ExceptionHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ import io.ktor.server.application.*
import io.ktor.server.response.*

class ExceptionHandler {
suspend fun handle(call: ApplicationCall, cause: Throwable) {
suspend fun handle(call: ApplicationCall, cause: Throwable) {
when (cause) {
is NoFileProvidedException -> {
call.respondText (
call.respondText(
"$cause\nStack trace: ${cause.stackTraceToString()}",
status = HttpStatusCode.UnprocessableEntity,
)
}

is InvalidParameterSet -> {
call.respondText(
"$cause",
status = HttpStatusCode.NotFound
)
}

else -> {
call.respondText(
"500: $cause.\nStack trace: ${cause.stackTraceToString()}",
Expand Down
2 changes: 1 addition & 1 deletion src/main/Solver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fun warnOnFewAvailableImporters() {
}
}

class NoFileProvidedException(message: String): Exception(message)
class NoFileProvidedException(message: String) : Exception(message)

fun networkFromFirstFile(files: List<FileContent>): Network {
if (files.isEmpty()) {
Expand Down
22 changes: 21 additions & 1 deletion src/test/AppTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class ApplicationTest {
@Test
fun `test default load parameters`() =
testApplication {
val response = client.get("/default-load-parameters")
val response = client.get("/default-values/load-params")
assertEquals(response.status, HttpStatusCode.OK)

val body: String = response.bodyAsText()
Expand Down Expand Up @@ -323,6 +323,26 @@ class ApplicationTest {
}
}

@Test
fun `test response 200 and some known content for default sensitivity parameters`() {
testApplication {
val response = client.get("/default-values/sensitivity-analysis-params")
assertEquals(HttpStatusCode.OK, response.status)
val body = response.bodyAsText()
assertTrue(body.contains("flow-voltage-sensitivity-value-threshold"))
}
}

@Test
fun `test response 404 on unknown parameter set`() {
testApplication {
val response = client.get("/default-values/non-existing-params")
assertEquals(HttpStatusCode.NotFound, response.status)
val body = response.bodyAsText()
assertTrue(body.contains("InvalidParameterSet"))
}
}


}

Expand Down

0 comments on commit 47f3672

Please sign in to comment.