File size: 55,952 Bytes
2200060 327238b 4435ed9 f0b4dbf 2200060 43ed509 dc5ba7f fa3addb 5d19a83 dc5ba7f 24aa71f b6cc3a5 43ed509 2200060 327238b 50bb610 327238b 50bb610 327238b 2200060 43ed509 2200060 3ab1145 2200060 f0b4dbf 2200060 4bfdcd7 2200060 f0b4dbf 2200060 4bfdcd7 2200060 4bfdcd7 2200060 3f43a4a ca1362c 4bfdcd7 2200060 d335da5 3116d80 4bfdcd7 d335da5 bb3dc77 d335da5 bb3dc77 d335da5 2200060 4bfdcd7 2200060 4bfdcd7 2200060 ca1362c 4bfdcd7 2200060 eafa610 4bfdcd7 eafa610 ca1362c 4bfdcd7 eafa610 be8288e 2200060 4bfdcd7 2200060 4bfdcd7 2200060 4bfdcd7 2200060 4bfdcd7 2200060 98e78c1 2200060 b68dcad 2200060 01e53e1 2200060 97ee575 2200060 98e78c1 f0b4dbf 98e78c1 aa69b47 98e78c1 aa69b47 98e78c1 d335da5 f0b4dbf d335da5 50bb610 d335da5 98e78c1 f0b4dbf 98e78c1 f0b4dbf 191f79c 98e78c1 2200060 728bce4 3752130 728bce4 dc2d342 728bce4 dc2d342 18bef21 dc2d342 18bef21 dc2d342 18bef21 dc2d342 728bce4 4435ed9 6bf9df2 7863f11 ae208df 6bf9df2 a99b51b 6bf9df2 e2db460 3881c5d 0455569 c2a5995 533d265 72b668c 0455569 80c3c5d 83c071f 6bf9df2 e2db460 c33a45d e2db460 b68dcad ef755bb c33a45d a5fe928 ae9b2d5 80c3c5d 4fc5115 2d76c04 0455569 2d76c04 0455569 2d76c04 3752130 f30e0c2 ed22ed5 3881c5d ae9b2d5 0455569 3752130 ae9b2d5 bb3dc77 3752130 e3974b2 31e0b3b b7dea3c ad66a13 3752130 6bf9df2 8aa8f19 1c5bbca e350313 fa43469 e350313 1c5bbca fa43469 1c5bbca e350313 1c5bbca 7c13ef8 8aa8f19 e5e43e2 83c071f |
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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 |
import keras
import numpy
import gradio
import pandas
import glob
import os
import shutil
import math
import platform
import scipy.spatial
import plotly.graph_objects as go
import random
from huggingface_hub import from_pretrained_keras
def load_data():
from datasets import load_dataset
S = 5
N = 1000
D = 3
F = 64
G = 32
data = load_dataset("cmudrc/wave-energy", data_files=["data.zip"], split='train')
geometry = numpy.reshape(data['geometry'], (S*N, G*G*G))
curves = numpy.reshape(data['curves'], (S*N, D*F))
return None, None, S, N, D, F, G, curves, geometry
# Disable eager execution because its bad
from tensorflow.python.framework.ops import disable_eager_execution
disable_eager_execution()
class Mesh:
def __init__(self):
# Define blank values
self.np = 0
self.nf = 0
self.X = []
self.Y = []
self.Z = []
self.P = []
def combine_meshes(self, ob1, ob2):
# Check for largest mesh
if ob1.nf < ob2.nf:
coin_test = ob1.make_coin()
coin_target = ob2.make_coin()
else:
coin_test = ob2.make_coin()
coin_target = ob1.make_coin()
# Check for duplicate panels
deletion_list = []
for iF in range(numpy.size(coin_test[1, 1, :])):
panel_test = coin_test[:, :, iF]
for iFF in range(numpy.size(coin_target[1, 1, :])):
panel_target = coin_target[:, :, iFF]
if numpy.sum(panel_test == panel_target) == 12:
coin_target = numpy.delete(coin_target, iFF, 2)
deletion_list.append(iF)
coin_test = numpy.delete(coin_test, deletion_list, 2)
# Concatenate unique meshes
coin = numpy.concatenate((coin_test, coin_target), axis=2)
self.np = numpy.size(coin[1, 1, :]) * 4
self.nf = numpy.size(coin[1, 1, :])
self.X = numpy.zeros(numpy.size(coin[1, 1, :]) * 4)
self.Y = numpy.zeros(numpy.size(coin[1, 1, :]) * 4)
self.Z = numpy.zeros(numpy.size(coin[1, 1, :]) * 4)
self.P = numpy.zeros((numpy.size(coin[1, 1, :]), 4), dtype=int)
iP = 0
for iF in range(numpy.size(coin[1, 1, :])):
for iC in range(4):
self.X[iP] = coin[0, iC, iF]
self.Y[iP] = coin[1, iC, iF]
self.Z[iP] = coin[2, iC, iF]
iP += 1
self.P[iF, 0] = 1 + iF * 4
self.P[iF, 1] = 2 + iF * 4
self.P[iF, 2] = 3 + iF * 4
self.P[iF, 3] = 4 + iF * 4
def make_coin(self):
coin = numpy.zeros((3, 4, self.nf))
for iF in range(self.nf):
for iC in range(4):
coin[0, iC, iF] = self.X[self.P[iF, iC] - 1]
coin[1, iC, iF] = self.Y[self.P[iF, iC] - 1]
coin[2, iC, iF] = self.Z[self.P[iF, iC] - 1]
return coin
def delete_horizontal_panels(self):
coin = self.make_coin()
apex = numpy.min(self.Z)
zLoc = numpy.zeros(4)
deletion_list = []
# Check every panel for horizontality and higher position than lowest point
for iP in range(self.nf):
for iC in range(4):
zLoc[iC] = coin[2, iC, iP]
if numpy.abs(numpy.mean(zLoc) - zLoc[0]) < 0.001 and numpy.mean(zLoc) > apex:
deletion_list.append(iP)
# Delete selected panels
coin = numpy.delete(coin, deletion_list, 2)
# Remake mesh
self.np = numpy.size(coin[1, 1, :]) * 4
self.nf = numpy.size(coin[1, 1, :])
self.X = numpy.zeros(numpy.size(coin[1, 1, :]) * 4)
self.Y = numpy.zeros(numpy.size(coin[1, 1, :]) * 4)
self.Z = numpy.zeros(numpy.size(coin[1, 1, :]) * 4)
self.P = numpy.zeros((numpy.size(coin[1, 1, :]), 4), dtype=int)
iP = 0
for iF in range(numpy.size(coin[1, 1, :])):
for iC in range(4):
self.X[iP] = coin[0, iC, iF]
self.Y[iP] = coin[1, iC, iF]
self.Z[iP] = coin[2, iC, iF]
iP += 1
self.P[iF, 0] = 1 + (iF) * 4
self.P[iF, 1] = 2 + (iF) * 4
self.P[iF, 2] = 3 + (iF) * 4
self.P[iF, 3] = 4 + (iF) * 4
def writeMesh(msh, filename):
with open(filename, 'w') as f:
f.write('{:d}\n'.format(msh.np))
f.write('{:d}\n'.format(msh.nf))
for iP in range(msh.np):
f.write(' {:.7f} {:.7f} {:.7f}\n'.format(msh.X[iP], msh.Y[iP], msh.Z[iP]))
for iF in range(msh.nf):
f.write(' {:d} {:d} {:d} {:d}\n'.format(msh.P[iF, 0], msh.P[iF, 1], msh.P[iF, 2], msh.P[iF, 3]))
return None
class box:
def __init__(self, length, width, height, cCor):
self.length = length
self.width = width
self.height = height
self.xC = cCor[0]
self.yC = cCor[1]
self.zC = cCor[2]
self.name = 'box'
self.panelize()
self.translate(self.xC, self.yC, self.zC)
def panelize(self):
self.nf = 6
self.np = 8
self.X = numpy.array(
[-self.length / 2.0, self.length / 2.0, -self.length / 2.0, self.length / 2.0, -self.length / 2.0,
self.length / 2.0, -self.length / 2.0, self.length / 2.0])
self.Y = numpy.array([self.width / 2.0, self.width / 2.0, self.width / 2.0, self.width / 2.0, -self.width / 2.0,
-self.width / 2.0, -self.width / 2.0, -self.width / 2.0])
self.Z = numpy.array(
[-self.height / 2.0, -self.height / 2.0, self.height / 2.0, self.height / 2.0, -self.height / 2.0,
-self.height / 2.0, self.height / 2.0, self.height / 2.0])
self.P = numpy.zeros([6, 4], dtype=int)
self.P[0, :] = numpy.array([3, 4, 2, 1])
self.P[1, :] = numpy.array([4, 8, 6, 2])
self.P[2, :] = numpy.array([8, 7, 5, 6])
self.P[3, :] = numpy.array([7, 3, 1, 5])
self.P[4, :] = numpy.array([2, 6, 5, 1])
self.P[5, :] = numpy.array([8, 4, 3, 7])
# Define triangles for plotting
self.trii = numpy.zeros([2 * self.nf, 3], dtype=int)
iT = 0
for iTr in range(self.nf):
self.trii[iT, :] = [self.P[iTr, 0] - 1, self.P[iTr, 1] - 1, self.P[iTr, 2] - 1]
self.trii[iT + 1, :] = [self.P[iTr, 0] - 1, self.P[iTr, 2] - 1, self.P[iTr, 3] - 1]
iT += 2
def translate(self, xT, yT, zT):
self.X += xT
self.Y += yT
self.Z += zT
def rotate(self, a1, a2, theta):
R = numpy.zeros([3, 3])
# Normal vector through origin
u = a2[0] - a1[0]
v = a2[1] - a1[1]
w = a2[2] - a1[2]
u = u / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
v = v / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
w = w / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
# Translate mesh so that rotation axis starts from the origin
self.X -= a1[0]
self.Y -= a1[1]
self.Z -= a1[2]
# Rotation matrix
R[0, 0] = u ** 2 + numpy.cos(theta) * (1 - u ** 2)
R[0, 1] = u * v * (1 - numpy.cos(theta)) - w * numpy.sin(theta)
R[0, 2] = u * w * (1 - numpy.cos(theta)) + v * numpy.sin(theta)
R[1, 0] = u * v * (1 - numpy.cos(theta)) + w * numpy.sin(theta)
R[1, 1] = v ** 2 + numpy.cos(theta) * (1 - v ** 2)
R[1, 2] = v * w * (1 - numpy.cos(theta)) - u * numpy.sin(theta)
R[2, 0] = w * u * (1 - numpy.cos(theta)) - v * numpy.sin(theta)
R[2, 1] = w * v * (1 - numpy.cos(theta)) + u * numpy.sin(theta)
R[2, 2] = w ** 2 + numpy.cos(theta) * (1 - w ** 2)
for iP in range(self.np):
p1 = numpy.array([self.X[iP], self.Y[iP], self.Z[iP]])
p2 = numpy.dot(R, p1)
self.X[iP] = p2[0]
self.Y[iP] = p2[1]
self.Z[iP] = p2[2]
# Translate back to original position
self.X += a1[0]
self.Y += a1[1]
self.Z += a1[2]
def makeCoin(self):
coin = numpy.zeros((3, 4, self.nf))
for iF in range(self.nf):
for iC in range(4):
coin[0, iC, iF] = self.X[self.P[iF, iC] - 1]
coin[1, iC, iF] = self.Y[self.P[iF, iC] - 1]
coin[2, iC, iF] = self.Z[self.P[iF, iC] - 1]
return coin
class cone:
def __init__(self, diameter, height, cCor):
self.diameter = diameter
self.height = height
self.xC = cCor[0]
self.yC = cCor[1]
self.zC = cCor[2]
self.name = 'cone'
self.panelize()
self.translate(self.xC, self.yC, self.zC)
def panelize(self):
Ntheta = 18
Nz = 3
theta = [xx * 2 * numpy.pi / (Ntheta - 1) for xx in range(Ntheta)]
self.nf = 0
self.np = 0
r = [0, self.diameter / 2.0, 0]
z = [0, 0, -self.height]
self.X = []
self.Y = []
self.Z = []
self.P = numpy.zeros([(len(r) - 1) * (Ntheta - 1), 4], dtype=int)
n = len(r)
for iT in range(Ntheta):
for iN in range(n):
self.X.append(r[iN] * numpy.cos(theta[iT]))
self.Y.append(r[iN] * numpy.sin(theta[iT]))
self.Z.append(z[iN])
self.np += 1
iP = 0
for iN in range(1, n):
for iT in range(1, Ntheta):
self.P[iP, 0] = iN + n * (iT - 1)
self.P[iP, 1] = iN + 1 + n * (iT - 1)
self.P[iP, 2] = iN + 1 + n * iT
self.P[iP, 3] = iN + n * iT
self.nf += 1
iP += 1
self.X = numpy.array(self.X)
self.Y = numpy.array(self.Y)
self.Z = numpy.array(self.Z)
# Define triangles for plotting
self.trii = numpy.zeros([2 * self.nf, 3], dtype=int)
iT = 0
for iTr in range(self.nf):
self.trii[iT, :] = [self.P[iTr, 0] - 1, self.P[iTr, 1] - 1, self.P[iTr, 2] - 1]
self.trii[iT + 1, :] = [self.P[iTr, 0] - 1, self.P[iTr, 2] - 1, self.P[iTr, 3] - 1]
iT += 2
def translate(self, xT, yT, zT):
self.X += xT
self.Y += yT
self.Z += zT
def rotate(self, a1, a2, theta):
R = numpy.zeros([3, 3])
# Normal vector through origin
u = a2[0] - a1[0]
v = a2[1] - a1[1]
w = a2[2] - a1[2]
u = u / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
v = v / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
w = w / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
# Translate mesh so that rotation axis starts from the origin
self.X -= a1[0]
self.Y -= a1[1]
self.Z -= a1[2]
# Rotation matrix
R[0, 0] = u ** 2 + numpy.cos(theta) * (1 - u ** 2)
R[0, 1] = u * v * (1 - numpy.cos(theta)) - w * numpy.sin(theta)
R[0, 2] = u * w * (1 - numpy.cos(theta)) + v * numpy.sin(theta)
R[1, 0] = u * v * (1 - numpy.cos(theta)) + w * numpy.sin(theta)
R[1, 1] = v ** 2 + numpy.cos(theta) * (1 - v ** 2)
R[1, 2] = v * w * (1 - numpy.cos(theta)) - u * numpy.sin(theta)
R[2, 0] = w * u * (1 - numpy.cos(theta)) - v * numpy.sin(theta)
R[2, 1] = w * v * (1 - numpy.cos(theta)) + u * numpy.sin(theta)
R[2, 2] = w ** 2 + numpy.cos(theta) * (1 - w ** 2)
for iP in range(self.np):
p1 = numpy.array([self.X[iP], self.Y[iP], self.Z[iP]])
p2 = numpy.dot(R, p1)
self.X[iP] = p2[0]
self.Y[iP] = p2[1]
self.Z[iP] = p2[2]
# Translate back to original position
self.X += a1[0]
self.Y += a1[1]
self.Z += a1[2]
def makeCoin(self):
coin = numpy.zeros((3, 4, self.nf))
for iF in range(self.nf):
for iC in range(4):
coin[0, iC, iF] = self.X[self.P[iF, iC] - 1]
coin[1, iC, iF] = self.Y[self.P[iF, iC] - 1]
coin[2, iC, iF] = self.Z[self.P[iF, iC] - 1]
return coin
class cylinder:
def __init__(self, diameter, height, cCor):
self.diameter = diameter
self.height = height
self.xC = cCor[0]
self.yC = cCor[1]
self.zC = cCor[2]
self.name = 'cylinder'
self.panelize()
self.translate(self.xC, self.yC, self.zC)
def panelize(self):
Ntheta = 18
Nz = 3
theta = [xx * 2 * numpy.pi / (Ntheta - 1) for xx in range(Ntheta)]
self.nf = 0
self.np = 0
r = [0, self.diameter / 2.0, self.diameter / 2.0, 0]
z = [0, 0, -self.height, -self.height]
self.X = []
self.Y = []
self.Z = []
self.P = numpy.zeros([(len(r) - 1) * (Ntheta - 1), 4], dtype=int)
n = len(r)
for iT in range(Ntheta):
for iN in range(n):
self.X.append(r[iN] * numpy.cos(theta[iT]))
self.Y.append(r[iN] * numpy.sin(theta[iT]))
self.Z.append(z[iN])
self.np += 1
iP = 0
for iN in range(1, n):
for iT in range(1, Ntheta):
self.P[iP, 0] = iN + n * (iT - 1)
self.P[iP, 1] = iN + 1 + n * (iT - 1)
self.P[iP, 2] = iN + 1 + n * iT
self.P[iP, 3] = iN + n * iT
self.nf += 1
iP += 1
self.X = numpy.array(self.X)
self.Y = numpy.array(self.Y)
self.Z = numpy.array(self.Z)
# Define triangles for plotting
self.trii = numpy.zeros([2 * self.nf, 3], dtype=int)
iT = 0
for iTr in range(self.nf):
self.trii[iT, :] = [self.P[iTr, 0] - 1, self.P[iTr, 1] - 1, self.P[iTr, 2] - 1]
self.trii[iT + 1, :] = [self.P[iTr, 0] - 1, self.P[iTr, 2] - 1, self.P[iTr, 3] - 1]
iT += 2
def translate(self, xT, yT, zT):
self.X += xT
self.Y += yT
self.Z += zT
def rotate(self, a1, a2, theta):
R = numpy.zeros([3, 3])
# Normal vector through origin
u = a2[0] - a1[0]
v = a2[1] - a1[1]
w = a2[2] - a1[2]
u = u / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
v = v / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
w = w / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
# Translate mesh so that rotation axis starts from the origin
self.X -= a1[0]
self.Y -= a1[1]
self.Z -= a1[2]
# Rotation matrix
R[0, 0] = u ** 2 + numpy.cos(theta) * (1 - u ** 2)
R[0, 1] = u * v * (1 - numpy.cos(theta)) - w * numpy.sin(theta)
R[0, 2] = u * w * (1 - numpy.cos(theta)) + v * numpy.sin(theta)
R[1, 0] = u * v * (1 - numpy.cos(theta)) + w * numpy.sin(theta)
R[1, 1] = v ** 2 + numpy.cos(theta) * (1 - v ** 2)
R[1, 2] = v * w * (1 - numpy.cos(theta)) - u * numpy.sin(theta)
R[2, 0] = w * u * (1 - numpy.cos(theta)) - v * numpy.sin(theta)
R[2, 1] = w * v * (1 - numpy.cos(theta)) + u * numpy.sin(theta)
R[2, 2] = w ** 2 + numpy.cos(theta) * (1 - w ** 2)
for iP in range(self.np):
p1 = numpy.array([self.X[iP], self.Y[iP], self.Z[iP]])
p2 = numpy.dot(R, p1)
self.X[iP] = p2[0]
self.Y[iP] = p2[1]
self.Z[iP] = p2[2]
# Translate back to original position
self.X += a1[0]
self.Y += a1[1]
self.Z += a1[2]
def makeCoin(self):
coin = numpy.zeros((3, 4, self.nf))
for iF in range(self.nf):
for iC in range(4):
coin[0, iC, iF] = self.X[self.P[iF, iC] - 1]
coin[1, iC, iF] = self.Y[self.P[iF, iC] - 1]
coin[2, iC, iF] = self.Z[self.P[iF, iC] - 1]
return coin
class hemicylinder:
def __init__(self, diameter, height, cCor):
self.diameter = diameter
self.height = height
self.xC = cCor[0]
self.yC = cCor[1]
self.zC = cCor[2]
self.name = 'hemicylinder'
self.panelize()
self.translate(self.xC, self.yC, self.zC)
def panelize(self):
Ntheta = 18
Nz = 3
theta = [xx * numpy.pi / (Ntheta - 1) - numpy.pi / 2.0 for xx in range(Ntheta)]
self.nf = 0
self.np = 0
r = [0, self.diameter / 2.0, self.diameter / 2.0, 0]
z = [self.height / 2.0, self.height / 2.0, -self.height / 2.0, -self.height / 2.0]
self.X = []
self.Y = []
self.Z = []
self.P = numpy.zeros([(len(r) - 1) * (Ntheta - 1), 4], dtype=int)
n = len(r)
for iT in range(Ntheta):
for iN in range(n):
self.Z.append(-r[iN] * numpy.cos(theta[iT]))
self.X.append(r[iN] * numpy.sin(theta[iT]))
self.Y.append(z[iN])
self.np += 1
iP = 0
for iN in range(1, n):
for iT in range(1, Ntheta):
self.P[iP, 3] = iN + n * (iT - 1)
self.P[iP, 2] = iN + 1 + n * (iT - 1)
self.P[iP, 1] = iN + 1 + n * iT
self.P[iP, 0] = iN + n * iT
self.nf += 1
iP += 1
self.X = numpy.array(self.X)
self.Y = numpy.array(self.Y)
self.Z = numpy.array(self.Z)
# Define triangles for plotting
self.trii = numpy.zeros([2 * self.nf, 3], dtype=int)
iT = 0
for iTr in range(self.nf):
self.trii[iT, :] = [self.P[iTr, 0] - 1, self.P[iTr, 1] - 1, self.P[iTr, 2] - 1]
self.trii[iT + 1, :] = [self.P[iTr, 0] - 1, self.P[iTr, 2] - 1, self.P[iTr, 3] - 1]
iT += 2
def translate(self, xT, yT, zT):
self.X += xT
self.Y += yT
self.Z += zT
def rotate(self, a1, a2, theta):
R = numpy.zeros([3, 3])
# Normal vector through origin
u = a2[0] - a1[0]
v = a2[1] - a1[1]
w = a2[2] - a1[2]
u = u / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
v = v / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
w = w / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
# Translate mesh so that rotation axis starts from the origin
self.X -= a1[0]
self.Y -= a1[1]
self.Z -= a1[2]
# Rotation matrix
R[0, 0] = u ** 2 + numpy.cos(theta) * (1 - u ** 2)
R[0, 1] = u * v * (1 - numpy.cos(theta)) - w * numpy.sin(theta)
R[0, 2] = u * w * (1 - numpy.cos(theta)) + v * numpy.sin(theta)
R[1, 0] = u * v * (1 - numpy.cos(theta)) + w * numpy.sin(theta)
R[1, 1] = v ** 2 + numpy.cos(theta) * (1 - v ** 2)
R[1, 2] = v * w * (1 - numpy.cos(theta)) - u * numpy.sin(theta)
R[2, 0] = w * u * (1 - numpy.cos(theta)) - v * numpy.sin(theta)
R[2, 1] = w * v * (1 - numpy.cos(theta)) + u * numpy.sin(theta)
R[2, 2] = w ** 2 + numpy.cos(theta) * (1 - w ** 2)
for iP in range(self.np):
p1 = numpy.array([self.X[iP], self.Y[iP], self.Z[iP]])
p2 = numpy.dot(R, p1)
self.X[iP] = p2[0]
self.Y[iP] = p2[1]
self.Z[iP] = p2[2]
# Translate back to original position
self.X += a1[0]
self.Y += a1[1]
self.Z += a1[2]
def makeCoin(self):
coin = numpy.zeros((3, 4, self.nf))
for iF in range(self.nf):
for iC in range(4):
coin[0, iC, iF] = self.X[self.P[iF, iC] - 1]
coin[1, iC, iF] = self.Y[self.P[iF, iC] - 1]
coin[2, iC, iF] = self.Z[self.P[iF, iC] - 1]
return coin
class sphere:
def __init__(self, diameter, cCor):
self.diameter = diameter
self.xC = cCor[0]
self.yC = cCor[1]
self.zC = cCor[2]
self.name = 'sphere'
self.panelize()
self.translate(self.xC, self.yC, self.zC)
def panelize(self):
Ntheta = 18
Nthetad2 = int(Ntheta / 2)
Nz = 3
theta = [xx * 2 * numpy.pi / (Ntheta - 1) for xx in range(Ntheta)]
phi = [xx * numpy.pi / (Ntheta / 2 - 1) for xx in range(Nthetad2)]
self.nf = 0
self.np = 0
r = self.diameter / 2.0
self.X = []
self.Y = []
self.Z = []
self.P = numpy.zeros([(Ntheta - 1) * (Nthetad2 - 1), 4], dtype=int)
for iT in range(Nthetad2):
for iTT in range(Ntheta):
self.X.append(r * numpy.cos(theta[iTT]) * numpy.sin(phi[iT]))
self.Y.append(r * numpy.sin(theta[iTT]) * numpy.sin(phi[iT]))
self.Z.append(r * numpy.cos(phi[iT]))
self.np += 1
iP = 0
for iN in range(1, Ntheta):
for iT in range(1, Nthetad2):
self.P[iP, 3] = iN + Ntheta * (iT - 1)
self.P[iP, 2] = iN + 1 + Ntheta * (iT - 1)
self.P[iP, 1] = iN + 1 + Ntheta * iT
self.P[iP, 0] = iN + Ntheta * iT
self.nf += 1
iP += 1
self.X = numpy.array(self.X)
self.Y = numpy.array(self.Y)
self.Z = numpy.array(self.Z)
# Define triangles for plotting
self.trii = numpy.zeros([2 * self.nf, 3], dtype=int)
iT = 0
for iTr in range(self.nf):
self.trii[iT, :] = [self.P[iTr, 0] - 1, self.P[iTr, 1] - 1, self.P[iTr, 2] - 1]
self.trii[iT + 1, :] = [self.P[iTr, 0] - 1, self.P[iTr, 2] - 1, self.P[iTr, 3] - 1]
iT += 2
def translate(self, xT, yT, zT):
self.X += xT
self.Y += yT
self.Z += zT
def rotate(self, a1, a2, theta):
R = numpy.zeros([3, 3])
# Normal vector through origin
u = a2[0] - a1[0]
v = a2[1] - a1[1]
w = a2[2] - a1[2]
u = u / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
v = v / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
w = w / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
# Translate mesh so that rotation axis starts from the origin
self.X -= a1[0]
self.Y -= a1[1]
self.Z -= a1[2]
# Rotation matrix
R[0, 0] = u ** 2 + numpy.cos(theta) * (1 - u ** 2)
R[0, 1] = u * v * (1 - numpy.cos(theta)) - w * numpy.sin(theta)
R[0, 2] = u * w * (1 - numpy.cos(theta)) + v * numpy.sin(theta)
R[1, 0] = u * v * (1 - numpy.cos(theta)) + w * numpy.sin(theta)
R[1, 1] = v ** 2 + numpy.cos(theta) * (1 - v ** 2)
R[1, 2] = v * w * (1 - numpy.cos(theta)) - u * numpy.sin(theta)
R[2, 0] = w * u * (1 - numpy.cos(theta)) - v * numpy.sin(theta)
R[2, 1] = w * v * (1 - numpy.cos(theta)) + u * numpy.sin(theta)
R[2, 2] = w ** 2 + numpy.cos(theta) * (1 - w ** 2)
for iP in range(self.np):
p1 = numpy.array([self.X[iP], self.Y[iP], self.Z[iP]])
p2 = numpy.dot(R, p1)
self.X[iP] = p2[0]
self.Y[iP] = p2[1]
self.Z[iP] = p2[2]
# Translate back to original position
self.X += a1[0]
self.Y += a1[1]
self.Z += a1[2]
def makeCoin(self):
coin = numpy.zeros((3, 4, self.nf))
for iF in range(self.nf):
for iC in range(4):
coin[0, iC, iF] = self.X[self.P[iF, iC] - 1]
coin[1, iC, iF] = self.Y[self.P[iF, iC] - 1]
coin[2, iC, iF] = self.Z[self.P[iF, iC] - 1]
return coin
class hemisphere:
def __init__(self, diameter, cCor):
self.diameter = diameter
self.xC = cCor[0]
self.yC = cCor[1]
self.zC = cCor[2]
self.name = 'hemisphere'
self.panelize()
self.translate(self.xC, self.yC, self.zC)
def panelize(self):
Ntheta = 18
theta = [xx * 2 * numpy.pi / (Ntheta - 1) for xx in range(Ntheta)]
phi = [xx * numpy.pi / 2.0 / (Ntheta / 2 - 1) for xx in range(Ntheta / 2)]
self.nf = 0
self.np = 0
r = self.diameter / 2.0
self.X = []
self.Y = []
self.Z = []
self.P = numpy.zeros([(Ntheta - 1) * (Ntheta / 2 - 1), 4], dtype=int)
for iT in range(Ntheta / 2):
for iTT in range(Ntheta):
self.X.append(r * numpy.cos(theta[iTT]) * numpy.sin(phi[iT]))
self.Y.append(r * numpy.sin(theta[iTT]) * numpy.sin(phi[iT]))
self.Z.append(-r * numpy.cos(phi[iT]))
self.np += 1
iP = 0
for iN in range(1, Ntheta):
for iT in range(1, Ntheta / 2):
self.P[iP, 0] = iN + Ntheta * (iT - 1)
self.P[iP, 1] = iN + 1 + Ntheta * (iT - 1)
self.P[iP, 2] = iN + 1 + Ntheta * iT
self.P[iP, 3] = iN + Ntheta * iT
self.nf += 1
iP += 1
self.X = numpy.array(self.X)
self.Y = numpy.array(self.Y)
self.Z = numpy.array(self.Z)
# Define triangles for plotting
self.trii = numpy.zeros([2 * self.nf, 3], dtype=int)
iT = 0
for iTr in range(self.nf):
self.trii[iT, :] = [self.P[iTr, 0] - 1, self.P[iTr, 1] - 1, self.P[iTr, 2] - 1]
self.trii[iT + 1, :] = [self.P[iTr, 0] - 1, self.P[iTr, 2] - 1, self.P[iTr, 3] - 1]
iT += 2
def translate(self, xT, yT, zT):
self.X += xT
self.Y += yT
self.Z += zT
def rotate(self, a1, a2, theta):
R = numpy.zeros([3, 3])
# Normal vector through origin
u = a2[0] - a1[0]
v = a2[1] - a1[1]
w = a2[2] - a1[2]
u = u / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
v = v / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
w = w / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
# Translate mesh so that rotation axis starts from the origin
self.X -= a1[0]
self.Y -= a1[1]
self.Z -= a1[2]
# Rotation matrix
R[0, 0] = u ** 2 + numpy.cos(theta) * (1 - u ** 2)
R[0, 1] = u * v * (1 - numpy.cos(theta)) - w * numpy.sin(theta)
R[0, 2] = u * w * (1 - numpy.cos(theta)) + v * numpy.sin(theta)
R[1, 0] = u * v * (1 - numpy.cos(theta)) + w * numpy.sin(theta)
R[1, 1] = v ** 2 + numpy.cos(theta) * (1 - v ** 2)
R[1, 2] = v * w * (1 - numpy.cos(theta)) - u * numpy.sin(theta)
R[2, 0] = w * u * (1 - numpy.cos(theta)) - v * numpy.sin(theta)
R[2, 1] = w * v * (1 - numpy.cos(theta)) + u * numpy.sin(theta)
R[2, 2] = w ** 2 + numpy.cos(theta) * (1 - w ** 2)
for iP in range(self.np):
p1 = numpy.array([self.X[iP], self.Y[iP], self.Z[iP]])
p2 = numpy.dot(R, p1)
self.X[iP] = p2[0]
self.Y[iP] = p2[1]
self.Z[iP] = p2[2]
# Translate back to original position
self.X += a1[0]
self.Y += a1[1]
self.Z += a1[2]
def makeCoin(self):
coin = numpy.zeros((3, 4, self.nf))
for iF in range(self.nf):
for iC in range(4):
coin[0, iC, iF] = self.X[self.P[iF, iC] - 1]
coin[1, iC, iF] = self.Y[self.P[iF, iC] - 1]
coin[2, iC, iF] = self.Z[self.P[iF, iC] - 1]
return coin
class pyramid:
def __init__(self, length, width, height, cCor):
self.length = length
self.width = width
self.height = height
self.xC = cCor[0]
self.yC = cCor[1]
self.zC = cCor[2]
self.name = 'pyramid'
self.panelize()
self.translate(self.xC, self.yC, self.zC)
def panelize(self):
self.nf = 6
self.np = 8
self.X = numpy.array(
[0.0, 0.0, -self.length / 2.0, self.length / 2.0, 0.0, 0.0, -self.length / 2.0, self.length / 2.0])
self.Y = numpy.array(
[0.0, 0.0, self.width / 2.0, self.width / 2.0, 0.0, 0.0, -self.width / 2.0, -self.width / 2.0])
self.Z = numpy.array([-self.height, -self.height, 0.0, 0.0, -self.height, -self.height, 0.0, 0.0])
self.P = numpy.zeros([6, 4], dtype=int)
self.P[0, :] = numpy.array([3, 4, 2, 1])
self.P[1, :] = numpy.array([4, 8, 6, 2])
self.P[2, :] = numpy.array([8, 7, 5, 6])
self.P[3, :] = numpy.array([7, 3, 1, 5])
self.P[4, :] = numpy.array([5, 6, 5, 1])
self.P[5, :] = numpy.array([8, 4, 3, 7])
# Define triangles for plotting
self.trii = numpy.zeros([2 * self.nf, 3], dtype=int)
iT = 0
for iTr in range(self.nf):
self.trii[iT, :] = [self.P[iTr, 0] - 1, self.P[iTr, 1] - 1, self.P[iTr, 2] - 1]
self.trii[iT + 1, :] = [self.P[iTr, 0] - 1, self.P[iTr, 2] - 1, self.P[iTr, 3] - 1]
iT += 2
def translate(self, xT, yT, zT):
self.X += xT
self.Y += yT
self.Z += zT
def rotate(self, a1, a2, theta):
R = numpy.zeros([3, 3])
# Normal vector through origin
u = a2[0] - a1[0]
v = a2[1] - a1[1]
w = a2[2] - a1[2]
u = u / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
v = v / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
w = w / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
# Translate mesh so that rotation axis starts from the origin
self.X -= a1[0]
self.Y -= a1[1]
self.Z -= a1[2]
# Rotation matrix
R[0, 0] = u ** 2 + numpy.cos(theta) * (1 - u ** 2)
R[0, 1] = u * v * (1 - numpy.cos(theta)) - w * numpy.sin(theta)
R[0, 2] = u * w * (1 - numpy.cos(theta)) + v * numpy.sin(theta)
R[1, 0] = u * v * (1 - numpy.cos(theta)) + w * numpy.sin(theta)
R[1, 1] = v ** 2 + numpy.cos(theta) * (1 - v ** 2)
R[1, 2] = v * w * (1 - numpy.cos(theta)) - u * numpy.sin(theta)
R[2, 0] = w * u * (1 - numpy.cos(theta)) - v * numpy.sin(theta)
R[2, 1] = w * v * (1 - numpy.cos(theta)) + u * numpy.sin(theta)
R[2, 2] = w ** 2 + numpy.cos(theta) * (1 - w ** 2)
for iP in range(self.np):
p1 = numpy.array([self.X[iP], self.Y[iP], self.Z[iP]])
p2 = numpy.dot(R, p1)
self.X[iP] = p2[0]
self.Y[iP] = p2[1]
self.Z[iP] = p2[2]
# Translate back to original position
self.X += a1[0]
self.Y += a1[1]
self.Z += a1[2]
def makeCoin(self):
coin = numpy.zeros((3, 4, self.nf))
for iF in range(self.nf):
for iC in range(4):
coin[0, iC, iF] = self.X[self.P[iF, iC] - 1]
coin[1, iC, iF] = self.Y[self.P[iF, iC] - 1]
coin[2, iC, iF] = self.Z[self.P[iF, iC] - 1]
return coin
class wedge:
def __init__(self, length, width, height, cCor):
self.length = length
self.width = width
self.height = height
self.xC = cCor[0]
self.yC = cCor[1]
self.zC = cCor[2]
self.name = 'wedge'
self.panelize()
self.translate(self.xC, self.yC, self.zC)
def panelize(self):
self.nf = 6
self.np = 8
self.X = numpy.array(
[0.0, 0.0, -self.length / 2.0, self.length / 2.0, 0.0, 0.0, -self.length / 2.0, self.length / 2.0])
self.Y = numpy.array([self.width / 2.0, self.width / 2.0, self.width / 2.0, self.width / 2.0, -self.width / 2.0,
-self.width / 2.0, -self.width / 2.0, -self.width / 2.0])
self.Z = numpy.array([-self.height, -self.height, 0.0, 0.0, -self.height, -self.height, 0.0, 0.0])
self.P = numpy.zeros([6, 4], dtype=int)
self.P[0, :] = numpy.array([3, 4, 2, 1])
self.P[1, :] = numpy.array([4, 8, 6, 2])
self.P[2, :] = numpy.array([8, 7, 5, 6])
self.P[3, :] = numpy.array([7, 3, 1, 5])
self.P[4, :] = numpy.array([2, 6, 5, 1])
self.P[5, :] = numpy.array([8, 4, 3, 7])
# Define triangles for plotting
self.trii = numpy.zeros([2 * self.nf, 3], dtype=int)
iT = 0
for iTr in range(self.nf):
self.trii[iT, :] = [self.P[iTr, 0] - 1, self.P[iTr, 1] - 1, self.P[iTr, 2] - 1]
self.trii[iT + 1, :] = [self.P[iTr, 0] - 1, self.P[iTr, 2] - 1, self.P[iTr, 3] - 1]
iT += 2
def translate(self, xT, yT, zT):
self.X += xT
self.Y += yT
self.Z += zT
def rotate(self, a1, a2, theta):
R = numpy.zeros([3, 3])
# Normal vector through origin
u = a2[0] - a1[0]
v = a2[1] - a1[1]
w = a2[2] - a1[2]
u = u / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
v = v / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
w = w / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
# Translate mesh so that rotation axis starts from the origin
self.X -= a1[0]
self.Y -= a1[1]
self.Z -= a1[2]
# Rotation matrix
R[0, 0] = u ** 2 + numpy.cos(theta) * (1 - u ** 2)
R[0, 1] = u * v * (1 - numpy.cos(theta)) - w * numpy.sin(theta)
R[0, 2] = u * w * (1 - numpy.cos(theta)) + v * numpy.sin(theta)
R[1, 0] = u * v * (1 - numpy.cos(theta)) + w * numpy.sin(theta)
R[1, 1] = v ** 2 + numpy.cos(theta) * (1 - v ** 2)
R[1, 2] = v * w * (1 - numpy.cos(theta)) - u * numpy.sin(theta)
R[2, 0] = w * u * (1 - numpy.cos(theta)) - v * numpy.sin(theta)
R[2, 1] = w * v * (1 - numpy.cos(theta)) + u * numpy.sin(theta)
R[2, 2] = w ** 2 + numpy.cos(theta) * (1 - w ** 2)
for iP in range(self.np):
p1 = numpy.array([self.X[iP], self.Y[iP], self.Z[iP]])
p2 = numpy.dot(R, p1)
self.X[iP] = p2[0]
self.Y[iP] = p2[1]
self.Z[iP] = p2[2]
# Translate back to original position
self.X += a1[0]
self.Y += a1[1]
self.Z += a1[2]
def makeCoin(self):
coin = numpy.zeros((3, 4, self.nf))
for iF in range(self.nf):
for iC in range(4):
coin[0, iC, iF] = self.X[self.P[iF, iC] - 1]
coin[1, iC, iF] = self.Y[self.P[iF, iC] - 1]
coin[2, iC, iF] = self.Z[self.P[iF, iC] - 1]
return coin
class torus:
def __init__(self, diamOut, diamIn, cCor):
self.diamOut = diamOut
self.diamIn = diamIn
self.xC = cCor[0]
self.yC = cCor[1]
self.zC = cCor[2]
self.name = 'torus'
self.panelize()
self.translate(self.xC, self.yC, self.zC)
def panelize(self):
Ntheta = 18
Nphi = 18
theta = [xx * 2 * numpy.pi / (Ntheta - 1) for xx in range(Ntheta)]
phi = [xx * 2 * numpy.pi / (Nphi - 1) for xx in range(Nphi)]
self.nf = 0
self.np = 0
self.X = []
self.Y = []
self.Z = []
R = self.diamOut / 2.0
r = self.diamIn / 2.0
for iT in range(Ntheta):
for iP in range(Nphi):
self.X.append((R + r * numpy.cos(theta[iT])) * numpy.cos(phi[iP]))
self.Y.append((R + r * numpy.cos(theta[iT])) * numpy.sin(phi[iP]))
self.Z.append(r * numpy.sin(theta[iT]))
self.np += 1
self.nf = (Ntheta - 1) * (Nphi - 1)
self.P = numpy.zeros([self.nf, 4], dtype=int)
iPan = 0
for iT in range(Ntheta - 1):
for iP in range(Nphi - 1):
self.P[iPan, 0] = iP + iT * Nphi + 1
self.P[iPan, 1] = iP + 1 + iT * Nphi + 1
self.P[iPan, 2] = iP + 1 + Ntheta + iT * Nphi + 1
self.P[iPan, 3] = iP + Ntheta + iT * Nphi + 1
iPan += 1
self.X = numpy.array(self.X)
self.Y = numpy.array(self.Y)
self.Z = numpy.array(self.Z)
# Define triangles for plotting
self.trii = numpy.zeros([2 * self.nf, 3], dtype=int)
iT = 0
for iTr in range(self.nf):
self.trii[iT, :] = [self.P[iTr, 0] - 1, self.P[iTr, 1] - 1, self.P[iTr, 2] - 1]
self.trii[iT + 1, :] = [self.P[iTr, 0] - 1, self.P[iTr, 2] - 1, self.P[iTr, 3] - 1]
iT += 2
def translate(self, xT, yT, zT):
self.X += xT
self.Y += yT
self.Z += zT
def rotate(self, a1, a2, theta):
R = numpy.zeros([3, 3])
# Normal vector through origin
u = a2[0] - a1[0]
v = a2[1] - a1[1]
w = a2[2] - a1[2]
u = u / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
v = v / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
w = w / numpy.sqrt(u ** 2 + v ** 2 + w ** 2)
# Translate mesh so that rotation axis starts from the origin
self.X -= a1[0]
self.Y -= a1[1]
self.Z -= a1[2]
# Rotation matrix
R[0, 0] = u ** 2 + numpy.cos(theta) * (1 - u ** 2)
R[0, 1] = u * v * (1 - numpy.cos(theta)) - w * numpy.sin(theta)
R[0, 2] = u * w * (1 - numpy.cos(theta)) + v * numpy.sin(theta)
R[1, 0] = u * v * (1 - numpy.cos(theta)) + w * numpy.sin(theta)
R[1, 1] = v ** 2 + numpy.cos(theta) * (1 - v ** 2)
R[1, 2] = v * w * (1 - numpy.cos(theta)) - u * numpy.sin(theta)
R[2, 0] = w * u * (1 - numpy.cos(theta)) - v * numpy.sin(theta)
R[2, 1] = w * v * (1 - numpy.cos(theta)) + u * numpy.sin(theta)
R[2, 2] = w ** 2 + numpy.cos(theta) * (1 - w ** 2)
for iP in range(self.np):
p1 = numpy.array([self.X[iP], self.Y[iP], self.Z[iP]])
p2 = numpy.dot(R, p1)
self.X[iP] = p2[0]
self.Y[iP] = p2[1]
self.Z[iP] = p2[2]
# Translate back to original position
self.X += a1[0]
self.Y += a1[1]
self.Z += a1[2]
def makeCoin(self):
coin = numpy.zeros((3, 4, self.nf))
for iF in range(self.nf):
for iC in range(4):
coin[0, iC, iF] = self.X[self.P[iF, iC] - 1]
coin[1, iC, iF] = self.Y[self.P[iF, iC] - 1]
coin[2, iC, iF] = self.Z[self.P[iF, iC] - 1]
return coin
def make_voxels_without_figure(shape, length, height, width, diameter):
pos = [0, 0, 0]
if shape == "box":
mesh = box(length, width, height, pos)
elif shape == "cone":
mesh = cone(diameter, height, pos)
elif shape == "cylinder":
mesh = cylinder(diameter, height, pos)
elif shape == "sphere":
mesh = sphere(diameter, pos)
elif shape == "wedge":
mesh = wedge(length, width, height, pos)
hull_points = numpy.array([mesh.X.tolist(), mesh.Y.tolist(), mesh.Z.tolist()]).T
# Set up test points
G = 32
ex = 5 - 5 / G
x, y, z = numpy.meshgrid(numpy.linspace(-ex, ex, G),
numpy.linspace(-ex, ex, G),
numpy.linspace(-(9.5 - 5 / G), 0.5 - 5 / G, G))
test_points = numpy.vstack((x.ravel(), y.ravel(), z.ravel())).T
hull = scipy.spatial.Delaunay(hull_points)
within = hull.find_simplex(test_points) >= 0
return within*1.0
def make_voxels(shape, length, height, width, diameter):
return plotly_fig(make_voxels_without_figure(shape, length, height, width, diameter))
# This function loads a fuckton of data
# def load_data():
# # Open all the files we downloaded at the beginning and take out hte good bits
# curves = numpy.load('data_curves.npz')['curves']
# geometry = numpy.load('data_geometry.npz')['geometry']
# constants = numpy.load('constants.npz')
# S = constants['S']
# N = constants['N']
# D = constants['D']
# F = constants['F']
# G = constants['G']
# # Some of the good bits need additional processining
# new_curves = numpy.zeros((S*N, D * F))
# for i, curveset in enumerate(curves):
# new_curves[i, :] = curveset.T.flatten() / 1000000
# new_geometry = numpy.zeros((S*N, G * G * G))
# for i, geometryset in enumerate(geometry):
# new_geometry[i, :] = geometryset.T.flatten()
# # Return good bits to user
# return curves, geometry, S, N, D, F, G, new_curves, new_geometry
curves, geometry, S, N, D, F, G, new_curves, new_geometry = load_data()
class Network(object):
def __init__(self, type):
# Instantiate variables
# self.curves = curves
# self.new_curves = new_curves
# self.geometry = geometry
# self.new_geometry = new_geometry
# self.S = S
# self.N = N
# self.D = D
# self.F = F
# self.G = G
# Load network
# with open(structure, 'r') as file:
# self.network = keras.models.model_from_json(file.read())
# self.network.load_weights(weights)
self.network = from_pretrained_keras("cmudrc/wave-energy-analysis") if type == "forward" else from_pretrained_keras("cmudrc/wave-energy-synthesis")
def analysis(self, idx=None):
print(idx)
if idx is None:
idx = numpy.random.randint(1, S * N)
else:
idx = int(idx)
# Get the input
data_input = new_geometry[idx:(idx+1), :]
other_data_input = data_input.reshape((G, G, G), order='F')
# Get the outputs
print(data_input.shape)
predicted_output = self.network.predict(data_input)
true_output = new_curves[idx].reshape((3, F))
predicted_output = predicted_output.reshape((3, F))
f = numpy.linspace(0.05, 2.0, 64)
fd = pandas.DataFrame(f).rename(columns={0: "Frequency"})
df_pred = pandas.DataFrame(predicted_output.transpose()).rename(columns={0: "Surge", 1: "Heave", 2: "Pitch"})
df_true = pandas.DataFrame(true_output.transpose()).rename(columns={0: "Surge", 1: "Heave", 2: "Pitch"})
# return idx, other_data_input, true_output, predicted_output
return pandas.concat([fd, df_pred], axis=1), pandas.concat([fd, df_true], axis=1)
def analysis_from_geometry(self, geometry):
# Get the outputs
predicted_output = self.network.predict(numpy.array([geometry.flatten().tolist()]))
predicted_output = predicted_output.reshape((3, F))
f = numpy.linspace(0.05, 2.0, 64)
fd = pandas.DataFrame(f).rename(columns={0: "Frequency"})
df_pred = pandas.DataFrame(predicted_output.transpose()).rename(columns={0: "Surge", 1: "Heave", 2: "Pitch"})
good_frame = pandas.concat([fd, df_pred], axis=1)
return good_frame, good_frame
def synthesis(self, idx=None):
print(idx)
if idx is None:
idx = numpy.random.randint(1, S * N)
else:
idx = int(idx)
# Get the input
data_input = new_curves[idx:(idx+1), :]
other_data_input = data_input.reshape((3, F))
# Get the outputs
predicted_output = self.network.predict(data_input)
true_output = new_geometry[idx].reshape((G, G, G), order='F')
predicted_output = predicted_output.reshape((G, G, G), order='F')
# return idx, other_data_input, true_output, predicted_output
return predicted_output, true_output
def synthesis_from_spectrum(self, other_data_input):
# Get the input
data_input = other_data_input.reshape((1, 3*F))
# Get the outputs
predicted_output = self.network.predict(data_input)
predicted_output = predicted_output.reshape((G, G, G), order='F')
# return idx, other_data_input, true_output, predicted_output
return predicted_output
def get_geometry(self, idx=None):
if idx is None:
idx = numpy.random.randint(1, S * N)
else:
idx = int(idx)
idx = int(idx)
# Get the input
data_input = new_geometry[idx:(idx+1), :]
other_data_input = data_input.reshape((G, G, G), order='F')
# return idx, other_data_input, true_output, predicted_output
return other_data_input
def get_performance(self, idx=None):
if idx is None:
idx = numpy.random.randint(1, S *N)
else:
idx = int(idx)
idx = int(idx)
# Get the input
data_input = new_curves[idx:(idx+1), :]
other_data_input = data_input.reshape((3, F))
f = numpy.linspace(0.05, 2.0, 64)
fd = pandas.DataFrame(f).rename(columns={0: "Frequency"})
df_pred = pandas.DataFrame(other_data_input.transpose()).rename(columns={0: "Surge", 1: "Heave", 2: "Pitch"})
table = pandas.concat([fd, df_pred], axis=1)
return table
def plotly_fig(values):
X, Y, Z = numpy.mgrid[0:1:32j, 0:1:32j, 0:1:32j]
fig = go.Figure(data=go.Volume(
x=X.flatten(),
y=Y.flatten(),
z=Z.flatten(),
value=values.flatten(),
isomin=0.0,
isomax=1.0,
opacity=0.1, # needs to be small to see through all surfaces
surface_count=21, # needs to be a large number for good volume rendering
colorscale='haline'
))
return fig
value_net = Network("forward")
def performance(index):
return value_net.get_performance(index)
def geometry(index):
values = value_net.get_geometry(index)
return plotly_fig(values)
def simple_analysis(index, choice, shape, length, width, height, diameter):
forward_net = Network("forward")
# forward_net = Network("16forward_structure.json", "16forward_weights.h5")
if choice == "Construct Shape from Parameters":
return forward_net.analysis_from_geometry(make_voxels_without_figure(shape, length, height, width, diameter))
elif choice == "Pick Shape from Dataset":
return forward_net.analysis(index)
def simple_synthesis(index):
inverse_net = Network("inverse")
# inverse_net = Network("16inverse_structure.json", "16inverse_weights.h5")
pred, true = inverse_net.synthesis(index)
return plotly_fig(pred), plotly_fig(true)
def synthesis_from_spectrum(df):
inverse_net = Network("inverse")
# inverse_net = Network("16inverse_structure.json", "16inverse_weights.h5")
pred = inverse_net.synthesis_from_spectrum(df.to_numpy()[:, 1:])
return plotly_fig(pred)
def change_textbox(choice, length, height, width, diameter):
fig = make_voxels(choice, length, height, width, diameter)
if choice == "cylinder":
return [gradio.Slider.update(visible=True), gradio.Slider.update(visible=False), gradio.Slider.update(visible=True), gradio.Slider.update(visible=False), gradio.Plot.update(fig)]
elif choice == "sphere":
return [gradio.Slider.update(visible=False), gradio.Slider.update(visible=False), gradio.Slider.update(visible=True), gradio.Slider.update(visible=False), gradio.Plot.update(fig)]
elif choice == "box":
return [gradio.Slider.update(visible=True), gradio.Slider.update(visible=True), gradio.Slider.update(visible=False), gradio.Slider.update(visible=True), gradio.Plot.update(fig)]
elif choice == "wedge":
return [gradio.Slider.update(visible=True), gradio.Slider.update(visible=True), gradio.Slider.update(visible=False), gradio.Slider.update(visible=True), gradio.Plot.update(fig)]
elif choice == "cone":
return [gradio.Slider.update(visible=True), gradio.Slider.update(visible=False), gradio.Slider.update(visible=True), gradio.Slider.update(visible=False), gradio.Plot.update(fig)]
def randomize_analysis(choice):
if choice == "Construct Shape from Parameters":
length = random.uniform(3.0, 10.0)
height = random.uniform(3.0, 10.0)
width = random.uniform(3.0, 10.0)
diameter = random.uniform(3.0, 10.0)
choice2 = random.choice(["box", "cone", "sphere", "wedge", "cone"])
if choice2 == "box" or choice2 == "wedge":
return [gradio.Radio.update(choice2), gradio.Slider.update(length), gradio.Slider.update(height), gradio.Slider.update(width), gradio.Slider.update(), gradio.Number.update(), gradio.Plot.update(make_voxels(choice2, length, height, width, diameter))]
elif choice2 == "cone" or choice2 == "cylinder":
return [gradio.Radio.update(choice2), gradio.Slider.update(), gradio.Slider.update(height), gradio.Slider.update(), gradio.Slider.update(diameter), gradio.Number.update(), gradio.Plot.update(make_voxels(choice2, length, height, width, diameter))]
elif choice2 == "sphere":
return [gradio.Radio.update(choice2), gradio.Slider.update(), gradio.Slider.update(), gradio.Slider.update(), gradio.Slider.update(diameter), gradio.Number.update(), gradio.Plot.update(make_voxels(choice2, length, height, width, diameter))]
elif choice == "Pick Shape from Dataset":
num = random.randint(1, 4999)
return [gradio.Radio.update(), gradio.Slider.update(), gradio.Slider.update(), gradio.Slider.update(), gradio.Slider.update(), gradio.Number.update(num), gradio.Plot.update(geometry(num))]
def geometry_change(choice, choice2, num, length, width, height, diameter):
if choice == "Construct Shape from Parameters":
[slider1, slider2, slider3, slider4, plot] = change_textbox(choice2, length, height, width, diameter)
return [gradio.Radio.update(visible=True), slider1, slider2, slider3, slider4, gradio.Number.update(visible=False), gradio.Timeseries.update(visible=False), gradio.Plot.update(make_voxels(choice2, length, height, width, diameter))]
elif choice == "Pick Shape from Dataset":
return [gradio.Radio.update(visible=False), gradio.Slider.update(visible=False), gradio.Slider.update(visible=False), gradio.Slider.update(visible=False), gradio.Slider.update(visible=False), gradio.Number.update(visible=True), gradio.Timeseries.update(visible=True), gradio.Plot.update(geometry(num))]
with gradio.Blocks() as demo:
with gradio.Accordion("✨ Read about the underlying ML model here! ✨", open=False):
with gradio.Row():
with gradio.Column():
gradio.Markdown("# Toward the Rapid Design of Engineered Systems Through Deep Neural Networks")
gradio.HTML("Christopher McComb, Carnegie Mellon University")
gradio.Markdown("__Abstract__: The design of a system commits a significant portion of the final cost of that system. Many computational approaches have been developed to assist designers in the analysis (e.g., computational fluid dynamics) and synthesis (e.g., topology optimization) of engineered systems. However, many of these approaches are computationally intensive, taking significant time to complete an analysis and even longer to iteratively synthesize a solution. The current work proposes a methodology for rapidly evaluating and synthesizing engineered systems through the use of deep neural networks. The proposed methodology is applied to the analysis and synthesis of offshore structures such as oil platforms. These structures are constructed in a marine environment and are typically designed to achieve specific dynamics in response to a known spectrum of ocean waves. Results show that deep learning can be used to accurately and rapidly synthesize and analyze offshore structure.")
with gradio.Column():
download = gradio.HTML("<a href=\"https://huggingface.co/spaces/cmudrc/wecnet/resolve/main/McComb2019_Chapter_TowardTheRapidDesignOfEngineer.pdf\" style=\"width: 60%; display: block; margin: auto;\"><img src=\"https://huggingface.co/spaces/cmudrc/wecnet/resolve/main/coverpage.png\"></a>")
gradio.Markdown("When designing offshore structure, like [wave energy converters](https://www.nrel.gov/news/program/2021/how-wave-energy-could-go-big-by-getting-smaller.html), it's important to know what forces will be placed on the structure as waves come at different speeds. Likewise, if we have some idea of how we want the structure to respond to different waves, we can use that to guide the design of the shape of the structure. We call the first process _Analysis_, and the second process _Synthesis_. This demo has ML models that do both, very quickly.")
with gradio.Tab("Analysis"):
with gradio.Row():
with gradio.Column():
whence_commeth_geometry = gradio.Radio(
["Construct Shape from Parameters", "Pick Shape from Dataset"], label="How would you like to generate the shape of the offshore structure for analysis?", value="Construct Shape from Parameters"
)
radio = gradio.Radio(
["box", "cone", "cylinder", "sphere", "wedge"], label="What kind of shape would you like to generate?", value="sphere"
)
height = gradio.Slider(label="Height", interactive=True, minimum=3.0, maximum=10.0, value=6.5, visible=False)
width = gradio.Slider(label="Width", interactive=True, minimum=3.0, maximum=10.0, value=6.5, visible=False)
diameter = gradio.Slider(label="Diameter", interactive=True, minimum=3.0, maximum=10.0, value=6.5, visible=True)
length = gradio.Slider(label="Length", interactive=True, minimum=3.0, maximum=10.0, value=6.5, visible=False)
num = gradio.Number(42, label="Type the index of the spectrum you would like to use or randomly select it.", visible=False)
btn1 = gradio.Button("Randomize")
with gradio.Column():
geo = gradio.Plot(make_voxels("sphere", 6.5, 6.5, 6.5, 6.5), label="Geometry")
with gradio.Row():
btn2 = gradio.Button("Estimate Spectrum")
with gradio.Row():
with gradio.Column():
pred = gradio.Timeseries(x="Frequency", y=['Surge', 'Heave', 'Pitch'], label="Predicted")
with gradio.Column():
true = gradio.Timeseries(x="Frequency", y=['Surge', 'Heave', 'Pitch'], label="True", visible=False)
radio.change(fn=change_textbox, inputs=[radio, length, height, width, diameter], outputs=[height, width, diameter, length, geo])
height.change(fn=make_voxels, inputs = [radio, length, height, width, diameter], outputs=[geo])
width.change(fn=make_voxels, inputs = [radio, length, height, width, diameter], outputs=[geo])
diameter.change(fn=make_voxels, inputs = [radio, length, height, width, diameter], outputs=[geo])
length.change(fn=make_voxels, inputs = [radio, length, height, width, diameter], outputs=[geo])
whence_commeth_geometry.change(fn=geometry_change, inputs=[whence_commeth_geometry, radio, num, length, width, height, diameter], outputs=[radio, height, width, diameter, length, num, true, geo])
num.change(fn=geometry, inputs=[num], outputs=[geo])
btn1.click(fn=randomize_analysis, inputs=[whence_commeth_geometry], outputs=[radio, length, height, width, diameter, num, geo])
btn2.click(fn=simple_analysis, inputs=[num, whence_commeth_geometry, radio, length, width, height, diameter], outputs=[pred, true], api_name="analyze")
with gradio.Tab("Synthesis"):
with gradio.Row():
with gradio.Column():
whence_commeth_performance = gradio.Radio(
["Pick Spectrum from Dataset"], label="How would you like to generate the desired response spectrum to synthesize from?", value="Construct Spectrum from Table"
)
num = gradio.Number(42, label="Type the index of the shape you would like to use or randomly select it.")
btn1 = gradio.Button("Randomize")
with gradio.Column():
perf = gradio.Timeseries(x="Frequency", y=['Surge', 'Heave', 'Pitch'], label="Performance")
with gradio.Row():
btn2 = gradio.Button("Synthesize Geometry")
with gradio.Row():
with gradio.Column():
pred = gradio.Plot(label="Predicted")
with gradio.Column():
true = gradio.Plot(label="True")
btn1.click(fn=lambda: random.randint(1, 4999), inputs=[], outputs=num)
num.change(fn=performance, inputs=[num], outputs=[perf])
btn2.click(fn=simple_synthesis, inputs=[num], outputs=[pred, true], api_name="synthesize")
demo.launch() |