Skip to content

Commit

Permalink
Merge pull request #79 from ie3-institute/to/#77-gitignore-update
Browse files Browse the repository at this point in the history
adjust gitignore and minor refactorings
  • Loading branch information
sebastian-peter authored Oct 13, 2022
2 parents 58b5644 + 9e67bdd commit 043c490
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 113 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ Network Trash Folder
Temporary Items
.apdisk

### macOS Patch ###
# iCloud generated files
*.icloud

### Scala ###

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
Expand Down
113 changes: 8 additions & 105 deletions src/main/scala/edu/ie3/mobsim/MobilitySimulator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ import edu.ie3.mobsim.MobilitySimulator.Movement
import edu.ie3.mobsim.config.{ArgsParser, ConfigFailFast}
import edu.ie3.mobsim.exceptions.{
InitializationException,
SourceException,
UninitializedException
}
import edu.ie3.mobsim.io.geodata.PoiEnums.CategoricalLocationDictionary
import edu.ie3.mobsim.io.geodata.{PoiUtils, PointOfInterest}
import edu.ie3.mobsim.io.probabilities._
import edu.ie3.mobsim.io.probabilities.factories._
import edu.ie3.mobsim.model.ChargingBehavior.chooseChargingStation
import edu.ie3.mobsim.model.TripSimulation.simulateNextTrip
import edu.ie3.mobsim.model.{ChargingStation, ElectricVehicle, EvType}
Expand All @@ -40,7 +38,7 @@ import javax.measure.quantity.Length
import scala.collection.immutable.{SortedSet, TreeSet}
import scala.collection.parallel.CollectionConverters._
import scala.jdk.CollectionConverters._
import scala.util.{Failure, Random, Success}
import scala.util.Random

final class MobilitySimulator(
evData: ExtEvData,
Expand Down Expand Up @@ -682,19 +680,6 @@ object MobilitySimulator
.toZonedDateTime(config.mobsim.simulation.startDate)
.withZoneSameInstant(ZoneId.of("UTC"))

logger.debug("Loading probabilities for first departure of day")
val firstDepartureOfDay = FirstDepartureFactory.getFromFile(
pathsAndSources.firstDepartureOfDayPath,
config.mobsim.input.mobility.source.colSep
) match {
case Failure(exception) =>
throw SourceException(
"Unable to get probabilities for first departure of day from path.",
exception
)
case Success(value) => value
}

/* Initialize all EV objects in the area */
val evModelPdf =
EvType.getEvInputModelsWithProbabilities(
Expand All @@ -709,6 +694,12 @@ object MobilitySimulator
s"Creating $numberOfEvsInArea evs with a targeted home charging share of ${"%.2f"
.format(targetShareOfHomeCharging * 100)} %."
)

val tripProbabilities = TripProbabilities.read(
pathsAndSources,
config.mobsim.input.mobility.source.colSep
)

val evs = ElectricVehicle.createEvs(
numberOfEvsInArea,
poisWithSizes
Expand All @@ -729,7 +720,7 @@ object MobilitySimulator
startTime,
targetShareOfHomeCharging,
evModelPdf,
firstDepartureOfDay
tripProbabilities.firstDepartureOfDay
)
ioUtils.writeEvs(evs)

Expand Down Expand Up @@ -774,94 +765,6 @@ object MobilitySimulator
s"necessarily within reach of the POIs!"
)

val categoricalLocation = CategoricalLocationFactory.getFromFile(
pathsAndSources.categoricalLocationPath,
config.mobsim.input.mobility.source.colSep
) match {
case Failure(exception) =>
throw SourceException(
"Unable to get categorical location probabilities from path.",
exception
)
case Success(value) => value
}
logger.debug("Done loading probabilities for categorical locations")

val drivingSpeed = DrivingSpeedFactory.getFromFile(
pathsAndSources.drivingSpeedPath,
config.mobsim.input.mobility.source.colSep
) match {
case Failure(exception) =>
throw SourceException(
"Unable to get driving speed parameters from path.",
exception
)
case Success(value) => value
}
logger.debug("Done loading probabilities for driving speed")

