Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Squants in powerflow and grid #555

Merged
merged 43 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
b90731c
init
danielfeismann Jun 20, 2023
f93b984
more squants
danielfeismann Jun 27, 2023
fb45906
even more squants
danielfeismann Jun 27, 2023
7d5eed8
more squants on grid elements
danielfeismann Jun 27, 2023
a414e67
more squants on grid elements
danielfeismann Jun 27, 2023
d2bf107
codacy
danielfeismann Jun 28, 2023
f85e2dd
Merge branch 'dev' into df/#554-squants_grid
danielfeismann Jun 28, 2023
16f5604
more squants
danielfeismann Jun 29, 2023
62f1ac7
transfer grid related tests to squants
danielfeismann Jun 29, 2023
bebe0bf
more transfer of grid related tests to squants
danielfeismann Jun 29, 2023
11208f4
transfer RefSystemTest to squants
danielfeismann Jun 29, 2023
b95c687
adapt parsing Refsystem for squants
danielfeismann Jul 3, 2023
8ddb158
Merge branch 'dev' into df/#554-squants_grid
danielfeismann Jul 3, 2023
d35958e
fix Transformer3wResultSupport
danielfeismann Jul 3, 2023
d20fce7
fmt
danielfeismann Jul 3, 2023
aa58787
squants
danielfeismann Jul 3, 2023
3426ccb
changelog
danielfeismann Jul 3, 2023
b8b80ba
fix tests
danielfeismann Jul 3, 2023
c583738
fix RefSystemTest
danielfeismann Jul 3, 2023
5190aae
fmt
danielfeismann Jul 3, 2023
fe9efd1
fix Transformer3wTestData
danielfeismann Jul 4, 2023
f317c0a
fix RefSystemTest
danielfeismann Jul 4, 2023
41a53e1
Merge branch 'dev' into df/#554-squants_grid
danielfeismann Jul 4, 2023
ddf7f08
fix assertion in Specs
danielfeismann Jul 6, 2023
72ef9b1
adapt tolerance
danielfeismann Jul 6, 2023
32d06d5
format testing for approx values
danielfeismann Jul 13, 2023
7a570aa
Merge branch 'dev' into df/#554-squants_grid
danielfeismann Jul 13, 2023
ee6e6f4
resolve more merge conflicts
danielfeismann Jul 13, 2023
012c0d9
fmt
danielfeismann Jul 13, 2023
49d3d0e
fix groovy tests after merge
danielfeismann Jul 13, 2023
799180d
Merge branch 'dev' into df/#554-squants_grid
danielfeismann Aug 1, 2023
267a6c2
Merge branch 'dev' into df/#554-squants_grid
danielfeismann Aug 1, 2023
f0a0f2b
Merge branch 'dev' into df/#554-squants_grid
danielfeismann Aug 23, 2023
e950bea
Merge branch 'dev' into df/#554-squants_grid
danielfeismann Sep 6, 2023
374e0e8
fix imports
danielfeismann Sep 6, 2023
d8a5d09
fmt
danielfeismann Sep 6, 2023
3245001
codacy
danielfeismann Sep 7, 2023
89c55e2
fmt
danielfeismann Sep 7, 2023
a06e6bb
Merge branch 'dev' into df/#554-squants_grid
danielfeismann Sep 12, 2023
862e7e9
Merge branch 'dev' into df/#554-squants_grid
danielfeismann Sep 19, 2023
9ad316e
some more quantities to squants
danielfeismann Sep 21, 2023
f89adc9
fixed test
danielfeismann Sep 21, 2023
0b8aefc
Merge branch 'dev' into df/#554-squants_grid
danielfeismann Sep 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 17 additions & 23 deletions src/main/scala/edu/ie3/simona/agent/grid/DBFSAlgorithm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,13 @@ import edu.ie3.simona.ontology.messages.VoltageMessage.{
}
import edu.ie3.simona.ontology.trigger.Trigger._
import edu.ie3.simona.util.TickUtil._
import edu.ie3.util.quantities.PowerSystemUnits._
import edu.ie3.util.scala.quantities.Megavars
import edu.ie3.util.scala.quantities.SquantsUtils.RichElectricPotential
import squants.Each
import squants.energy.Megawatts
import tech.units.indriya.quantity.Quantities

import java.time.{Duration, ZonedDateTime}
import java.util.UUID
import javax.measure.quantity.ElectricPotential
import scala.concurrent.{ExecutionContext, Future}

