Skip to content

Commit c8ec30e

Browse files
Add helper function for converting graph attribute to dolfin
1 parent 1abe47f commit c8ec30e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

graphnics/fenics_graph.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,29 @@ def copy_from_nx_graph(G_nx):
432432

433433

434434
return G
435+
436+
437+
def nxgraph_attribute_to_dolfin(G, attr):
438+
'''
439+
Make a dolfin function representing edge attributes of a networkx graph
440+
441+
Args:
442+
G (nx.Graph): graph representing the network
443+
attr (str): attribute to be represented
444+
445+
Returns:
446+
func (dolfin.Function): a DG function representing the attribute
447+
'''
448+
449+
mesh0, foo = G.get_mesh(0)
450+
DG_coarse = FunctionSpace(mesh0, 'DG', 0) # trick: Interpolate the radius from the coarse mesh to the fine mesh
451+
452+
attr_dict = nx.get_edge_attributes(G, attr)
453+
func = Function(DG_coarse)
454+
func.vector()[:] = np.asarray(list(attr_dict.values()))
455+
func.set_allow_extrapolation(True)
456+
457+
DG = FunctionSpace(G.mesh, 'DG', 1)
458+
func = interpolate(func, DG)
459+
460+
return func

0 commit comments

Comments
 (0)