I work in the scientific program development field and we use Java OO methodology for everything.
Simulation-wise OO provides a means to more easily simulate a physical system by describing its components and attributes in term of classes. As an example, in a solar system simulation you could define a Planet class that contains the planet's attributes:
- a position vector, and
- a velocity vector
- planetary mass
- ...other relevant attributes
and the position and velocity attributes would be described by a vector class containing these spherical coordinate attributes:
- radial distance for the origin
- theta angle
- phi angle
- ...
with methods to get and set these attributes, methods to get the x,y,z equivalents of these attributes for display and methods to update them based on running the ODE calculations to simulate motion.
Your simulation program(ie class) would then perform the necessary ODE computations using an array of planet objects which in turn rely on vector attributes ie classes within classes within classes each one testable independently.
You can learn more about OO based physical system simulation from Open Source Physics:
https://en.wikipedia.org/wiki/Open_Source_Physics
www.compadre.org/osp/
They provide many working examples including this generic Galaxy simulation:
[CODE lang="java" title="OSP Galaxy Simulation"]/*
* Open Source Physics software is free software as described near the bottom of this code file.
*
* For additional information and documentation on Open Source Physics please see:
* <http://www.opensourcephysics.org/>
*/
package org.opensourcephysics.sip.ch19;
import org.opensourcephysics.controls.*;
import org.opensourcephysics.frames.*;
import org.opensourcephysics.display.*;
import java.awt.*;
/**
* GalaxyApp models evolution of a galaxy using percolation ideas
* Model proposed by Schulman and Seiden
* @version 1.0
* @author J. Tobochnik 3/25/05
*
*/
public class GalaxyApp extends AbstractSimulation implements Drawable {
DisplayFrame frame = new DisplayFrame("Galaxy");
final static double twoPi = 2.0*Math.PI;
final static double twoPiOver6 = twoPi/6;
double p, v, dt, t;
int numberOfRings, numberOfActiveCells, numberOfCells;
int[] starLifeTime, cellR, cellA, activeCellLabel;
public GalaxyApp() {
frame.addDrawable(this);
}
public void initialize() {
t = 0;
numberOfRings = control.getInt("Number of rings");
numberOfActiveCells = control.getInt("Initial number of active cells");
p = control.getDouble("star formation probability");
v = control.getDouble("cell velocity");
dt = control.getDouble("time step");
frame.setPreferredMinMax(-numberOfRings-1.0, numberOfRings+1.0, -numberOfRings-1.0, numberOfRings+1.0);
numberOfCells = 3*numberOfRings*(numberOfRings+1);
cellR = new int[numberOfCells];
cellA = new int[numberOfCells];
int cellLabel = 0;
// initial values of r and a for each cell label
for(int r = 1;r<=numberOfRings;r++) {
for(int a = 0;a<r*6;a++) {
cellR[cellLabel] = r;
cellA[cellLabel] = a;
cellLabel++;
}
}
starLifeTime = new int[numberOfCells];
activeCellLabel = new int[numberOfCells];
initializeGalaxy();
}
public void initializeGalaxy() {
int i = 0;
while(i<numberOfActiveCells) {
int label = (int) (Math.random()*numberOfCells);
if(starLifeTime[label]!=15) {
starLifeTime[label] = 15; // activate region for 15 time steps
activeCellLabel
= label;
i++;
}
}
}
public void formNewStars() {
// copy list of active cells into another array
int[] currentActiveCellLabel = activeCellLabel.clone();
int currentNumberOfActiveCells = numberOfActiveCells;
numberOfActiveCells = 0;
for(int i = 0;i<currentNumberOfActiveCells;++i) {
int cellLabel = currentActiveCellLabel;
int r = cellR[cellLabel];
int a = cellA[cellLabel];
// activate neighboring cells in same ring
createStars(r, pbc(a+1, r));
createStars(r, pbc(a-1, r));
// activate cells in next larger ring
if(r<numberOfRings-1) {
int ap = aForOtherRadius(a, r, r+1);
createStars(r+1, pbc(ap, r+1));
createStars(r+1, pbc(ap+1, r+1));
}
// activate cells in next smaller ring
if(r>1) {
int am = aForOtherRadius(a, r, r-1);
createStars(r-1, pbc(am, r-1));
createStars(r-1, pbc(am+1, r-1));
}
}
}
public int pbc(int a, int r) {
return(a+6*r)%(6*r);
}
public int aForOtherRadius(int a, int r, int rOther) {
// angle corresponding to a at time t
double angle = twoPiOver6*a/r+((v*t)/r);
angle -= twoPi*(int) (angle/twoPi);
// change in angle for cell at other r
double angleChange = ((v*t)/rOther);
angleChange -= twoPi*(int) (angleChange/twoPi);
// return value of a for cell at other r
return(int) ((rOther/twoPiOver6)*(angle-angleChange));
}
public void createStars(int r, int a) {
int label = a+3*r*(r-1);
if((Math.random()<p)&&(starLifeTime[label]!=15)) {
activeCellLabel[numberOfActiveCells] = label;
numberOfActiveCells++;
starLifeTime[label] = 15;
}
}
public void doStep() {
t += dt;
formNewStars();
frame.setMessage("t = "+decimalFormat.format(t)+" #active = "+numberOfActiveCells);
}
public void reset() {
control.setValue("Number of rings", 50);
control.setValue("Initial number of active cells", 200);
control.setValue("star formation probability", 0.18);
control.setValue("cell velocity", 1.0);
control.setValue("time step", 10.0);
}
public void draw(DrawingPanel panel, Graphics g) {
g.setColor(Color.black);
g.fillRect(0, 0, panel.getWidth(), panel.getHeight()); // color background black
g.setColor(Color.yellow);
for(int label = 0;label<numberOfCells;label++) {
if(starLifeTime[label]>0) {
int r = cellR[label];
int a = cellA[label];
double angle = twoPiOver6*a/r+(v*t)/r; // angle for cell at time t
double x = r*Math.cos(angle);
double y = r*Math.sin(angle);
double ds = starLifeTime[label]/15.0;
int leftPixels = panel.xToPix(x);
int topPixels = panel.yToPix(y);
int heightPixels = (int) (panel.getYPixPerUnit()*ds);
int widthPixels = (int) (panel.getXPixPerUnit()*ds);
g.fillOval(leftPixels, topPixels, widthPixels, heightPixels);
starLifeTime[label]--; // decrease star cluster lifetime
}
}
}
public static void main(String[] args) {
SimulationControl.createApp(new GalaxyApp());
}
}
/*
* Open Source Physics software is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License (GPL) as
* published by the Free Software Foundation; either version 2 of the License,
* or(at your option) any later version.
* Code that uses any portion of the code in the org.opensourcephysics package
* or any subpackage (subdirectory) of this package must must also be be released
* under the GNU GPL license.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 USA
* or view the license online at http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2007 The Open Source Physics project
* http://www.opensourcephysics.org
*/
[/CODE]
Development-wise OO provides a better means of organizing your code into packages, classes and methods. Because of this you can build larger programs without losing sight of what you are developing. Prior to OO techniques, programmers would write very large simulation programs and would package their functions into libraries and use those libraries to develop their applications.
A library is akin to a Java jar file, a program is akin to a java class and a function is akin to a class method. In java then my code is organized in jars of packages and in classes within each package so that Java provides and additional level of organization with packages. Packaging allows you to bring together classes of the same name made unique by the packaging name.
Having come from a background of writing mostly fortran code where variables were either local to a function or global in scope and optionally organized in common blocks, I find the OO methodology to be better for code refactoring and for making classes that are testable individually and then again when combined to make a program.
Other OO languages that have a lot of traction today are Python and C++. I always preferred Java to C++ and now that I've played enough with python and its OO features prefer Python to Java for some projects especially any kind of data analysis or machine learning work using numpy, pandas and matplotlib.
However as a programmer, you're always looking for the next great language that will solve all the world's problems or at least your own and that includes Clojure, Kotlin, Groovy, Scala and Julia.
https://en.wikipedia.org/wiki/Clojurehttps://en.wikipedia.org/wiki/Kotlin_(programming_language)https://en.wikipedia.org/wiki/Apache_Groovyhttps://en.wikipedia.org/wiki/Julia_(programming_language)https://en.wikipedia.org/wiki/Scala_(programming_language)
and to see these languages in action:
https://en.wikipedia.org/wiki/Rosetta_Code
http://rosettacode.org/wiki/Rosetta_Code
Lastly, many scientific projects use Matlab as the language of choice and it too has evolved to use OO methodology although you can still write plain old function and workspace based MATLAB code.
https://www.mathworks.com/discovery/object-oriented-programming.html