-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask3.cpp
More file actions
125 lines (89 loc) · 2.75 KB
/
Copy pathtask3.cpp
File metadata and controls
125 lines (89 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <vector>
#include <fstream>
#include <unordered_map>
#include <cmath>
#include <cstdlib>
using namespace std;
struct punto {
int n, i, j;
double x, y;
};
struct arco {
int e, n1, n2;
};
// funzione per tradurre da n a m se richiesto
int map_nm(int indice_n, bool nested_dissection, unordered_map<int,int>& n_to_m) {
if (nested_dissection) {
return n_to_m[indice_n];
}
else {
return indice_n;
}
}
int main(int argc, char* argv[]) { // si deve passare N e ndTrue o ndFalse in base a come si vuole l'ordinamento: "./task3 N ndTrue" o "./task3 N ndFalse"
int N = atoi(argv[1]);
double h = 1.0/(N+1);
double K = 0.01;
// lettura coords.txt
int n, i, j;
double x, y;
vector<punto> punti;
ifstream fileCoord("coords.txt");
while (fileCoord>>n>>i>>j>>x>>y) {
punto P;
P.n = n;
P.i = i;
P.j = j;
P.x = x;
P.y = y;
punti.push_back(P);
}
fileCoord.close();
// lettura connectivity.txt
int e, n1, n2;
vector<arco> archi;
ifstream fileConn("connectivity.txt");
while (fileConn>>e>>n1>>n2) {
arco A;
A.e = e;
A.n1 = n1;
A.n2 = n2;
archi.push_back(A);
}
fileConn.close();
// mappatura n->m e lettura ordering.txt
unordered_map<int, int> n_to_m;
int m, n_ord;
ifstream fileOrder("ordering.txt");
while (fileOrder>>m>>n_ord) {
n_to_m[n_ord] = m;
}
fileOrder.close();
bool nested_dissection; // flag che governa che ordinamento usare
string modalità = argv[2];
if (modalità == "ndTrue") {
nested_dissection = true;
}
else if (modalità == "ndFalse") {
nested_dissection = false;
}
// generazione A.txt e rhs.txt
vector<double> rhs(punti.size());
ofstream fileA("A.txt");
ofstream filerhs("rhs.txt");
for (const punto& p : punti) {
fileA << map_nm(p.n, nested_dissection, n_to_m) << " " << map_nm(p.n, nested_dissection, n_to_m) << " " << -4*K/(h*h) << endl; // diagonale di A
double f_val = exp(-10*(p.x*p.x + p.y*p.y)); // valutazione di f sul punto
rhs[map_nm(p.n, nested_dissection, n_to_m)] = f_val; // creazione rhs ordinato secondo l'ordinamento deciso dal flag nested_dissection
}
for (double f : rhs) {
filerhs << f << endl;
}
filerhs.close();
for (const arco& a : archi) {
fileA << map_nm(a.n1, nested_dissection, n_to_m) << " " << map_nm(a.n2, nested_dissection, n_to_m) << " " << K/(h*h) << endl; // elementi fuori diagonale
fileA << map_nm(a.n2, nested_dissection, n_to_m) << " " << map_nm(a.n1, nested_dissection, n_to_m) << " " << K/(h*h) << endl;
}
fileA.close();
return 0;
}