/** Trait that is normally mixed into every [[GridAgent]] to enable distributed
Expand Down Expand Up @@ -216,8 +214,8 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {
currentSweepNo - 1
)

val refSystemVUnit =
gridAgentBaseData.gridEnv.gridModel.mainRefSystem.nominalVoltage.getUnit
val refSystem =
gridAgentBaseData.gridEnv.gridModel.mainRefSystem

/* Determine the slack node voltage under consideration of the target voltage set point */
val vTarget =
Expand All @@ -226,17 +224,13 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {
uuid == nodeUuid && isSlack
}
.map(_.vTarget)
.getOrElse(Quantities.getQuantity(1d, PU))
val vSlack = vTarget
.multiply(
gridAgentBaseData.gridEnv.gridModel.mainRefSystem.nominalVoltage
)
.asType(classOf[ElectricPotential])
.to(refSystemVUnit)
.getOrElse(Each(1d))
val vSlack =
refSystem.nominalVoltage.multiplyWithDimensionles(vTarget)

(
vSlack,
Quantities.getQuantity(0, refSystemVUnit)
refSystem.vInSi(0d)
)
} match {
case (slackE, slackF) =>
Expand Down Expand Up @@ -330,14 +324,14 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {
// To model the exchanged power from the superior grid's point of view, -1 has to be multiplied.
// (Inferior grid is a feed in facility to superior grid, which is negative then). Analogously for load case.
(
refSystem.pInSi(pInPu).multiply(-1),
refSystem.qInSi(qInPu).multiply(-1)
refSystem.pInSi(pInPu) * (-1),
refSystem.qInSi(qInPu) * (-1)
)
case _ =>
/* TODO: As long as there are no multiple slack nodes, provide "real" power only for the slack node */
(
Quantities.getQuantity(0d, MEGAWATT),
Quantities.getQuantity(0d, MEGAVAR)
Megawatts(0d),
Megavars(0d)
)
}
.getOrElse {
Expand All @@ -349,8 +343,8 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {
.map { case (nodeUuid, (p, q)) =>
ProvideGridPowerMessage.ExchangePower(
nodeUuid,
Megawatts(p.to(MEGAWATT).getValue.doubleValue),
Megavars(q.to(MEGAVAR).getValue.doubleValue)
p,
q
)
}

Expand Down Expand Up @@ -1107,15 +1101,15 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {
)
case None =>
(
Quantities.getQuantity(1, PU),
Quantities.getQuantity(0, PU)
Each(1d),
Each(0d)
)
}

(assetAgent ? RequestAssetPowerMessage(
currentTick,
Each(eInPu.to(PU).getValue.doubleValue),
Each(fInPU.to(PU).getValue.doubleValue)
eInPu,
fInPU
)).map {
case providedPowerValuesMessage: AssetPowerChangedMessage =>
(assetAgent, providedPowerValuesMessage)
Expand Down
83 changes: 44 additions & 39 deletions src/main/scala/edu/ie3/simona/agent/grid/GridResultsSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ import edu.ie3.simona.model.grid.Transformer3wPowerFlowCase.{
import edu.ie3.simona.model.grid._
import edu.ie3.util.quantities.PowerSystemUnits
import edu.ie3.util.scala.quantities.QuantityUtil
import edu.ie3.util.scala.quantities.QuantityUtil._
import tech.units.indriya.ComparableQuantity
import squants.electro.Amperes
import squants.space.Degrees
import tech.units.indriya.quantity.Quantities
import tech.units.indriya.unit.Units

import java.time.ZonedDateTime
import java.util.UUID
import javax.measure.Quantity
import javax.measure.quantity.{Angle, ElectricCurrent}
import scala.math._

/** Trait that holds methods to convert the results of a power flow calculation
Expand Down Expand Up @@ -72,7 +70,7 @@ private[grid] trait GridResultsSupport {
)
.toMap

implicit val iNominal: ComparableQuantity[ElectricCurrent] =
implicit val iNominal: squants.ElectricCurrent =
grid.mainRefSystem.nominalCurrent

/* When creating node results, we have to consider two things:
Expand Down Expand Up @@ -112,7 +110,7 @@ private[grid] trait GridResultsSupport {
*/
private def buildLineResults(lines: Set[LineModel])(implicit
sweepValueStoreData: Map[UUID, SweepValueStoreData],
iNominal: ComparableQuantity[ElectricCurrent],
iNominal: squants.ElectricCurrent,
timestamp: ZonedDateTime
): Set[LineResult] = {
lines.flatMap(lineModel => {
Expand Down Expand Up @@ -159,7 +157,7 @@ private[grid] trait GridResultsSupport {
private def buildTransformer2wResults(transformers: Set[TransformerModel])(
implicit
sweepValueStoreData: Map[UUID, SweepValueStoreData],
iNominal: ComparableQuantity[ElectricCurrent],
iNominal: squants.ElectricCurrent,
timestamp: ZonedDateTime
): Set[Transformer2WResult] = {
transformers.flatMap(trafo2w => {
Expand Down Expand Up @@ -206,7 +204,7 @@ private[grid] trait GridResultsSupport {
def buildTransformer3wResults(transformers3w: Set[Transformer3wModel])(
implicit
sweepValueStoreData: Map[UUID, SweepValueStoreData],
iNominal: ComparableQuantity[ElectricCurrent],
iNominal: squants.ElectricCurrent,
timestamp: ZonedDateTime
): Set[PartialTransformer3wResult] = transformers3w.flatMap { trafo3w =>
{
Expand Down Expand Up @@ -316,18 +314,18 @@ private[grid] trait GridResultsSupport {
line: LineModel,
nodeAStateData: StateData,
nodeBStateData: StateData,
iNominal: Quantity[ElectricCurrent],
iNominal: squants.ElectricCurrent,
timestamp: ZonedDateTime
): LineResult = {

if (line.isInOperation) {
val yij = new Complex(
line.gij().getValue.doubleValue,
line.bij().getValue.doubleValue
line.gij().value.doubleValue,
line.bij().value.doubleValue
)
val y0 = new Complex(
line.g0().getValue.doubleValue,
line.b0().getValue.doubleValue
line.g0().value.doubleValue,
line.b0().value.doubleValue
)

val (iAComplexPu, iBComplexPu) =
Expand All @@ -336,7 +334,14 @@ private[grid] trait GridResultsSupport {
val (iAMag, iAAng) = iMagAndAngle(iAComplexPu, iNominal)
val (iBMag, iBAng) = iMagAndAngle(iBComplexPu, iNominal)

new LineResult(timestamp, line.uuid, iAMag, iAAng, iBMag, iBAng)
new LineResult(
timestamp,
line.uuid,
Quantities.getQuantity(iAMag.toAmperes, Units.AMPERE),
Quantities.getQuantity(iAAng.toDegrees, PowerSystemUnits.DEGREE_GEOM),
Quantities.getQuantity(iBMag.toAmperes, Units.AMPERE),
Quantities.getQuantity(iBAng.toDegrees, PowerSystemUnits.DEGREE_GEOM)
)
} else {
new LineResult(
timestamp,
Expand Down Expand Up @@ -371,7 +376,7 @@ private[grid] trait GridResultsSupport {
trafo2w: TransformerModel,
hvNodeStateData: StateData,
lvNodeStateData: StateData,
iNominal: Quantity[ElectricCurrent],
iNominal: squants.ElectricCurrent,
timestamp: ZonedDateTime
): Transformer2WResult = {
if (trafo2w.isInOperation) {
Expand All @@ -393,16 +398,16 @@ private[grid] trait GridResultsSupport {

/* Transfer port current A to high voltage level */
val (iAMag, iAAng) =
iMagAndAngle(iAComplexPu, iNominal.divide(voltRatioNominal))
iMagAndAngle(iAComplexPu, iNominal / voltRatioNominal.toDouble)
val (iBMag, iBAng) = iMagAndAngle(iBComplexPu, iNominal)

new Transformer2WResult(
timestamp,
trafo2w.uuid,
iAMag,
iAAng,
iBMag,
iBAng,
Quantities.getQuantity(iAMag.toAmperes, Units.AMPERE),
Quantities.getQuantity(iAAng.toDegrees, PowerSystemUnits.DEGREE_GEOM),
Quantities.getQuantity(iBMag.toAmperes, Units.AMPERE),
Quantities.getQuantity(iBAng.toDegrees, PowerSystemUnits.DEGREE_GEOM),
trafo2w.currentTapPos
)
} else {
Expand Down Expand Up @@ -440,7 +445,7 @@ private[grid] trait GridResultsSupport {
trafo3w: Transformer3wModel,
nodeStateData: StateData,
internalNodeStateData: StateData,
iNominal: ComparableQuantity[ElectricCurrent],
iNominal: squants.ElectricCurrent,
timestamp: ZonedDateTime
): PartialTransformer3wResult = {
val (_, iComplexPu) = iIJComplexPu(
Expand Down Expand Up @@ -505,10 +510,10 @@ private[grid] trait GridResultsSupport {
*/
private def iMagAndAngle(
iPu: Complex,
iNominal: Quantity[ElectricCurrent]
): (ComparableQuantity[ElectricCurrent], ComparableQuantity[Angle]) =
iNominal: squants.ElectricCurrent
): (squants.ElectricCurrent, squants.Angle) =
(
iNominal.multiply(iPu.abs).asComparable,
Amperes(iNominal.toAmperes * iPu.abs),
complexToAngle(iPu)
)

Expand All @@ -520,16 +525,16 @@ private[grid] trait GridResultsSupport {
* @return
* The angle of the complex value
*/
private def complexToAngle(cplx: Complex): ComparableQuantity[Angle] =
private def complexToAngle(cplx: Complex): squants.Angle =
cplx match {
case Complex(0d, 0d) =>
/* The complex value has no magnitude, therefore define the angle to zero */
Quantities.getQuantity(0d, PowerSystemUnits.DEGREE_GEOM)
Degrees(0d)
case Complex(0d, imag) =>
/* There is only an imaginary part:
Angle can be 90 or 270 degrees, depending on sign of the imaginary part */
angleOffsetCorrection(
Quantities.getQuantity(90d, PowerSystemUnits.DEGREE_GEOM),
Degrees(90d),
imag
)
case Complex(real, imag) =>
Expand All @@ -541,7 +546,7 @@ private[grid] trait GridResultsSupport {
* part is negative. */
val baseAngle = atan(imag / real).toDegrees
angleOffsetCorrection(
Quantities.getQuantity(baseAngle, PowerSystemUnits.DEGREE_GEOM),
Degrees(baseAngle),
real
)
}
Expand All @@ -550,11 +555,11 @@ private[grid] trait GridResultsSupport {
* direction is negative, 180 degrees are added
*/
private def angleOffsetCorrection(
angle: ComparableQuantity[Angle],
angle: squants.Angle,
dir: Double
): ComparableQuantity[Angle] =
): squants.Angle =
if (dir < 0)
angle.add(Quantities.getQuantity(180d, PowerSystemUnits.DEGREE_GEOM))
angle + Degrees(180d)
else
angle

Expand Down Expand Up @@ -601,8 +606,8 @@ object GridResultsSupport {
sealed trait PartialTransformer3wResult {
val time: ZonedDateTime
val input: UUID
protected val currentMagnitude: ComparableQuantity[ElectricCurrent]
protected val currentAngle: ComparableQuantity[Angle]
protected val currentMagnitude: squants.ElectricCurrent
protected val currentAngle: squants.Angle
}

object PartialTransformer3wResult {
Expand All @@ -623,8 +628,8 @@ object GridResultsSupport {
final case class PortA(
override val time: ZonedDateTime,
override val input: UUID,
override val currentMagnitude: ComparableQuantity[ElectricCurrent],
override val currentAngle: ComparableQuantity[Angle],
override val currentMagnitude: squants.ElectricCurrent,
override val currentAngle: squants.Angle,
tapPos: Int
) extends PartialTransformer3wResult

Expand All @@ -642,8 +647,8 @@ object GridResultsSupport {
final case class PortB(
override val time: ZonedDateTime,
override val input: UUID,
override val currentMagnitude: ComparableQuantity[ElectricCurrent],
override val currentAngle: ComparableQuantity[Angle]
override val currentMagnitude: squants.ElectricCurrent,
override val currentAngle: squants.Angle
) extends PartialTransformer3wResult

/** Partial result for the port at the low voltage side
Expand All @@ -660,8 +665,8 @@ object GridResultsSupport {
final case class PortC(
override val time: ZonedDateTime,
override val input: UUID,
override val currentMagnitude: ComparableQuantity[ElectricCurrent],
override val currentAngle: ComparableQuantity[Angle]
override val currentMagnitude: squants.ElectricCurrent,
override val currentAngle: squants.Angle
) extends PartialTransformer3wResult
}
}
Loading