-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_gals_heat_instance.cpp
More file actions
160 lines (125 loc) · 5.02 KB
/
create_gals_heat_instance.cpp
File metadata and controls
160 lines (125 loc) · 5.02 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "graph.hpp"
#include "graph_persist_sax_writer.hpp"
#include "rapidjson/document.h"
#include "gals_heat.graph.hpp"
#include <string>
#include <cstdlib>
int main(int argc, char *argv[])
{
RegistryImpl registry;
if(argc<2){
fprintf(stderr, "usage : create gals_heat_instance dstFile [n]\n");
fprintf(stderr, " Note that dstFile can't be - at the moment.\n");
exit(1);
}
auto writer=createSAXWriterOnFile(argv[1]);
GraphTypePtr graphType=registry.lookupGraphType("gals_heat");
unsigned exportDeltaMask=1023;
unsigned n=16;
if(argc>2){
n=strtol(argv[2],0,0);
}
assert(n>=3);
double h=1.0/n;
double alpha=1;
double dt=h*h / (4*alpha) * 0.5;
assert(h*h/(4*alpha) >= dt);
double weightOther = dt*alpha/(h*h);
double weightSelf = (1.0 - 4*weightOther);
auto cellType=graphType->getDeviceType("cell");
auto dirichletType=graphType->getDeviceType("dirichlet_variable");
std::string instName="heat_"+std::to_string(n)+"_"+std::to_string(n);
DataPtr<gals_heat_properties_t> graphProperties(new gals_heat_properties_t());
graphProperties->maxTime=n;
graphProperties->exportDeltaMask=exportDeltaMask;
auto xyToIndex=[=](unsigned x, unsigned y)
{ return x*n+y; };
std::vector<std::pair<uint64_t,bool> > nodes(n*n);// Used to hold mapping to writers view of node ids, plus true=dirichlet, false=cell
writer->onGraphType(graphType);
auto gInst=writer->onBeginGraphInstance(graphType, instName, graphProperties, rapidjson::Document());
writer->onBeginDeviceInstances(gInst);
for(unsigned x=0;x<n;x++){
fprintf(stderr," Devices : Row %d of %d\n", x, n);
for(unsigned y=0;y<n;y++){
double nativeLocation[2]={(double)x,(double)y};
bool edgeX = (x==0) || (x==n-1);
bool edgeY = (y==0) || (y==n-1);
if(edgeX && edgeY)
continue;
rapidjson::Document meta;
rapidjson::Value loc(rapidjson::kArrayType);
loc.PushBack(x, meta.GetAllocator());
loc.PushBack(y, meta.GetAllocator());
meta.AddMember("loc", loc, meta.GetAllocator());
if(x==n/2 && y==n/2){
auto props=make_data_ptr<dirichlet_variable_properties_t>();
props->bias=0;
props->amplitude=1;
props->phase=1.5;
props->frequency=100*dt;
props->neighbours=4;
std::string id="v_"+std::to_string(x)+"_"+std::to_string(y);
uint64_t index=writer->onDeviceInstance(gInst, dirichletType, id, props, std::move(meta));
nodes[xyToIndex(x,y)]=std::make_pair(index,true);
//fprintf(stderr, " (%d,%d) -> %llu\n", x,y, index);
}else if(edgeX != edgeY){
auto props=make_data_ptr<dirichlet_variable_properties_t>();
props->bias=0;
props->amplitude=1;
props->phase=1;
props->frequency=70*dt*(x/(double)n + y/(double)n);
props->neighbours=1;
std::string id="v_"+std::to_string(x)+"_"+std::to_string(y);
uint64_t index=writer->onDeviceInstance(gInst, dirichletType, id, props, std::move(meta));
nodes[xyToIndex(x,y)]=std::make_pair(index,true);
//fprintf(stderr, " (%d,%d) -> %llu\n", x,y, index);
}else{
auto props=make_data_ptr<cell_properties_t>();
props->iv=drand48()*2-1;
std::string id="c_"+std::to_string(x)+"_"+std::to_string(y);
uint64_t index=writer->onDeviceInstance(gInst, cellType, id, props, std::move(meta));
nodes[xyToIndex(x,y)]=std::make_pair(index,true);
//fprintf(stderr, " (%d,%d) -> %llu\n", x,y, index);
}
}
}
writer->onEndDeviceInstances(gInst);
auto dirichletIn=dirichletType->getInput("in");
auto cellIn=cellType->getInput("in");
auto dirichletOut=dirichletType->getOutput("out");
auto cellOut=cellType->getOutput("out");
auto add_channel=[&](unsigned x,unsigned y ,unsigned dx,unsigned dy)
{
auto dst=nodes[ xyToIndex(x,y) ];
auto src=nodes[ xyToIndex( (x+dx+n)%n, (y+dy+n)%n ) ];
// fprintf(stderr,"(%d,%d):%u <- (%d,%d):%u\n", x,y, dst.first, (x+dx+n)%n, (y+dy+n)%n, src.first);
writer->onEdgeInstance
(
gInst,
dst.first, dst.second ? dirichletType : cellType, dst.second ? dirichletIn : cellIn,
src.first, src.second ? dirichletType : cellType, src.second ? dirichletOut : cellOut,
TypedDataPtr(), TypedDataPtr()
);
};
writer->onBeginEdgeInstances(gInst);
for(unsigned x=0; x<n; x++){
fprintf(stderr, " Edges : Row %d of %d\n", x,n);
for(unsigned y=0; y<n; y++){
bool edgeX = (x==0) || (x==n-1);
bool edgeY = (y==0) || (y==n-1);
if(edgeX && edgeY)
continue;
if(y!=0 && !edgeX)
add_channel(x,y, 0, -1);
if(x!=n-1 && ! edgeY)
add_channel(x,y, +1, 0);
if(y!=n-1 && ! edgeX)
add_channel(x,y, 0, +1);
if(x!=0 && ! edgeY)
add_channel(x,y, -1, 0);
}
}
writer->onEndEdgeInstances(gInst);
writer->onEndGraphInstance(gInst);
return 0;
}