val lastTripOfDay = LastTripFactory.getFromFile(
pathsAndSources.lastTripPath,
config.mobsim.input.mobility.source.colSep
) match {
case Failure(exception) =>
throw SourceException(
"Unable to get last trip probabilities from path.",
exception
)
case Success(value) => value
}
logger.debug("Done loading probabilities for last trip of day")

val parkingTime = ParkingTimeFactory.getFromFile(
pathsAndSources.parkingTimePath,
config.mobsim.input.mobility.source.colSep
) match {
case Failure(exception) =>
throw SourceException(
"Unable to get probabilities for parking time from path.",
exception
)
case Success(value) => value
}
logger.debug("Done loading probabilities for parking time")

val poiTransition = PoiTransitionFactory.getFromFile(
pathsAndSources.poiTransitionPath,
config.mobsim.input.mobility.source.colSep
) match {
case Failure(exception) =>
throw SourceException(
"Unable to get probabilities for poi type transitions from path.",
exception
)
case Success(value) => value
}
logger.debug("Done loading probabilities for poi transition")

val tripDistance = TripDistanceFactory.getFromFile(
pathsAndSources.tripDistancePath,
config.mobsim.input.mobility.source.colSep
) match {
case Failure(exception) =>
throw SourceException(
"Unable to get probabilities for trip distance transitions from path.",
exception
)
case Success(value) => value
}
logger.debug("Done loading probabilities for trip distance")

val tripProbabilities = TripProbabilities(
categoricalLocation,
drivingSpeed,
firstDepartureOfDay,
lastTripOfDay,
parkingTime,
poiTransition,
tripDistance
)

