summaryrefslogtreecommitdiff
path: root/cad/src/experimental/CoNTub/P25.java
blob: 45613a1b5e5a05adab66b8dbe2e83807b09e005c (plain)
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

//package nt;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.text.*;

public class P25 extends JPanel implements ActionListener, ItemListener
{
	private JLabel l1, l2, l3, l4, l5;
	private JTextField t1, t2, t3, t4, t5;
	private JCheckBox cb1, cb2, cb3;
	private JTextArea txtout;
	private JScrollPane ct;
	private JComboBox combo;
	boolean bst, atn, hbk;
	Mira3D pan3D;
	public MoleculaT MW;
	formato fd;


	public P25 ()
	{
		setLayout (new BorderLayout ());
		Box ct1 = Box.createHorizontalBox ();
		  ct1.setBorder (BorderFactory.
				 createCompoundBorder (BorderFactory.
						       createTitledBorder ("Inner indices and length. (\u212B)"), BorderFactory.createEtchedBorder (1)));
		  l1 = new JLabel ("   i=  ");
		  l2 = new JLabel ("   j=  ");
		  l3 = new JLabel ("   l=  ");
		  t1 = new JTextField (" 5");
		  t1.addActionListener (this);
		  t2 = new JTextField (" 5");
		  t2.addActionListener (this);
		  t3 = new JTextField ("10");
		  t3.addActionListener (this);
		  ct1.add (l1);
		  ct1.add (t1);
		  ct1.add (l2);
		  ct1.add (t2);
		  ct1.add (l3);
		  ct1.add (t3);

		Box cls = Box.createHorizontalBox ();
		  cls.setBorder (BorderFactory.
				 createCompoundBorder (BorderFactory.createTitledBorder ("Shells and spacing."), BorderFactory.createEtchedBorder (1)));
		  l4 = new JLabel (" N= ");
		  l5 = new JLabel (" S= ");
		  t4 = new JTextField ("2");
		  t4.addActionListener (this);
		  t5 = new JTextField ("2.48");
		  t5.addActionListener (this);
		  cls.add (l4);
		  cls.add (t4);
		  cls.add (l5);
		  cls.add (t5);

		  combo = new JComboBox ();
		  combo.addItem ("No endings");
		  combo.addItem ("Hydrogen");
		  combo.addItem ("Nitrogen");

		Box cv = Box.createVerticalBox ();
		  cb1 = new JCheckBox ("Ball & Stick (b)");
		  cb1.addItemListener (this);
		  bst = false;
		  cb2 = new JCheckBox ("Atom labels (l)");
		  cb2.addItemListener (this);
		  atn = false;
		  cb3 = new JCheckBox ("Cut back (c)");
		  cb3.addItemListener (this);
		  hbk = false;
		  cv.add (cb1);
		  cv.add (cb2);
		  cv.add (cb3);

		JButton b = new JButton ("CREATE");
		  b.addActionListener (this);

		  txtout = new JTextArea ("", 20, 6);
		  ct = new JScrollPane (txtout);
		  txtout.setEditable (false);
		Font fo = new Font ("Courier", 1, 12);
		  txtout.setFont (fo);

		Box c0 = Box.createVerticalBox ();
		  c0.add (ct1);
		  c0.add (cls);
		  c0.add (combo);
		  c0.add (b);

		JPanel c1 = new JPanel ();
		  c1.setLayout (new BorderLayout ());
		  c1.add (c0, BorderLayout.NORTH);
		  c1.add (ct, BorderLayout.CENTER);
		  c1.add (cv, BorderLayout.SOUTH);

		  pan3D = new Mira3D ();

		  add (pan3D, BorderLayout.CENTER);
		  add (c1, BorderLayout.WEST);

	}
	public void actionPerformed (ActionEvent ev)
	{

		int i1, j1, n;
		double l, s;
		try {
			i1 = Integer.parseInt (t1.getText ().trim ());	//pillamos indice se tubos
			j1 = Integer.parseInt (t2.getText ().trim ());
			n = Integer.parseInt (t4.getText ().trim ());
			l = Double.parseDouble (t3.getText ().trim ());
			s = Double.parseDouble (t5.getText ().trim ());
		}

		catch (Exception e) {
			JOptionPane.showMessageDialog (null, "Stop: wrong input", "Stop", 0);
			return;
		}

		if (i1 == 0 && j1 == 0) {
			JOptionPane.showMessageDialog (null, "Error: Inner tube's indices are incorrect", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		//Y giramos automaticamente los indices de los tubos hasta meterlos el el primer sextante
		boolean cambio1 = false;
		for (; (i1 < 0) || (j1 < 0);) {
			int i1n = -j1;
			int j1n = i1 + j1;
			i1 = i1n;
			j1 = j1n;
			cambio1 = true;
		}

		if (i1 == 0) {
			i1 = j1;
			j1 = 0;
		}		//minirevis

		if (cambio1) {
			JOptionPane.showMessageDialog (null,
						       "Warning: Indices automatically translated to ("
						       + i1 + "," + j1 + ")", "Warning", JOptionPane.INFORMATION_MESSAGE);
			t1.setText ("" + i1);
			t2.setText ("" + j1);
		}

		generatubo (i1, j1, l, n, s);
	}

	void generatubo (int a, int b, double c, int nshells, double sshell)
	{
		formato fi, fd;
		fd = new formato (8, "##0.00");	//para los radios de los tubos
		MW = new MoleculaT ();

		Nanotubo NT = new Nanotubo (a, b);

		MW.setInfo ("Multi-walled nanotube with ");	//+nshells+" shells and inner indices ("+a+","+b+")");

		int guess = (int) (NT.radio () * 2 * Math.PI * c * 0.34);

		for (int i = 1; i <= nshells; i++)

			guess = guess + (int) ((NT.radio () + sshell * i) * 2 * Math.PI * c * 0.34);

		if (guess > 6000) {
			JOptionPane.showMessageDialog (null, "Structure will have more than 6000 Atoms. Process stopped.", "STOP", 0);
			return;
		} else if (guess > 4000) {
			int sale = JOptionPane.showConfirmDialog (null,
								  "Structure will have more than 4000 Atoms. Continue at your own risk",
								  "Warning", 0);
			if (sale == 1)
				return;
		} else if (guess > 2000)
			JOptionPane.showMessageDialog (null, "Big Structure: using low-consuming display.", "Big molecule", 1);



		double x, xc, y, yc, z, zc;
		for (int i = 1; i * NT.deltaz () <= c; i++) {
			for (int j = 1; j <= NT.d (); j++) {
				x = NT.deltaz () * i;
				xc = NT.deltaz () * i + NT.deltazc ();
				y = NT.radio () * (float) Math.sin (NT.deltaphi () * i + 2 * (float) Math.PI / NT.d () * j);
				yc = NT.radio () * (float) Math.sin (NT.deltaphi () * i + NT.deltaphic () + 2 * (float) Math.PI / NT.d () * j);
				z = NT.radio () * (float) Math.cos (NT.deltaphi () * i + 2 * (float) Math.PI / NT.d () * j);
				zc = NT.radio () * (float) Math.cos (NT.deltaphi () * i + NT.deltaphic () + 2 * (float) Math.PI / NT.d () * j);
				MW.addVert (x, y, z, 6);
				MW.addVert (xc, yc, zc, 6);

		}}
		double rad1 = 0.01 * (int) (100 * NT.radio ());
		txtout.setText ("Tube 1 is (" + a + "," + b + "), r=" + fd.aCadena (rad1));


		//GENERAMOS EL RESTO DE CAPAS
		double A = 2.46;
		double dz1 = A * Math.sin (Math.PI / 3 - NT.quiral ());
		double dz2 = A * Math.sin (NT.quiral ());
		double dx1 = A * Math.cos (Math.PI / 3 - NT.quiral ());
		double dx2 = A * Math.cos (NT.quiral ());
		int ni = a;
		int nj = b;
		double zeta = 0;

		double cshell = NT.radio () * 2 * Math.PI;	//circungitud primera capa
		double radpaso = NT.radio () * 2 * Math.PI;	//que a su vez es inicio


		for (int j = 1; j <= nshells - 1; j++) {
			cshell = cshell + sshell * 2 * Math.PI;	//Objetivo

			for (int k = 0; radpaso < cshell; k++) {	//radpaso busca al objetivo
				if (zeta < 0) {
					nj++;
					zeta = zeta + dz2;
					radpaso = radpaso + dx2;
				} else {
					ni++;
					zeta = zeta - dz1;
					radpaso = radpaso + dx1;
				}
			}

			Nanotubo NTC = new Nanotubo (ni, nj);
			double rad = 0.01 * (int) (100 * NTC.radio ());
			txtout.setText (txtout.getText () + "\nTube " + (j + 1) + " is (" + ni + "," + nj + "), r=" + fd.aCadena (rad));

			for (int i = 1; i * NTC.deltaz () <= c; i++) {
				for (int k = 1; k <= NTC.d (); k++) {
					x = NTC.deltaz () * i;
					xc = NTC.deltaz () * i + NTC.deltazc ();
					y = NTC.radio () * (float) Math.sin (NTC.deltaphi ()
									     * i + 2 * (float)
									     Math.PI / NTC.d () * k);
					yc = NTC.radio () * (float) Math.sin (NTC.deltaphi ()
									      * i + NTC.deltaphic ()
									      + 2 * (float)
									      Math.PI / NTC.d () * k);
					z = NTC.radio () * (float) Math.cos (NTC.deltaphi ()
									     * i + 2 * (float)
									     Math.PI / NTC.d () * k);
					zc = NTC.radio () * (float) Math.cos (NTC.deltaphi ()
									      * i + NTC.deltaphic ()
									      + 2 * (float)
									      Math.PI / NTC.d () * k);
					MW.addVert (x, y, z, 6);
					MW.addVert (xc, yc, zc, 6);
			}}
		}
		MW.centrar ();
		MW.ponconec ();
		if (combo.getSelectedItem () == "Hydrogen") {
			MW.cierraH ();
			MW.ponconec ();
		} else if (combo.getSelectedItem () == "Nitrogen") {
			MW.cierraN ();
			MW.ponconec ();
		}
		//MW.setInfo(MW.getInfo()+" and length "+c+" A.");

		fi = new formato (5, "#####");	//para los enteros
		StringBuffer pdb = new StringBuffer ("");
		Minimol mmol = new Minimol (MW);
		for (int i = 0; i < mmol.nvert; i++) {
			String lab = mmol.minietiqs[i];
			pdb.append ("HETATM" + fi.aCadena (i + 1) + "  " +
				    lab + "      " + fi.aCadena (i + 1) +
				    "    " + fd.aCadena (mmol.miniverts[i].x) + fd.aCadena (mmol.miniverts[i].y) + fd.aCadena (mmol.miniverts[i].z) + "\n");
		}
		for (int i = 0; i < mmol.nvert; i++)	//CONECTIVIDAD
		{
			pdb.append ("CONECT" + fi.aCadena (i + 1));
			for (int j = 1; j <= mmol.miniconec[i][0]; j++)
				pdb.append (fi.aCadena (mmol.miniconec[i][j] + 1));
			pdb.append ("\n");
		}
		pdb = pdb.append ("END");

		txtout.setText (pdb + "");



		pan3D.cargarMol (MW);
		pan3D.repaint ();

	}
	public MoleculaT molT ()
	{
		return MW;
	}

	public void itemStateChanged (ItemEvent e)
	{
		Object source = e.getItemSelectable ();
		if (source == cb1) {
			if (e.getStateChange () == ItemEvent.DESELECTED)
				bst = false;
			else
				bst = true;
			pan3D.repinta (bst, atn, hbk);
		} else if (source == cb2) {
			if (e.getStateChange () == ItemEvent.DESELECTED)
				atn = false;
			else
				atn = true;
			pan3D.repinta (bst, atn, hbk);
		} else if (source == cb3) {
			if (e.getStateChange () == ItemEvent.DESELECTED)
				hbk = false;
			else
				hbk = true;
			pan3D.repinta (bst, atn, hbk);
		}
		//Now that we know which button was pushed, find out
		//whether it was selected or deselected.

	}
}