-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathASH.java
More file actions
4794 lines (4424 loc) · 155 KB
/
ASH.java
File metadata and controls
4794 lines (4424 loc) · 155 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
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
import java.io.*;
import java.awt.image.BufferedImage;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
// replace jline>/< with comment start/end to create a jline-independent source
//jline>
import jline.*;
//jline<
/**
* Atomic Structure Handler
*
* A tool for simultaneous editing and visualization of atomic structures.
*
* (c) Teemu Hynninen 2010
*
*/
public class ASH{
// commands
public static final String C_PICK = "pick";
public static final String C_UNPICK = "unpick";
public static final String C_JOIN = "union";
public static final String C_INTERSECT = "intersect";
public static final String C_OVER = "replace";
public static final String C_ALL = "all";
public static final String C_XM = "xmore";
public static final String C_XL = "xless";
public static final String C_YM = "ymore";
public static final String C_YL = "yless";
public static final String C_ZM = "zmore";
public static final String C_ZL = "zless";
public static final String C_RANGE = "range";
public static final String C_ELE = "element";
public static final String C_PART = "particle";
public static final String C_PARTS = "particles";
public static final String C_LIST = "list";
public static final String C_COMM = "command";
public static final String C_SHIFT = "shift";
public static final String C_ROT = "rotate";
public static final String C_COPY = "copy";
public static final String C_CUT = "cut";
public static final String C_PASTE = "paste";
public static final String C_CELL = "cell";
public static final String C_DEL = "delete";
public static final String C_ADD = "create";
public static final String C_RM = "remove";
public static final String C_DUPLO = "duplicates";
public static final String C_NBOR = "neighbors";
public static final String C_INTERP = "interpolate";
public static final String C_REIND = "reindex";
public static final String C_NEW = "new";
public static final String C_NEXT = "next";
public static final String C_JUMP = "switch";
public static final String C_MOVE = "move";
public static final String C_PREV = "previous";
public static final String C_FIRST = "first";
public static final String C_LAST = "last";
public static final String C_FRAME = "frame";
public static final String C_PROJ = "projection";
public static final String C_PERSP = "perspective";
public static final String C_ISOM = "isometric";
public static final String C_LENS = "lenticular";
public static final String C_WRITE = "write";
public static final String C_OPEN = "open";
public static final String C_XYZ = "xyz";
public static final String C_POSCAR = "poscar";
public static final String C_POSCAR4 = "poscar4";
public static final String C_PNG = "png";
public static final String C_ASE = "ase";
public static final String C_SCRIPT = "script";
public static final String C_SET = "define";
public static final String C_DEFAULT = "default";
public static final String C_ECHO = "echo";
public static final String C_VALUE = "value";
public static final String C_FUNC = "function";
public static final String C_EXIT = "exit";
public static final String C_SHOW = "show";
public static final String C_HIDE = "hide";
public static final String C_AXIS = "axis";
public static final String C_VIEW = "view";
public static final String C_RESET = "reset";
public static final String C_ZOOM = "zoom";
public static final String C_POINT = "point";
public static final String C_ANGLE = "angle";
public static final String C_COLOR = "recolor";
public static final String C_BG = "bg";
public static final String C_RAD = "resize";
public static final String C_INV = "mirror";
public static final String C_X = "x";
public static final String C_Y = "y";
public static final String C_Z = "z";
public static final String C_PLANE = "plane";
public static final String C_MOD = "cell";
public static final String C_CELLX = "x";
public static final String C_CELLY = "y";
public static final String C_CELLZ = "z";
public static final String C_EXP = "expand";
public static final String C_CLU = "cluster";
public static final String C_UNCLU = "uncluster";
public static final String C_UNDO = "undo";
public static final String C_ALIAS = "alias";
public static final String C_MAN = "man";
public static final String C_CALCMAN = "calcman";
public static final String C_PRINT = "print";
public static final String C_STRING = "string";
public static final String C_SHRINK = "shrink";
public static final String C_GROW = "grow";
public static final String C_RENAME = "rename";
public static final String C_SWITCH = "switch";
public static final String C_MOUSE = "mouse";
public static final String C_INFO = "info";
public static final String C_DIR = "directory";
public static final String C_SCALE = "scale";
public static final String C_WAIT = "wait";
public static final String C_BEND = "bend";
public static final String C_SPHERE = "sphere";
public static final String C_SCREEN = "screen";
public static final String C_OLDFILE = "append";
public static final String C_NEWFILE = "file";
public static final String C_FREEZE = "constrain";
public static final String C_DATA = "data";
public static final String C_NOOPTION = "xxxxxxxx";
public static final String C_IF = "if";
public static final String C_ELSE = "else";
public static final String C_ENDIF = "endif";
public static final String C_WHILE = "while";
public static final String C_ENDWHILE = "endwhile";
public static final String INSET = " ";
public static final String COMMENT = "###";
public static String[] mainCommands;
private String[] commands;
//jline>
private SimpleCompletor complete;
private ConsoleReader con;
//jline<
private static final String HISTORY_FILE = ".ash_history";
private static final String LAUNCH_FILE = ".ash_launch";
// key values
public static final String K_CX = "cellx";
public static final String K_CY = "celly";
public static final String K_CZ = "cellz";
public static final String K_BX = "pbcx";
public static final String K_BY = "pbcy";
public static final String K_BZ = "pbcz";
public static final String K_FR = "frame";
public static final String K_NFR = "nframes";
public static final String K_NPART = "nparts";
public static final String K_NATOM = "natoms";
public static final String K_VIEW = "view";
public static final String K_VUP = "viewup";
public static final String K_VZOOM = "viewzoom";
public static final String K_VDIR = "viewto";
public static final String K_PI = "pi";
public static final String K_E = "e";
public static final String K_VERSION = "version";
public static String[] keys = {K_CX,K_CY,K_CZ,
K_BX,K_BY,K_BZ,
K_FR,K_NFR,K_NPART,K_NATOM,
K_VIEW,K_VUP,
K_VZOOM,K_VDIR,
K_PI,K_E,K_VERSION};
private static final String VERSION = "0.4";
private static final String[] DEFAULT_LAUNCH = {
"print \"This is Atomic Structure Handler $"+K_VERSION+"$\"",
"print \" \"",
"print \"- view the available commands by typing 'list command'\"",
"print \"- view the manual by typing 'man' followed by the name of a command\"",
"print \"- edit launch options in the file .ash_launch\""
};
public static Random RNG;
public static Calculator adder;
public static long startTime;
public static PeriodicTable elementTable = new PeriodicTable();
private GeoPainter painter;
private boolean notFinished;
public static boolean runningScript = false;
private ArrayList<Structure> undos;
private ArrayList<Structure> frames;
private int currentFrame;
private Hashtable<String,Command> commandTable;
private Hashtable<String,Variable> variables;
private Hashtable<String,StringVariable> stringVariables;
private Hashtable<String,Alias> aliases;
private Structure[] clipboardAtoms;
private static final int CLIPSIZE = 10;
private boolean showDir = false;
public static boolean defaultPick = true;
private static String PROMPT = " >";
public ASH(String[] args){
startTime = System.currentTimeMillis();
FileHandler.CWD = new File(".");
FileHandler.readPath();
variables = new Hashtable<String,Variable>();
stringVariables = new Hashtable<String,StringVariable>();
aliases = new Hashtable<String,Alias>();
commandTable = new Hashtable<String,Command>();
adder = new Calculator(this);
commandTable.put(C_EXIT,new Exit(this));
commandTable.put(C_PRINT,new Print(this));
commandTable.put(C_PICK,new Pick(this));
commandTable.put(C_UNPICK,new Unpick(this));
commandTable.put(C_LIST,new List(this));
commandTable.put(C_CLU,new Clusterize(this));
commandTable.put(C_UNCLU,new Unclusterize(this));
commandTable.put(C_ROT,new Rotate(this));
commandTable.put(C_SHIFT,new Shift(this));
commandTable.put(C_DEL,new Delete(this));
commandTable.put(C_ADD,new Add(this));
commandTable.put(C_COPY,new Copy(this));
commandTable.put(C_CUT,new Cut(this));
commandTable.put(C_PASTE,new Paste(this));
commandTable.put(C_MOD,new SetCell(this));
commandTable.put(C_EXP,new Expand(this));
commandTable.put(C_RAD,new Resize(this));
commandTable.put(C_COLOR,new Recolor(this));
commandTable.put(C_INV,new Invert(this));
commandTable.put(C_SHOW,new Show(this));
commandTable.put(C_HIDE,new Hide(this));
commandTable.put(C_VIEW,new Reposition(this));
commandTable.put(C_INTERP,new Interpolate(this));
commandTable.put(C_REIND,new Reindex(this));
commandTable.put(C_FRAME,new FrameSwitch(this));
commandTable.put(C_WRITE,new Write(this));
commandTable.put(C_OPEN,new Open(this));
commandTable.put(C_UNDO,new Undo(this));
commandTable.put(C_SET,new Define(this));
commandTable.put(C_VALUE,new Evaluate(this));
commandTable.put(C_ALIAS,new MakeAlias(this));
commandTable.put(C_ELE,new ElementSwitch(this));
commandTable.put(C_MOUSE,new MouseSwitch(this));
commandTable.put(C_DIR,new DirectorySwitch(this));
commandTable.put(C_SCALE,new Scale(this));
commandTable.put(C_BEND,new Bend(this));
commandTable.put(C_WAIT,new Wait(this));
commandTable.put(C_FREEZE,new Constrain(this));
commandTable.put(C_CALCMAN,new CalcManual(this));
Command manual = new Manual(this);
commandTable.put(C_MAN,manual);
//jline>
try{
con = new ConsoleReader();
con.setUseHistory(true);
con.getHistory().setHistoryFile(new File(HISTORY_FILE));
} catch(Exception error){}
//jline<
commands = new String[0];
mainCommands = new String[0];
Enumeration<String> commandList = commandTable.keys();
while(commandList.hasMoreElements()){
String nextKey = commandList.nextElement();
String[] keyAsList = {nextKey};
mainCommands = StringCombiner.combine(mainCommands,keyAsList);
}
Arrays.sort(mainCommands);
String[][] manOps = new String[1][];
manOps[0] = mainCommands;
manual.setOptions(manOps);
commandList = commandTable.keys();
while(commandList.hasMoreElements()){
String nextKey = commandList.nextElement();
String[] keyAsList = {nextKey};
commands = StringCombiner.combine(commands,
StringCombiner.comboPermute(keyAsList,commandTable.get(nextKey).getOptions(),""));
}
//Arrays.sort(keys);
//jline>
complete = new SimpleCompletor(commands);
con.addCompletor(complete);
//jline<
// This will print the man pages to a text file.
// The function should not be on usually, but if
// you need to get the full manual easily, uncomment,
// recompile, and run Ash and it will be generated.
//printManual("Ash_manual_pages.txt");
notFinished = true;
runProgram(args);
}
private void runProgram(String[] args){
GeoWindow window = new GeoWindow(this);
window.startGraphics();
window.pauseGraphics();
this.painter = (GeoPainter)window.getPainter();
this.painter.recordSize(GeoWindow.WINDOW_WIDTH,GeoWindow.WINDOW_HEIGHT);
Atom[] noatoms = new Atom[0];
Structure geo = new Structure(noatoms);
frames = new ArrayList<Structure>();
undos = new ArrayList<Structure>();
currentFrame = 0;
frames.add(geo);
clipboardAtoms = new Structure[CLIPSIZE];
for(int i=0; i<CLIPSIZE; i++){
clipboardAtoms[i] = new Structure(noatoms);
}
ViewPoint look = new ViewPoint(new Vector(0.0,0.0,50.0),
new Vector(0.0,0.0,-10.0),
new Vector(0.0,1.0,0.0) );
painter.initGeo(geo,look);
window.resumeGraphics();
notFinished = true;
// Execute initialization script
try{
FileHandler io = new FileHandler();
if(io.findsFile(LAUNCH_FILE)){
executeScript(LAUNCH_FILE);
/**
File launchfile = new File(LAUNCH_FILE);
if(launchfile.canRead()){
executeScript(LAUNCH_FILE);
*/
} else {
io.writeFile(DEFAULT_LAUNCH,LAUNCH_FILE+"_default");
executeScript(LAUNCH_FILE+"_default");
}
} catch(Exception error){
//printMessage("");
}
// Execute command line arguments
if(args.length > 0){
String command = "";
for(int i=0; i<args.length; i++){
command += args[i]+" ";
}
executeCommand(command);
}
painter.startInteraction();
while(notFinished){
String theprompt;
if(showDir){
try{
theprompt = FileHandler.CWD.getCanonicalPath()+PROMPT;
} catch(Exception error){
theprompt = PROMPT;
}
} else {
theprompt = PROMPT;
}
String command = readCommandLine(theprompt);
if(!command.equals("")){
executeCommand(command);
}
}
System.exit(0);
}
public int[] findChars(String full, String delims, boolean ignoreInQuotes){
int[] spots = new int[full.length()];
int found = 0;
boolean doubleQuoteOn = false;
boolean singleQuoteOn = false;
int depth = 0;
for(int i=0; i<full.length(); i++){
if(full.charAt(i) == '"'){
//if(!singleQuoteOn){
if(doubleQuoteOn){
depth--;
doubleQuoteOn = false;
if(depth > 0){
singleQuoteOn = true;
}
} else {
depth++;
doubleQuoteOn = true;
singleQuoteOn = false;
}
//} else {
//}
}
if(full.charAt(i) == '\''){
//if(!doubleQuoteOn){
if(singleQuoteOn){
depth--;
singleQuoteOn = false;
if(depth > 0){
doubleQuoteOn = true;
}
} else {
depth++;
singleQuoteOn = true;
doubleQuoteOn = false;
}
//}
}
for(int j=0; j<delims.length(); j++){
if(full.charAt(i) == delims.charAt(j)){
if(!ignoreInQuotes || (!doubleQuoteOn && !singleQuoteOn)){
spots[found] = i;
found++;
}
}
}
}
int[] chars = new int[found];
for(int i=0; i<found; i++){
chars[i] = spots[i];
}
return chars;
}
public static String removeComment(String command){
try{
return command.substring(0,command.indexOf(COMMENT));
} catch(Exception error){
return command;
}
}
public String[] splitCommand(String command){
if(command.length() == 0){
return new String[0];
}
int[] semis = findChars(command,";",true);
int[] splits = new int[semis.length+2];
splits[0] = -1;
splits[splits.length-1] = command.length();
for(int i=1; i<splits.length-1; i++){
splits[i] = semis[i-1];
}
String[] coms = new String[semis.length+1];
for(int i=0; i<coms.length; i++){
coms[i] = command.substring(splits[i]+1,splits[i+1]).trim();
}
return coms;
}
public String[] splitArgument(String command){
if(command.length() < 1){
String[] empty = new String[1];
empty[0] = "";
return empty;
}
int[] semis = findChars(command," \t",true);
int[] splits = new int[semis.length+2];
splits[0] = -1;
splits[splits.length-1] = command.length();
for(int i=1; i<splits.length-1; i++){
splits[i] = semis[i-1];
}
String[] coms = new String[semis.length+1];
//printMessage(semis.length+" "+splits.length+" "+coms.length);
for(int i=0; i<coms.length; i++){
if( ( command.charAt(splits[i]+1) == '"' && command.charAt(splits[i+1]-1) == '"' ) ||
( command.charAt(splits[i]+1) == '\'' && command.charAt(splits[i+1]-1) == '\'' ) ){
coms[i] = command.substring(splits[i]+2,splits[i+1]-1);
} else {
coms[i] = command.substring(splits[i]+1,splits[i+1]);
}
}
int empty = 0;
for(int i=0; i<coms.length; i++){
try{
coms[i] = parseS(coms[i]);
} catch(Exception error){
coms[i] = "";
}
if(coms[i].length() == 0){
empty++;
}
}
String[] tidycoms = new String[coms.length-empty];
if(tidycoms.length == 0){
String[] emptycoms = {""};
return emptycoms;
}
empty = 0;
for(int i=0; i<coms.length; i++){
if(coms[i].length() == 0){
empty++;
} else {
tidycoms[i-empty] = coms[i];
}
}
return tidycoms;
}
public void executeCommand(String command){
/**
StringTokenizer commandFinder = new StringTokenizer(command, ";");
while(commandFinder.hasMoreTokens()){
executeSingleCommand(commandFinder.nextToken().trim());
}
*/
String[] singles = splitCommand(ASH.removeComment(command).trim());
for(int i=0; i<singles.length; i++){
executeSingleCommand(singles[i]);
}
}
public void executeSingleCommand(String command){
/**
StringTokenizer commandSplitter = new StringTokenizer(command, " \t");
String[] coms = new String[commandSplitter.countTokens()];
*/
String[] coms = splitArgument(command);
try{
boolean inverse = false;
Command action = null;
try{
action = commandTable.get(coms[0]);
action.getName();
} catch(Exception error6){
action = null;
}
// if the command didn't work, try to swap the first two words: e.g., new frame -> frame new
if(action == null){
try{
action = commandTable.get(coms[1]);
action.getName();
} catch(Exception error7){
action = null;
}
if(action == null){
throw new Exception();
}
inverse = true;
String tempcom = coms[0];
coms[0] = coms[1];
coms[1] = tempcom;
}
String[] args = new String[coms.length-1];
for(int a=0; a<args.length; a++){
args[a] = coms[a+1];
}
try{
action.execute(args);
} catch(Exception error){
printMessage("Invalid call for "+coms[0]+". Syntax for the command is:\n"+action.getUsage(),
"Invalid command while executing script: '"+command+"'");
//error.printStackTrace();
}
return;
} catch(Exception error2){
//error2.printStackTrace();
// check for alias
Alias shorthand = null;
try{
shorthand = getAlias(coms[0]);
if(shorthand == null){
throw new Exception();
}
try{
String[] arguments = new String[coms.length-1];
for(int i=0; i<arguments.length; i++){
arguments[i] = coms[i+1];
}
String commander = shorthand.getCommand(arguments);
//printMessage("alias command '"+commander+"'");
executeCommand(commander);
} catch(Exception error3){
printMessage("invalid arguments for "+coms[0],
"Invalid command while executing script: '"+command+"'");
//error3.printStackTrace();
}
} catch(Exception error4){
//error4.printStackTrace();
/*
try{ // Try to execute a unix command
Process p = Runtime.getRuntime().exec(coms, null, FileHandler.CWD);
String s = null;
int i = 0;
if (i == 0){
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
// read the output from the command
while ((s = stdInput.readLine()) != null) {
printMessage(s);
}
} else {
BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
while ((s = stdErr.readLine()) != null) {
printMessage(s);
}
}
} catch(Exception error5){
*/
if(coms[0].length() > 0){
printMessage("unknown command "+coms[0],
"Invalid command while executing script: '"+command+"'");
}
//}
}
}
}
public void rememberStructure(){
if(!runningScript){
undos.add(frames.get(currentFrame).copy());
if(undos.size() > 30){ // if the buffer is too big, forget the first structure
undos.remove(0);
}
}
}
public void forgetStructures(){
undos.clear();
}
public Structure recallStructure(){
if(undos.size() > 0){
//Structure memory = undos.get(undos.size()-1);
return undos.remove(undos.size()-1);
//return memory;
}
return frames.get(currentFrame);
}
public int makeCluster()
throws Exception{
Structure geo = frames.get(currentFrame);
//int pickedOnes = 0;
int pickedParts = 0;
for(int i=0; i<geo.countParticles(); i++){
Particle part = geo.getParticle(i);
if(part.isPicked()){
pickedParts++;
}
}
if(pickedParts == 0){
throw new Exception();
}
Particle[] cluster = new Particle[pickedParts];
int index = 0;
for(int i=0; i<geo.countParticles(); i++){
Particle part = geo.getParticle(i);
if(part.isPicked()){
cluster[index] = part;
index++;
try{
geo.removeParticle(i);
i--;
} catch(Exception error){}
}
}
geo.addParticle(new Cluster(cluster));
return cluster.length;
}
public int breakCluster(){
Structure geo = frames.get(currentFrame);
int ats = 0;
for(int i=0; i<geo.countParticles(); i++){
Particle part = geo.getParticle(i);
if(part.isPicked() && !part.isAtomic()){
Particle[] cluster = ((Cluster)part).getParticles();
try{
geo.removeParticle(i);
i--;
} catch(Exception error){}
for(int j=0; j<cluster.length; j++){
i++;
geo.addParticle(i,cluster[j],false);
ats++;
}
}
}
return ats;
}
public void addParticle(Particle newp){
frames.get(currentFrame).addParticle(newp);
}
public void removeParticle(int index)
throws Exception{
frames.get(currentFrame).removeParticle(index);
}
public void removePicked(){
try{
Structure geo = frames.get(currentFrame);
for(int i=geo.countParticles()-1; i>=0; i--){
if(geo.getParticle(i).isPicked()){
geo.removeParticle(i);
}
}
} catch(Exception error){}
}
public void addFrame(Structure geo){
if(currentFrame == frames.size()-1){
frames.add(geo);
} else {
frames.add(currentFrame+1,geo);
}
}
public void deleteFrame(int index){
if(index >= 0 && index < frames.size()){
try{
if(currentFrame == index){ // deleting the current frame
if(index < frames.size()-1){ // not last frame, jump to next
changeFrame(currentFrame+1);
currentFrame--; // a frame will be deleted before this one, so subtract one from frame number
this.painter.aimAtCenter();
this.painter.updateGeo();
} else if(frames.size() > 1){ // last frame, but there are more than one
changeFrame(currentFrame-1);
this.painter.aimAtCenter();
this.painter.updateGeo();
} else { // the only frame, replace by an empty one
currentFrame = 0;
Atom[] nullAtom = new Atom[0];
Structure nullGeo = new Structure(nullAtom);
frames.add(nullGeo);
this.painter.aimAtCenter();
this.painter.updateGeo(nullGeo);
}
}
frames.remove(index);
} catch(Exception error){
printMessage("could not delete frame");
//error.printStackTrace();
}
} else {
printMessage("no such frame");
}
}
public void printMessage(String words,boolean always){
if(!ASH.runningScript || always){
System.out.println(words);
}
}
public void printMessage(String words){
if(ASH.runningScript){
} else {
System.out.println(words);
}
}
public void printMessage(String words, String scriptwords){
if(ASH.runningScript){
System.out.println(scriptwords);
} else {
System.out.println(words);
}
}
public void nextFrame(){
try{
Structure newGeo = frames.get(currentFrame+1);
this.painter.setGeometry(newGeo);
currentFrame++;
} catch(Exception error){
//printMessage("no such frame");
}
}
public void previousFrame(){
try{
Structure newGeo = frames.get(currentFrame-1);
this.painter.setGeometry(newGeo);
currentFrame--;
} catch(Exception error){
//printMessage("no such frame");
}
}
public void changeFrame(int index){
try{
Structure newGeo = frames.get(index);
this.painter.setGeometry(newGeo);
currentFrame = index;
forgetStructures();
} catch(Exception error){
printMessage("no such frame");
}
}
public void pickAll(boolean yesno, boolean join){
try{
if(!join){
Structure newGeo = frames.get(currentFrame);
int natoms = newGeo.countParticles();
for(int i=0; i<natoms; i++){
newGeo.getParticle(i).pick(yesno);
}
}
} catch(Exception error){
}
}
public void pickElement(int ele, boolean yesno, boolean join){
try{
Structure newGeo = frames.get(currentFrame);
int natoms = newGeo.countParticles();
for(int i=0; i<natoms; i++){
if(newGeo.getParticle(i).isAtomic()){
Atom picky = (Atom)newGeo.getParticle(i);
if(!join){
if(picky.getElement() == ele){
picky.pick(yesno);
}
} else {
if(picky.getElement() != ele){
picky.pick(!yesno);
}
}
}
}
} catch(Exception error){
}
}
public void pickAtom(int index, boolean yesno, boolean join){
try{
Structure newGeo = frames.get(currentFrame);
int natoms = newGeo.countParticles();
if(!join){
newGeo.getParticle(index).pick(yesno);
} else {
for(int i=0; i<newGeo.countParticles(); i++){
if(i != index){
newGeo.getParticle(i).pick(!yesno);
}
}
}
} catch(Exception error){
}
}
public void pickSphere(Vector point, double radius, boolean yesno, boolean join){
try{
Structure newGeo = frames.get(currentFrame);
int natoms = newGeo.countParticles();
for(int i=0; i<natoms; i++){
Particle check = newGeo.getParticle(i);
Vector coord = check.getCoordinates();
if(!join){
if(coord.minus(point).norm() < radius){
check.pick(yesno);
}
} else {
if(coord.minus(point).norm() >= radius){
check.pick(!yesno);
}
}
}
} catch(Exception error){
}
}
public void pickArea(Vector point, Vector direction, boolean yesno, boolean join){
try{
Structure newGeo = frames.get(currentFrame);
int natoms = newGeo.countParticles();
for(int i=0; i<natoms; i++){
Particle check = newGeo.getParticle(i);
Vector coord = check.getCoordinates();
if(!join){
if(coord.minus(point).dot(direction) > 0.0){
check.pick(yesno);
}
} else {
if(coord.minus(point).dot(direction) <= 0.0){
check.pick(!yesno);
}
}
}
} catch(Exception error){
}
}
public void shiftAtoms(Vector shift){
try{
Structure newGeo = frames.get(currentFrame);
int natoms = newGeo.countParticles();
for(int i=0; i<natoms; i++){
if(newGeo.getParticle(i).isPicked()){
newGeo.getParticle(i).shiftCoordinates(shift);
}
}
newGeo.forcePeriodicBounds();
} catch(Exception error){
}
}
public void listCell(){
try{
Structure newGeo = frames.get(currentFrame);
for(int i=0; i<3; i++){
Vector axis = newGeo.getCell()[i];
printMessage("cell vector "+(i+1)+
" ("+FileHandler.formattedDouble(axis.element(0),12,5)+
", "+FileHandler.formattedDouble(axis.element(1),12,5)+
", "+FileHandler.formattedDouble(axis.element(2),12,5)+")",true);
}
} catch(Exception error){
//error.printStackTrace();
}
}
public void listVariables(){
try{
FileHandler format = new FileHandler();
printMessage("Pre-defined:",true);
for(int i=0; i<keys.length; i++){
printMessage(format.formattedString(keys[i]+" = ",20)+parseVariable(keys[i]),true);
}
printMessage("Custom:",true);
Enumeration<String> vkeys = variables.keys();
String[] varList = new String[variables.size()];
int vi = 0;
while(vkeys.hasMoreElements()){
String nextKey = vkeys.nextElement();
String value = variables.get(nextKey).toString();
varList[vi] = format.formattedString(nextKey+" = ",20)+value;
vi++;
}
Arrays.sort(varList);
for(int i=0; i<varList.length; i++){
printMessage(varList[i],true);
}
// strings
vkeys = stringVariables.keys();
varList = new String[stringVariables.size()];
vi = 0;
while(vkeys.hasMoreElements()){
String nextKey = vkeys.nextElement();
String value = stringVariables.get(nextKey).toString();
varList[vi] = format.formattedString(nextKey+" = ",20)+"'"+value+"'";
vi++;
}
Arrays.sort(varList);
for(int i=0; i<varList.length; i++){
printMessage(varList[i],true);
}
} catch(Exception error){
//error.printStackTrace();
}
}
public void listFunctions(){
try{
FileHandler format = new FileHandler();
printMessage("Pre-defined:",true);
Operator[] mainFunctions = adder.listOperators();
for(int i=0; i<mainFunctions.length; i++){
printMessage(format.formattedString(mainFunctions[i].getType(),15)+" "+
format.formattedString(mainFunctions[i].getSyntax(),40)+" ("+mainFunctions[i].getName()+")",true);
}
printMessage("Custom:",true);
CustomFunction[] functions = adder.listFunctions();
for(int i=0; i<functions.length; i++){
String[] args = new String[functions[i].getArgumentCount()];
for(int j=0; j<args.length; j++){