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

Merged
merged 18 commits into from
Jan 30, 2025
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed SonarQube quality gate using the correct parameter '-Dsonar.qualitygate.wait=true' [#1072](https://github.com/ie3-institute/simona/issues/1072)
- Updated `simonaAPI` to version `0.6.0` [#1080](https://github.com/ie3-institute/simona/issues/1080)
- Enhanced title in `CITATION.cff` [#1088](https://github.com/ie3-institute/simona/issues/1088)
- Refactor ThermalEnergyDemand definitions [#917](https://github.com/ie3-institute/simona/issues/917)

### Fixed
- Fix rendering of references in documentation [#505](https://github.com/ie3-institute/simona/issues/505)
Expand Down
21 changes: 15 additions & 6 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.participant.HpModel.{HpRelevantData, HpState}
import edu.ie3.simona.model.thermal.ThermalGrid.{
Expand Down Expand Up @@ -581,8 +582,7 @@ object ThermalGrid {
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 @@ -594,10 +594,19 @@ object ThermalGrid {
required: Energy,
possible: Energy,
): ThermalEnergyDemand = {
if (possible < required)
new ThermalEnergyDemand(possible, possible)
else
new ThermalEnergyDemand(required, 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."
)

if (possible.toKilowattHours < 0 || required.toKilowattHours < 0)
throw new InvalidParameterException(
s"The possible $possible or required $required amount of energy cannot be negative. This is not supported."
)

new ThermalEnergyDemand(required, possible)
}

def noDemand: ThermalEnergyDemand = ThermalEnergyDemand(
Expand Down
54 changes: 43 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 @@ -7,6 +7,7 @@
package edu.ie3.simona.model.thermal

import edu.ie3.datamodel.models.input.thermal.ThermalStorageInput
import edu.ie3.simona.exceptions.InvalidParameterException
import edu.ie3.simona.model.thermal.ThermalGrid.ThermalEnergyDemand
import edu.ie3.simona.test.common.UnitSpec
import squants.energy.{KilowattHours, MegawattHours, WattHours, Watts}
Expand All @@ -26,14 +27,13 @@ class ThermalGridSpec

"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)

energyDemand.required should approximate(possible)
energyDemand.possible should approximate(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."
}

"set the correct values, if they are sensible" in {
Expand All @@ -57,6 +57,15 @@ class ThermalGridSpec
}

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

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

"return proper information, if no required but additional demand is apparent" in {
val required = MegawattHours(0d)
val possible = MegawattHours(45d)
Expand All @@ -66,13 +75,36 @@ class ThermalGridSpec
energyDemand.hasAdditionalDemand shouldBe true
}

"return proper information, if required but no additional demand is apparent" in {
val required = MegawattHours(45d)
val possible = MegawattHours(45d)
"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."
}

val energyDemand = ThermalEnergyDemand(required, possible)
energyDemand.hasRequiredDemand shouldBe true
energyDemand.hasAdditionalDemand shouldBe false
"throw exception, if required demand is smaller than zero" in {
val required = MegawattHours(-2d)
val possible = MegawattHours(5d)
intercept[InvalidParameterException] {
ThermalEnergyDemand(required, possible)
}.getMessage shouldBe s"The possible $possible or required $required amount of energy cannot be negative. This is not supported."
}

"throw exception, if possible demand is smaller than zero" in {
val required = MegawattHours(2d)
val possible = MegawattHours(-5d)
intercept[InvalidParameterException] {
ThermalEnergyDemand(required, possible)
}.getMessage shouldBe s"The possible $possible or required $required amount of energy cannot be negative. This is not supported."
}

"throw exception, if possible and required demand are smaller than zero" in {
val required = MegawattHours(-2d)
val possible = MegawattHours(-5d)
intercept[InvalidParameterException] {
ThermalEnergyDemand(required, possible)
}.getMessage shouldBe s"The possible $possible or required $required amount of energy cannot be negative. This is not supported."
}

"return proper information, if required and additional demand is apparent" in {
Expand Down