Skip to content

Commit

Permalink
added ode coupling
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobrehmann committed Dec 5, 2024
1 parent 12f42ab commit 839eb42
Show file tree
Hide file tree
Showing 18 changed files with 1,145 additions and 105 deletions.
41 changes: 39 additions & 2 deletions src/main/java/org/matsim/episim/EpisimConfigGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
*/
public final class EpisimConfigGroup extends ReflectiveConfigGroup {

private static final String ODE_COUPLING_FACTOR = "odeCouplingFactor";
private static final String ODE_COUPLING_DISTRICT = "odeCouplingDistrict";
private static final Splitter.MapSplitter SPLITTER = Splitter.on(";").withKeyValueSeparator("=");
private static final Joiner.MapJoiner JOINER = Joiner.on(";").withKeyValueSeparator("=");

Expand Down Expand Up @@ -85,6 +87,8 @@ public final class EpisimConfigGroup extends ReflectiveConfigGroup {
private static final Logger log = LogManager.getLogger(EpisimConfigGroup.class);
private static final String GROUPNAME = "episim";

private static final String FACILITIES_PATH = "facilitiesPath";

private final Trie<String, InfectionParams> paramsTrie = Tries.forStrings();

/**
Expand All @@ -111,7 +115,11 @@ public final class EpisimConfigGroup extends ReflectiveConfigGroup {
*/
private final Map<LocalDate, DayOfWeek> inputDays = new HashMap<>();

/**
private double odeCouplingFactor = 0.0;

private String odeCouplingDistrict = null;

/**
* Which events to write in the output.
*/
private WriteEvents writeEvents = WriteEvents.episim;
Expand Down Expand Up @@ -167,6 +175,7 @@ public final class EpisimConfigGroup extends ReflectiveConfigGroup {
private SingleEventFile singleEventFile = SingleEventFile.yes;
private boolean endEarly = false;
private int threads = 2;
private String facilitiesPath = null;


/**
Expand All @@ -181,6 +190,26 @@ public EpisimConfigGroup() {
super(GROUPNAME);
}


@StringGetter(ODE_COUPLING_FACTOR)
public double getOdeCouplingFactor() {
return odeCouplingFactor;
}

@StringSetter(ODE_COUPLING_FACTOR)
public void setOdeCouplingFactor(double odeCouplingFactor) {
this.odeCouplingFactor = odeCouplingFactor;
}
@StringGetter(ODE_COUPLING_DISTRICT)
public String getOdeCouplingDistrict() {
return odeCouplingDistrict;
}

@StringSetter(ODE_COUPLING_DISTRICT)
public void setOdeCouplingDistrict(String odeCouplingDistrict) {
this.odeCouplingDistrict = odeCouplingDistrict;
}

public String getInputEventsFile() {
List<EventFileParams> list = Lists.newArrayList(getInputEventsFiles());

Expand Down Expand Up @@ -702,6 +731,15 @@ public void setReportTimeUse(ReportTimeUse reportTimeUse) {
this.reportTimeUse = reportTimeUse;
}

@StringSetter(FACILITIES_PATH)
public void setFacilitiesFile(final String facilitiesPath){
this.facilitiesPath = facilitiesPath;
}

@StringGetter(FACILITIES_PATH)
public String getFacilitiesFile() {
return facilitiesPath;
}

@Override
public void addParameterSet(final ConfigGroup set) {
Expand Down Expand Up @@ -809,7 +847,6 @@ public EventFileParams addInputEventsFile(final String path) {
public void clearInputEventsFiles() {
clearParameterSetsForType(EventFileParams.SET_TYPE);
}

/**
* Get a copy of container params. Don't use this heavily, it is slow because a new map is created every time.
*/
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/org/matsim/episim/EpisimContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.population.Person;
import org.matsim.episim.policy.Restriction;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

import static org.matsim.episim.EpisimUtils.*;

Expand Down Expand Up @@ -91,7 +90,7 @@ public class EpisimContainer<T> {

/**
* This counts the number of persons in this container
* which have the DiseaseStatus contagious or showingSymptoms.
* which have the DiseaseStatus contagious or showingSymptoms.
*/
private int contagiousCounter = 0;

Expand Down Expand Up @@ -134,7 +133,7 @@ boolean containsPerson(EpisimPerson person) {
return persons.contains(index);
}

void addPerson(EpisimPerson person, double now, EpisimPerson.PerformedActivity act) {
public void addPerson(EpisimPerson person, double now, EpisimPerson.PerformedActivity act) {
final int index = person.getPersonId().index();

//assert !persons.contains(index) : "Person already contained in this container.";
Expand Down Expand Up @@ -209,7 +208,7 @@ public double getNumSpaces() {
*
* @param num number of persons
*/
void setMaxGroupSize(int num) {
public void setMaxGroupSize(int num) {
maxGroupSize = num;
}

Expand Down Expand Up @@ -238,7 +237,7 @@ public int getTaskId() {
return taskId;
}


void clearPersons() {
this.persons.clear();
this.personsAsList.clear();
Expand Down Expand Up @@ -267,13 +266,13 @@ public List<EpisimPerson> getPersons() {

public void countContagious(int add) {
contagiousCounter += add;
assert contagiousCounter >= 0 : "We can not have a negative number of contagious persons";
assert contagiousCounter >= 0 : "We can not have a negative number of contagious persons";
}

public void resetContagiousCounter() {
contagiousCounter = 0;
}

public boolean containsContagious() {
return contagiousCounter > 0;
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/matsim/episim/EpisimReporting.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.matsim.core.utils.io.IOUtils;
import org.matsim.episim.EpisimPerson.VaccinationStatus;
import org.matsim.episim.events.*;
import org.matsim.episim.model.ContactModel;
import org.matsim.episim.model.VaccinationType;
import org.matsim.episim.model.VirusStrain;
import org.matsim.episim.policy.Restriction;
Expand Down Expand Up @@ -850,6 +851,18 @@ void reportDiseaseImport(Object2IntMap<VirusStrain> infectedByStrain, int iterat
}
}

/**
* Write number of initially infected persons.
*/
void reportDiseaseImportOde(int infected, int iteration, String date) {
String[] out = new String[4];
out[0] = String.valueOf(iteration);
out[1] = date;
out[2] = "ODE";
out[3] = String.valueOf(infected / sampleSize);
writer.append(diseaseImport, out);
}

/**
* Write average antibody level.
*/
Expand Down
42 changes: 40 additions & 2 deletions src/main/java/org/matsim/episim/InfectionEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
*/
package org.matsim.episim;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.inject.*;
import com.google.inject.name.Names;
import com.google.inject.util.Types;
import com.typesafe.config.ConfigFactory;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.objects.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -45,6 +47,7 @@
import org.matsim.episim.policy.Restriction;
import org.matsim.episim.policy.ShutdownPolicy;
import org.matsim.facilities.ActivityFacility;
import org.matsim.facilities.MatsimFacilitiesReader;
import org.matsim.run.AnalysisCommand;
import org.matsim.utils.objectattributes.attributable.Attributes;
import org.matsim.vehicles.Vehicle;
Expand All @@ -57,7 +60,6 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutorService;
import java.util.function.Function;

import static org.matsim.episim.EpisimUtils.*;

Expand Down Expand Up @@ -187,6 +189,9 @@ public final class InfectionEventHandler implements Externalizable {
*/
private Set<VaccinationModel> vaccinations;

@Inject
ContactModel contactModel;

@Inject
public InfectionEventHandler(Injector injector, SplittableRandom rnd) {
this.injector = injector;
Expand Down Expand Up @@ -833,6 +838,10 @@ public void reset(int iteration) {

activityParticipationModel.setRestrictionsForIteration(iteration, im);

// long susceptibleCommuters = 0L;
// long nonsusceptibleCommuters = 0L;
// long naive = 0L;
// long notNaive = 0L;
for (EpisimPerson person : personMap.values()) {
// update person activity participation for the day
activityParticipationModel.updateParticipation(person, person.getActivityParticipation(),
Expand All @@ -842,7 +851,27 @@ public void reset(int iteration) {

activityParticipationModel.applyQuarantine(person, person.getActivityParticipation(), person.getStartOfDay(day), person.getActivities(day));

// if(person.getAttributes().getAsMap().containsKey("berlin")){
// if(person.getDiseaseStatus().equals(EpisimPerson.DiseaseStatus.susceptible)){
// susceptibleCommuters++;
// } else {
// nonsusceptibleCommuters++;
// }
//
// if (person.getAntibodies(VirusStrain.SARS_CoV_2) == 0.0) {
// naive++;
// }else {
// notNaive++;
// }
//
// }
}

// log.warn("Susceptible: " + susceptibleCommuters);
// log.warn("Non Susceptible " + nonsusceptibleCommuters);
// log.warn("Naive " + naive);
// log.warn("Not Naive " + notNaive);

reporting.reportCpuTime(iteration, "TestingModel", "finished", -1);

handlers.forEach(h -> {
Expand Down Expand Up @@ -998,6 +1027,15 @@ void handleEvents(DayOfWeek day, List<Event> events) {
l.onIterationEnd(iteration, episimConfig.getStartDate().plusDays(iteration - 1));
}

LocalDate date = episimConfig.getStartDate().plusDays(iteration - 1);

if (contactModel instanceof SymmetricContactModelWithOdeCoupling) {
long odeImport = ((SymmetricContactModelWithOdeCoupling) contactModel).getOdeDiseaseImportCount();
reporting.reportDiseaseImportOde((int) odeImport, iteration, date.toString());
((SymmetricContactModelWithOdeCoupling) contactModel).resetOdeDiseaseImportCount();

}

}


Expand Down Expand Up @@ -1031,7 +1069,7 @@ public static final class EpisimVehicle extends EpisimContainer<Vehicle> {
* Container that is a facility and occurred during an activity.
*/
public static final class EpisimFacility extends EpisimContainer<ActivityFacility> {
EpisimFacility(Id<ActivityFacility> facilityId) {
public EpisimFacility(Id<ActivityFacility> facilityId) {
super(facilityId);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/matsim/episim/TrajectoryHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.ImmutableMap;
import com.google.inject.Inject;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Id;
Expand All @@ -11,6 +12,7 @@
import org.matsim.api.core.v01.events.PersonLeavesVehicleEvent;
import org.matsim.api.core.v01.population.Person;
import org.matsim.episim.model.ContactModel;
import org.matsim.episim.model.SymmetricContactModelWithOdeCoupling;
import org.matsim.episim.policy.Restriction;
import org.matsim.facilities.ActivityFacility;
import org.matsim.vehicles.Vehicle;
Expand Down
Loading

0 comments on commit 839eb42

Please sign in to comment.