diff --git a/src/Model/InitialConditionsAdisTS/InitialConditionsAdisTS.py b/src/Model/InitialConditionsAdisTS/InitialConditionsAdisTS.py index 1a8d59c6c300122265357d3057e8967ff5e33484..d1a785877a55a2887f32f12798eedb77d93b80fc 100644 --- a/src/Model/InitialConditionsAdisTS/InitialConditionsAdisTS.py +++ b/src/Model/InitialConditionsAdisTS/InitialConditionsAdisTS.py @@ -27,3 +27,219 @@ from Model.Except import NotImplementedMethodeError from Model.InitialConditionsAdisTS.InitialConditionsAdisTSSpec import ICAdisTSSpec logger = logging.getLogger() + +class InitialConditionsAdisTS(SQLSubModel): + _sub_classes = [ + ICAdisTSSpec, + ] + _id_cnt = 0 + + def __init__(self, id: int = -1, name: str = "default", + status=None): + super(InitialConditionsAdisTS, self).__init__() + + self._status = status + + if id == -1: + self.id = InitialConditionsAdisTS._id_cnt + else: + self.id = id + + self._name = name + self._pollutant = None + self._concentration = None + self._eg = None + self._em = None + self._ed = None + self._enabled = True + self._data = [] + + InitialConditionsAdisTS._id_cnt = max( + InitialConditionsAdisTS._id_cnt + 1, + self.id + ) + + @classmethod + def _db_create(cls, execute): + execute(""" + CREATE TABLE initial_conditions( + id INTEGER NOT NULL PRIMARY KEY, + pollutant INTEGER NOT NULL, + name TEXT NOT NULL, + concentration REAL NOT NULL, + eg REAL NOT NULL, + em REAL NOT NULL, + ed REAL NOT NULL, + enabled BOOLEAN NOT NULL, + FOREIGN KEY(pollutant) REFERENCES Pollutants(id) + ) + """) + + return cls._create_submodel(execute) + + @classmethod + def _db_update(cls, execute, version): + major, minor, release = version.strip().split(".") + if major == minor == "0": + if int(release) < 6: + cls._db_create(execute) + + return True + + @classmethod + def _db_load(cls, execute, data=None): + new = [] + + table = execute( + "SELECT id, pollutant, name, concentration, eg, em, ed, " + + "enabled " + + "FROM initial_conditions" + ) + + if table is not None: + for row in table: + IC_id = row[0] + pollutant = row[1] + name = row[2] + concentration = row[3] + eg = row[4] + em = row[5] + ed = row[6] + enabled = (row[7] == 1) + + IC = cls( + id=IC_id, + name=name, + status=data['status'] + ) + + IC.concentration = concentration + IC.eg = eg + IC.em = em + IC.ed = ed + IC.enabled = enabled + + data['ic_default_id'] = IC_id + IC._data = ICAdisTSSpec._db_load(execute, data) + + new.append(IC) + + return new + + def _db_save(self, execute, data=None): + execute(f"DELETE FROM initial_conditions WHERE id = {self.id}") + + pollutant = -1 + if self.pollutant is not None: + pollutant = self.pollutant + + concentration = -1 + if self.concentration is not None: + concentration = self.concentration + + eg = -1 + if self.eg is not None: + eg = self.eg + + em = -1 + if self.em is not None: + em = self.em + + ed = -1 + if self.ed is not None: + ed = self.ed + + sql = ( + "INSERT INTO " + + "initial_conditions(" + + "id, pollutant, name, concentration, " + + "eg, em, ed, enabled" + + ") " + + "VALUES (" + + f"{self.id}, {pollutant}, '{self._db_format(self._name)}', " + + f"{concentration}, {eg}, {em}, {ed}, {self._enabled}" + + ")" + ) + execute(sql) + + data['ic_default_id'] = self.id + execute( + "DELETE FROM initial_conditions_spec " + + f"WHERE ic_default = {self.id}" + ) + + for ic_spec in self._data: + ic_spec._db_save(execute, data) + + return True + + def __len__(self): + return len(self._data) + + @property + def name(self): + return self._name + + @name.setter + def name(self, name): + self._name = name + self._status.modified() + + @property + def pollutant(self): + return self._pollutant + + @pollutant.setter + def pollutant(self, pollutant): + self._pollutant = pollutant + self._status.modified() + + @property + def concentration(self): + return self._concentration + + @concentration.setter + def concentration(self, concentration): + self._concentration = concentration + self._status.modified() + + @property + def eg(self): + return self._eg + + @eg.setter + def eg(self, eg): + self._eg = eg + self._status.modified() + + @property + def em(self): + return self._em + + @em.setter + def em(self, em): + self._em = em + self._status.modified() + + @property + def ed(self): + return self._ed + + @ed.setter + def ed(self, ed): + self._ed = ed + self._status.modified() + + @property + def enabled(self): + return self._enabled + + @enabled.setter + def enabled(self, enabled): + self._enabled = enabled + self._status.modified() + + + + + diff --git a/src/Model/InitialConditionsAdisTS/InitialConditionsAdisTSList.py b/src/Model/InitialConditionsAdisTS/InitialConditionsAdisTSList.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..9c30d5b7a792a45cdffc53ef48b41b2afd28af2b 100644 --- a/src/Model/InitialConditionsAdisTS/InitialConditionsAdisTSList.py +++ b/src/Model/InitialConditionsAdisTS/InitialConditionsAdisTSList.py @@ -0,0 +1,56 @@ +# InitialConditionsAdisTSList.py -- Pamhyr +# Copyright (C) 2023-2024 INRAE +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +# -*- coding: utf-8 -*- + +from copy import copy +from tools import trace, timer + +from Model.Tools.PamhyrList import PamhyrModelList +from Model.InitialConditionsAdisTS.InitialConditionsAdisTS import InitialConditionsAdisTS + +class InitialConditionsAdisTSList(PamhyrModelList): + _sub_classes = [ + InitialConditionsAdisTS, + ] + + @classmethod + def _db_load(cls, execute, data=None): + new = cls(status=data['status']) + + if data is None: + data = {} + + new._lst = InitialConditionsAdisTS._db_load( + execute, data + ) + + return new + + def _db_save(self, execute, data=None): + execute("DELETE FROM initial_conditions") + + if data is None: + data = {} + + for ic in self._lst: + ic._db_save(execute, data=data) + + return True + + + + diff --git a/src/Model/InitialConditionsAdisTS/InitialConditionsAdisTSSpec.py b/src/Model/InitialConditionsAdisTS/InitialConditionsAdisTSSpec.py index ab26bd8cd650b7866ce60db8f2b3f9c309faf2b9..a2838aba916ffcf84aacfd88ad662680c0849c8b 100644 --- a/src/Model/InitialConditionsAdisTS/InitialConditionsAdisTSSpec.py +++ b/src/Model/InitialConditionsAdisTS/InitialConditionsAdisTSSpec.py @@ -64,6 +64,7 @@ class ICAdisTSSpec(SQLSubModel): reach INTEGER NOT NULL, start_kp REAL NOT NULL, end_kp REAL NOT NULL, + concentration REAL NOT NULL, eg REAL NOT NULL, em REAL NOT NULL, ed REAL NOT NULL, @@ -91,23 +92,24 @@ class ICAdisTSSpec(SQLSubModel): table = execute( "SELECT id, ic_default, name, reach, start_kp, end_kp, " + - "eg, em, ed, rate, enabled " + + "concentration, eg, em, ed, rate, enabled " + "FROM initial_conditions_spec " + f"WHERE ic_default = {data['ic_default_id']} " ) for row in table: - name = row[2] - reach = row[3] - start_kp = row[4] - end_kp = row[5] - eg = row[6] - em = row[7] - ed = row[8] - rate = row[9] - enabled = (row[10] == 1) - - new_spec = [name, reach, start_kp, end_kp, eg, em, ed, rate, enabled] + name = row[2] + reach = row[3] + start_kp = row[4] + end_kp = row[5] + concentration = row[6] + eg = row[7] + em = row[8] + ed = row[9] + rate = row[10] + enabled = (row[11] == 1) + + new_spec = [name, reach, start_kp, end_kp, concentration, eg, em, ed, rate, enabled] new.append(new_spec) return new @@ -118,7 +120,7 @@ class ICAdisTSSpec(SQLSubModel): sql = ( "INSERT INTO " + "initial_conditions_spec(id, ic_default, name, reach, " + - "start_kp, end_kp, eg, em, ed, rate, enabled) " + + "start_kp, end_kp, concentration, eg, em, ed, rate, enabled) " + "VALUES (" + f"{self.id}, " + f"{ic_default}, " + @@ -126,6 +128,7 @@ class ICAdisTSSpec(SQLSubModel): f"{self._reach}, " + f"{self._start_kp}, " + f"{self._end_kp}, " + + f"{self._concentration}, " + f"{self._eg}, " + f"{self._em}, " + f"{self._ed}, " + diff --git a/src/Model/River.py b/src/Model/River.py index d380220dbb85b42433e551c3816d368c8be81da3..2bef4b3b8aa490fc8c2c1a8e723353e24afb6e06 100644 --- a/src/Model/River.py +++ b/src/Model/River.py @@ -47,6 +47,7 @@ from Solver.Solvers import solver_type_list from Model.OutputKpAdists.OutputKpListAdists import OutputKpAdistsList from Model.Pollutants.PollutantsList import PollutantsList +from Model.InitialConditionsAdisTS.InitialConditionsAdisTSList import InitialConditionsAdisTSList class RiverNode(Node, SQLSubModel): @@ -258,6 +259,7 @@ class River(Graph, SQLSubModel): self._rep_lines = REPLineList(status=self._status) self._Output_kp_adists = OutputKpAdistsList(status=self._status) self._Pollutants = PollutantsList(status=self._status) + self._InitialConditionsAdisTS = InitialConditionsAdisTSList(status=self._status) @classmethod def _db_create(cls, execute): @@ -338,6 +340,8 @@ class River(Graph, SQLSubModel): new._Pollutants = PollutantsList._db_load(execute, data) + new._InitialConditionsAdisTS = InitialConditionsAdisTSList._db_load(execute, data) + return new def _db_save(self, execute, data=None): @@ -359,6 +363,7 @@ class River(Graph, SQLSubModel): objs.append(self._Output_kp_adists) objs.append(self._Pollutants) + objs.append(self._InitialConditionsAdisTS) self._save_submodel(execute, objs, data) return True