/[ascend]/trunk/base/generic/system/graph.c
ViewVC logotype

Contents of /trunk/base/generic/system/graph.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1853 - (show annotations) (download) (as text)
Thu Sep 11 06:13:47 2008 UTC (14 years, 2 months ago) by jpye
File MIME type: text/x-csrc
File size: 5400 byte(s)
Fixed detection of Graphviz on linux where pkg-config is available (should be always).
This will break detection of Graphviz on Windows though.
1 /* ASCEND modelling environment
2 Copyright (C) 2007 Carnegie Mellon University
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18 *//** @file
19 System graph output
20
21 Use variable and relation filters to generate a graph between the selected
22 vars and rels, by querying their incidence data. Write the graph in DOT
23 format to the provided FILE*.
24
25 by John Pye, March 2007
26 */
27
28 #ifdef HAVE_GRAPHVIZ_BOOLEAN
29 # define HAVE_BOOLEAN
30 #endif
31 #include <utilities/ascConfig.h>
32
33 #ifdef WITH_GRAPHVIZ
34 # ifdef __WIN32__
35 # include <gvc.h>
36 # else
37 # include <graphviz/gvc.h>
38 # endif
39 # define HAVE_BOOLEAN
40 #endif
41
42 boolean X;
43
44 #include "graph.h"
45 #include "slv_client.h"
46 #include "incidence.h"
47 #include <utilities/ascMalloc.h>
48 #include <utilities/ascPanic.h>
49
50 int system_write_graph(slv_system_t sys
51 , FILE *fp
52 , const char *format
53 ){
54 incidence_vars_t id;
55 build_incidence_data(sys, &id);
56
57 #ifdef WITH_GRAPHVIZ
58 Agraph_t *g;
59 GVC_t *gvc;
60
61 unsigned edgecount = 0;
62 unsigned nodecount = 0;
63
64 gvc = gvContext();
65 g = agopen("g",AGDIGRAPH);
66 agnodeattr(g,"shape","ellipse");
67 agnodeattr(g,"label","");
68 agnodeattr(g,"color","");
69 agnodeattr(g,"style","");
70
71 char temp[200];
72
73 /* first create nodes for the relations */
74 unsigned i;
75 Agnode_t *n, *m;
76 for(i=0; i < id.neqn; ++i){
77 char *relname;
78 relname = rel_make_name(sys,id.rlist[i]);
79 sprintf(temp,"r%d",rel_sindex(id.rlist[i]));
80 n = agnode(g,temp);
81 agset(n,"label",relname);
82 ASC_FREE(relname);
83 nodecount++;
84 }
85
86 /* now create nodes for the variables */
87 unsigned j;
88 for(j=0; j < id.nvar; ++j){
89 char *varname;
90 varname = var_make_name(sys,id.vlist[j]);
91 sprintf(temp,"v%d",var_sindex(id.vlist[j]));
92 n = agnode(g,temp);
93 agset(n,"label",varname);
94 agset(n, "shape", "box");
95 if(var_fixed(id.vlist[j])){
96 CONSOLE_DEBUG("VAR '%s' IS FIXED",varname);
97 agset(n,"style","filled");
98 agset(n,"color","green");
99 }
100 ASC_FREE(varname);
101 nodecount++;
102 }
103
104 /* now create edges */
105 const struct var_variable **ivars;
106 unsigned niv;
107 char reltemp[200];
108 struct Agedge_t *e;
109 for(i=0; i < id.nprow; ++i){
110 ivars = rel_incidence_list(id.rlist[i]);
111 niv = rel_n_incidences(id.rlist[i]);
112 sprintf(reltemp,"r%d",rel_sindex(id.rlist[i]));
113 char *relname;
114 relname = rel_make_name(sys,id.rlist[i]);
115 CONSOLE_DEBUG("rel = '%s'",relname);
116 ASC_FREE(relname);
117 for(j=0; j < niv; ++j){
118 const struct var_variable *v;
119 v = ivars[j];
120 sprintf(temp,"v%d",var_sindex(v));
121 n = agnode(g, reltemp);
122 m = agnode(g, temp);
123
124 if(id.v2pc[var_sindex(v)]==id.e2pr[rel_sindex(id.rlist[i])]){
125 e = agedge(g,n,m); /* from rel to var */
126 }else{
127 e = agedge(g,m,n); /* from var to rel */
128 }
129 edgecount++;
130 }
131 }
132
133 if(nodecount > 300 || edgecount > 300){
134 ERROR_REPORTER_HERE(ASC_USER_ERROR,"Graph is too complex, will not launch GraphViz (%d nodes, %d edges)", nodecount, edgecount);
135 return 1;
136 }
137
138 gvLayout(gvc, g, "dot");
139 gvRender(gvc, g, (char*)format, fp);
140
141 #else
142 ERROR_REPORTER_HERE(ASC_PROG_ERR,"Function system_write_graph not available (GraphViz not present at build-time)");
143 return 1; /* error */
144 #endif
145
146 #if 0
147 int nr,nsr,nv,nsv,niv;
148 int i,j;
149 struct rel_relation **srels;
150 struct var_variable **svars, **ivars;
151 char *relname, *varname;
152
153 CONSOLE_DEBUG("Writing graph...");
154 asc_assert(fp!=NULL);
155
156 CONSOLE_DEBUG("FP = %p",fp);
157 fprintf(fp,"digraph G{\n");
158
159 /* first create nodes for the rels */
160 nr = slv_count_solvers_rels(sys,rfilter);
161 nsr = slv_get_num_solvers_rels(sys);
162 srels = slv_get_solvers_rel_list(sys);
163 fprintf(fp,"\n\n\t/* %d relations */\n\n",nr);
164 for(i=0; i<nsr; ++i){
165 if(rel_apply_filter(srels[i],rfilter)){
166 relname = rel_make_name(sys,srels[i]);
167 fprintf(fp,"\tr%d[shape=box,label=\"%s\"]\n",i,relname);
168 ASC_FREE(relname);
169 }
170 }
171
172 /* and the vars */
173 nsv = slv_get_num_solvers_vars(sys);
174 nv = slv_count_solvers_vars(sys,vfilter);
175 svars = slv_get_solvers_var_list(sys);
176 fprintf(fp,"\n\n\t/* %d variables */\n\n",nv);
177 for(j=0; j<nsv; ++j){
178 if(var_apply_filter(svars[j],vfilter)){
179 varname = var_make_name(sys,svars[j]);
180 if(var_fixed(svars[j])){
181 fprintf(fp,"s\tv%d[label=\"%s\",style=filled,color=green]\n",j,varname);
182 }else{
183 fprintf(fp,"\tv%d[label=\"%s\"]\n",j,varname);
184 }
185 ASC_FREE(varname);
186 }
187 }
188
189 /* now output the edges between them */
190 fprintf(fp,"\n\n\t/* incidences */\n\n");
191 for(i=0; i<nsr; ++i){
192 if(!rel_apply_filter(srels[i],rfilter))continue;
193
194 ivars = rel_incidence_list(srels[i]);
195 niv = rel_n_incidences(srels[i]);
196
197 for(j=0; j<niv; ++j){
198 if(!var_apply_filter(ivars[j],vfilter))continue;
199 if(j==i){
200 fprintf(fp,"\tr%d->v%d\n",i,var_sindex(ivars[j]));
201 }else{
202 fprintf(fp,"\tv%d->r%d\n",var_sindex(ivars[j]),i);
203 }
204 }
205 }
206
207 fprintf(fp,"}\n");
208 #endif
209 CONSOLE_DEBUG("Completed graph output");
210 return 0;
211 }
212
213
214
215

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22