Skip to content

Commit

Permalink
WIP: Add sensitivity analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkleiven committed Oct 8, 2023
1 parent 7f61c8f commit e59411b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/main/App.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.statnett.loadflowservice

import com.powsybl.sensitivity.SensitivityAnalysis
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
Expand Down Expand Up @@ -111,6 +112,24 @@ fun Application.module() {
call.respondText(diagram, ContentType.Image.SVG, HttpStatusCode.OK)
}
}

post("/sensitivity-analysis") {
val loadParamCnt = LoadParameterContainer()
val sensParamCnt = SensitivityAnalysisParametersContainer()
val sensFactorCnt = SensitivityFactorContainer()
val contingencyCnt = ContingencyListContainer()
val itemHandler = MultiFormItemLoaders(listOf(loadParamCnt, sensParamCnt, sensFactorCnt, contingencyCnt))

val files = multiPartDataHandler(call.receiveMultipart(), itemHandler::formItemHandler)
if (files.isEmpty()) {
call.response.status(HttpStatusCode.UnprocessableEntity)
} else {
sensParamCnt.parameters.setLoadFlowParameters(loadParamCnt.parameters)
val network = networkFromFileContent(files[0])
val result = runSensitivityAnalysis(network, sensFactorCnt.factors, sensParamCnt.parameters, contingencyCnt.contingencies)
call.respondText(result, ContentType.Application.Json, HttpStatusCode.OK)
}
}
swaggerUI(path = "openapi", swaggerFile = "openapi/documentation.yaml")
}
}
3 changes: 2 additions & 1 deletion src/main/ContingencyListContainer.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.github.statnett.loadflowservice

import com.powsybl.contingency.contingency.list.ContingencyList
import com.powsybl.contingency.contingency.list.DefaultContingencyList
import com.powsybl.contingency.json.JsonContingencyListLoader
import io.github.oshai.kotlinlogging.KotlinLogging
import io.ktor.http.content.*

private val logger = KotlinLogging.logger {}

class ContingencyListContainer : AutoVersionableJsonParser(), FormItemLoadable {
var contingencies: ContingencyList? = null
var contingencies: ContingencyList = DefaultContingencyList()

override fun currentVersion(): String {
return ContingencyList.VERSION
Expand Down
10 changes: 10 additions & 0 deletions src/main/MultiFormItemLoaders.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.statnett.loadflowservice

import io.ktor.http.content.*

class MultiFormItemLoaders(private val loaders: List<FormItemLoadable>): FormItemLoadable {

override fun formItemHandler(part: PartData.FormItem) {
this.loaders.forEach {loader -> loader.formItemHandler(part) }
}
}
29 changes: 29 additions & 0 deletions src/main/Solver.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.github.statnett.loadflowservice

import com.powsybl.commons.PowsyblException
import com.powsybl.commons.json.JsonUtil
import com.powsybl.commons.reporter.Reporter
import com.powsybl.commons.reporter.ReporterModel
import com.powsybl.computation.local.LocalComputationManager
import com.powsybl.contingency.contingency.list.ContingencyList
import com.powsybl.iidm.network.*
import com.powsybl.loadflow.LoadFlow
import com.powsybl.loadflow.LoadFlowParameters
import com.powsybl.loadflow.json.JsonLoadFlowParameters
import com.powsybl.sensitivity.*
import com.powsybl.sensitivity.json.SensitivityJsonModule
import io.github.oshai.kotlinlogging.KotlinLogging
import java.io.ByteArrayOutputStream
import java.io.StringWriter
Expand Down Expand Up @@ -90,3 +94,28 @@ fun solve(
report = reporterToString(reporter)
)
}

fun runSensitivityAnalysis(network: Network, factors: List<SensitivityFactor>, params: SensitivityAnalysisParameters, contingenciesList: ContingencyList): String {
val reporter = ReporterModel("sensitivity", "")
val variableSets: List<SensitivityVariableSet> = listOf()
val contingencies = contingenciesList.getContingencies(network)
val factorReader = SensitivityFactorModelReader(factors, network)

val factory = JsonUtil.createJsonFactory()
val writer = StringWriter()
val jsonGenerator = factory.createGenerator(writer)
val resultWriter = SensitivityResultJsonWriter(jsonGenerator, contingencies)

SensitivityAnalysis.run(
network,
network.variantManager.workingVariantId,
factorReader,
resultWriter,
contingencies,
variableSets,
params,
LocalComputationManager.getDefault(),
reporter
)
return resultWriter.toString()
}

0 comments on commit e59411b

Please sign in to comment.