olumydot / isingmodel Goto Github PK
View Code? Open in Web Editor NEWThis is a java program to model the monte carlo walk used as my project at the end of the semester at NCCU
This is a java program to model the monte carlo walk used as my project at the end of the semester at NCCU
can some one please review to see if code is optimized?
/*Olumide Oni
class GraphingData extends JPanel {
double[] data;
final int PAD = 20;
GraphingData(double[] iData){
data = new double[iData.length];
System.arraycopy(iData, 0, data, 0, iData.length);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
// Draw ordinate.
g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
// Draw abcissa.
g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
// Draw labels.
Font font = g2.getFont();
FontRenderContext frc = g2.getFontRenderContext();
LineMetrics lm = font.getLineMetrics("0", frc);
float sh = lm.getAscent() + lm.getDescent();
// Ordinate label.
String s = "M";
float sy = PAD + ((h - 2*PAD) - s.length()*sh)/2 + lm.getAscent();
for(int i = 0; i < s.length(); i++) {
String letter = String.valueOf(s.charAt(i));
float sw = (float)font.getStringBounds(letter, frc).getWidth();
float sx = (PAD - sw)/2;
g2.drawString(letter, sx, sy);
sy += sh;
}
// Abcissa label.
s = "Temperature";
sy = h - PAD + (PAD - sh)/2 + lm.getAscent();
float sw = (float)font.getStringBounds(s, frc).getWidth();
float sx = (w - sw)/2;
g2.drawString(s, sx, sy);
// Draw lines.
s = "0";
sy = h - PAD/2;
sx = PAD/2;
g2.drawString(s, sx, sy);
s = "10.0";
sy = h - PAD/2;
sx = w - PAD - 2 * s.length();
g2.drawString(s, sx, sy);
s = String.valueOf(getMax());
sy = PAD*2/3;
sx = PAD/2;
g2.drawString(s, sx, sy);
double xInc = (double)(w - 2*PAD)/(data.length-1);
double scale = (double)(h - 2*PAD)/getMax();
g2.setPaint(Color.green.darker());
for(int i = 0; i < data.length-1; i++) {
double x1 = PAD + i*xInc;
double y1 = h - PAD - scale*data[i];
double x2 = PAD + (i+1)*xInc;
double y2 = h - PAD - scale*data[i+1];
g2.draw(new Line2D.Double(x1, y1, x2, y2));
}
// Mark data points.
g2.setPaint(Color.red);
for(int i = 0; i < data.length; i++) {
double x = PAD + i*xInc;
double y = h - PAD - scale*data[i];
g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
}
}
private double getMax() {
double max = -Double.MAX_VALUE;
for(int i = 0; i < data.length; i++) {
if(data[i] > max)
max = data[i];
}
return max;
}
}
class Lattice {
public void SetSpin(int[][] spin){
Random rand = new Random();
for (int[] spin1 : spin) {
for (int col = 0; col < spin[0].length; col ++) {
int value = rand.nextInt(2);
spin1[col] = 2 * value - 1;
}
}
}
// this idea for the lattice spin area of the program below was gotten from the internet
//start lattice spin
double energy(int[][] spin, int i, int j) {
int initEn=0;
int leftSite, rightSite, topSite, bottomSite;
if (i == 0) leftSite = spin[spin.length-1][j]; else leftSite = spin[i-1][j];
if (i == spin.length-1) rightSite = spin[0][j]; else rightSite = spin[i+1][j];
if (j == 0) topSite = spin[i][spin[0].length-1]; else topSite = spin[i][j-1];
if (j == spin[0].length-1) bottomSite = spin[i][0]; else bottomSite = spin[i][j+1];
return 2.0 * spin[i][j] * (leftSite + rightSite + topSite + bottomSite);
}
//end
public double energy(int spin1, int spin) {
double initEn= 0.0;
int length = 0;
for (int i = 0; i < length; i++)
for (int j = 0; j < length; j++)
initEn += 0.5 * energy(i,j);
return initEn;
}
public void FlipSpin(int[][] spin, double temperature){
Random rand = new Random();
int row = rand.nextInt(spin.length);
int col = rand.nextInt(spin[0].length);
double energyChange = energy(spin, row, col);
if ((energyChange <= 0) || (Math.random() < Math.exp(-energyChange/temperature))) {
spin[row][col] *= -1;
}
}
double Magnetization(int[][] spin){
double m = 0.0;
for (int[] spin1 : spin) {
for (int col = 0; col < spin[0].length; col ++) {
m = m + spin1[col];
}
}
return m;
}
public void ShowLattice(int[][] spin){
for (int[] spin1 : spin) {
for (int col = 0; col < spin[0].length; col ++) {
System.out.format("%d\t", spin1[col]);
}
System.out.format("\n");
}
}
}
// following the Pseudo code given:
public class Ising {
public static void main(String[] args){
int [][]spin = new int[10][10];
Lattice latt = new Lattice();
double[] magnet = new double[101];
for (int t=0; t<magnet.length; t++){
double aver_mag = 0;
for (int i=0; i<100; i++){
latt.SetSpin(spin);
for (int j=0; j<10000; j++){
latt.FlipSpin(spin, (double)t*0.1);
}
aver_mag = aver_mag + Math.abs(latt.Magnetization(spin));
}
aver_mag = aver_mag / 100;
magnet[t] = aver_mag;
}
System.out.format("Temperature\tMagnet\n");
for (int t=0; t<magnet.length; t++){
System.out.format("%f:\t%f\n", (double)t * 0.1, magnet[t]);
}
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new GraphingData(magnet));
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.