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

Date and time #205

Merged
merged 2 commits into from
Feb 14, 2024
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gov.hhs.aspr.ms.gcm.nucleus;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
Expand Down Expand Up @@ -180,17 +180,21 @@ public void removeActor(ActorId actorId) {
}

/**
* Returns the time (floating point days) of simulation start.
* Returns the simulation time in days from the given LocalDateTime based on the
* LocalDate associated with simulation time=0 in the SimulationState used to
* initialize the simulation.
*/
public double getStartTime() {
return simulation.getStartTime();
public double getSimulationTime(LocalDateTime localDateTime) {
return simulation.getSimulationTime(localDateTime);
}

/**
* Returns the base date that synchronizes with simulation time zero.
* Returns the LocalDateTime from the given simulation time based on the
* LocalDate associated with simulation time=0 in the SimulationState used to
* initialize the simulation.
*/
public LocalDate getBaseDate() {
return simulation.getBaseDate();
public LocalDateTime getLocalDateTime(double simulationTime) {
return simulation.getLocalDateTime(simulationTime);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gov.hhs.aspr.ms.gcm.nucleus;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
Expand Down Expand Up @@ -220,17 +220,21 @@ public void releaseOutput(Object output) {
}

/**
* Returns the time (floating point days) of simulation start.
* Returns the simulation time in days from the given LocalDateTime based on the
* LocalDate associated with simulation time=0 in the SimulationState used to
* initialize the simulation.
*/
public double getStartTime() {
return simulation.getStartTime();
public double getSimulationTime(LocalDateTime localDateTime) {
return simulation.getSimulationTime(localDateTime);
}

/**
* Returns the base date that synchronizes with simulation time zero.
* Returns the LocalDateTime from the given simulation time based on the
* LocalDate associated with simulation time=0 in the SimulationState used to
* initialize the simulation.
*/
public LocalDate getBaseDate() {
return simulation.getBaseDate();
public LocalDateTime getLocalDateTime(double simulationTime) {
return simulation.getLocalDateTime(simulationTime);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gov.hhs.aspr.ms.gcm.nucleus;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
Expand Down Expand Up @@ -152,17 +152,21 @@ public void releaseOutput(Object output) {
}

/**
* Returns the time (floating point days) of simulation start.
* Returns the simulation time in days from the given LocalDateTime based on the
* LocalDate associated with simulation time=0 in the SimulationState used to
* initialize the simulation.
*/
public double getStartTime() {
return simulation.getStartTime();
public double getSimulationTime(LocalDateTime localDateTime) {
return simulation.getSimulationTime(localDateTime);
}

/**
* Returns the base date that synchronizes with simulation time zero.
* Returns the LocalDateTime from the given simulation time based on the
* LocalDate associated with simulation time=0 in the SimulationState used to
* initialize the simulation.
*/
public LocalDate getBaseDate() {
return simulation.getBaseDate();
public LocalDateTime getLocalDateTime(double simulationTime) {
return simulation.getLocalDateTime(simulationTime);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gov.hhs.aspr.ms.gcm.nucleus;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -195,6 +196,7 @@ private static enum PlanningQueueMode {
private long masterPlanningArrivalId = 0;
private long initialArrivalId = -1;
protected double time;
private SimulationTimeConverter simulationTimeConverter;
double simulationHaltTime;
boolean forcedHaltPresent;
private boolean eventProcessingAllowed;
Expand Down Expand Up @@ -668,6 +670,12 @@ public void execute() {
started = true;

time = data.simulationState.getStartTime();

LocalDateTime dateTime = LocalDateTime.of(data.simulationState.getBaseDate(), LocalTime.of(0, 0));


simulationTimeConverter = new SimulationTimeConverter(dateTime);


forcedHaltPresent = false;
if (data.simulationHaltTime != null) {
Expand Down Expand Up @@ -1688,20 +1696,6 @@ protected <T extends Event> void unsubscribeActorFromEventByFilter(EventFilter<T

}

/**
* Returns the time (floating point days) of simulation start.
*/
protected double getStartTime() {
return data.simulationState.getStartTime();
}

/**
* Returns the base date that synchronizes with simulation time zero.
*/
protected LocalDate getBaseDate() {
return data.simulationState.getBaseDate();
}

private Map<ActorId, List<ActorPlan>> actorPlanDump = null;
private Map<DataManagerId, List<DataManagerPlan>> dataManagerPlanDump = null;
private Map<ReportId, List<ReportPlan>> reportPlanDump = null;
Expand Down Expand Up @@ -1795,5 +1789,26 @@ protected List<DataManagerPlan> retrievePlansForDataManager(DataManagerId dataMa
}
return result;
}

/*
* Registers the given consumer to be executed at the end of the simulation.
* Activity associated with the consumer should be limited to querying data
* state and releasing output.
*
* @throws ContractException {@link NucleusError#NULL_ACTOR_CONTEXT_CONSUMER} if
* the consumer is null
*/
protected double getSimulationTime(LocalDateTime localDateTime) {
return simulationTimeConverter.getSimulationTime(localDateTime);
}

/*
* Returns the LocalDateTime from the given simulation time based on the
* LocalDate associated with simulation time=0 in the SimulationState used to
* initialize the simulation.
*/
protected LocalDateTime getLocalDateTime(double simulationTime) {
return simulationTimeConverter.getLocalDateTime(simulationTime);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package gov.hhs.aspr.ms.gcm.nucleus;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

public final class SimulationTimeConverter {

private final LocalDateTime baseDateTime;
private final double baseSecondsSinceEpoch;
private final double billion = 1_000_000_000.0;
private final double billionith = 1.0/billion;

public SimulationTimeConverter(LocalDateTime baseDateTime){
this.baseDateTime = baseDateTime;
baseSecondsSinceEpoch = getSecondsSinceEpoch(baseDateTime);
}

/*
* Returns the number of seconds since the epoch
*/
private double getSecondsSinceEpoch(LocalDateTime dateTime) {
double result = dateTime.getNano();
result *= billionith;
result += dateTime.toEpochSecond(ZoneOffset.UTC);
return result;
}

public double getSimulationTime(LocalDateTime dateTime) {
return (getSecondsSinceEpoch(dateTime) - baseSecondsSinceEpoch)/86400;
}

public LocalDateTime getLocalDateTime(double simulationTime) {
double t = getSecondsSinceEpoch(baseDateTime);
t += simulationTime*86400;
long epochSeconds = (long)t;
int nanos = (int)((t-epochSeconds)*1_000_000_000.0);
LocalDateTime result = LocalDateTime.ofEpochSecond(epochSeconds,nanos, ZoneOffset.UTC);
return result;
}



}
Loading