-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursiveSampler.pl
320 lines (252 loc) · 7.85 KB
/
recursiveSampler.pl
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/local/bin/perl
use IPC::Open2;
#use Getopt::Long;
$| = 1;
if ($#ARGV < 0) {
print <<END_OF_USAGE;
Usage::recursiveSampler.pl [-fraction frac] [-bbox xmin xmax ymin ymax zmin zmax]
ellipsoidFunction
where:
-fraction frac specifies the threshold of the shortest ellipsoid dimension
below which refinement is triggered. default is 0.333
-bbox specifies the bounding box of the region to be sampled. 0 1 0 1 0 1 by default.
ellipsoidFunction points to an executable that reads triples from stdin and writes
ellipsoids to stdout. ellipsoids are defined in the following manner:
center (3 doubles)
semi-axis lengths (3 doubles)
major axis (3 doubles - a unit vector)
middle axis (3 doubles - a unit vector)
minor axis (3 doubles - a unit vector)
orientation (3 doubles)
recursiveSampler samples the computational space (either specified by -bbox, or the unit cube)
randomly, and calls ellipsoidFunction at each of the sample points. it keeps track of the smallest
dimension of the ellipsoid, and compares that with the dimension of it\'s bounding box. if the
bounding box is greater than the -fraction parameter (0.333 by default) times the smallest dimension
of the ellipsoid, then recursiveSampler resamples the box with recursively subdivided boxes.
recursiveSampler prints all the ellipsoids on stdout...
END_OF_USAGE
exit(1);
}
$fraction = 1.05;
$bbox = [0,1,0,1,0,1];
$GLOBAL_MAX = 2000;
$listFile = "ellipsoids.txt";
$listbool = 0;
$scale=1;
# foreach $ind (0..$#ARGV) {
# print $ARGV[$ind];
# print "\n";
# }
# $result = GetOptions ("age=i" => $age);
# $result = GetOptions("fraction=i" => $fraction);
# print $fraction;
# print "\n";
foreach $ind (0..$#ARGV) {
if ($ARGV[$ind] =~ /^-fraction/) {
$fraction = $ARGV[$ind+1];
}
if ($ARGV[$ind] =~ /^-bbox/) {
$bbox = [@ARGV[$ind+1..$ind+6]];
}
if ($ARGV[$ind] =~ /^-list/) {
$listbool = 1;
$listFile = $ARGV[$ind+1];
}
}
if ($listbool == 1 ) {
# Open the data file and extract the data set
open(DAT, $listFile) || die("Could not open file!");
@raw_data=<DAT>;
close(DAT);
$globalCount = 0;
# Recursively sample the list and put ellipsoids in the unit RVE
&sampleList($bbox,$fraction,$scale);
} else {
$ellipsoidFunction = pop(@ARGV);
# ok, open up a double ended pipe to ellipsoidFunction...
$pid = open2(*rdfh,*wrfh,"$ellipsoidFunction");
if ($! =~ /^open2/) {
die $!;
}
&sample($bbox,$fraction,rdfh,wrfh);
close($rdfh);
close($wrfh);
}
sub sample {
my $bbox = shift(@_);
my $fraction = shift(@_);
my $rdfh = shift(@_);
my $wrfh = shift(@_);
my $x;
my $y;
my $z;
my $maxDim;
my @parts;
$x = $bbox->[0]+(rand())*($bbox->[1] - $bbox->[0]);
$maxDim = $bbox->[1] - $bbox->[0];
$y = $bbox->[2]+(rand())*($bbox->[3] - $bbox->[2]);
if (($bbox->[3] - $bbox->[2]) > $maxDim) {
$maxDim = $bbox->[3] - $bbox->[2];
}
$z = $bbox->[4]+(rand())*($bbox->[5] - $bbox->[4]);
if (($bbox->[5] - $bbox->[4]) > $maxDim) {
$maxDim = $bbox->[5] - $bbox->[4];
}
print $wrfh "$x $y $z\n";
my $center = <$rdfh>;
my $axes = <$rdfh>;
my $axis1 = <$rdfh>;
my $axis2 = <$rdfh>;
my $axis3 = <$rdfh>;
my $orientation = <$rdfh>;
print $center;
print $axes;
print $axis1;
print $axis2;
print $axis3;
print $orientation;
$globalCount++;
@parts = split(" ",$axes);
#
# Redefine the limiting dimension as the Middle dimension not the Min.
#
# $minAxis = $parts[0];
# if ($parts[1] < $minAxis) {
# $minAxis = $parts[1];
# }
# if ($parts[2] < $minAxis) {
# $minAxis = $parts[2];
# }
# if ($minAxis eq $parts[1]) {
# if ( $parts[0] < $parts[2] )
# {
# $midAxis = $parts[0];
# } else {
# $midAxis = $parts[2];
# }
# }
# if ($minAxis eq $parts[0]) {
# if ( $parts[1] < $parts[2] )
# {
# $midAxis = $parts[1];
# } else {
# $midAxis = $parts[2];
# }
# }
@parts = sort(@parts);
$midAxis = $parts[2];
if ($maxDim > $fraction*$midAxis) {
my $midx = ($bbox->[0] + $bbox->[1])/2.0;
my $midy = ($bbox->[2] + $bbox->[3])/2.0;
my $midz = ($bbox->[4] + $bbox->[5])/2.0;
&sample([$bbox->[0],$midx,$bbox->[2],$midy,$bbox->[4],$midz],$fraction,$rdfh,$wrfh);
&sample([$midx,$bbox->[1],$bbox->[2],$midy,$bbox->[4],$midz],$fraction,$rdfh,$wrfh);
&sample([$bbox->[0],$midx,$midy,$bbox->[3],$bbox->[4],$midz],$fraction,$rdfh,$wrfh);
&sample([$midx,$bbox->[1],$midy,$bbox->[3],$bbox->[4],$midz],$fraction,$rdfh,$wrfh);
&sample([$bbox->[0],$midx,$bbox->[2],$midy,$midz,$bbox->[5]],$fraction,$rdfh,$wrfh);
&sample([$midx,$bbox->[1],$bbox->[2],$midy,$midz,$bbox->[5]],$fraction,$rdfh,$wrfh);
&sample([$bbox->[0],$midx,$midy,$bbox->[3],$midz,$bbox->[5]],$fraction,$rdfh,$wrfh);
&sample([$midx,$bbox->[1],$midy,$bbox->[3],$midz,$bbox->[5]],$fraction,$rdfh,$wrfh);
}
# if ( $globalCount > $GLOBAL_MAX ){
# exit(1);
# }
}
#waitpid $pid, 0;
sub sampleList {
my $bbox = shift(@_);
my $fraction = shift(@_);
my $scale = shift(@_);
my $x;
my $y;
my $z;
my $maxDim;
my @parts;
$x = $bbox->[0]+(rand())*($bbox->[1] - $bbox->[0]);
$maxDim = $bbox->[1] - $bbox->[0];
$y = $bbox->[2]+(rand())*($bbox->[3] - $bbox->[2]);
if (($bbox->[3] - $bbox->[2]) > $maxDim) {
$maxDim = $bbox->[3] - $bbox->[2];
}
$z = $bbox->[4]+(rand())*($bbox->[5] - $bbox->[4]);
if (($bbox->[5] - $bbox->[4]) > $maxDim) {
$maxDim = $bbox->[5] - $bbox->[4];
}
my @el = randomEllipse();
my @Axis = split(/\s+/,$el[0]);
my $a = ("$Axis[0]" / $scale);
my $b = ("$Axis[1]" / $scale);
my $c = ("$Axis[2]" / $scale);
my $center = "$x $y $z\n";
my $axes = "$a $b $c\n";
my $axis1 = $el[1];
my $axis2 = $el[2];
my $axis3 = $el[3];
my $orientation = $el[4];
print $center;
print $axes;
print $axis1;
print $axis2;
print $axis3;
print $orientation;
$globalCount++;
@parts = split(" ",$axes);
#
# Redefine the limiting dimension as the Middle dimension not the Min.
#
# $minAxis = $parts[0];
# if ($parts[1] < $minAxis) {
# $minAxis = $parts[1];
# }
# if ($parts[2] < $minAxis) {
# $minAxis = $parts[2];
# }
# if ($minAxis eq $parts[1]) {
# if ( $parts[0] < $parts[2] )
# {
# $midAxis = $parts[0];
# } else {
# $midAxis = $parts[2];
# }
# }
# if ($minAxis eq $parts[0]) {
# if ( $parts[1] < $parts[2] )
# {
# $midAxis = $parts[1];
# } else {
# $midAxis = $parts[2];
# }
# }
@parts = sort(@parts);
$midAxis = $parts[0];
if ($maxDim > $fraction*$midAxis) {
my $midx = ($bbox->[0] + $bbox->[1])/2.0;
my $midy = ($bbox->[2] + $bbox->[3])/2.0;
my $midz = ($bbox->[4] + $bbox->[5])/2.0;
&sampleList([$bbox->[0],$midx,$bbox->[2],$midy,$bbox->[4],$midz],$fraction,$scale);
&sampleList([$midx,$bbox->[1],$bbox->[2],$midy,$bbox->[4],$midz],$fraction,$scale);
&sampleList([$bbox->[0],$midx,$midy,$bbox->[3],$bbox->[4],$midz],$fraction,$scale);
&sampleList([$midx,$bbox->[1],$midy,$bbox->[3],$bbox->[4],$midz],$fraction,$scale);
&sampleList([$bbox->[0],$midx,$bbox->[2],$midy,$midz,$bbox->[5]],$fraction,$scale);
&sampleList([$midx,$bbox->[1],$bbox->[2],$midy,$midz,$bbox->[5]],$fraction,$scale);
&sampleList([$bbox->[0],$midx,$midy,$bbox->[3],$midz,$bbox->[5]],$fraction,$scale);
&sampleList([$midx,$bbox->[1],$midy,$bbox->[3],$midz,$bbox->[5]],$fraction,$scale);
}
#if ( $globalCount > $GLOBAL_MAX ){
#exit(1);
#}
}
sub randomEllipse{
my $rline = rand(@raw_data);
# Determine the starting line of the dataset.
my $dline = ($rline / 6);
my $il = int ($dline);
# Read the dataset from the file
my $line1 = $raw_data[$il*6];
my $line2 = $raw_data[$il*6+1];
my $line3 = $raw_data[$il*6+2];
my $line4 = $raw_data[$il*6+3];
my $line5 = $raw_data[$il*6+4];
my $line6 = $raw_data[$il*6+5];
my @lines = ($line2, $line3, $line4, $line5, $line6);
}