LateralContribution: add D50 and sigma to solid lateral contributions (database 0.2.11)

This commit is contained in:
Dylan Jeannin committed 2026-10-21 14:23:47 +02:00
1 parent 5f64362f65
commit be7b5b892a
6 files changed
+123 -5

No files matched your search

@@ -223,6 +223,11 @@ class LateralContribution(SQLSubModel):
self._data = [] self._data = []
self._header = [] self._header = []
self._types = [float, float] self._types = [float, float]
# Grain size of the sediment brought by a solid contribution:
# median diameter (m) and spread sqrt(d84/d16), as for a solid
# boundary condition
self._d50 = 0.002
self._sigma = 1.0
@classmethod @classmethod
def _db_create(cls, execute, ext=""): def _db_create(cls, execute, ext=""):
@@ -236,6 +241,8 @@ class LateralContribution(SQLSubModel):
reach INTEGER, reach INTEGER,
begin_section INTEGER, begin_section INTEGER,
end_section INTEGER, end_section INTEGER,
d50 REAL DEFAULT 0.002,
sigma REAL DEFAULT 1,
{Scenario.create_db_add_scenario()}, {Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()}, {Scenario.create_db_add_scenario_fk()},
FOREIGN KEY(reach) REFERENCES river_reach(pamhyr_id), FOREIGN KEY(reach) REFERENCES river_reach(pamhyr_id),
@@ -286,8 +293,32 @@ class LateralContribution(SQLSubModel):
"ADD COLUMN deleted BOOLEAN NOT NULL DEFAULT FALSE" "ADD COLUMN deleted BOOLEAN NOT NULL DEFAULT FALSE"
) )
if major == "0" and (int(minor) < 2 or
(minor == "2" and int(release) < 11)):
cls._db_update_to_0_2_11(execute)
return cls._update_submodel(execute, version, data) return cls._update_submodel(execute, version, data)
@classmethod
def _db_update_to_0_2_11(cls, execute):
# The table may already have been recreated with these columns
# by _db_update_to_0_2_0
columns = [
row[0] for row in execute(
"SELECT name FROM pragma_table_info('lateral_contribution')"
)
]
if "d50" not in columns:
execute(
"ALTER TABLE lateral_contribution " +
"ADD COLUMN d50 REAL DEFAULT 0.002"
)
if "sigma" not in columns:
execute(
"ALTER TABLE lateral_contribution " +
"ADD COLUMN sigma REAL DEFAULT 1"
)
@classmethod @classmethod
def _db_update_to_0_2_0(cls, execute, data): def _db_update_to_0_2_0(cls, execute, data):
table = "lateral_contribution" table = "lateral_contribution"
@@ -339,7 +370,7 @@ class LateralContribution(SQLSubModel):
table = execute( table = execute(
"SELECT pamhyr_id, deleted, name, type, " + "SELECT pamhyr_id, deleted, name, type, " +
"reach, begin_section, end_section, scenario " + "reach, begin_section, end_section, d50, sigma, scenario " +
"FROM lateral_contribution " + "FROM lateral_contribution " +
f"WHERE tab = '{tab}'" + f"WHERE tab = '{tab}'" +
f"AND scenario = {scenario.id} " + f"AND scenario = {scenario.id} " +
@@ -356,6 +387,8 @@ class LateralContribution(SQLSubModel):
reach = next(it) reach = next(it)
b_section = next(it) b_section = next(it)
e_section = next(it) e_section = next(it)
d50 = next(it)
sigma = next(it)
owner_scenario = next(it) owner_scenario = next(it)
ctor = cls._get_ctor_from_type(t) ctor = cls._get_ctor_from_type(t)
@@ -367,6 +400,10 @@ class LateralContribution(SQLSubModel):
lc.reach = None lc.reach = None
lc._begin_section = None lc._begin_section = None
lc._end_section = None lc._end_section = None
if d50 is not None:
lc._d50 = float(d50)
if sigma is not None:
lc._sigma = float(sigma)
if row[3] != -1: if row[3] != -1:
lc.reach = next( lc.reach = next(
@@ -426,12 +463,13 @@ class LateralContribution(SQLSubModel):
"INSERT INTO " + "INSERT INTO " +
"lateral_contribution(" + "lateral_contribution(" +
"pamhyr_id, deleted, name, type, tab, " + "pamhyr_id, deleted, name, type, tab, " +
"reach, begin_section, end_section, scenario) " + "reach, begin_section, end_section, d50, sigma, scenario) " +
"VALUES (" + "VALUES (" +
f"{self.id}, {self._db_format(self.is_deleted())}, " + f"{self.id}, {self._db_format(self.is_deleted())}, " +
f"'{self._db_format(self._name)}', " + f"'{self._db_format(self._name)}', " +
f"'{self._db_format(self._type)}', '{tab}', {reach}, " + f"'{self._db_format(self._type)}', '{tab}', {reach}, " +
f"{begin_section}, {end_section}, " + f"{begin_section}, {end_section}, " +
f"{self._d50}, {self._sigma}, " +
f"{self._status.scenario_id}" + f"{self._status.scenario_id}" +
")" ")"
) )
@@ -523,10 +561,30 @@ class LateralContribution(SQLSubModel):
new._reach = reach new._reach = reach
new._begin_section = begin_section new._begin_section = begin_section
new._end_section = end_section new._end_section = end_section
new._d50 = self._d50
new._sigma = self._sigma
new._data = [data.cloned() for data in self.data] new._data = [data.cloned() for data in self.data]
new.modified() new.modified()
return new return new
@property
def d50(self):
return self._d50
@d50.setter
def d50(self, value):
self._d50 = float(value)
self.modified()
@property
def sigma(self):
return self._sigma
@sigma.setter
def sigma(self, value):
self._sigma = float(value)
self.modified()
@property @property
def begin_rk(self): def begin_rk(self):
if self._begin_section is None: if self._begin_section is None:
+1 -1
View File
@@ -48,7 +48,7 @@ logger = logging.getLogger()
class Study(SQLModel): class Study(SQLModel):
_version = "0.2.10" _version = "0.2.11"
_sub_classes = [ _sub_classes = [
Scenario, Scenario,
@@ -44,6 +44,22 @@ class SetDataCommand(QUndoCommand):
self._data._set_i_c_v(self._index, self._column, self._new) self._data._set_i_c_v(self._index, self._column, self._new)
class SetMetaDataCommand(QUndoCommand):
def __init__(self, data, column, new_value):
QUndoCommand.__init__(self)
self._data = data
self._column = column
self._old = getattr(self._data, self._column)
self._new = float(new_value)
def undo(self):
setattr(self._data, self._column, self._old)
def redo(self):
setattr(self._data, self._column, self._new)
class AddCommand(QUndoCommand): class AddCommand(QUndoCommand):
def __init__(self, data, index): def __init__(self, data, index):
QUndoCommand.__init__(self) QUndoCommand.__init__(self)
+39 -2
View File
@@ -35,7 +35,7 @@ from PyQt5.QtWidgets import (
QDialogButtonBox, QPushButton, QLineEdit, QDialogButtonBox, QPushButton, QLineEdit,
QFileDialog, QTableView, QAbstractItemView, QFileDialog, QTableView, QAbstractItemView,
QUndoStack, QShortcut, QAction, QItemDelegate, QUndoStack, QShortcut, QAction, QItemDelegate,
QHeaderView, QHeaderView, QVBoxLayout,
) )
from View.Tools.Plot.PamhyrCanvas import MplCanvas from View.Tools.Plot.PamhyrCanvas import MplCanvas
@@ -44,6 +44,8 @@ from View.Tools.Plot.PamhyrToolbar import PamhyrPlotToolbar
from View.LateralContribution.Edit.translate import LCETranslate from View.LateralContribution.Edit.translate import LCETranslate
from View.LateralContribution.Edit.Table import TableModel from View.LateralContribution.Edit.Table import TableModel
from View.LateralContribution.Edit.Plot import Plot from View.LateralContribution.Edit.Plot import Plot
from View.LateralContribution.Edit.UndoCommand import SetMetaDataCommand
from View.BoundaryCondition.Edit.Window import WD50Sigma
_translate = QCoreApplication.translate _translate = QCoreApplication.translate
@@ -54,8 +56,10 @@ class EditLateralContributionWindow(PamhyrWindow):
def __init__(self, data=None, def __init__(self, data=None,
study=None, config=None, study=None, config=None,
parent=None): tab="liquid", parent=None):
self._data = data self._data = data
# Grain size of the sediment brought: solid contributions only
self._is_solid = tab == "solid"
trad = LCETranslate() trad = LCETranslate()
name = trad[self._pamhyr_name] name = trad[self._pamhyr_name]
@@ -81,6 +85,7 @@ class EditLateralContributionWindow(PamhyrWindow):
self.setup_table() self.setup_table()
self.setup_plot() self.setup_plot()
self.setup_d50sigma()
self.setup_connections() self.setup_connections()
def setup_table(self): def setup_table(self):
@@ -139,6 +144,15 @@ class EditLateralContributionWindow(PamhyrWindow):
) )
self.plot.draw() self.plot.draw()
def setup_d50sigma(self):
if not self._is_solid:
return
layout = self.find(QVBoxLayout, "verticalLayout_table")
self._d50sigma = WD50Sigma(parent=self)
layout.addWidget(self._d50sigma)
self.widget_update()
def setup_connections(self): def setup_connections(self):
if self._study.is_editable(): if self._study.is_editable():
self.find(QAction, "action_add").triggered.connect(self.add) self.find(QAction, "action_add").triggered.connect(self.add)
@@ -148,10 +162,31 @@ class EditLateralContributionWindow(PamhyrWindow):
self._table.dataChanged.connect(self.update) self._table.dataChanged.connect(self.update)
self._table.layoutChanged.connect(self.update) self._table.layoutChanged.connect(self.update)
if self._is_solid:
if self._study.is_editable():
self._d50sigma.d50Changed.connect(self.d50_changed)
self._d50sigma.sigmaChanged.connect(self.sigma_changed)
else:
self._d50sigma.setEnabled(False)
def d50_changed(self, value):
self._undo_stack.push(SetMetaDataCommand(self._data, "d50", value))
def sigma_changed(self, value):
self._undo_stack.push(
SetMetaDataCommand(self._data, "sigma", value)
)
def widget_update(self):
if self._is_solid:
self._d50sigma.set_d50(self._data.d50)
self._d50sigma.set_sigma(self._data.sigma)
def _propagated_update(self, key=Modules(0)): def _propagated_update(self, key=Modules(0)):
if Modules.STUDY in key: if Modules.STUDY in key:
self._table.layoutChanged.emit() self._table.layoutChanged.emit()
self.plot.update() self.plot.update()
self.widget_update()
def update(self): def update(self):
self.plot.update() self.plot.update()
@@ -235,7 +270,9 @@ class EditLateralContributionWindow(PamhyrWindow):
def _undo(self): def _undo(self):
self._table.undo() self._table.undo()
self.plot.update() self.plot.update()
self.widget_update()
def _redo(self): def _redo(self):
self._table.redo() self._table.redo()
self.plot.update() self.plot.update()
self.widget_update()
+1
View File
@@ -303,6 +303,7 @@ class LateralContributionWindow(PamhyrWindow):
win = EditLateralContributionWindow( win = EditLateralContributionWindow(
data=data, data=data,
study=self._study, study=self._study,
tab=tab,
parent=self parent=self
) )
win.show() win.show()
+6
View File
@@ -35,7 +35,13 @@
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
</property> </property>
<widget class="QWidget" name="verticalLayoutWidget_2">
<layout class="QVBoxLayout" name="verticalLayout_table">
<item>
<widget class="QTableView" name="tableView"/> <widget class="QTableView" name="tableView"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="verticalLayoutWidget"> <widget class="QWidget" name="verticalLayoutWidget">
<layout class="QVBoxLayout" name="verticalLayout"/> <layout class="QVBoxLayout" name="verticalLayout"/>
</widget> </widget>