1 import java.awt.*;
2 import java.io.*;
3 import java.awt.event.*;
4 import javax.swing.*;
5 import javax.swing.event.*;
6 
7 class nwchem_Free extends JFrame implements ActionListener, ChangeListener, WindowListener {
8 
9     Font defaultFont;
10     int setnumber=0;
11     JFileChooser chooser;
12     ExtensionFilter gibFilter;
13     JFrame dialogFrame;
14     int type = 0;
15     double lambda;
16     double dlambda;
17     double deriv[] = new double[24];
18     double free, freem, freep, freepm, ep2, ep3, tmp, ep2m, ep3m;
19     double dfree, freeb, dfbias, dfreem;
20     Graph gibPlot = new Graph();
21     Graph cnvPlot = new Graph();
22 
nwchem_Free()23     public nwchem_Free(){
24 
25 	super("Free Energy");
26 
27 	defaultFont = new Font("Dialog", Font.BOLD,12);
28 
29 	super.getContentPane().setLayout(new GridBagLayout());
30 	super.getContentPane().setForeground(Color.black);
31 	super.getContentPane().setBackground(Color.lightGray);
32 	super.getContentPane().setFont(defaultFont);
33 	super.addWindowListener(this);
34 
35 	chooser = new JFileChooser("./");
36 	gibFilter = new ExtensionFilter(".gib");
37 	chooser.setFileFilter(gibFilter);
38 	dialogFrame = new JFrame();
39 	dialogFrame.setSize(300,400);
40 	chooser.showOpenDialog(dialogFrame);
41 
42 	JPanel header = new JPanel();
43 	header.setLayout(new GridBagLayout());
44 	header.setForeground(Color.black);
45 	header.setBackground(Color.lightGray);
46 	addComponent(super.getContentPane(),header,0,0,2,1,1,1,
47 		     GridBagConstraints.NONE,GridBagConstraints.WEST);
48 
49 	JLabel systemLabel = new JLabel(chooser.getSelectedFile().toString());
50 	addComponent(header,systemLabel,0,0,1,1,1,1,
51 		     GridBagConstraints.NONE,GridBagConstraints.CENTER);
52 	systemLabel.setForeground(Color.black);
53 
54 	JButton doneButton = new JButton("Done");
55 	addComponent(header,doneButton,5,0,1,1,1,1,
56 		     GridBagConstraints.NONE,GridBagConstraints.CENTER);
57 	doneButton.addActionListener(new ActionListener(){
58 		public void actionPerformed(ActionEvent e){
59 		    setVisible(false); }});
60 
61 	JButton addButton = new JButton("Add");
62 	addComponent(header,addButton,6,0,1,1,1,1,
63 		     GridBagConstraints.NONE,GridBagConstraints.CENTER);
64 	addButton.addActionListener(new ActionListener(){
65 		public void actionPerformed(ActionEvent e){
66 		    chooser.showOpenDialog(dialogFrame);
67 		}});
68 
69 	setLocation(25,225);
70 	setSize(900,700);
71 	setVisible(true);
72 
73 	try{
74 	    addComponent(header,gibPlot,0,1,5,5,1,1,
75 			 GridBagConstraints.NONE,GridBagConstraints.CENTER);
76 	    addComponent(header,cnvPlot,0,7,5,5,1,1,
77 			 GridBagConstraints.NONE,GridBagConstraints.CENTER);
78 	    gibPlot.init();
79 	    gibPlot.resize(500,300);
80 	    gibPlot.setTitle("Free Energy vs Lambda");
81 	    gibPlot.setXLabel("Lambda");
82 	    gibPlot.setMarksStyle("dots",2);
83 	    cnvPlot.init();
84 	    cnvPlot.resize(500,300);
85 	    cnvPlot.setTitle("Free Energy Convergence");
86 	    cnvPlot.setXLabel("Time");
87 	    validate();
88 	} catch(Exception e) {e.printStackTrace();};
89 
90 	try{
91 	    BufferedReader br = new BufferedReader(new FileReader(chooser.getSelectedFile().toString()));
92 	    String card;
93 	    free=0.0;
94 	    freem=0.0;
95             freep=0.0;
96 	    freepm=0.0;
97             freeb=0.0;
98             lambda=0.0;
99 	    boolean first=true;
100             int j,ndata;
101             double lam;
102             double freem=0.0;
103 	    double cnv[] = new double[10000];
104 	    double cnvm[] = new double[10000];
105             int mdata=10000;
106             int numdat=0;
107             int ndec=0;
108             int nsa=0;
109 	    while((card=br.readLine()) != null){
110 		int nderiv = Integer.parseInt(card.substring(0,8).trim());
111 		ndata = Integer.parseInt(card.substring(8,15).trim());
112                 numdat=ndata;
113                 if(numdat>mdata){numdat=mdata;};
114 		if(first){
115 		    for(int i=0; i<numdat; i++){cnv[i]=0.0;cnvm[i]=0.0;};
116 		};
117 		lam=Double.valueOf(card.substring(15,27)).doubleValue();
118 		dlambda=Double.valueOf(card.substring(27,39)).doubleValue();
119                 ndec=Integer.parseInt(card.substring(39,47).trim());
120                 nsa=Integer.parseInt(card.substring(47,54).trim());
121 		if(first){
122 		    gibPlot.addData(0,lambda,free,!first,true);
123 		    gibPlot.addData(1,lambda,freep,!first,true);
124 		    gibPlot.addData(2,lambda,freeb,!first,true);
125 		    lambda=lam;
126 		};
127 		first=false;
128 		for(int i=0; i<6; i++){
129 		    card=br.readLine();
130 		    deriv[4*i]=Double.valueOf(card.substring(0,20)).doubleValue();
131 		    deriv[4*i+1]=Double.valueOf(card.substring(20,40)).doubleValue();
132 		    deriv[4*i+2]=Double.valueOf(card.substring(40,60)).doubleValue();
133 		    deriv[4*i+3]=Double.valueOf(card.substring(60,80)).doubleValue();
134 		};
135                 j=0;
136 		for(int i=0; i<ndata; i=i+4){
137 		    card=br.readLine();
138                     if(j<numdat){cnv[j]=cnv[j]+dlambda*Double.valueOf(card.substring(0,20)).doubleValue(); j++;};
139                     if(j<numdat){cnv[j]=cnv[j]+dlambda*Double.valueOf(card.substring(20,40)).doubleValue(); j++;};
140                     if(j<numdat){cnv[j]=cnv[j]+dlambda*Double.valueOf(card.substring(40,60)).doubleValue(); j++;};
141                     if(j<numdat){cnv[j]=cnv[j]+dlambda*Double.valueOf(card.substring(60,80)).doubleValue(); j++;};
142 		};
143                 j=0;
144 		for(int i=0; i<ndata; i=i+4){
145 		    card=br.readLine();
146                     if(j<numdat){cnvm[j]=cnvm[j]+dlambda*Double.valueOf(card.substring(0,20)).doubleValue(); j++;};
147                     if(j<numdat){cnvm[j]=cnvm[j]+dlambda*Double.valueOf(card.substring(20,40)).doubleValue(); j++;};
148                     if(j<numdat){cnvm[j]=cnvm[j]+dlambda*Double.valueOf(card.substring(40,60)).doubleValue(); j++;};
149                     if(j<numdat){cnvm[j]=cnvm[j]+dlambda*Double.valueOf(card.substring(60,80)).doubleValue(); j++;};
150 		};
151 		dfree=0.0;
152 		for(int i=0; i<24; i++){
153 		    dfree=dfree+deriv[i];
154 		};
155 		dfreem=dfree-deriv[0]-deriv[12];
156 		dfree=dfree/nderiv;
157 		dfreem=dfreem/nderiv;
158 		free=free+dfree*dlambda;
159 		freem=freem+dfreem*dlambda;
160 		lambda=lambda+dlambda;
161 		gibPlot.addData(0,lambda,free,!first,false);
162 		gibPlot.addData(1,lambda,freem,!first,false);
163 		card=br.readLine();
164 		card=br.readLine();
165 		tmp=Double.valueOf(card.substring(0,20)).doubleValue();
166 		ep2=Double.valueOf(card.substring(20,40)).doubleValue();
167 		ep3=Double.valueOf(card.substring(40,60)).doubleValue();
168 		dfbias=Double.valueOf(card.substring(60,80)).doubleValue();
169 		card=br.readLine();
170 		ep2m=Double.valueOf(card.substring(0,20)).doubleValue();
171 		ep3m=Double.valueOf(card.substring(20,40)).doubleValue();
172 		freeb=freeb+dfbias*dlambda;
173 		if(freeb!=0.0) gibPlot.addData(2,lambda,freeb,!first,false);
174 		freep=freep+0.00831151*tmp*(Math.log(ep2)-Math.log(ep3));
175 		gibPlot.addData(3,lambda,freep,!first,false);
176 		freepm=freepm+0.00831151*tmp*(Math.log(ep2m)-Math.log(ep3m));
177 		gibPlot.addData(4,lambda,freepm,!first,false);
178 		if(ndec>0) {for(int i=0; i<6*nsa; i=i+4){ card=br.readLine(); };};
179 	    };
180 	    gibPlot.fillPlot();
181 	    br.close();
182 	    for(int i=1; i<numdat; i++){ cnv[i]=cnv[i]+cnv[i-1];cnvm[i]=cnvm[i]+cnvm[i-1]; };
183 	    for(int i=1; i<numdat; i++){ cnv[i]=cnv[i]/(i+1);cnvm[i]=cnvm[i]/(i+1); };
184             first=true;
185             for(int i=0; i<numdat; i++){
186 		cnvPlot.addData(0,i+1,cnv[i],!first,false);
187 		cnvPlot.addData(1,i+1,cnvm[i],!first,false); first=false;
188 	    };
189 	    cnvPlot.fillPlot();
190 	} catch(Exception e) {e.printStackTrace();};
191     }
192 
buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy)193     void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh,
194 			  int wx, int wy){
195 
196 	gbc.gridx = gx;
197 	gbc.gridy = gy;
198 	gbc.gridwidth = gw;
199 	gbc.gridheight = gh;
200 	gbc.weightx = wx;
201 	gbc.weighty = wy;
202     }
203 
addComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int fill, int anchor)204     static void addComponent(Container container, Component component,
205 			     int gridx, int gridy, int gridwidth,
206 			     int gridheight, double weightx,
207 			     double weighty, int fill, int anchor) {
208 	LayoutManager lm = container.getLayout();
209 	if(!(lm instanceof GridBagLayout)){
210 	    System.out.println("Illegal layout"); System.exit(1);
211 	} else {
212 	    GridBagConstraints gbc = new GridBagConstraints();
213 	    gbc.gridx=gridx;
214 	    gbc.gridy=gridy;
215 	    gbc.gridwidth=gridwidth;
216 	    gbc.gridheight=gridheight;
217 	    gbc.weightx=weightx;
218 	    gbc.weighty=weighty;
219 	    gbc.fill=fill;
220 	    gbc.anchor=anchor;
221 	    container.add(component,gbc);
222 	}
223     }
224 
actionPerformed(ActionEvent e)225     public void actionPerformed(ActionEvent e) {}
226 
stateChanged(ChangeEvent e)227     public void stateChanged(ChangeEvent e) {}
228 
windowClosing(WindowEvent event)229     public void windowClosing(WindowEvent event) {}
230 
windowClosed(WindowEvent event)231     public void windowClosed(WindowEvent event) { }
232 
windowDeiconified(WindowEvent event)233     public void windowDeiconified(WindowEvent event) {}
234 
windowIconified(WindowEvent event)235     public void windowIconified(WindowEvent event) {}
236 
windowActivated(WindowEvent event)237     public void windowActivated(WindowEvent event) {}
238 
windowDeactivated(WindowEvent e)239     public void windowDeactivated(WindowEvent e) {}
240 
windowOpened(WindowEvent event)241     public void windowOpened(WindowEvent event) {}
242 
mouseClicked(MouseEvent mouse)243     public void mouseClicked(MouseEvent mouse) {}
244 
245 }
246