Hydro/Thermodynamics Simulation

I've created a number of simulations related to Nuclear power plants, but I've never gotten it right. Its actually quite a feat. I've used two models:

1) There are a collection of vessels (reactor core, exchanger, reservoir, turbine, condenser) and pipes connecting them all. They all have a temperature value, and volume of, and a pressure which is basically temp*pressure. High pressure vessels push water into low pressure vessels via pipes and heat is exchanged. The problem is I can't describe things like boiling, or temperature and pressure differences between the top and the bottom of the vessel easily with this model.

2) There are thousands of little squares of water that act a little like the vessels in (1). This model always seems to give me error after error after divide by zero error until I get too frustrated to continue.

[864 byte] By [PremierSullivana] at [2007-10-2 20:39:52]
# 1

let me post some code:

public class Water

{

int x,y;

int temp=532, volume=10000,lastPressure=532000;

public Water(int xpos,int ypos)

{

x=xpos;

y=ypos;

}

public void paint(Graphics g)

{

//System.out.println(temp+"\t"+volume+"\t"+temp*1000.0/volume);

if(volume==0)

{

volume=0;

g.setColor(new Color(255,255,255));

}else if(volume<0)

{

System.out.println("hey");

}else if(temp*1000/volume>=511)

g.setColor(new Color(255,255,255));

else if(temp*1000/volume<25)

g.setColor(new Color(0,0,25));

else if(temp*1000/volume>255)

g.setColor(new Color((int)(temp*1000/volume-255),(int)(temp*1000/volume-255),255));

else

g.setColor(new Color(0,0,(int)(temp*1000/volume)));

//System.out.println(temp*10/volume);

g.fillRect(x*4,y*4,4,4);

}

public void run()

{

calcPressure();

transferTemp();

}

public int calcPressure()

{

lastPressure=temp*volume;

if(y!=0)

for(int i=Math.max(0,x-1);i<Math.min(map.length,x+2);i++)

lastPressure+=map[i][y-1].volume;

return lastPressure;

}

public int pressure()

{

return lastPressure;

}

public void transferTemp(Water w)

{

if(w.pressure()>pressure())

{

int wV=(w.pressure()-pressure())/2;

volume+=wV/w.temp;

w.volume-=wV/w.temp;

temp=(wV+temp*volume)/(wV/w.temp+volume);

}

}

//This is for the movement of water due to pressure

public void transferTemp()

{

if(Math.random()*4<1)

for(int i=x-1<0?0:x-1;i<map.length&&i><=x+1;i++)

for(int j=y-1<0?0:y-1;j<map[i].length&&j><=y+1;j++)

transferTemp(map[i][j]);

else if(Math.random()*3<1)

for(int i=x+1>=map.length?map.length-1:x+1;i>=0&&i>=x-1;i--)

for(int j=y+1>=map.length?map.length-1:y+1;j>=0&&j>=y-1;j--)

transferTemp(map[i][j]);

else if(Math.random()*2<1)

for(int i=x+1>=map.length?map.length-1:x+1;i>=0&&i>=x-1;i--)

for(int j=y-1<0?0:y-1;j<map[i].length&&j><=y+1;j++)

transferTemp(map[i][j]);

else

for(int i=x-1<0?0:x-1;i<map.length&&i><=x+1;i++)

for(int j=y+1>=map.length?map.length-1:y+1;j>=0&&j>=y-1;j--)

transferTemp(map[i][j]);

/*if(Math.random()<0.5)

{for(int i=x-1<0?0:x-1;i<map.length&&i><=x+1;i++)

for(int j=y-1<0?0:y-1;j<map[i].length&&j><=y+1;j++)

}else

for(int i=x+1>=map.length?map.length-1:x+1;i>=0&&i>=x-1;i--)

for(int j=y+1>=map.length?map.length-1:y+1;j>=0&&j>=y-1;j--)

if(map[i][j].pressure()>pressure())

{

int wV=(map[i][j].pressure()-pressure())/2;

//System.out.println(wV+"\t"+volume+"\t"+map[i][j].volume);

volume+=wV/map[i][j].temp;

map[i][j].volume-=wV/map[i][j].temp;

temp=(wV+temp*volume)/(wV/map[i][j].temp+volume);

//temp=(wV*map[i][j].temp+temp*volume)/(wV+volume);

temp=(map[i][j].volume*map[i][j].temp+temp*volume)/(map[i][j].volume+volume);

/*double wP=(pressure()-map[i][j].pressure())/2;

System.out.println(wP+"\t"+map[i][j].temp);

//temp=(wP+pressure())/(wP/map[i][j].temp+volume);

volume+=wP/map[i][j].temp;

map[i][j].volume-=wP/map[i][j].temp;

}*/

//This part is for conduction of heat between squares of water

for(int i=x+1>=map.length?map.length-1:x+1;i>=0&&i>=x-1;i--)

for(int j=y+1>=map.length?map.length-1:y+1;j>=0&&j>=y-1;j--)

temp=(map[i][j].volume*map[i][j].temp/5+temp*volume)/(map[i][j].volume/5+volume);

for(int i=x+1>=map.length?map.length-1:x+1;i>=0&&i>=x-1;i--)

temp=(map[i][y-1].volume*map[i][y-1].temp/5+temp*volume)/(map[i][y-1].volume/5+volume);

//else

//for(int i=x+1>=map.length?map.length-1:x+1;i>=0&&i>=x-1;i--)

//for(int j=y+1>=map.length?map.length-1:y+1;j>=0&&j>=y-1;j--)

//temp=(map[i][j].volume*map[i][j].temp/5+temp*volume)/(map[i][j].volume/5+volume);

}

}

PremierSullivana at 2007-7-13 23:23:02 > top of Java-index,Other Topics,Algorithms...
# 2

> I've created a number of simulations related to

> Nuclear power plants, but I've never gotten it right.

> Its actually quite a feat.

Really? I always thought nuclear power plant technology was sort of

an unskilled field.

I would suggest that making your code readable and otherwise cleaning

it up (use OO and polymorphism instead of a bazillion if statements) would have

three effects:

1. You will be able to see where you are going wrong.

2. Other people might bother to read and understand it

3. You will be able to incrementally improve upon your model over time

dubwaia at 2007-7-13 23:23:02 > top of Java-index,Other Topics,Algorithms...
# 3

From Sun's JDK license agreement (http://java.sun.com/j2se/1.5.0/jdk-1_5_0_06-license.txt):

3. RESTRICTIONS. ... You acknowledge that Licensed

Software is not designed or intended for use in the design,

construction, operation or maintenance of any nuclear facility...

; )

prometheuzza at 2007-7-13 23:23:03 > top of Java-index,Other Topics,Algorithms...
# 4
> construction, operation or maintenance of any> nuclear facility...Huh, I thought that was something MS added in their old license agreement for their 1.1 JVM.
dubwaia at 2007-7-13 23:23:03 > top of Java-index,Other Topics,Algorithms...