Skip to content

Commit

Permalink
Made pr changes
Browse files Browse the repository at this point in the history
  • Loading branch information
emilinmathew committed Aug 22, 2024
1 parent 9d7f5c4 commit 3afd8ef
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 128 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dirgraph.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test Dirgraph
name: Test Visualization Application

on: [push, pull_request]

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ build
.settings
*.swp

#Node Modules
# Node modules (for directed graph visualization)
node_modules/
12 changes: 0 additions & 12 deletions applications/dirgraph_visualization/dirgraph_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ def connect_blocks_by_inblock_list(
# Check if connection definition is consistent
for bA in block_list:
for bBnm in bA.connecting_block_list:
# print("Current block name being searched:", bBnm)
# print(bA.connecting_block_list)

bB = block_list[bnames.index(bBnm)]
check_block_pair_flow_consistency(bA, bB)

Expand All @@ -77,14 +74,11 @@ def connect_blocks_by_inblock_list(
if bA.flow_directions[i] == +1 and (id_bA, id_bB) not in connectivity:
name_wire = bA.name + '_' + bB.name
connecting_elements = (block_list[id_bA], block_list[id_bB])
# wire_dict[name_wire] = wire(connecting_elements,name=name_wire)
connectivity.append((id_bA,
id_bB)) # connectivity stores pair-wise tuples of indices of the blocks that are connected; basically, if block 1 is connected to block 2 and the flow goes from block 1 to block 2, then connectivity will store a 2-element tuple, where the first element is the index at which block 1 is stored in block_list and the 2nd element is the index at which block 2 is stored in block_list. if the flow goes from block 2 to block 1, then connectivity will store a 2-element tuple, where the first element is the index at which block 2 is stored in block_list and the 2nd element is the index at which block 1 is stored in block_list.
elif bA.flow_directions[i] == -1:
name_wire = bB.name + '_' + bA.name
connecting_elements = (block_list[id_bB], block_list[id_bA])
# block_list[id_bA].add_connecting_wire(name_wire)
# block_list[id_bB].add_connecting_wire(name_wire)
else:
continue # if this line is executed, then the next two lines (wire_dict[name_wire] = ... and block_list[id_bA] = ...) will not be executed
wire_dict[name_wire] = wire(connecting_elements, name=name_wire)
Expand Down Expand Up @@ -113,12 +107,6 @@ def connect_blocks_by_connectivity_list(block_list, connectivity):
if e1name not in block_list[e2].connecting_block_list:
block_list[e2].add_connecting_wire(name_wire)
block_list[e2].add_connecting_block(e1name, -1)

# print name_wire
# print block_list[e1].name, block_list[e1].flow_directions
# print block_list[e2].name, block_list[e2].flow_directions

# print wire_dict
return wire_dict


Expand Down
48 changes: 33 additions & 15 deletions applications/dirgraph_visualization/dirgraph_main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import pysvzerod
import pandas as pd
import matplotlib.pyplot as plt
Expand All @@ -15,41 +16,58 @@
This file enables the visualization of 0D simulation results in a web app that displays the 0D network
as a directed graph. Users can interactively select nodes to view their parameters and simulation results.
Simply provide the filepath for your simulation input JSON file and specify the directory where
you want to save the output directed graph.
Enter the filepath for your simulation input JSON file and the directory where
you want to save the output directed graph as command line arguments to run the script.
If you want to save the raw svZeroDSolver simulation results, add "export-csv" as the third command line argument.
Created by Emilin Mathew ([email protected])
'''


def dirgraph(filepath, output_dir):
def dirgraph(filepath, output_dir, export_csv):
solver = pysvzerod.Solver(filepath)
solver.run()
results = pd.DataFrame(solver.get_full_result())
# If you want to export the simulation results as a csv
# results.to_csv('insert_file_name', sep=',', index=False, encoding='utf-8')

if export_csv:
results.to_csv('results.csv', sep=',', index=False, encoding='utf-8')
print(f"Results exported to results.csv")

with open(filepath, 'r') as infile:
parameters = json.load(infile)

set_up_0d_network(
filepath,
name_type='id', # Will take either 'id' or 'name',
draw_directed_graph=False, # Enter false if you don't want to save the directed graph
output_dir=output_dir
name_type='id', # Options 'name' or 'id' specifies whether vessel names or ids should be used for each node
draw_directed_graph= False, # Enter True if you want to save the directed graph
output_dir= output_dir
)
base_name = filepath.rsplit('/', 1)[-1]
output_file = os.path.join(output_dir, os.path.splitext(base_name)[0] + "_directed_graph.dot")
G = nx.DiGraph(nx.nx_pydot.read_dot(output_file))
return results, parameters, G


# Input filepath for 0d simulation & specify the output directory.
results, parameters, G = dirgraph(
filepath=''
, output_dir='')
def main():
# Check if the correct number of arguments is provided
if len(sys.argv) < 3:
print("Please pass in at least two arguments: 1) svZeroDSolver input (json file) 2) Output directory to store the results.")
sys.exit(1)

# Retrieve arguments
filepath = sys.argv[1]
output_dir = sys.argv[2]

export_csv = False

if len(sys.argv) > 3 and sys.argv[3] == 'export_csv':
export_csv = True

# Call the dirgraph function with the provided arguments
return dirgraph(filepath, output_dir, export_csv)

if __name__ == '__main__':
results, parameters, G = main()

# Mapping vessel names to IDs
# Mapping vessel names to IDs. The block parameters are obtained from the user's input json file.
vessel_name_to_vessel_id_map = {}
vessel_id_to_vessel_name_map = {}
vessel_params = {}
Expand Down
Loading

0 comments on commit 3afd8ef

Please sign in to comment.