Skip to content

Commit

Permalink
Merge pull request #304 from ie3-institute/ms/#303-adapt-to-recent-ch…
Browse files Browse the repository at this point in the history
…anges-in-api

Adapting to recent changes in the API.
  • Loading branch information
sebastian-peter authored Dec 2, 2024
2 parents 21df118 + 0b3b7ba commit 6eba515
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 53 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- MobSim informs EVCS in SIMONA about estimated next arrivals [#254](https://github.com/ie3-institute/MobilitySimulator/issues/254)
- Updated `Gradle` to version V8.10 [#282](https://github.com/ie3-institute/MobilitySimulator/issues/282)
- Changed Spotless to format with trailing commas [#301](https://github.com/ie3-institute/MobilitySimulator/issues/301)
- Adapting to recent changes in the API [#303](https://github.com/ie3-institute/MobilitySimulator/issues/303)

### Fixed
- Adapting to SIMONA-API changes introduced by [PR#37](https://github.com/ie3-institute/simonaAPI/pull/37) [[#7](https://github.com/ie3-institute/MobilitySimulator/issues/7)]
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ dependencies {
exclude group: 'com.github.ie3-institute'
}

implementation 'com.github.ie3-institute:simonaAPI:0.5.0'
implementation 'com.github.ie3-institute:simonaAPI:0.6.0'

implementation 'tech.units:indriya:2.2.1' // Physical units
implementation "org.typelevel:squants_${scalaVersion}:1.8.3"
Expand Down
19 changes: 4 additions & 15 deletions src/main/scala/edu/ie3/mobsim/ExtLink.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,14 @@
package edu.ie3.mobsim

import edu.ie3.simona.api.ExtLinkInterface
import edu.ie3.simona.api.data.ExtDataSimulation
import edu.ie3.simona.api.simulation.ExtSimulation

import java.util
import edu.ie3.simona.api.simulation.{ExtSimAdapterData, ExtSimulation}

class ExtLink extends ExtLinkInterface {

private val simulator: MobilitySimulator.type = MobilitySimulator

@Override
def getExtSimulation: ExtSimulation = {
simulator
}
override def getExtSimulation: ExtSimulation = MobilitySimulator

@Override
def getExtDataSimulations: util.List[ExtDataSimulation] = {
val list = new util.ArrayList[ExtDataSimulation]()
list.add(simulator)
list
}

override def setup(extSimAdapterData: ExtSimAdapterData): Unit =
MobilitySimulator.setAdapterData(extSimAdapterData)
}
45 changes: 22 additions & 23 deletions src/main/scala/edu/ie3/mobsim/MobilitySimulator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,25 @@ import edu.ie3.mobsim.model.{
EvType,
}
import edu.ie3.mobsim.utils.{IoUtils, PathsAndSources}
import edu.ie3.simona.api.data.ExtDataSimulation
import edu.ie3.simona.api.data.ExtDataConnection
import edu.ie3.simona.api.data.ev.ExtEvDataConnection
import edu.ie3.simona.api.data.ev.model.EvModel
import edu.ie3.simona.api.data.ev.{ExtEvData, ExtEvSimulation}
import edu.ie3.simona.api.simulation.ExtSimulation
import edu.ie3.util.TimeUtil
import squants.Length
import squants.space.{Kilometers, Meters}

import java.time.temporal.ChronoUnit
import java.time.{ZoneId, ZonedDateTime}
import java.util
import java.util.{Optional, UUID}
import scala.collection.parallel.CollectionConverters._
import scala.jdk.CollectionConverters._
import scala.jdk.OptionConverters._
import scala.util.Random

final class MobilitySimulator(
evData: ExtEvData,
evDataConnection: ExtEvDataConnection,
chargingStations: Seq[ChargingStation],
poisWithSizes: Map[
CategoricalLocationDictionary.Value,
Expand All @@ -64,6 +65,14 @@ final class MobilitySimulator(
thresholdChargingHubDistance: Length,
round15: Boolean,
) extends LazyLogging {

/** Activities to be performed every time the mobility simulator is triggered.
*
* @param tick
* Current time tick
* @return
* Next time tick when simulation should be triggered again
*/
private def doActivity(tick: Long): Optional[java.lang.Long] = {
/* Update current time */
val currentTime = startTime.plusSeconds(tick)
Expand All @@ -73,15 +82,15 @@ final class MobilitySimulator(
)

/* Receive available charging points of evcs from SIMONA and converting them to scala values */
val availableChargingPoints = evData
val availableChargingPoints = evDataConnection
.requestAvailablePublicEvcs()
.asScala
.view
.mapValues(_.toInt)
.toMap

/* Receive current prices for public evcs situation and converting them to scala values */
val currentPricesAtChargingStations = evData
val currentPricesAtChargingStations = evDataConnection
.requestCurrentPrices()
.asScala
.view
Expand Down Expand Up @@ -160,7 +169,7 @@ final class MobilitySimulator(
val (departures, updatedChargingPoints) =
handleDepartures(departingEvs, availableChargingPoints)

val departedEvs = evData
val departedEvs = evDataConnection
.requestDepartingEvs(
EvMovement.buildMovementsUuidMap(departures)
)
Expand Down Expand Up @@ -203,7 +212,7 @@ final class MobilitySimulator(
.getTimeUntilNextDeparture(electricVehicles, currentTime)
)
.map(_ + tick)
evData.provideArrivingEvs(
evDataConnection.provideArrivingEvs(
EvMovement.buildMovementsMap(arrivals),
timeUntilNextEvent.map(long2Long).toJava,
)
Expand Down Expand Up @@ -537,9 +546,7 @@ final class MobilitySimulator(
}

object MobilitySimulator
extends ExtSimulation
with ExtEvSimulation
with ExtDataSimulation
extends ExtSimulation("MobilitySimulator")
with LazyLogging {

private var simulator: Option[MobilitySimulator] = None
Expand All @@ -551,12 +558,10 @@ object MobilitySimulator
/* random seed */
val seed: Random = new scala.util.Random(6)

private var evData: Option[ExtEvData] = None
private val evDataConnection: ExtEvDataConnection = new ExtEvDataConnection()

/** Set external EvAdapter during simulation setup */
override def setExtEvData(evAdapter: ExtEvData): Unit = {
this.evData = Some(evAdapter)
}
override def getDataConnections: util.Set[ExtDataConnection] =
util.Set.of(evDataConnection)

/** Activities to be performed every time the mobility simulator is triggered.
*
Expand All @@ -582,12 +587,6 @@ object MobilitySimulator

val initTick = -1L

val availableEvData = evData.getOrElse(
throw InitializationException(
"Unable to access external ev data, although setup should have provided them."
)
)

logger.info("Starting setup...")

logger.debug("Parsing config")
Expand Down Expand Up @@ -797,7 +796,7 @@ object MobilitySimulator
)

val mobSim = new MobilitySimulator(
availableEvData,
evDataConnection,
chargingStations,
poisWithSizes,
startTime,
Expand All @@ -822,7 +821,7 @@ object MobilitySimulator
val firstArrivalTick = initTick + timeUntilFirstArrival

// Also provide first tick to the data service
availableEvData.provideArrivingEvs(
evDataConnection.provideArrivingEvs(
Map.empty[UUID, java.util.List[EvModel]].asJava,
Some(long2Long(firstArrivalTick)).toJava,
)
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/edu/ie3/mobsim/model/ChargingStation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ object ChargingStation extends LazyLogging {
cs.evcsType.getElectricCurrentType match {
case ElectricCurrentType.AC =>
Kilowatts(
ev.getSRatedAC
ev.getPRatedAC
.min(
cs.evcsType
.getsRated()
Expand All @@ -222,7 +222,7 @@ object ChargingStation extends LazyLogging {

case ElectricCurrentType.DC =>
Kilowatts(
ev.getSRatedDC
ev.getPRatedDC
.min(cs.evcsType.getsRated())
.to(KILOWATT)
.getValue
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/edu/ie3/mobsim/model/ElectricVehicle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ final case class ElectricVehicle(
PowerSystemUnits.KILOWATTHOUR,
)

def getSRatedAC: ComparableQuantity[Power] = Quantities
def getPRatedAC: ComparableQuantity[Power] = Quantities
.getQuantity(evType.acPower.toKilowatts, PowerSystemUnits.KILOWATT)

def getSRatedDC: ComparableQuantity[Power] = Quantities
def getPRatedDC: ComparableQuantity[Power] = Quantities
.getQuantity(evType.dcPower.toKilowatts, PowerSystemUnits.KILOWATT)

def getStoredEnergy: ComparableQuantity[Energy] = storedEnergy
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/edu/ie3/mobsim/model/TripSimulation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1102,9 +1102,9 @@ object TripSimulation extends LazyLogging {
val availableChargingPowerForEV =
chargingHub.evcsType.getElectricCurrentType match {
case ElectricCurrentType.AC =>
ev.getSRatedAC.min(chargingPowerOfChargingHub).to(KILOWATT)
ev.getPRatedAC.min(chargingPowerOfChargingHub).to(KILOWATT)
case ElectricCurrentType.DC =>
ev.getSRatedDC.min(chargingPowerOfChargingHub).to(KILOWATT)
ev.getPRatedDC.min(chargingPowerOfChargingHub).to(KILOWATT)
}

val energyUntilFullCharge: Energy = KilowattHours(
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/edu/ie3/mobsim/utils/IoUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ final case class IoUtils private (
"battery_capacity" ->
ev.getEStorage.to(KILOWATTHOUR).getValue.doubleValue().toString,
"max_charging_power_ac" ->
ev.getSRatedAC.to(KILOWATT).getValue.doubleValue().toString,
ev.getPRatedAC.to(KILOWATT).getValue.doubleValue().toString,
"max_charging_power_dc" ->
ev.getSRatedDC.to(KILOWATT).getValue.doubleValue().toString,
ev.getPRatedDC.to(KILOWATT).getValue.doubleValue().toString,
"consumption" ->
ev.evType.consumption.toKilowattHoursPerKilometer.toString,
"home_poi" -> ev.homePoi.id,
Expand Down
9 changes: 5 additions & 4 deletions src/test/scala/edu/ie3/mobsim/MobilitySimulatorTestData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@

package edu.ie3.mobsim

import org.apache.pekko.actor.ActorRef
import edu.ie3.mobsim.io.geodata.PoiEnums.PoiTypeDictionary
import edu.ie3.mobsim.model.ElectricVehicle
import edu.ie3.mobsim.utils.IoUtilsTestData
import edu.ie3.simona.api.data.ev.ExtEvData
import edu.ie3.simona.api.data.ev.ExtEvDataConnection
import org.apache.pekko.actor.ActorRef

import java.util.UUID

trait MobilitySimulatorTestData extends IoUtilsTestData {
val evData: ExtEvData = new ExtEvData(ActorRef.noSender, ActorRef.noSender)
val evDataConnection: ExtEvDataConnection = new ExtEvDataConnection()
evDataConnection.setActorRefs(ActorRef.noSender, ActorRef.noSender)

def setEvsAsParking(
evs: Seq[ElectricVehicle]
Expand Down Expand Up @@ -83,7 +84,7 @@ trait MobilitySimulatorTestData extends IoUtilsTestData {
}

def mobSim(): MobilitySimulator = new MobilitySimulator(
evData = evData,
evDataConnection = evDataConnection,
chargingStations = chargingStations,
poisWithSizes = poisWithSizes,
startTime = givenSimulationStart,
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/edu/ie3/mobsim/model/ElectricVehicleSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ElectricVehicleSpec extends UnitSpec with TripSimulationTestData {
) match {
case ElectricVehicle(
simulationStart,
uuid,
_,
id,
evType,
homePoi,
Expand Down Expand Up @@ -86,7 +86,7 @@ class ElectricVehicleSpec extends UnitSpec with TripSimulationTestData {
isChargingAtHomePossible = true,
) match {
case model: ElectricVehicle =>
model.getSRatedDC shouldBe Quantities.getQuantity(
model.getPRatedDC shouldBe Quantities.getQuantity(
givenModel.acPower.toKilowatts,
PowerSystemUnits.KILOWATT,
)
Expand Down

0 comments on commit 6eba515

Please sign in to comment.