Adists: BC: Update for scenario.

scenarios
Pierre-Antoine 2025-08-14 14:26:07 +02:00
parent 89a898410f
commit 05dcd3bcca
1 changed files with 55 additions and 21 deletions

View File

@ -147,27 +147,45 @@ class BoundaryConditionAdisTS(SQLSubModel):
@classmethod @classmethod
def _db_load(cls, execute, data=None): def _db_load(cls, execute, data=None):
new = [] new = []
status = data['status']
nodes = data['nodes']
scenario = data["scenario"]
loaded = data['loaded_pid']
table = execute( table = execute(
"SELECT pamhyr_id, pollutant, type, node " + "SELECT pamhyr_id, pollutant, type, node, scenario " +
"FROM boundary_condition_adists" "FROM boundary_condition_adists " +
f"WHERE scenario = {scenario.id} " +
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))}) "
) )
if table is not None: if table is not None:
for row in table: for row in table:
it = iter(row)
pid = next(it)
pollutant = next(it)
bc_type = next(it)
node = next(it)
owner_scenario = next(it)
bc = cls( bc = cls(
id=row[0], id=pid,
pollutant=row[1], pollutant=pollutant,
status=data['status'] status=status,
owner_scenario=owner_scenario
) )
bc.type = row[2] bc.type = bc_type
bc.node = None bc.node = None
if row[3] != -1: if row[3] != -1:
tmp = next(filter( tmp = next(
lambda n: n.id == row[3], data["nodes"]), filter(
None) lambda n: n.id == node, nodes
),
None
)
if tmp is not None: if tmp is not None:
bc.node = tmp.id bc.node = tmp.id
else: else:
@ -176,7 +194,8 @@ class BoundaryConditionAdisTS(SQLSubModel):
values = execute( values = execute(
"SELECT data0, data1 FROM " + "SELECT data0, data1 FROM " +
"boundary_condition_data_adists " + "boundary_condition_data_adists " +
f"WHERE bc = '{bc.id}'" f"WHERE bc = '{bc.id}' " +
f"AND scenario = {scenario.id}"
) )
# Write data # Write data
@ -186,40 +205,55 @@ class BoundaryConditionAdisTS(SQLSubModel):
# Replace data at pos ind # Replace data at pos ind
bc._data.append((data0, data1)) bc._data.append((data0, data1))
loaded.add(pid)
new.append(bc) new.append(bc)
data["scenario"] = scenario.parent
new += cls._db_load(execute, data)
data["scenario"] = scenario
return new return new
def _db_save(self, execute, data=None): def _db_save(self, execute, data=None):
if not self.must_be_saved():
return True
execute(f"DELETE FROM boundary_condition_adists WHERE id = {self.id}") execute(
execute(f"DELETE FROM boundary_condition_data_adists" + "DELETE FROM boundary_condition_adists " +
f" WHERE bc = {self.id}") f"WHERE pamhyr_id = {self.id} " +
f"AND scenario = {self._status.scenario_id} "
)
execute(
"DELETE FROM boundary_condition_data_adists " +
f"WHERE bc = {self.id} " +
f"AND scenario = {self._status.scenario_id} "
)
node = -1 node = -1
if self._node is not None: if self._node is not None:
node = self._node node = self._node
sql = ( execute(
"INSERT INTO " + "INSERT INTO " +
"boundary_condition_adists(id, pollutant, type, node) " + "boundary_condition_adists(id, pollutant, type, " +
"node, scenario) " +
"VALUES (" + "VALUES (" +
f"{self.id}, {self._pollutant}, " + f"{self.id}, {self._pollutant}, " +
f"'{self._db_format(self._type)}', {node}" + f"'{self._db_format(self._type)}', {node}, " +
f"{self._status.scenario_id}" +
")" ")"
) )
execute(sql)
for d in self._data: for d in self._data:
data0 = self._db_format(str(d[0])) data0 = self._db_format(str(d[0]))
data1 = self._db_format(str(d[1])) data1 = self._db_format(str(d[1]))
sql = ( execute(
"INSERT INTO " + "INSERT INTO " +
"boundary_condition_data_adists(data0, data1, bc) " + "boundary_condition_data_adists(data0, data1, bc, scenario) " +
f"VALUES ('{data0}', {data1}, {self.id})" f"VALUES ('{data0}', {data1}, {self.id}, " +
f"{self._status.scenario_id})"
) )
execute(sql)
return True return True