val mobSim = new MobilitySimulator(
availableEvData,
chargingStations,
Expand Down
125 changes: 125 additions & 0 deletions src/main/scala/edu/ie3/mobsim/io/probabilities/TripProbabilities.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@

package edu.ie3.mobsim.io.probabilities

import com.typesafe.scalalogging.LazyLogging
import edu.ie3.mobsim.exceptions.SourceException
import edu.ie3.mobsim.io.probabilities.factories.{
CategoricalLocationFactory,
DrivingSpeedFactory,
FirstDepartureFactory,
LastTripFactory,
ParkingTimeFactory,
PoiTransitionFactory,
TripDistanceFactory
}
import edu.ie3.mobsim.utils.PathsAndSources

import scala.util.{Failure, Success}

/** Container class to wrap probabilities for trip generation.
*
* @param categoricalLocation
Expand All @@ -32,3 +47,113 @@ final case class TripProbabilities(
poiTransition: PoiTransition,
tripDistance: TripDistance
)

object TripProbabilities extends LazyLogging {

def read(
pathsAndSources: PathsAndSources,
colSep: String
): TripProbabilities = {

val firstDepartureOfDay = FirstDepartureFactory.getFromFile(
pathsAndSources.firstDepartureOfDayPath,
colSep
) match {
case Failure(exception) =>
throw SourceException(
"Unable to get probabilities for first departure of day from path.",
exception
)
case Success(value) => value
}

val categoricalLocation = CategoricalLocationFactory.getFromFile(
pathsAndSources.categoricalLocationPath,
colSep
) match {
case Failure(exception) =>
throw SourceException(
"Unable to get categorical location probabilities from path.",
exception
)
case Success(value) => value
}
logger.debug("Done loading probabilities for categorical locations")

val drivingSpeed = DrivingSpeedFactory.getFromFile(
pathsAndSources.drivingSpeedPath,
colSep
) match {
case Failure(exception) =>
throw SourceException(
"Unable to get driving speed parameters from path.",
exception
)
case Success(value) => value
}
logger.debug("Done loading probabilities for driving speed")

val lastTripOfDay = LastTripFactory.getFromFile(
pathsAndSources.lastTripPath,
colSep
) match {
case Failure(exception) =>
throw SourceException(
"Unable to get last trip probabilities from path.",
exception
)
case Success(value) => value
}
logger.debug("Done loading probabilities for last trip of day")

val parkingTime = ParkingTimeFactory.getFromFile(
pathsAndSources.parkingTimePath,
colSep
) match {
case Failure(exception) =>
throw SourceException(
"Unable to get probabilities for parking time from path.",
exception
)
case Success(value) => value
}
logger.debug("Done loading probabilities for parking time")

val poiTransition = PoiTransitionFactory.getFromFile(
pathsAndSources.poiTransitionPath,
colSep
) match {
case Failure(exception) =>
throw SourceException(
"Unable to get probabilities for poi type transitions from path.",
exception
)
case Success(value) => value
}
logger.debug("Done loading probabilities for poi transition")

val tripDistance = TripDistanceFactory.getFromFile(
pathsAndSources.tripDistancePath,
colSep
) match {
case Failure(exception) =>
throw SourceException(
"Unable to get probabilities for trip distance transitions from path.",
exception
)
case Success(value) => value
}
logger.debug("Done loading probabilities for trip distance")

TripProbabilities(
categoricalLocation,
drivingSpeed,
firstDepartureOfDay,
lastTripOfDay,
parkingTime,
poiTransition,
tripDistance
)
}

}
19 changes: 12 additions & 7 deletions src/main/scala/edu/ie3/mobsim/model/TripSimulation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import edu.ie3.util.quantities.PowerSystemUnits.{
KILOWATTHOUR
}
import edu.ie3.util.quantities.QuantityUtil
import edu.ie3.util.quantities.interfaces.SpecificEnergy
import tech.units.indriya.ComparableQuantity
import tech.units.indriya.unit.Units.KILOMETRE_PER_HOUR

Expand Down Expand Up @@ -590,7 +591,8 @@ object TripSimulation extends LazyLogging {
/* Calculate stored energy at the end of the trip based on planned values */
val plannedStoredEnergyEndOfTrip: ComparableQuantity[Energy] =
calculateStoredEnergyAtEndOfTrip(
ev: ElectricVehicle,
ev.evType.consumption,
ev.getStoredEnergy,
plannedDrivingDistance: ComparableQuantity[Length]
)

Expand Down Expand Up @@ -1037,28 +1039,31 @@ object TripSimulation extends LazyLogging {
/** Calculate stored energy at the end of the trip based on the trip distance.
* The minimum stored energy is zero, even if the trip is longer.
*
* @param ev
* The ev for which the stored energy shall be calculated
* @param consumption
* Consumption of the ev
* @param storedEnergy
* Stored energy of the ev
* @param drivingDistance
* The driving distance of the ev
* @return
* stored energy at the end of the trip
*/
def calculateStoredEnergyAtEndOfTrip(
ev: ElectricVehicle,
consumption: ComparableQuantity[SpecificEnergy],
storedEnergy: ComparableQuantity[Energy],
drivingDistance: ComparableQuantity[Length]
): ComparableQuantity[Energy] = {

/* Calculate consumed energy during the trip */
val consumedEnergy: ComparableQuantity[Energy] = drivingDistance
.multiply(ev.evType.consumption)
.multiply(consumption)
.asType(classOf[Energy])
.to(KILOWATTHOUR)

/* Calculate storedEnergy at end of trip */
if (ev.getStoredEnergy.subtract(consumedEnergy).isLessThan(ZERO_ENERGY))
if (storedEnergy.subtract(consumedEnergy).isLessThan(ZERO_ENERGY))
ZERO_ENERGY
else ev.getStoredEnergy.subtract(consumedEnergy)
else storedEnergy.subtract(consumedEnergy)
}

/** Calculate departure time for the trip. The next departure might be sampled
Expand Down
Loading

0 comments on commit 043c490

Please sign in to comment.