Skip to content

Commit

Permalink
Merge branch 'df/#930-refactor-handleInfeed' into df/#878-thermalGridIT
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
#	src/main/scala/edu/ie3/simona/model/participant/HpModel.scala
  • Loading branch information
danielfeismann committed Nov 22, 2024
2 parents 135ea15 + cbd997e commit a0e95af
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 82 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ jobs:
- name: Checkout Source
uses: actions/checkout@v4
with:
fetch-depth: 1
ref: ${{ github.event.pull_request.head.ref || github.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name || github.repository }}
fetch-depth: 0

- name: Setup Java
uses: actions/setup-java@v4
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Refactoring of `ThermalGrid.energyGrid` to distinguish between demand of house and storage [#928](https://github.com/ie3-institute/simona/issues/928)
- Refactoring to use zeroKW and zeroKWH in thermal grid unit tests [#1023](https://github.com/ie3-institute/simona/issues/1023)
- Refactor `ResultFileHierarchy` [#1031](https://github.com/ie3-institute/simona/issues/1031)
- Removing logs in `logs/simona` [#1017](https://github.com/ie3-institute/simona/issues/1017)
- Fix implausible test cases of HpModelSpec [#1042](https://github.com/ie3-institute/simona/issues/1042)

### Fixed
- Fix rendering of references in documentation [#505](https://github.com/ie3-institute/simona/issues/505)
Expand Down Expand Up @@ -140,6 +142,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix grammar and spelling in docs and comments [#1022](https://github.com/ie3-institute/simona/issues/1022)
- Fix some minor issues and findings from inspections [#1019](https://github.com/ie3-institute/simona/issues/1019)
- Fix initialisation freezing on empty primary data [#981](https://github.com/ie3-institute/simona/issues/981)
- Shallow fetch in CI [#1041](https://github.com/ie3-institute/simona/issues/1041)
- Refactoring of `ThermalGrid.handleInfeed` to fix thermal storage recharge correctly when empty [#930](https://github.com/ie3-institute/simona/issues/930)
- Refactoring of `ThermalGrid.handleInfeed` to fix thermal storage recharge correctly when empty [#930](https://github.com/ie3-institute/simona/issues/930)

## [3.0.0] - 2023-08-07
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugins {
id "de.undercouch.download" version "5.6.0" // downloads plugin
id "kr.motd.sphinx" version "2.10.1" // documentation generation
id "com.github.johnrengelman.shadow" version "8.1.1" // fat jar
id "org.sonarqube" version "5.1.0.4882" // sonarqube
id "org.sonarqube" version "6.0.0.5145" // sonarqube
id "org.scoverage" version "8.1" // scala code coverage scoverage
id "com.github.maiflai.scalatest" version "0.32" // run scalatest without specific spec task
id 'org.hidetake.ssh' version '2.11.2'
Expand Down Expand Up @@ -145,7 +145,7 @@ dependencies {
implementation 'org.apache.commons:commons-math3:3.6.1' // apache commons math3
implementation 'org.apache.poi:poi-ooxml:5.3.0' // used for FilenameUtils
implementation 'javax.measure:unit-api:2.2'
implementation 'tech.units:indriya:2.2' // quantities
implementation 'tech.units:indriya:2.2.1' // quantities
implementation "org.typelevel:squants_${scalaVersion}:1.8.3"
implementation 'org.apache.commons:commons-csv:1.12.0'
implementation 'org.scalanlp:breeze_2.13:2.1.0' // scientific calculations (http://www.scalanlp.org/)
Expand Down
2 changes: 2 additions & 0 deletions input/samples/vn_simona/vn_simona.conf
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ simona.output.thermal = {
]
}

simona.output.log.level = "INFO"

##################################################################
# Runtime Configuration // todo refactor as this naming is misleading and partly unneeded
##################################################################
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config/config-template.conf
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ simona.output.thermal = {
}
simona.output.flex = Boolean | false

simona.output.log.level = String | "INFO"

##################################################################
# Runtime Configuration // todo refactor as this naming is misleading
##################################################################
Expand Down
18 changes: 1 addition & 17 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,8 @@
<level>INFO</level>
</filter>
</appender>
<appender name="RF" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/simona/simona.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logs/simona/archive/simona-%d{yyyyMMdd'T'HHmmss}-${bySecond}.log</fileNamePattern>
<!-- keep 30 days' worth of history capped at 3GB total size -->
<maxHistory>10</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
</appender>
<!-- File appender is configured and added by edu.ie3.simona.logging.logback.LogbackConfiguration -->
<root level="debug">
<appender-ref ref="STDOUT"/>
<appender-ref ref="RF"/>
</root>
</configuration>
52 changes: 39 additions & 13 deletions src/main/scala/edu/ie3/simona/config/ConfigFailFast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import scala.util.{Failure, Success, Try}
* missing config parameters where at least one is needed or check for invalid
* or contradicting parameters
*/
case object ConfigFailFast extends LazyLogging {
object ConfigFailFast extends LazyLogging {

def check(typeSafeConfig: Config, simonaConfig: SimonaConfig): Unit = {
check(typeSafeConfig)
Expand Down Expand Up @@ -139,18 +139,7 @@ case object ConfigFailFast extends LazyLogging {
/* Check if the provided combination of data source and parameters are valid */
checkWeatherDataSource(simonaConfig.simona.input.weather.datasource)

/* check if at least one data sink is defined */
checkDataSink(simonaConfig.simona.output.sink)

/* Check all output configurations for participant models */
checkParticipantsOutputConfig(
simonaConfig.simona.output.participant
)

/* Check all output configurations for thermal models */
checkThermalOutputConfig(
simonaConfig.simona.output.thermal
)
checkOutputConfig(simonaConfig.simona.output)

/* Check power flow resolution configuration */
checkPowerFlowResolutionConfiguration(simonaConfig.simona.powerflow)
Expand All @@ -162,6 +151,28 @@ case object ConfigFailFast extends LazyLogging {
checkStoragesConfig(simonaConfig.simona.runtime.participant.storage)
}

/** Checks for valid output configuration
*
* @param outputConfig
* the output configuration that should be checked
*/
private def checkOutputConfig(
outputConfig: SimonaConfig.Simona.Output
): Unit = {

/* check if at least one data sink is defined */
checkDataSink(outputConfig.sink)

/* Check all output configurations for participant models */
checkParticipantsOutputConfig(outputConfig.participant)

/* Check all output configurations for thermal models */
checkThermalOutputConfig(outputConfig.thermal)

/* Check output configurations for log */
checkLogOutputConfig(outputConfig.log)
}

/** Checks for valid sink configuration
*
* @param sink
Expand Down Expand Up @@ -698,6 +709,21 @@ case object ConfigFailFast extends LazyLogging {
checkIndividualOutputConfigs(subConfig.individualConfigs)
}

/** Check the config subtree for log output parameterization
*
* @param subConfig
* Output sub config tree for log
*/
private def checkLogOutputConfig(
subConfig: SimonaConfig.Simona.Output.Log
): Unit = {
val validLogLevels = Seq("TRACE", "DEBUG", "INFO", "WARN", "ERROR")
if (!validLogLevels.contains(subConfig.level))
throw new InvalidConfigParameterException(
s"Invalid log level \"${subConfig.level}\". Valid log levels: ${validLogLevels.mkString(", ")}"
)
}

/** Checks resolution of power flow calculation
*
* @param powerFlow
Expand Down
23 changes: 23 additions & 0 deletions src/main/scala/edu/ie3/simona/config/SimonaConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1957,6 +1957,7 @@ object SimonaConfig {
base: SimonaConfig.Simona.Output.Base,
flex: scala.Boolean,
grid: SimonaConfig.GridOutputConfig,
log: SimonaConfig.Simona.Output.Log,
participant: SimonaConfig.Simona.Output.Participant,
sink: SimonaConfig.Simona.Output.Sink,
thermal: SimonaConfig.Simona.Output.Thermal,
Expand Down Expand Up @@ -1997,6 +1998,22 @@ object SimonaConfig {

}

final case class Log(
level: java.lang.String
)
object Log {
def apply(
c: com.typesafe.config.Config,
parentPath: java.lang.String,
$tsCfgValidator: $TsCfgValidator,
): SimonaConfig.Simona.Output.Log = {
SimonaConfig.Simona.Output.Log(
level =
if (c.hasPathOrNull("level")) c.getString("level") else "INFO"
)
}
}

final case class Participant(
defaultConfig: SimonaConfig.ParticipantBaseOutputConfig,
individualConfigs: scala.List[
Expand Down Expand Up @@ -2236,6 +2253,12 @@ object SimonaConfig {
parentPath + "grid.",
$tsCfgValidator,
),
log = SimonaConfig.Simona.Output.Log(
if (c.hasPathOrNull("log")) c.getConfig("log")
else com.typesafe.config.ConfigFactory.parseString("log{}"),
parentPath + "log.",
$tsCfgValidator,
),
participant = SimonaConfig.Simona.Output.Participant(
if (c.hasPathOrNull("participant")) c.getConfig("participant")
else com.typesafe.config.ConfigFactory.parseString("participant{}"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,44 @@ import ch.qos.logback.classic.LoggerContext
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.classic.filter.ThresholdFilter
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.rolling.RollingFileAppender
import ch.qos.logback.core.FileAppender
import ch.qos.logback.core.filter.Filter
import com.typesafe.scalalogging.LazyLogging
import org.slf4j.LoggerFactory

import java.nio.file.Path
import scala.jdk.CollectionConverters._

object LogbackConfiguration extends LazyLogging {

def default(logPath: Path): Unit = {
def default(logLevel: String)(logPath: Path): Unit = {
LoggerFactory.getILoggerFactory match {
case loggerContext: LoggerContext =>
val rootLogger = loggerContext.getLogger("root")
val log = logPath.resolve("simona.log")
// stop all appenders
rootLogger.iteratorForAppenders().asScala.foreach(_.stop())
/* Identify the filters of existing rolling file appender */
val fileLoggerFilterList = rootLogger
.iteratorForAppenders()
.asScala
.find(_.getName == "RF")
.map(_.getCopyOfAttachedFiltersList.asScala.toSeq)

// Since logback configuration is static
val logFile = logPath.resolve("simona.log")

rootLogger.addAppender(
fileAppender(
log,
createFileAppender(
logFile,
logLevel,
"simona-default",
fileLoggerFilterList,
loggerContext,
)
)

rootLogger.iteratorForAppenders().asScala.foreach {
case rf: RollingFileAppender[_] =>
rf.getTriggeringPolicy.start()
rf.start()
case appender => appender.start()
}
case factory =>
logger.error(
s"Cannot configure simulation run logger! Invalid factory: $factory"
)
}

}

private def fileAppender(
/** Creates a FileAppender that logs to given path
*/
private def createFileAppender(
logPath: Path,
logLevel: String,
appenderName: String,
maybeFilterList: Option[Seq[Filter[ILoggingEvent]]],
loggerContext: LoggerContext,
): FileAppender[ILoggingEvent] = {

Expand All @@ -69,23 +56,16 @@ object LogbackConfiguration extends LazyLogging {
layoutEncoder.setContext(loggerContext)
layoutEncoder.start()

val filter = new ThresholdFilter()
filter.setLevel(logLevel)
filter.start()

val fileAppender = new FileAppender[ILoggingEvent]
fileAppender.setFile(logPath.toString)
fileAppender.setEncoder(layoutEncoder)
fileAppender.setContext(loggerContext)
fileAppender.setName(appenderName)
/* If applicable, apply the filters from existing file logger else log with "INFO"-Level */
maybeFilterList match {
case Some(filterList) =>
if (filterList.isEmpty) { // No filters in appenders -> Empty List
val filter = new ThresholdFilter()
filter.setLevel("INFO")
filter.start()
fileAppender.addFilter(filter)
} else {
filterList.foreach(fileAppender.addFilter)
}
}
fileAppender.addFilter(filter)
fileAppender.start()

fileAppender
Expand Down
10 changes: 9 additions & 1 deletion src/main/scala/edu/ie3/simona/model/participant/HpModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,20 @@ final case class HpModel(
.map(_.qDot)
.getOrElse(zeroKW)

val (newActivePower, newThermalPower) =
val (newActivePower, newThermalPower) = {
if (isRunning)
(pRated, pThermal)
else if (lastStateStorageQDot < zeroKW)
(zeroKW, lastStateStorageQDot * (-1))
else if (
lastStateStorageQDot == zeroKW && (demandWrapper.houseDemand.hasRequiredDemand || demandWrapper.heatStorageDemand.hasRequiredDemand)
)
(
zeroKW,
thermalGrid.storage.map(_.getChargingPower: squants.Power).get * (-1),
)
else (zeroKW, zeroKW)
}

/* Push thermal energy to the thermal grid and get its updated state in return */
val (thermalGridState, maybeThreshold) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,7 @@ final case class ThermalGrid(
qDotHouse: Power,
qDotHeatStorage: Power,
): (ThermalGridState, Option[ThermalThreshold]) = {
// FIXME: Is there any case where we get back some remainingQDotHouse?
val (updatedHouseState, thermalHouseThreshold, remainingQDotHouse) =
val (updatedHouseState, thermalHouseThreshold, _) =
handleInfeedHouse(
tick,
lastAmbientTemperature,
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/edu/ie3/simona/sim/setup/SetupHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import edu.ie3.simona.config.SimonaConfig
import edu.ie3.simona.exceptions.InitializationException
import edu.ie3.simona.exceptions.agent.GridAgentInitializationException
import edu.ie3.simona.io.result.ResultSinkType
import edu.ie3.simona.logging.logback.LogbackConfiguration
import edu.ie3.simona.model.grid.RefSystem
import edu.ie3.simona.util.ConfigUtil.{GridOutputConfigUtil, OutputConfigUtil}
import edu.ie3.simona.util.ResultFileHierarchy.ResultEntityPathConfig
Expand Down Expand Up @@ -226,6 +227,8 @@ trait SetupHelper extends LazyLogging {
simonaConfig.simona.simulationName,
),
),
configureLogger =
LogbackConfiguration.default(simonaConfig.simona.output.log.level),
config = Some(config),
addTimeStampToOutputDir =
simonaConfig.simona.output.base.addTimestampToOutputDir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object ResultFileHierarchy extends LazyLogging {
outputDir: String,
simulationName: String,
resultEntityPathConfig: ResultEntityPathConfig,
configureLogger: Path => Unit = LogbackConfiguration.default,
configureLogger: Path => Unit = LogbackConfiguration.default("INFO"),
config: Option[TypesafeConfig] = None,
addTimeStampToOutputDir: Boolean = true,
): ResultFileHierarchy = {
Expand Down
Loading

0 comments on commit a0e95af

Please sign in to comment.