Skip to content

Commit

Permalink
Merge pull request #846 from ie3-institute/ms/#794-differentiate-betw…
Browse files Browse the repository at this point in the history
…een-different-power-types

Differentiate between different power types
  • Loading branch information
danielfeismann authored Nov 19, 2024
2 parents 9e0871d + 0a90ee1 commit c03b6a7
Show file tree
Hide file tree
Showing 74 changed files with 879 additions and 708 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added weatherData HowTo for Copernicus ERA5 data [#967](https://github.com/ie3-institute/simona/issues/967)
- Add some quote to 'printGoodbye' [#997](https://github.com/ie3-institute/simona/issues/997)
- Add unapply method for ThermalHouseResults [#934](https://github.com/ie3-institute/simona/issues/934)
- Added `ApparentPower` to differentiate between different power types [#794](https://github.com/ie3-institute/simona/issues/794)

### Changed
- Adapted to changed data source in PSDM [#435](https://github.com/ie3-institute/simona/issues/435)
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/edu/ie3/simona/agent/em/EmAgent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package edu.ie3.simona.agent.em

import edu.ie3.datamodel.models.input.EmInput
import edu.ie3.datamodel.models.result.system.{EmResult, FlexOptionsResult}
import edu.ie3.simona.agent.participant.data.Data.PrimaryData.ApparentPower
import edu.ie3.simona.agent.participant.data.Data.PrimaryData.ComplexPower
import edu.ie3.simona.agent.participant.statedata.BaseStateData.FlexControlledData
import edu.ie3.simona.config.SimonaConfig.EmRuntimeConfig
import edu.ie3.simona.event.ResultEvent
Expand Down Expand Up @@ -409,7 +409,7 @@ object EmAgent {
// After initialization, there are no results yet.
val maybeResult = inactiveCore.getResults
.reduceOption { (power1, power2) =>
ApparentPower(power1.p + power2.p, power1.q + power2.q)
ComplexPower(power1.p + power2.p, power1.q + power2.q)
}

maybeResult.foreach { result =>
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/edu/ie3/simona/agent/em/EmDataCore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package edu.ie3.simona.agent.em

import edu.ie3.simona.agent.em.EmAgent.Actor
import edu.ie3.simona.agent.em.FlexCorrespondenceStore.WithTime
import edu.ie3.simona.agent.participant.data.Data.PrimaryData.ApparentPower
import edu.ie3.simona.agent.participant.data.Data.PrimaryData.ComplexPower
import edu.ie3.simona.exceptions.CriticalFailureException
import edu.ie3.simona.ontology.messages.flex.FlexibilityMessage._
import edu.ie3.simona.util.SimonaConstants.INIT_SIM_TICK
Expand Down Expand Up @@ -163,7 +163,7 @@ object EmDataCore {

/** Returns relevant results for all connected agents.
*/
def getResults: Iterable[ApparentPower] =
def getResults: Iterable[ComplexPower] =
correspondences.store.values.flatMap(_.receivedResult.map(_.get))

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import edu.ie3.simona.agent.em.FlexCorrespondenceStore.{
FlexCorrespondence,
WithTime,
}
import edu.ie3.simona.agent.participant.data.Data.PrimaryData.ApparentPower
import edu.ie3.simona.agent.participant.data.Data.PrimaryData.ComplexPower
import edu.ie3.simona.ontology.messages.flex.FlexibilityMessage.{
IssueFlexControl,
ProvideFlexOptions,
Expand Down Expand Up @@ -88,7 +88,7 @@ final case class FlexCorrespondenceStore(
*/
def updateResult(
modelUuid: UUID,
result: ApparentPower,
result: ComplexPower,
tick: Long,
): FlexCorrespondenceStore =
updateCorrespondence(
Expand Down Expand Up @@ -124,7 +124,7 @@ object FlexCorrespondenceStore {
final case class FlexCorrespondence(
receivedFlexOptions: Option[WithTime[ProvideFlexOptions]] = None,
issuedCtrlMsg: Option[WithTime[IssueFlexControl]] = None,
receivedResult: Option[WithTime[ApparentPower]] = None,
receivedResult: Option[WithTime[ComplexPower]] = None,
)

/** Wrapper that allows storing a tick with an object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import edu.ie3.simona.agent.participant.ParticipantAgent.StartCalculationTrigger
import edu.ie3.simona.agent.participant.ParticipantAgentFundamentals.RelevantResultValues
import edu.ie3.simona.agent.participant.data.Data
import edu.ie3.simona.agent.participant.data.Data.PrimaryData.{
ApparentPower,
ApparentPowerAndHeat,
ComplexPower,
ComplexPowerAndHeat,
EnrichableData,
PrimaryDataWithApparentPower,
}
Expand Down Expand Up @@ -799,7 +799,7 @@ protected trait ParticipantAgentFundamentals[

flexStateData.emAgent ! FlexResult(
baseStateData.modelUuid,
result.primaryData.toApparentPower,
result.primaryData.toComplexPower,
)

flexStateData.emAgent ! FlexCompletion(
Expand Down Expand Up @@ -1739,8 +1739,8 @@ protected trait ParticipantAgentFundamentals[
baseStateData.foreseenDataTicks,
)

averageResult.toApparentPower match {
case ApparentPower(p, q) =>
averageResult.toComplexPower match {
case ComplexPower(p, q) =>
stay() using nextStateData replying AssetPowerChangedMessage(p, q)
}
}
Expand Down Expand Up @@ -2003,14 +2003,14 @@ object ParticipantAgentFundamentals {
* The averaged apparent power
*/
def averageApparentPower(
tickToResults: Map[Long, ApparentPower],
tickToResults: Map[Long, ComplexPower],
windowStart: Long,
windowEnd: Long,
activeToReactivePowerFuncOpt: Option[
Power => ReactivePower
] = None,
log: LoggingAdapter,
): ApparentPower = {
): ComplexPower = {
val p = QuantityUtil.average[Power, Energy](
tickToResults.map { case (tick, pd) =>
tick -> pd.p
Expand All @@ -2034,8 +2034,8 @@ object ParticipantAgentFundamentals {
case Some(qFunc) =>
// NOTE: The type conversion to Megawatts is done to satisfy the methods type constraints
// and is undone after unpacking the results
tick -> Megawatts(qFunc(pd.toApparentPower.p).toMegavars)
case None => tick -> Megawatts(pd.toApparentPower.q.toMegavars)
tick -> Megawatts(qFunc(pd.toComplexPower.p).toMegavars)
case None => tick -> Megawatts(pd.toComplexPower.q.toMegavars)
}
},
windowStart,
Expand All @@ -2051,7 +2051,7 @@ object ParticipantAgentFundamentals {
zeroMVAr
}

ApparentPower(p, q)
ComplexPower(p, q)
}

/** Determine the average apparent power within the given tick window
Expand All @@ -2068,20 +2068,20 @@ object ParticipantAgentFundamentals {
* The averaged apparent power
*/
def averageApparentPowerAndHeat(
tickToResults: Map[Long, ApparentPowerAndHeat],
tickToResults: Map[Long, ComplexPowerAndHeat],
windowStart: Long,
windowEnd: Long,
activeToReactivePowerFuncOpt: Option[
Power => ReactivePower
] = None,
log: LoggingAdapter,
): ApparentPowerAndHeat = {
): ComplexPowerAndHeat = {

val tickToResultsApparentPower: Map[Long, ApparentPower] =
val tickToResultsApparentPower: Map[Long, ComplexPower] =
tickToResults.map { case (tick, pd) =>
(
tick,
ApparentPower(Megawatts(pd.p.toMegawatts), Megavars(pd.q.toMegavars)),
ComplexPower(Megawatts(pd.p.toMegawatts), Megavars(pd.q.toMegavars)),
)
}

Expand Down Expand Up @@ -2109,7 +2109,7 @@ object ParticipantAgentFundamentals {
zeroMW
}

ApparentPowerAndHeat(apparentPower.p, apparentPower.q, qDot)
ComplexPowerAndHeat(apparentPower.p, apparentPower.q, qDot)
}

}
54 changes: 27 additions & 27 deletions src/main/scala/edu/ie3/simona/agent/participant/data/Data.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
package edu.ie3.simona.agent.participant.data

import edu.ie3.datamodel.models.value._
import edu.ie3.simona.agent.participant.data.Data.PrimaryData.ApparentPower
import edu.ie3.simona.agent.participant.data.Data.PrimaryData.ComplexPower
import edu.ie3.util.quantities.PowerSystemUnits
import edu.ie3.util.quantities.interfaces.EnergyPrice
import edu.ie3.util.scala.quantities.{Kilovars, Megavars, ReactivePower}
import squants.energy.{Power, Kilowatts, Megawatts}
import tech.units.indriya.ComparableQuantity
import edu.ie3.util.scala.quantities.DefaultQuantities._
import edu.ie3.util.scala.quantities.{Kilovars, ReactivePower}
import squants.energy.{Kilowatts, Power}
import tech.units.indriya.ComparableQuantity

import java.time.ZonedDateTime
import scala.jdk.OptionConverters.RichOptional
Expand All @@ -36,7 +36,7 @@ object Data {
*/
sealed trait PrimaryData extends Data {
val p: Power
def toApparentPower: ApparentPower
def toComplexPower: ComplexPower
}

object PrimaryData {
Expand All @@ -61,7 +61,7 @@ object Data {
val qDot: Power
}

val ZERO_POWER: ApparentPower = ApparentPower(zeroMW, zeroMVAr)
val ZERO_POWER: ComplexPower = ComplexPower(zeroMW, zeroMVAr)

/** Active power as participant simulation result
*
Expand All @@ -70,15 +70,15 @@ object Data {
*/
final case class ActivePower(override val p: Power)
extends PrimaryData
with EnrichableData[ApparentPower] {
override def toApparentPower: ApparentPower =
ApparentPower(
with EnrichableData[ComplexPower] {
override def toComplexPower: ComplexPower =
ComplexPower(
p,
zeroMVAr,
)

override def add(q: ReactivePower): ApparentPower =
ApparentPower(p, q)
override def add(q: ReactivePower): ComplexPower =
ComplexPower(p, q)
}

/** Active and Reactive power as participant simulation result
Expand All @@ -88,13 +88,13 @@ object Data {
* @param q
* Reactive power
*/
final case class ApparentPower(
final case class ComplexPower(
override val p: Power,
override val q: ReactivePower,
) extends PrimaryDataWithApparentPower[ApparentPower] {
override def toApparentPower: ApparentPower = this
) extends PrimaryDataWithApparentPower[ComplexPower] {
override def toComplexPower: ComplexPower = this

override def withReactivePower(q: ReactivePower): ApparentPower =
override def withReactivePower(q: ReactivePower): ComplexPower =
copy(q = q)
}

Expand All @@ -110,15 +110,15 @@ object Data {
override val qDot: Power,
) extends PrimaryData
with Heat
with EnrichableData[ApparentPowerAndHeat] {
override def toApparentPower: ApparentPower =
ApparentPower(
with EnrichableData[ComplexPowerAndHeat] {
override def toComplexPower: ComplexPower =
ComplexPower(
p,
zeroMVAr,
)

override def add(q: ReactivePower): ApparentPowerAndHeat =
ApparentPowerAndHeat(p, q, qDot)
override def add(q: ReactivePower): ComplexPowerAndHeat =
ComplexPowerAndHeat(p, q, qDot)
}

/** Apparent power and heat demand as participant simulation result
Expand All @@ -130,16 +130,16 @@ object Data {
* @param qDot
* Heat demand
*/
final case class ApparentPowerAndHeat(
final case class ComplexPowerAndHeat(
override val p: Power,
override val q: ReactivePower,
override val qDot: Power,
) extends PrimaryDataWithApparentPower[ApparentPowerAndHeat]
) extends PrimaryDataWithApparentPower[ComplexPowerAndHeat]
with Heat {
override def toApparentPower: ApparentPower =
ApparentPower(p, q)
override def toComplexPower: ComplexPower =
ComplexPower(p, q)

override def withReactivePower(q: ReactivePower): ApparentPowerAndHeat =
override def withReactivePower(q: ReactivePower): ComplexPowerAndHeat =
copy(q = q)
}

Expand All @@ -150,7 +150,7 @@ object Data {
(hs.getP.toScala, hs.getQ.toScala, hs.getHeatDemand.toScala) match {
case (Some(p), Some(q), Some(qDot)) =>
Success(
ApparentPowerAndHeat(
ComplexPowerAndHeat(
Kilowatts(
p.to(PowerSystemUnits.KILOWATT).getValue.doubleValue
),
Expand All @@ -173,7 +173,7 @@ object Data {
(s.getP.toScala, s.getQ.toScala) match {
case (Some(p), Some(q)) =>
Success(
ApparentPower(
ComplexPower(
Kilowatts(
p.to(PowerSystemUnits.KILOWATT).getValue.doubleValue
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

package edu.ie3.simona.agent.participant.data.primary

import org.apache.pekko.actor.ActorRef
import edu.ie3.simona.agent.participant.data.Data.PrimaryData
import edu.ie3.simona.agent.participant.data.Data.PrimaryData.ApparentPower
import edu.ie3.simona.agent.participant.data.Data.PrimaryData.ComplexPower
import edu.ie3.simona.agent.participant.data.DataService
import org.apache.pekko.actor.ActorRef

/** Enum-like trait to denote possible external data sources for systems
*/
Expand All @@ -23,5 +23,5 @@ object PrimaryDataService {
* actor reference of the actual source
*/
final case class DummyPrimaryService(override val actorRef: ActorRef)
extends PrimaryDataService[ApparentPower]
extends PrimaryDataService[ComplexPower]
}
Loading

0 comments on commit c03b6a7

Please sign in to comment.