//package regularsolution.elasticity; import java.applet.Applet; //import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Vector; import java.util.Hashtable; import java.util.Enumeration; import java.io.*; //import java.awt.geom.Point2D; //import java.awt.geom.Point2D.Double; /** * Title: * Description: * Copyright: Copyright (c) 2001 * Company: * @author Cyfer( Zhang, Xin ) * @version 1.0 */ public class Main1 extends Applet { Panel jPanel1 = new Panel(); BorderLayout borderLayout1 = new BorderLayout(); Panel jPanel3 = new Panel(); Panel jPanel2 = new Panel(); TextField jTextFieldTstep = new TextField(); TextField jTextFieldEa = new TextField(); TextField jTextFieldTma = new TextField(); Label jLabel3 = new Label(); Label jLabel2 = new Label(); Label jLabel1 = new Label(); Panel jPanel4 = new Panel(); TextField jTextFieldTmb = new TextField(); Label jLabel4 = new Label(); BorderLayout borderLayout2 = new BorderLayout(); Label jLabel5 = new Label(); TextField jTextFieldEb = new TextField(); Label jLabel6 = new Label(); TextField jTextFieldXstep = new TextField(); Panel jPanel5 = new Panel(); Button jButtonClear = new Button(); Button jButtonCal = new Button(); BorderLayout borderLayout3 = new BorderLayout(); Panel jPanelCanvas = new Panel(); BorderLayout borderLayout4 = new BorderLayout(); /** @todo: data needed in calculation */ public double Tma, Tmb; public double T; public double Tstep; public double Xa, Xb; public double Xstep; double Omega_alpha; double Omega_beta; double Ebsilon; double fa,fb; double x; double c_alpha, c_beta; double minX, minC_alpha, minC_beta; double minV3; double V1, V2, V3; int Csteps = 100; double diff = 0.01; double S; boolean a_b = true;; public static double R = 8.3144; Hashtable Points = new Hashtable(); Hashtable PointsSingle = new Hashtable(); //double Ualphak[], Ubetak[]; double critical = -1000; boolean bFindDouble = false; Label label1 = new Label(); TextField textFieldS = new TextField(); Label label2 = new Label(); TextField textFieldEbsilon = new TextField(); public Main1() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { Main1 main1 = new Main1(); if( args.length > 0 ){ main1.Ebsilon = Double.valueOf(args[0]).doubleValue(); } Frame frame = new Frame(); frame.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit( 0 ); } }); frame.add( main1 ); frame.pack(); frame.show(); } private void jbInit() throws Exception { this.setLayout(borderLayout1); //jPanel1.setBorder(BorderFactory.createEtchedBorder()); jTextFieldEa.setColumns(5); jTextFieldEa.setText("0"); jTextFieldEa.setSize(new Dimension(63, 22)); //jTextFieldTa.setPreferredSize(new Dimension(63, 22)); jTextFieldTma.setColumns(5); jTextFieldTma.setText("800"); jTextFieldTma.setSize(new Dimension(63, 22)); //jTextFieldPa.setPreferredSize(new Dimension(63, 22)); jLabel3.setText("Tstep:"); jLabel2.setText("¦¸¦Á:"); jLabel1.setText("Tma:"); jLabel4.setText("Tmb:"); jPanel3.setLayout(borderLayout2); jLabel5.setText("¦¸¦Â:"); jLabel6.setText("Xstep:"); jButtonClear.setLabel("Redraw"); jButtonClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButtonClear_actionPerformed(e); } }); jButtonCal.setLabel("Calculate"); jButtonCal.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButtonCal_actionPerformed(e); } }); jPanel5.setLayout(borderLayout3); jTextFieldTstep.setColumns(5); jTextFieldTstep.setText("10"); jTextFieldTstep.setSize(new Dimension(63, 22)); jTextFieldTmb.setColumns(5); jTextFieldTmb.setText("1200"); jTextFieldTmb.setSize(new Dimension(63, 22)); jTextFieldEb.setColumns(5); jTextFieldEb.setText("-20000"); jTextFieldEb.setSize(new Dimension(63, 22)); jTextFieldXstep.setColumns(5); jTextFieldXstep.setText("0.01"); jTextFieldXstep.setSize(new Dimension(63, 22)); jPanelCanvas.setSize( new Dimension( 500, 450 ) ); jPanelCanvas.setBounds( 0, 0, 500, 450 ); jPanelCanvas.setBackground( Color.white ); jPanelCanvas.setLayout( borderLayout4 ); //jPanel6.setBorder(BorderFactory.createEtchedBorder()); //this.setBackground(Color.black); jPanel1.setBackground(SystemColor.control); this.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(FocusEvent e) { this_focusGained(e); } }); label1.setText("S(j):"); textFieldS.setColumns(5); textFieldS.setText("10"); label2.setText("¦Å(j):"); textFieldEbsilon.setColumns(5); textFieldEbsilon.setText("0"); jPanel4.add(label2, null); jPanel4.add(textFieldEbsilon, null); this.add(jPanel1, BorderLayout.SOUTH); jPanel1.add(jPanel3, null); jPanel4.add(jLabel4, null); jPanel4.add(jTextFieldTmb, null); jPanel4.add(jLabel5, null); jPanel4.add(jTextFieldEb, null); jPanel4.add(jLabel6, null); jPanel4.add(jTextFieldXstep, null); jPanel3.add(jPanel4, BorderLayout.SOUTH); jPanel3.add(jPanel2, BorderLayout.NORTH); jPanel2.add(label1, null); jPanel2.add(textFieldS, null); jPanel2.add(jLabel1, null); jPanel2.add(jTextFieldTma, null); jPanel2.add(jLabel2, null); jPanel2.add(jTextFieldEa, null); jPanel2.add(jLabel3, null); jPanel2.add(jTextFieldTstep, null); jPanel1.add(jPanel5, null); jPanel5.add(jButtonClear, BorderLayout.NORTH); jPanel5.add(jButtonCal, BorderLayout.CENTER); this.add(jPanelCanvas, BorderLayout.CENTER); } public Dimension getPreferredSize(){ return new Dimension( 550, 500 ); } void doGetData(){ try{ this.Tma = Double.valueOf( this.jTextFieldTma.getText() ).doubleValue(); this.Tmb = Double.valueOf( this.jTextFieldTmb.getText() ).doubleValue(); this.Omega_alpha = Double.valueOf( this.jTextFieldEa.getText() ).doubleValue(); this.Omega_beta = Double.valueOf( this.jTextFieldEb.getText() ).doubleValue(); this.Tstep = Double.valueOf( this.jTextFieldTstep.getText()).doubleValue(); this.diff = Double.valueOf( this.jTextFieldXstep.getText()).doubleValue(); this.Csteps = (int)( 1 / this.diff ); this.T = Math.max( this.Tma, this.Tmb)*1.1 ; this.S = Double.valueOf( this.textFieldS.getText() ).doubleValue(); this.Ebsilon = Double.valueOf( this.textFieldEbsilon.getText() ).doubleValue(); this.a_b = true; }catch( Exception e ){ e.printStackTrace(); } } void insertPoint( MyPoint1 p ){ if( 0.0 == p.getX() || 0.0 == p.getY() ) return; Vector vPoints = (Vector) Points.get( new Double(this.T) ); vPoints.addElement( p ); } void calculation(){ this.jButtonCal.setEnabled(false); this.Points = new Hashtable(); this.PointsSingle = new Hashtable(); doGetData(); Cursor backup = this.getCursor(); this.setCursor( new Cursor( Cursor.WAIT_CURSOR ) ); calmain(); this.setCursor( backup ); this.jButtonCal.setEnabled( true ); this.invalidate(); this.repaint(); } public void paint(Graphics g ){ super.paint( g ); //System.err.println("paint called"); Graphics newG = this.jPanelCanvas.getGraphics(); int width = this.jPanelCanvas.getSize().width; int height = this.jPanelCanvas.getSize().height; newG.clearRect( 0, 0, width, height ); width -= 20; height -= 20; Enumeration vaDoubleCheck = this.Points.elements(); //int steps = vaDoubleCheck.length; newG.setColor( Color.red ); //for( int i = 0 ; i < steps ; i ++ ){ while( vaDoubleCheck.hasMoreElements()){ //Vector vPoints = (Vector) vaDoubleCheck[ i ]; Vector vPoints = (Vector) vaDoubleCheck.nextElement(); for( int j = 0 ; j < vPoints.size(); j ++ ){ MyPoint1 Point2Draw = ( MyPoint1 ) vPoints.elementAt( j ); int x = (int)( Point2Draw.getX()* ( width ) ) + 10; int y = (int)(height - ( Point2Draw.getY() / ( Math.max( this.Tma, this.Tmb ) * 1.2 ) ) * height ); newG.setColor( Point2Draw.getColor() ); newG.drawOval( x, y , 1, 1 ); } } //Object[] vaSingleCheck = this.PointsSingle.elements().toArray(); Enumeration vaSingleCheck = this.PointsSingle.elements(); //steps = vaSingleCheck.length; //for( int i = 0 ; i < steps ; i ++ ){ while( vaSingleCheck.hasMoreElements()){ //Vector vPoints = (Vector) vaSingleCheck[ i ]; Vector vPoints = (Vector) vaSingleCheck.nextElement(); for( int j = 0 ; j < vPoints.size(); j ++ ){ MyPoint1 Point2Draw = ( MyPoint1 ) vPoints.elementAt( j ); int x = (int)( Point2Draw.getX()* ( width ) ) + 10; int y = (int)(height - ( Point2Draw.getY() / ( Math.max( this.Tma, this.Tmb ) * 1.2 ) ) * height ); //double x = Point2Draw.getX() * this.jPanelCanvas.getWidth(); //double y = Point2Draw.getY(); //if( y > 800 ) System.err.println("x="+x+";y="+y); newG.setColor( Point2Draw.getColor() ); newG.drawOval( x, y , 1, 1 ); } } newG.setColor(Color.red ); newG.drawLine( 10, 10, 10, height ); newG.drawLine( 10, height, width + 10, height ); newG.drawLine( width + 10, 10, width + 10, height ); for( int i = 0 ; i <= 10 ; i ++){ newG.drawLine( 10 + width/10 * i,height,10 + width/10 * i, height+5); } int i=0; int deltah = (int)(height /( Math.max(Tma,Tmb)*1.2/100)); while( i < Math.max(Tma,Tmb)*1.2-100){ newG.drawLine( 5, height - i * deltah, 10,height - i * deltah ); newG.drawLine( width+10, height - i * deltah, width+15,height - i * deltah ); i++; } //System.err.println(steps); newG.dispose(); } void jButtonCal_actionPerformed(ActionEvent e) { calculation(); } void this_focusGained(FocusEvent e) { this.invalidate(); this.repaint(); } void jButtonClear_actionPerformed(ActionEvent e) { this.invalidate(); this.repaint(); } double calculateF_beta( double S, double Tm, double T, double Omega, double c ){ double G = 0; if( T > Tm ) G = S * ( T - Tm ) ; if( T < Tm ) G = S * ( Tm - T ) ; return G + R * T * ( c * Math.log( c ) + ( 1 - c ) * Math.log( 1 - c ) ) + Omega * c * ( 1 - c ) ; } double calculateF_alpha( double S, double Tm, double T, double Omega, double c ){ double G = 0; //if( T > Tm ) G = S * ( T - Tm ) * ( 1 - c ); //if( T < Tm ) G = S * ( Tm - T ) * ( 1 - c ); return - S * ( Tm - T )// * ( 1 - c ) + R * T * ( c * Math.log( c ) + ( 1 - c ) * Math.log( 1 - c ) ) + Omega * c * ( 1 - c ) ; } double calV1( double alpha, double beta){ return R * T * Math.log( alpha * ( 1 - beta ) / ( ( 1 - alpha ) * beta ) ) + Omega_alpha * ( 1 - 2 * alpha ) - Omega_beta * ( 1 - 2 * beta )- S * ( Tmb - Tma ); } double calV2( double fa, double fb,double alpha, double beta, double X){ return fa - fb + Ebsilon * ( 1 - 2 * X ) - ( alpha - beta ) * ( R * T * Math.log( alpha / ( 1 - alpha ) ) - Omega_alpha * ( 1 - 2 * alpha ) ); } void calmain(){ int i, j, k, n; MyCouple min1, min2; while( T >= 0 ){ this.Points.put( new Double(this.T) , new Vector() ); this.PointsSingle.put( new Double(this.T), new Vector() ); min1 = new MyCouple( 0, .99, (double)( 1 << 30 ), 0 ); min2 = new MyCouple( 0, .99, (double)( 1 << 30 ), 0 ); for( i = 1; i < Csteps; i ++){ c_alpha = i * diff; double c_alpha2 = 1 - c_alpha; fa = calculateF_alpha( S, Tma, T, Omega_alpha, c_alpha ); double fa2 = calculateF_alpha( S, Tma, T, Omega_alpha, c_alpha2 ); for( j = 1; j < Csteps; j ++ ){ c_beta = j * diff; double c_beta2 = 1 - c_beta; fb = calculateF_beta( S, Tmb, T, Omega_beta, c_beta); double fb2 = calculateF_beta( S, Tmb, T, Omega_beta, c_beta2); V1 = calV1( c_alpha, c_beta ); double V12 = calV1( c_alpha2, c_beta2 ); for( k = 0; k <= 2; k ++ ){ x = k * 0.5; V2 = calV2( fa, fb, c_alpha, c_beta, x ); double V22 = calV2( fa2, fb2, c_alpha2, c_beta2, x ); V3 = V1 * V1 + V2 * V2 ; double V32 = V12 * V12 + V22 * V22 ; if( V3 < min1.getV() ){ min1 = new MyCouple( c_alpha, c_beta, V3, x ); } if( V32 < min2.getV() ){ min2 = new MyCouple( c_alpha2, c_beta2, V32, x ); } if( 0 == Ebsilon ) break; } if( c_beta == c_beta2 ) break; } if( c_alpha == c_alpha2 ) break; } if( min1.getV() < 6000 ) insertPoint( new MyPoint1( (float)min1.getAlpha(), (float)T,(float)min1.getX() ) ); if( min2.getV() < 6000 ) insertPoint( new MyPoint1( (float)min2.getBeta(), (float)T,(float)min2.getX() ) ); /*if( null != min2 ){ insertPoint( new MyPoint1( (float)min2.getAlpha(), (float)T,(float)min2.getX() ) ); insertPoint( new MyPoint1( (float)min2.getBeta(), (float)T,(float)min2.getX() ) ); }*/ this.jButtonCal.setLabel("T="+T); T -= Tstep; if( T < 700 ) break; } this.jButtonCal.setLabel("Calculate"); } } class MyPoint{ double x,y; double uaA, uaB, ubB, ubA; public MyPoint(){} public MyPoint(double x, double y){ this.setLocation( x, y ); } public void setLocation( double x, double y ){ this.x = x; this.y = y; } public double getX(){ return this.x; } public double getY(){ return this.y; } } class MyCouple{ double alpha; double beta; double v; double x; public MyCouple( double alpha, double beta, double v, double x){ this.alpha = alpha; this.beta = beta; this.v = v; this.x = x; } public double getAlpha() { return alpha; } public double getBeta() { return beta; } public double getV() { return v; } public double getX(){ return x; } } class MyPoint1{ float c; float T; float x; public MyPoint1( float c, float T, float x){ this.c = c; this.T = T; this.x = x; } public float getX(){return c;} public float getY(){return T;} public Color getColor(){return new Color( x, 0f, 0f );} }