2019-02-12 18:56:05 +01:00
|
|
|
import json
|
|
|
|
import pickle
|
2021-02-03 22:16:58 +01:00
|
|
|
from pathlib import Path
|
2019-02-13 12:53:17 +01:00
|
|
|
from typing import List
|
2019-02-12 18:56:05 +01:00
|
|
|
|
2019-03-04 19:33:16 +01:00
|
|
|
import numpy as np
|
|
|
|
|
2019-08-21 12:54:27 +02:00
|
|
|
from config import water_fraction
|
2019-02-12 18:56:05 +01:00
|
|
|
from simulation import Simulation
|
|
|
|
|
|
|
|
|
|
|
|
class SimulationList:
|
2019-02-13 12:53:17 +01:00
|
|
|
simlist: List[Simulation]
|
|
|
|
|
2019-02-12 18:56:05 +01:00
|
|
|
def __init__(self, simlist: list = None):
|
|
|
|
if simlist is None:
|
|
|
|
self.simlist = []
|
|
|
|
else:
|
|
|
|
self.simlist = simlist
|
|
|
|
|
|
|
|
def append(self, value: Simulation):
|
|
|
|
self.simlist.append(value)
|
|
|
|
|
2021-02-03 22:16:58 +01:00
|
|
|
def pickle_save(self, pickle_file: Path):
|
|
|
|
with pickle_file.open("wb") as file:
|
2019-02-12 18:56:05 +01:00
|
|
|
pickle.dump(self.simlist, file)
|
|
|
|
|
|
|
|
@classmethod
|
2021-02-03 22:16:58 +01:00
|
|
|
def pickle_load(cls, pickle_file: Path):
|
2019-09-11 11:32:18 +02:00
|
|
|
tmp = cls()
|
2021-02-03 22:16:58 +01:00
|
|
|
with pickle_file.open("rb") as file:
|
2019-02-12 18:56:05 +01:00
|
|
|
return cls(pickle.load(file))
|
|
|
|
|
2021-02-03 22:16:58 +01:00
|
|
|
def jsonlines_save(self, jsonl_file: Path):
|
|
|
|
with jsonl_file.open("w") as file:
|
2019-02-12 18:56:05 +01:00
|
|
|
for sim in self.simlist:
|
|
|
|
file.write(json.dumps(vars(sim)) + "\n")
|
|
|
|
|
|
|
|
@classmethod
|
2021-02-03 22:16:58 +01:00
|
|
|
def jsonlines_load(cls, jsonl_file: Path):
|
2019-02-12 18:56:05 +01:00
|
|
|
simlist = cls()
|
2021-02-03 22:16:58 +01:00
|
|
|
with jsonl_file.open() as file:
|
2019-02-12 18:56:05 +01:00
|
|
|
for line in file:
|
|
|
|
sim = Simulation.from_dict(json.loads(line))
|
|
|
|
simlist.append(sim)
|
|
|
|
return simlist
|
2019-03-04 19:33:16 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def as_matrix(self):
|
|
|
|
entrylist = []
|
|
|
|
for sim in self.simlist:
|
2019-08-01 13:06:46 +02:00
|
|
|
if not sim.testcase:
|
2019-07-06 15:50:16 +02:00
|
|
|
entrylist.append(
|
2019-08-01 13:06:46 +02:00
|
|
|
[sim.alpha, sim.v, sim.projectile_mass, sim.gamma, sim.target_water_fraction,
|
|
|
|
sim.projectile_water_fraction, sim.water_retention_both]
|
2019-07-06 15:50:16 +02:00
|
|
|
)
|
2019-03-04 19:33:16 +01:00
|
|
|
return np.asarray(entrylist)
|
|
|
|
|
2019-05-02 15:30:17 +02:00
|
|
|
@property
|
|
|
|
def X(self):
|
|
|
|
return np.array([
|
2019-07-06 15:50:16 +02:00
|
|
|
[s.alpha, s.v, s.projectile_mass, s.gamma, s.target_water_fraction, s.projectile_water_fraction]
|
2019-07-29 14:23:22 +02:00
|
|
|
for s in self.simlist if not s.testcase
|
2019-05-02 15:30:17 +02:00
|
|
|
])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def Y(self):
|
2021-03-08 13:25:11 +01:00
|
|
|
return self.Y_water if water_fraction else self.Y_mantle
|
2019-08-21 12:54:27 +02:00
|
|
|
|
|
|
|
@property
|
2021-03-08 13:25:11 +01:00
|
|
|
def Y_core(self):
|
|
|
|
return np.array([s.core_retention_both for s in self.simlist if not s.testcase])
|
2019-08-21 12:54:27 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def Y_water(self):
|
2019-07-29 14:23:22 +02:00
|
|
|
return np.array([s.water_retention_both for s in self.simlist if not s.testcase])
|
2021-03-08 13:25:11 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def Y_mantle(self):
|
|
|
|
return np.array([s.mantle_retention_both for s in self.simlist if not s.testcase])
|
2021-10-12 15:45:43 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def Y_mass_fraction(self):
|
|
|
|
return np.array([s.output_mass_fraction for s in self.simlist if not s.testcase])
|