Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed directional convention of energy flows in thermal_simulation.py #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions pydhn/solving/thermal_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ def _prepare_arrays(E, mass_flow):
edge_mask = np.where(np.abs(E).sum(axis=0) != 0)[0]
E = E[np.ix_(node_mask, edge_mask)]

E_in = (E + np.abs(E)) / 2
E_out = (np.abs(E)-E) / 2
rows = np.argmax(E, axis=0)
columns = np.argmin(E, axis=0)

# Compute total inlet mass flow of each node
mass_flow_in = E_in @ np.abs(mass_flow[edge_mask])
# Compute total outlet mass flow of each node
mass_flow_out = E_out @ np.abs(mass_flow[edge_mask])

return E, E_in, edge_mask, node_mask, rows, columns, mass_flow_in
return E, E_out, edge_mask, node_mask, rows, columns, mass_flow_out


def solve_thermal(
Expand Down Expand Up @@ -134,11 +134,11 @@ def solve_thermal(

# Initialize matrices of the network graph oriented as the mass flow:
# - E -> Incidence matrix , without 0 mass flow elements
# - E_in -> Incidence matrix of inlet edges only built from E
# - E_out -> Incidence matrix of outlet edges only built from E
E = net.incidence_matrix

arrays = _prepare_arrays(E, mass_flow)
E, E_in, edge_mask, node_mask, rows, columns, mass_flow_in = arrays
E, E_out, edge_mask, node_mask, rows, columns, mass_flow_out = arrays

dim = len(node_mask)

Expand All @@ -159,15 +159,15 @@ def solve_thermal(

jac = np.zeros((dim, dim))
for n, (i, j) in enumerate(zip(rows, columns)):
jac[i][j] = t_out_der[edge_mask][n] * np.abs(mass_flow)[edge_mask][n]
jac[i][j] = -t_out_der[edge_mask][n] * np.abs(mass_flow)[edge_mask][n]

jac -= np.diag(mass_flow_in)
jac += np.diag(mass_flow_out)

errors = np.zeros((dim, dim))
for n, (i, j) in enumerate(zip(rows, columns)):
errors[i][j] = t_out[edge_mask][n] * np.abs(mass_flow)[edge_mask][n]
errors[i][j] = -t_out[edge_mask][n] * np.abs(mass_flow)[edge_mask][n]

errors -= np.diag(mass_flow_in * t_nodes[node_mask])
errors += np.diag(mass_flow_out * t_nodes[node_mask])
errors = errors.sum(axis=1)

error = np.max(np.abs(errors))
Expand Down