General Question

RedDeerGuy1's avatar

Can you show me a piece of code for weather forecasts on a super computer?

Asked by RedDeerGuy1 (24460points) October 6th, 2016
11 responses
“Great Question” (0points)

Or any bits of beautiful code?

Observing members: 0
Composing members: 0

Answers

Mariah's avatar

Er, I don’t have anything that meets your specific request, but here’s a fun little algorithm for simulating schooling in an aquarium game that I wrote. There’s an absurd amount of comments throughout it because I have shared it with people before and wanted to explain my thought process. Fluther’s gonna kill the tabbing though so good luck reading it.

/*
Schooling algorithm. Establish a “leader” fish (the first fish of this species occurring in the tank.fish list).
The leader fish swims normally, but all other fish of its species will be drawn towards it. This is achieved by
adding a vector towards the leader fish’s position to other fish’s velocities.
Effect: No effect if this fish is the leader fish. Otherwise, make a change to this fish’s velocity.
*/
public void pullTowardsSchool(){
//iterate to find the leader fish
for(int i = 0; i < tank.fish.size(); i++){
Fish f = (Fish) tank.fish.get(i);
if(f.name == this.name){
//if leader fish is this fish (determined by fish’s nicknames – nicknames are unique,
//so name comparison is a safe and efficient way to determine equality), no effect.
return;
}
else if(f.species == this.species){
Vector3D pullToward = f.position;
//BUT, we don’t want to move toward the leader fish’s exact position.
//instead we pull toward a point that’s near him, offset by some pseudorandom amount
//we base this offset on the fish’s name so that it is consistent
//so one fish will consistently tend to be above the leader, another behind, etc
//this helps prevent the fish from just piling up on top the leader
int offset = int(name.toCharArray())[0];
pullToward = pullToward.addVector(new Vector3D(offset, offset, offset));
//convert to a vector pointing from the current position to the desired position and normalize
pullToward = pullToward.addVector(this.position.multiplyScalar(-1)).normalize();
//schoolingCoefficient is the “strength” of the pull, different for each species
//(since some real fish species school more tightly than others)
this.velocity = this.velocity.addVector(pullToward.multiplyScalar(this.schoolingCoefficient));
return;
}
}
}

ARE_you_kidding_me's avatar

A lot of this stuff is done using matlab, here is a decent very basic example that shows what that kind of work is like. I did some similar work years back for river modeling.
Many scientists use this kind of software because you don’t have to be a genius programmer to code it up and plot it. There is a freeware version I have not played with but hear it’s fun to tinker with.

imrainmaker's avatar

GA both..@mariah – just curious did you handle schooling of different species of fish to avoid overlapping and how?

LostInParadise's avatar

The question is like asking to see a piece of a beautifully built car. Computer code, like cars, is assembled in pieces. The beauty of the code, like that of a car, is in how the pieces are put together. Furthermore, there can be different levels of assembly. At one level you can talk about a car having an ignition system, engine and exhaust system. At another level, you can talk about how the engine is put together. The same holds for computer code.

If you want to learn more about code, I highly recommend Udacity’s coding intro course. What’s nice about the course is that it is centered on a project, which you put together in stages. This allows you to see how the components of a small but non-trivial program are assembled.

LostInParadise's avatar

Computer code should be constructed to reflect what it is modeling. The beauty of the code is a reflection of the beauty of the algorithms in the model. Here is some code for creating the Koch snowflake fractal. It just requires a few lines of code, because the self-referential nature of the fractal is reflected in the way that the function in the code makes calls to itself, which is known as recursion.

Mariah's avatar

Yeah @imrainmaker you might notice the line in my algorithm that says

else if(f.species == this.species)...

This makes it so that fish only school with their own species.

imrainmaker's avatar

Yeah I got that..in case of multiple species there are chances of overlapping of fishes depending on position of lead fish (if 2 lead fishes happen to be very close) I was talking about that.

ARE_you_kidding_me's avatar

@Mariah I wish all programmers commented their code like this.

Mariah's avatar

I don’t do this all the time, only because I’ve shared that particular algorithm with people before. As a general rule in industry, if you’re writing code that needs that many comments, your code is bad and you should rewrite it so that it is more self-evident.

ARE_you_kidding_me's avatar

I know, it’s just nice to see code provided as an example to be heavily commented up. I used to give kids extra credit when they commented the hell out of their assignments because it made this poor TA’s life easier.

Mariah's avatar

Gotcha! That’s a good policy lol. I handed in lots of trash code during school because “well, it works” and I knew I’d never have to see it again when the assignment was done. The poor TAs!

Answer this question

Login

or

Join

to answer.

Mobile | Desktop


Send Feedback   

`