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

Refactor ThermalEnergyDemand #918

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 9 additions & 5 deletions src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import edu.ie3.datamodel.models.result.thermal.{
CylindricalStorageResult,
ThermalHouseResult,
}
import edu.ie3.simona.exceptions.InvalidParameterException
import edu.ie3.simona.exceptions.agent.InconsistentStateException
import edu.ie3.simona.model.thermal.ThermalGrid.{
ThermalEnergyDemand,
Expand Down Expand Up @@ -483,13 +484,12 @@ object ThermalGrid {

def hasRequiredDemand: Boolean = required > zeroMWH

def hasAdditionalDemand: Boolean = possible > required
def hasAdditionalDemand: Boolean = possible > zeroMWH
}
object ThermalEnergyDemand {

/** Builds a new instance of [[ThermalEnergyDemand]]. If the possible energy
* is less than the required energy, this is considered to be a bad state
* and the required energy is curtailed to the possible energy.
* is less than the required energy, this is considered to be a bad state.
* @param required
* The absolutely required energy to reach target state
* @param possible
Expand All @@ -501,8 +501,12 @@ object ThermalGrid {
required: Energy,
possible: Energy,
): ThermalEnergyDemand = {
if (possible < required)
new ThermalEnergyDemand(possible, possible)
if (
math.abs(possible.toKilowattHours) < math.abs(required.toKilowattHours)
)
throw new InvalidParameterException(
s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported."
)
else
new ThermalEnergyDemand(required, possible)
}
Expand Down
49 changes: 38 additions & 11 deletions src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package edu.ie3.simona.model.thermal

import edu.ie3.simona.exceptions.InvalidParameterException
import edu.ie3.simona.model.thermal.ThermalGrid.ThermalEnergyDemand
import edu.ie3.simona.test.common.UnitSpec
import squants.energy.{MegawattHours, WattHours, Watts}
Expand All @@ -20,14 +21,22 @@ class ThermalGridSpec extends UnitSpec {

"Testing the thermal energy demand" when {
"instantiating it from given values" should {
"correct non-sensible input" in {
"throw exception for non-sensible input (positive)" in {
val possible = MegawattHours(40d)
val required = MegawattHours(42d)

val energyDemand = ThermalEnergyDemand(required, possible)
intercept[InvalidParameterException] {
ThermalEnergyDemand(required, possible)
}.getMessage shouldBe s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported."
}

energyDemand.required should approximate(possible)
energyDemand.possible should approximate(possible)
"throw exception for non-sensible input (negative)" in {
val possible = MegawattHours(-40d)
val required = MegawattHours(-42d)

intercept[InvalidParameterException] {
ThermalEnergyDemand(required, possible)
}.getMessage shouldBe s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported."
}

"set the correct values, if they are sensible" in {
Expand All @@ -51,22 +60,30 @@ class ThermalGridSpec extends UnitSpec {
}

"checking for required and additional demand" should {
"return proper information, if no required but additional demand is apparent" in {
"return proper information, if no required and no additional demand is apparent" in {
val required = MegawattHours(0d)
val possible = MegawattHours(45d)
val possible = MegawattHours(0d)

val energyDemand = ThermalEnergyDemand(required, possible)
energyDemand.hasRequiredDemand shouldBe false
energyDemand.hasAdditionalDemand shouldBe true
energyDemand.hasAdditionalDemand shouldBe false
}

"return proper information, if required but no additional demand is apparent" in {
val required = MegawattHours(45d)
"return proper information, if no required but additional demand is apparent" in {
val required = MegawattHours(0d)
val possible = MegawattHours(45d)

val energyDemand = ThermalEnergyDemand(required, possible)
energyDemand.hasRequiredDemand shouldBe true
energyDemand.hasAdditionalDemand shouldBe false
energyDemand.hasRequiredDemand shouldBe false
energyDemand.hasAdditionalDemand shouldBe true
}

"throw exception, if required demand is higher than possible demand" in {
val required = MegawattHours(1d)
val possible = MegawattHours(0d)
intercept[InvalidParameterException] {
ThermalEnergyDemand(required, possible)
}.getMessage shouldBe s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported."
}

"return proper information, if required and additional demand is apparent" in {
Expand All @@ -77,6 +94,16 @@ class ThermalGridSpec extends UnitSpec {
energyDemand.hasRequiredDemand shouldBe true
energyDemand.hasAdditionalDemand shouldBe true
}

// FIXME: Think about "negative demand", maybe add more cases as well
"return proper information, if no required but additional demand is apparent (negative)" in {
val required = MegawattHours(-10d)
val possible = MegawattHours(-45d)

val energyDemand = ThermalEnergyDemand(required, possible)
energyDemand.hasRequiredDemand shouldBe false
energyDemand.hasAdditionalDemand shouldBe true
}
}

"adding two demands" should {
Expand Down