OpenRadioss 2025.1.11
OpenRadioss project
Loading...
Searching...
No Matches
set_graph.cpp
Go to the documentation of this file.
1//Copyright> OpenRadioss
2//Copyright> Copyright (C) 1986-2025 Altair Engineering Inc.
3//Copyright>
4//Copyright> This program is free software: you can redistribute it and/or modify
5//Copyright> it under the terms of the GNU Affero General Public License as published by
6//Copyright> the Free Software Foundation, either version 3 of the License, or
7//Copyright> (at your option) any later version.
8//Copyright>
9//Copyright> This program is distributed in the hope that it will be useful,
10//Copyright> but WITHOUT ANY WARRANTY; without even the implied warranty of
11//Copyright> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12//Copyright> GNU Affero General Public License for more details.
13//Copyright>
14//Copyright> You should have received a copy of the GNU Affero General Public License
15//Copyright> along with this program. If not, see <https://www.gnu.org/licenses/>.
16//Copyright>
17//Copyright>
18//Copyright> Commercial Alternative: Altair Radioss Software
19//Copyright>
20//Copyright> As an alternative to this open-source version, Altair also offers Altair Radioss
21//Copyright> software under a commercial license. Contact Altair to discuss further if the
22//Copyright> commercial version may interest you: https://www.altair.com/radioss/.
23#include <iostream>
24#include <iterator>
25#include<tuple>
26#include <vector>
27#include <algorithm>
28
29
30using namespace std;
31
32struct Edge {
33 int id;
34 int color;
37 int * set_list;
38};
39
41{
42
43 vector <struct Edge> set_gr;
45
46 private :
47
48 void recur_graph(int set_id,int * dependancy_list,int current_tree,int * check)
49 // ---------------------------------------------------------------------------------
50 // recur_graph recursive function goes through the tree & builds dependency array int * check)
51 // ---------------------------------------------------------------------------------
52 // INPUT
53 // set_id : current set ID
54 // dependancy_list : dependency list to build
55 // current_tree : "Root" Set to inspect need for circular dependency check
56 // check : check value, if negative - gets the SET ID which has circular dependency
57 // OUTPUT
58 // ----------------------------------------------------------------------------
59 {
60
61 if (*check < 0) return; // Error occurred do not continue
62
63 auto edge = set_gr.begin()+set_id-1;
64 if (edge -> color > 0) return; // Current set appears already in list
65
66 if (edge->closed_tree_check == current_tree) {
67 // cout << "error infinite dependency in SET found" << endl; // if check fails this SET has been visited twice for Same Root SET.
68 *check = -current_tree; // Do not continue
69 return;
70 }
71
72 edge->closed_tree_check=current_tree; // closed_check is colored with the Root SET of the tree
73
74 for (int i=0; i< edge->dep_size; i++) {
75 int new_set = edge->set_list[i];
76 // cout << "curr_tree= "<< edge->closed_tree_check << " cur_set= " << set_id << " new_set= " << new_set << endl;
77 recur_graph(new_set, dependancy_list, current_tree,check );
78
79 if (*check < 0) return; // Error occurred do not continue
80 }
81
82
83 if ( edge->color == 0){ // If leave store it in dependency list
84 edge->color =edge->id;
85 dependancy_list[depend_stack] = edge -> id;
86 // cout << "Stack " << edge -> id << endl;
88 }
89
90 }
91
92 public :
93
94 void init_edge(int edge, int dep_sz,int * dep_list)
95 // ---------------------------------------------------------------------------------
96 // init_edge
97 // Create the Graph
98 // Add a SET with its dependencies
99 // ---------------------------------------------------------------------------------
100 // INPUT
101 // edge : the SET user ID
102 // dep_sz : SET/SET number of childs SETs
103 // dep_list : List of Child SET
104 // OUTPUT
105 // ----------------------------------------------------------------------------
106 {
107 struct Edge edg;
108 edg.id=edge;
109 edg.dep_size=dep_sz;
110 edg.color=0;
111 edg.closed_tree_check = 0;
112
113 edg.set_list = new int [dep_sz];
114
115 for (int i=0;i<dep_sz;i++)
116 {
117 edg.set_list[i] = dep_list[i];
118
119 }
120 set_gr.push_back(edg);
121 }
122
123
124 void dependancy_sort(int * dependancy_list, int * check)
125 // ---------------------------------------------------------------------------------
126 // dependancy_sort
127 // Sorting according to the dependencies - Used for SET of SETs
128 // when a SET depends from another SET : ensure Child SET is before.
129 // ---------------------------------------------------------------------------------
130 // INPUT
131 // dependancy_list : dependency list to build
132 // check : check value, if negative - gets the SET ID which has circular dependency
133 // OUTPUT
134 // ----------------------------------------------------------------------------
135 {
136 depend_stack=0;
137
138 for (auto edge = set_gr.begin(); edge != set_gr.end(); edge++){
139 int edg_id = edge -> id;
140
141 if ( edge->dep_size == 0 && edge->color == 0){
142
143 edge->color =edge->id;
144 dependancy_list[depend_stack] = edg_id;
145 depend_stack++;
146
147 }else {
148 recur_graph(edg_id, dependancy_list, edg_id ,check);
149 if (*check < 0) return; // Error occurred do not continue
150 }
151 }
152 }
153
155 {
156 std::vector<struct Edge>().swap(set_gr);
157 }
158
159 void print()
160 // ---------------------------------------------------------------------------------
161 // print the content of SET tree
162 // ---------------------------------------------------------------------------------
163
164 {
165 for (auto edge = set_gr.begin(); edge != set_gr.end(); edge++){
166 cout << "id: " << edge->id << " color: " << edge->color << " size: " << edge->dep_size << endl;
167 cout << " ";
168 int * list = edge-> set_list;
169 for (int i=0; i< edge->dep_size ; i++){
170 cout << list[i] << "," ;
171 }
172 cout << endl;
173 }
174 }
175
176};
177
178
180
181/* ----------------------------------------------------------------------------
182 C/Fortran usable
183 ---------------------------------------------------------------------------- */
184#ifdef _WIN64
185#define set_graph_add_set_ SET_GRAPH_ADD_SET
186#define set_graph_sort_ SET_GRAPH_SORT
187#define set_graph_clean_ SET_GRAPH_CLEAN
188#endif
189
190#define _FCALL
191
192extern "C"
193{
194 void _FCALL set_graph_add_set_(int *set_id, int *set_list, int *list_size){
195 // ---------------------------------------------------------------------------------
196 // Create a SET graph
197 // Add an Edge with its list of child SET
198 // ---------------------------------------------------------------------------------
199 // INPUT
200 // set_id : Integer internal SET ID : must be set between 1 & nsets
201 // set_list : list of child SETs : all child Sets must be existing internal SETid
202 // list_size : number of Child SET (size of list below)
203 // OUTPUT
204 // ----------------------------------------------------------------------------
205
206 set_of_set.init_edge(*set_id, *list_size, set_list);
207 }
208
209
210 void _FCALL set_graph_sort_(int * dependancy_list, int * check){
211 // ---------------------------------------------------------------------------------
212 // Sort the sets according to their dependency
213 // Child SET are placed before Parent SET.
214 // ---------------------------------------------------------------------------------
215 // OUTPUT
216 // int * dependancy_list : sorted dependency list
217 // int * check : check flag - 0=ok -SET=circular dependancy on SET
218 // --------------------------------------------------------------------------------
219 *check = 0;
220 set_of_set.dependancy_sort(dependancy_list,check);
221
222 }
223
225 // ---------------------------------------------------------------------------------
226 // Destroy the graph & clean the memory
227 // ---------------------------------------------------------------------------------
228 set_of_set.delete_tree();
229
230 }
231
232
233}
234
235
236
237
238
239
240#ifdef MAIN
241
242int main()
243{
244 int N=2;
245 int S1L[2]={2,5};
246 int S4L[3]={1,2,3};
247 int S5L[1]={2};
248 int *S2L=NULL;
249
250set_graph my_set_graph;
251
252 my_set_graph.init_edge(1,2,S1L);
253 my_set_graph.init_edge(2,0,NULL);
254 my_set_graph.init_edge(3,0,NULL);
255 my_set_graph.init_edge(4,3,S4L);
256 my_set_graph.init_edge(5,1,S5L);
257 my_set_graph.init_edge(6,0,NULL);
258
259 my_set_graph.print();
260 cout << endl;
261 cout << endl << " dependency computation " << endl ;
262 cout << " ------------------------" << endl << endl ;
263
264 int slist=6;
265 int list[slist];
266 int check =0;
267 for (int i=0;i<slist;i++) { list[i]=0; }
268 my_set_graph.dependancy_sort(list,&check);
269
270 if (check < 0){
271 cout << endl << "error - SET " << -check << " has circular dependency" << endl;
272}
273
274 cout << "-------------------" << endl << endl ;
275 for (int i=0;i<slist;i++) { cout << list[i] << "-" ; }
276 cout << endl << endl ;
277
278
279 return 0;
280}
281
282#endif
int main()
void init_edge(int edge, int dep_sz, int *dep_list)
Definition set_graph.cpp:94
vector< struct Edge > set_gr
Definition set_graph.cpp:43
void recur_graph(int set_id, int *dependancy_list, int current_tree, int *check)
Definition set_graph.cpp:48
int depend_stack
Definition set_graph.cpp:44
void print()
void dependancy_sort(int *dependancy_list, int *check)
void delete_tree()
#define N
#define _FCALL
initmumps id
void _FCALL set_graph_add_set_(int *set_id, int *set_list, int *list_size)
void _FCALL set_graph_clean_()
void _FCALL set_graph_sort_(int *dependancy_list, int *check)
set_graph set_of_set
int id
Definition set_graph.cpp:33
int closed_tree_check
Definition set_graph.cpp:35
int * set_list
Definition set_graph.cpp:37
int color
Definition set_graph.cpp:34
int dep_size
Definition set_graph.cpp:36