Skip to content

Commit

Permalink
Merge pull request #284 from MC-kit/devel
Browse files Browse the repository at this point in the history
save gbins table
  • Loading branch information
dvp2015 authored Dec 25, 2023
2 parents 20b1ed1 + bc8f85e commit 75268dd
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 48 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "xpypact"
version = "0.5.1"
version = "0.5.2"
description = "\"Python workflow framework for FISPACT.\""
authors = ["dvp <[email protected]>"]
license = "MIT"
Expand Down Expand Up @@ -605,7 +605,7 @@ min_alias_length = 3
# which will auto-detect the setting from the rest of the file. This
# is less desirable in a new project and you may find this (slightly
# more strict) setting more useful.
# Typically we find users rely on syntax highlighting rather than
# Typically, we find users rely on syntax highlighting rather than
# capitalisation to distinguish between keywords and identifiers.
# Clearly, if your organisation has already settled on uppercase
# formatting for any of these syntax elements then set them to "upper".
Expand Down
76 changes: 41 additions & 35 deletions src/xpypact/dao/duckdb/create_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ create table if not exists timestep (
material_id uinteger not null,
case_id uinteger not null,
time_step_number uinteger not null,
elapsed_time float4 not null,
irradiation_time float4 not null,
cooling_time float4 not null,
duration float4 not null,
flux float4 not null,
total_atoms float4 not null,
total_activity float4 not null,
alpha_activity float4 not null,
beta_activity float4 not null,
gamma_activity float4 not null,
total_mass float4 not null,
total_heat float4 not null,
alpha_heat float4 null,
beta_heat float4 not null,
gamma_heat float4 not null,
ingest1ion_dose float4 not null,
inhalation_dose float4 not null,
dose_rate float4 not null,
elapsed_time real not null,
irradiation_time real not null,
cooling_time real not null,
duration real not null,
flux real not null,
total_atoms real not null,
total_activity real not null,
alpha_activity real not null,
beta_activity real not null,
gamma_activity real not null,
total_mass real not null,
total_heat real not null,
alpha_heat real null,
beta_heat real not null,
gamma_heat real not null,
ingest1ion real not null,
inhalation real not null,
dose real not null,
primary key (material_id, case_id, time_step_number),
foreign key (material_id, case_id) references rundata (material_id, case_id)
);
Expand All @@ -43,7 +43,7 @@ create table if not exists nuclide (
mass_number usmallint not null check (0 < mass_number),
state varchar(1) not null,
zai uinteger not null check (10010 <= zai) unique,
half_life float4 not null check (0 <= half_life),
half_life real not null check (0 <= half_life),
primary key (element, mass_number, state)
);

Expand All @@ -52,34 +52,40 @@ create table if not exists timestep_nuclide (
case_id uinteger not null,
time_step_number uinteger not null,
zai uinteger not null,
atoms float4 not null,
grams float4 not null,
atoms real not null,
grams real not null,

activity float4 not null,
alpha_activity float4 not null,
beta_activity float4 not null,
gamma_activity float4 not null,
activity real not null,
alpha_activity real not null,
beta_activity real not null,
gamma_activity real not null,

heat float4 not null,
alpha_heat float4 not null,
beta_heat float4 not null,
gamma_heat float4 not null,
heat real not null,
alpha_heat real not null,
beta_heat real not null,
gamma_heat real not null,

dose float4 not null,
ingestion float4 not null,
inhalation float4 not null,
dose real not null,
ingestion real not null,
inhalation real not null,

primary key (material_id, case_id, time_step_number, zai),
foreign key (material_id, case_id, time_step_number) references timestep (material_id, case_id, time_step_number),
foreign key (zai) references nuclide (zai)
);

create table if not exists gbins (
g utinyint primary key,
boundary real not null check (0.0 <= boundary) unique
);

create table if not exists timestep_gamma (
material_id uinteger not null,
case_id uinteger not null,
time_step_number uinteger not null,
boundary real not null check (0 <= boundary),
g utinyint not null, -- only upper bin boundaries in this table
rate real not null,
primary key (material_id, case_id, time_step_number, boundary),
foreign key (material_id, case_id, time_step_number) references timestep (material_id, case_id, time_step_number)
primary key (material_id, case_id, time_step_number, g),
foreign key (material_id, case_id, time_step_number) references timestep (material_id, case_id, time_step_number),
foreign key (g) references gbins (g)
);
31 changes: 22 additions & 9 deletions src/xpypact/dao/duckdb/implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def save(self, inventory: Inventory, material_id=1, case_id=1) -> None:
_save_nuclides(cursor, inventory)
_save_time_steps(cursor, inventory, material_id, case_id)
_save_time_step_nuclides(cursor, inventory, material_id, case_id)
_save_gbins(cursor, inventory)
_save_gamma(cursor, inventory, material_id, case_id)

def load_rundata(self) -> db.DuckDBPyRelation:
Expand Down Expand Up @@ -123,6 +124,14 @@ def load_time_step_nuclides(self) -> db.DuckDBPyRelation:
"""
return self.con.table("timestep_nuclide")

def load_gbins(self) -> db.DuckDBPyRelation:
"""Load gbins table.
Returns:
gbins table
"""
return self.con.table("gbins")

def load_gamma(self, time_step_number: int | None = None) -> db.DuckDBPyRelation:
"""Load time step x gamma table.
Expand Down Expand Up @@ -259,23 +268,27 @@ def _save_time_step_nuclides(


# noinspection SqlNoDataSourceInspection
def _save_gamma(cursor: db.DuckDBPyConnection, inventory: Inventory, material_id=1, case_id=1):
"""Material_id uinteger not null,.
case_id uinteger not null,
time_step_number uinteger not null,
boundary real not null check (0 <= boundary),
rate real not null,
def _save_gbins(cursor: db.DuckDBPyConnection, inventory: Inventory) -> None:
gs = inventory[0].gamma_spectrum
if gs is None:
return
sql = """
insert into gbins values(?, ?);
"""
cursor.executemany(sql, enumerate(gs.boundaries))


# noinspection SqlNoDataSourceInspection
def _save_gamma(cursor: db.DuckDBPyConnection, inventory: Inventory, material_id=1, case_id=1):
sql = """
insert into timestep_gamma values(?, ?, ?, ?, ?);
"""
cursor.executemany(
sql,
(
(material_id, case_id, t.number, x[0], x[1])
(material_id, case_id, t.number, x[0] + 1, x[1][1]) # timestep number # g # rate
for t in inventory.inventory_data
if t.gamma_spectrum
for x in zip(t.gamma_spectrum.boundaries[1:], t.gamma_spectrum.values)
for x in enumerate(zip(t.gamma_spectrum.boundaries[1:], t.gamma_spectrum.values))
),
)
4 changes: 2 additions & 2 deletions tests/test_duckdb_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_save(inventory_with_gamma) -> None:
assert not time_step_nuclides.loc[2, 290630].empty
gamma = dao.load_gamma().df()
assert not gamma.empty
gamma = gamma.set_index(["time_step_number", "boundary"])
assert not gamma.loc[2, 1.0].empty
gamma = gamma.set_index(["time_step_number", "g"])
assert not gamma.loc[2, 1].empty
gamma2 = dao.load_gamma(2).df()
assert not gamma2.empty

0 comments on commit 75268dd

Please sign in to comment.