Skip to content

Commit

Permalink
Merge pull request #1068 from ie3-institute/mh/#1065-Java-to-Scala-Du…
Browse files Browse the repository at this point in the history
…rations

Change Java Durations into Scala Durations
  • Loading branch information
sebastian-peter authored Feb 19, 2025
2 parents 1c389ab + bc30b36 commit 5f0fc63
Show file tree
Hide file tree
Showing 19 changed files with 45 additions and 59 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Changed `pvInput` values in `PvInputTestData` to more realistic values [#1144](https://github.com/ie3-institute/simona/issues/1144)
- Refactor `RuntimeConfig` [#1172](https://github.com/ie3-institute/simona/issues/1172)
- Renamed some methods and variables within `ThermalGrid` and `ThermalHouse` [#1193](https://github.com/ie3-institute/simona/issues/1193)
- Replaced Java Durations with Scala Durations [#1068](https://github.com/ie3-institute/simona/issues/1068)

### Fixed
- Fix rendering of references in documentation [#505](https://github.com/ie3-institute/simona/issues/505)
Expand Down
17 changes: 9 additions & 8 deletions src/main/scala/edu/ie3/simona/agent/grid/DBFSAlgorithm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ import org.apache.pekko.actor.typed.scaladsl.{
StashBuffer,
}
import org.apache.pekko.actor.typed.{ActorRef, ActorSystem, Behavior, Scheduler}
import org.apache.pekko.util.{Timeout => PekkoTimeout}
import org.apache.pekko.util.Timeout
import org.slf4j.Logger
import squants.Each

import java.time.{Duration, ZonedDateTime}
import java.time.ZonedDateTime
import java.util.UUID
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success}

Expand Down Expand Up @@ -1122,13 +1123,13 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {
sweepValueStore: Option[SweepValueStore],
nodeToAssetAgents: Map[UUID, Set[ActorRef[ParticipantAgent.Request]]],
refSystem: RefSystem,
askTimeout: Duration,
askTimeout: FiniteDuration,
)(implicit
ctx: ActorContext[GridAgent.Request]
): Boolean = {
implicit val ec: ExecutionContext = ctx.executionContext

implicit val timeout: PekkoTimeout = PekkoTimeout.create(askTimeout)
implicit val timeout: Timeout = Timeout(askTimeout)
implicit val system: ActorSystem[_] = ctx.system

ctx.log.debug(s"asking assets for power values: {}", nodeToAssetAgents)
Expand Down Expand Up @@ -1207,11 +1208,11 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {
currentSweepNo: Int,
subGridGateToActorRef: Map[SubGridGate, ActorRef[GridAgent.Request]],
inferiorGridGates: Seq[SubGridGate],
askTimeout: Duration,
askTimeout: FiniteDuration,
)(implicit
ctx: ActorContext[GridAgent.Request]
): Boolean = {
implicit val timeout: PekkoTimeout = PekkoTimeout.create(askTimeout)
implicit val timeout: Timeout = Timeout(askTimeout)
implicit val ec: ExecutionContext = ctx.executionContext
implicit val scheduler: Scheduler = ctx.system.scheduler

Expand Down Expand Up @@ -1278,11 +1279,11 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {
currentSweepNo: Int,
subGridGateToActorRef: Map[SubGridGate, ActorRef[GridAgent.Request]],
superiorGridGates: Vector[SubGridGate],
askTimeout: Duration,
askTimeout: FiniteDuration,
)(implicit
ctx: ActorContext[GridAgent.Request]
): Boolean = {
implicit val timeout: PekkoTimeout = PekkoTimeout.create(askTimeout)
implicit val timeout: Timeout = Timeout(askTimeout)
implicit val ec: ExecutionContext = ctx.executionContext
implicit val scheduler: Scheduler = ctx.system.scheduler

Expand Down
9 changes: 4 additions & 5 deletions src/main/scala/edu/ie3/simona/agent/grid/GridAgent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ import org.apache.pekko.actor.typed.scaladsl.{Behaviors, StashBuffer}
import org.apache.pekko.actor.typed.{ActorRef, Behavior}

import java.time.ZonedDateTime
import java.time.temporal.ChronoUnit
import java.util.UUID
import scala.language.postfixOps
import scala.language.{implicitConversions, postfixOps}

object GridAgent extends DBFSAlgorithm {

Expand All @@ -55,9 +54,9 @@ object GridAgent extends DBFSAlgorithm {
context.messageAdapter[Activation](msg => WrappedActivation(msg))

// val initialization
val resolution: Long = simonaConfig.simona.powerflow.resolution.get(
ChronoUnit.SECONDS
) // this determines the agents regular time bin it wants to be triggered e.g. one hour

// this determines the agents regular time bin it wants to be triggered e.g. one hour
val resolution: Long = simonaConfig.simona.powerflow.resolution.toSeconds

val simStartTime: ZonedDateTime = TimeUtil.withDefaults
.toZonedDateTime(simonaConfig.simona.time.startDateTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

package edu.ie3.simona.agent.grid

import java.time.Duration
import scala.concurrent.duration.FiniteDuration

/** Holds all power flow configuration parameters used in
* [[edu.ie3.simona.agent.grid]]
Expand All @@ -29,6 +29,6 @@ final case class PowerFlowParams(
maxSweepPowerDeviation: Double,
epsilon: Vector[Double],
maxIterations: Int,
sweepTimeout: Duration,
sweepTimeout: FiniteDuration,
stopOnFailure: Boolean,
)
19 changes: 5 additions & 14 deletions src/main/scala/edu/ie3/simona/config/ConfigFailFast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import tech.units.indriya.unit.Units

import java.time.ZonedDateTime
import java.time.format.DateTimeParseException
import java.time.temporal.ChronoUnit
import java.util.UUID
import scala.util.{Failure, Success, Try}

Expand Down Expand Up @@ -772,19 +771,11 @@ object ConfigFailFast extends LazyLogging {
): Unit = {

// check if time bin is not smaller than in seconds
if (
(powerFlow.resolution.getUnits.contains(
ChronoUnit.NANOS
) && powerFlow.resolution.getNano != 0) ||
(powerFlow.resolution.getUnits.contains(
ChronoUnit.MICROS
) && powerFlow.resolution
.get(ChronoUnit.MICROS) != 0) ||
(powerFlow.resolution.getUnits.contains(
ChronoUnit.MILLIS
) && powerFlow.resolution
.get(ChronoUnit.MILLIS) != 0)
) {
val hasNanos = (powerFlow.resolution.toNanos / 1e9) % 1 != 0
val hasMicros = (powerFlow.resolution.toMicros / 1e6) % 1 != 0
val hasMillis = (powerFlow.resolution.toMillis / 1e3) % 1 != 0

if (hasNanos || hasMicros || hasMillis) {
throw new InvalidConfigParameterException(
s"Invalid time resolution. Please ensure, that " +
s"the time resolution for power flow calculation is at least rounded to a full second!"
Expand Down
14 changes: 3 additions & 11 deletions src/main/scala/edu/ie3/simona/config/SimonaConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import pureconfig.error._
import pureconfig.generic.ProductHint
import pureconfig.generic.auto._

import java.time.Duration
import scala.concurrent.duration.{DurationInt, FiniteDuration}
import scala.language.implicitConversions
import scala.util.Try

final case class SimonaConfig(
simona: SimonaConfig.Simona
Expand All @@ -29,13 +28,6 @@ object SimonaConfig {
implicit def productHint[T]: ProductHint[T] =
ProductHint[T](ConfigFieldMapping(CamelCase, CamelCase))

// TODO: replace with finite duration
implicit def durationConvert: ConfigConvert[Duration] =
ConfigConvert.viaStringTry(
str => Try(Duration.parse(("PT" + str).toUpperCase)),
x => x.toString,
)

/** Method to extract a config from a [[pureconfig.ConfigReader.Result]]
* @param either
* that may contain a config
Expand Down Expand Up @@ -382,9 +374,9 @@ object SimonaConfig {
final case class Powerflow(
maxSweepPowerDeviation: Double,
newtonraphson: Powerflow.Newtonraphson,
resolution: Duration = Duration.ofHours(1),
resolution: FiniteDuration = 1.hours,
stopOnFailure: Boolean = false,
sweepTimeout: Duration = Duration.ofSeconds(30),
sweepTimeout: FiniteDuration = 30.seconds,
)
object Powerflow {
final case class Newtonraphson(
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/edu/ie3/simona/agent/em/EmAgentIT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class EmAgentIT
TimeUtil.withDefaults.toZonedDateTime("2020-01-02T02:00:00Z")

private val resolution =
simonaConfig.simona.powerflow.resolution.getSeconds
simonaConfig.simona.powerflow.resolution.toSeconds

private val outputConfigOn = NotifierConfig(
simulationResultInfo = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ import squants.energy.Megawatts
import squants.{Dimensionless, Each}
import tech.units.indriya.ComparableQuantity

import java.time.{Duration, ZonedDateTime}
import java.time.ZonedDateTime
import java.util.UUID
import javax.measure.quantity.Angle
import scala.concurrent.duration.DurationInt
import scala.jdk.CollectionConverters.SetHasAsJava
import scala.language.implicitConversions

Expand Down Expand Up @@ -394,7 +395,7 @@ class PowerFlowSupportSpec
1e-5,
Vector(1e-12),
50,
Duration.ofMinutes(30),
30.minutes,
stopOnFailure = true,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ThermalGridIT
TimeUtil.withDefaults.toZonedDateTime("2020-01-02T02:00:00Z")

private val resolution =
simonaConfig.simona.powerflow.resolution.getSeconds
simonaConfig.simona.powerflow.resolution.toSeconds

private val outputConfigOn = NotifierConfig(
simulationResultInfo = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class FixedFeedInAgentModelCalculationSpec
voltageSensitiveInput.getUuid
)
private val services = Iterable.empty
private val resolution = simonaConfig.simona.powerflow.resolution.getSeconds
private val resolution = simonaConfig.simona.powerflow.resolution.toSeconds

"A fixed feed in agent with model calculation " should {
val initStateData = ParticipantInitializeStateData[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class HpAgentModelCalculationSpec
private val services = Iterable(
ActorWeatherService(weatherService.ref)
)
private val resolution = simonaConfig.simona.powerflow.resolution.getSeconds
private val resolution = simonaConfig.simona.powerflow.resolution.toSeconds

"A heat pump agent depending on no services" should {
val initStateData = ParticipantInitializeStateData[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class LoadAgentFixedModelCalculationSpec
voltageSensitiveInput.getUuid
)
private val services = Iterable.empty
private val resolution = simonaConfig.simona.powerflow.resolution.getSeconds
private val resolution = simonaConfig.simona.powerflow.resolution.toSeconds

private implicit val powerTolerance: squants.Power = Watts(0.1)
private implicit val reactivePowerTolerance: ReactivePower = Vars(0.1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class LoadAgentProfileModelCalculationSpec
voltageSensitiveInput.getUuid
)
private val services = Iterable.empty
private val resolution = simonaConfig.simona.powerflow.resolution.getSeconds
private val resolution = simonaConfig.simona.powerflow.resolution.toSeconds

private implicit val powerTolerance: squants.Power = Watts(0.1)
private implicit val reactivePowerTolerance: ReactivePower = Vars(0.1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ParticipantAgent2ListenerSpec
secondaryDataServices = services,
simulationStartDate = defaultSimulationStart,
simulationEndDate = defaultSimulationEnd,
resolution = simonaConfig.simona.powerflow.resolution.getSeconds,
resolution = simonaConfig.simona.powerflow.resolution.toSeconds,
requestVoltageDeviationThreshold =
simonaConfig.simona.runtime.participant.requestVoltageDeviationThreshold,
outputConfig = outputConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class ParticipantAgentExternalSourceSpec
flexResult = false,
)

private val resolution = simonaConfig.simona.powerflow.resolution.getSeconds
private val resolution = simonaConfig.simona.powerflow.resolution.toSeconds

private implicit val powerTolerance: Power = Watts(0.1)
private implicit val reactivePowerTolerance: ReactivePower = Vars(0.1)
Expand All @@ -139,7 +139,7 @@ class ParticipantAgentExternalSourceSpec
secondaryDataServices = Iterable.empty,
simulationStartDate = defaultSimulationStart,
simulationEndDate = defaultSimulationEnd,
resolution = simonaConfig.simona.powerflow.resolution.getSeconds,
resolution = simonaConfig.simona.powerflow.resolution.toSeconds,
requestVoltageDeviationThreshold =
simonaConfig.simona.runtime.participant.requestVoltageDeviationThreshold,
outputConfig = defaultOutputConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class PvAgentModelCalculationSpec
private val withServices = Iterable(
ActorWeatherService(weatherService.ref)
)
private val resolution = simonaConfig.simona.powerflow.resolution.getSeconds
private val resolution = simonaConfig.simona.powerflow.resolution.toSeconds

private implicit val powerTolerance: Power = Watts(0.1)
private implicit val reactivePowerTolerance: ReactivePower = Vars(0.1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class StorageAgentModelCalculationSpec
storageInputQv.getUuid
)
private val services = Iterable.empty
private val resolution = simonaConfig.simona.powerflow.resolution.getSeconds
private val resolution = simonaConfig.simona.powerflow.resolution.toSeconds

private implicit val powerTolerance: Power = Watts(0.1)
private implicit val reactivePowerTolerance: ReactivePower = Vars(0.1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class WecAgentModelCalculationSpec

private val withServices = Iterable(ActorWeatherService(weatherService.ref))

private val resolution = simonaConfig.simona.powerflow.resolution.getSeconds
private val resolution = simonaConfig.simona.powerflow.resolution.toSeconds

private implicit val powerTolerance: squants.Power = Watts(0.1)
private implicit val reactivePowerTolerance: ReactivePower = Vars(0.1)
Expand All @@ -130,7 +130,7 @@ class WecAgentModelCalculationSpec
inputModel = voltageSensitiveInput,
simulationStartDate = simulationStartDate,
simulationEndDate = simulationEndDate,
resolution = simonaConfig.simona.powerflow.resolution.getSeconds,
resolution = simonaConfig.simona.powerflow.resolution.toSeconds,
requestVoltageDeviationThreshold =
simonaConfig.simona.runtime.participant.requestVoltageDeviationThreshold,
modelConfig = modelConfig,
Expand Down
9 changes: 5 additions & 4 deletions src/test/scala/edu/ie3/simona/config/ConfigFailFastSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import edu.ie3.util.TimeUtil

import java.time.temporal.ChronoUnit
import java.time.{Duration, ZonedDateTime}
import scala.concurrent.duration.DurationInt

class ConfigFailFastSpec extends UnitSpec with ConfigTestData {
"Validating the configs" when {
Expand Down Expand Up @@ -99,9 +100,9 @@ class ConfigFailFastSpec extends UnitSpec with ConfigTestData {
List(10, 30),
100,
),
Duration.of(3600, ChronoUnit.SECONDS),
3600.seconds,
stopOnFailure = false,
Duration.of(3600, ChronoUnit.SECONDS),
3600.seconds,
)
)
}
Expand All @@ -116,9 +117,9 @@ class ConfigFailFastSpec extends UnitSpec with ConfigTestData {
List(10, 30),
100,
),
resolution = Duration.of(3600, ChronoUnit.NANOS),
resolution = 3600.nanos,
stopOnFailure = false,
sweepTimeout = Duration.of(3600, ChronoUnit.SECONDS),
sweepTimeout = 3600.seconds,
)
)
}.getMessage shouldBe "Invalid time resolution. Please ensure, that the time resolution for power flow calculation is at least rounded to a full second!"
Expand Down

0 comments on commit 5f0fc63

Please sign in to comment.