-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathTestData.java
More file actions
169 lines (156 loc) · 5.65 KB
/
TestData.java
File metadata and controls
169 lines (156 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* 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.display2d;
/**
* Title: TestData
* Description: Static methods to generate test data for various programs.
* Copyright: Copyright (c) 2005 Gould, Christian, and Tobochnik
* @author Wolfgang Christian
* @version 1.0
*/
public class TestData {
/**
* Dipole distribution with poles at +1 and -1 on the x axis.
*/
public static double dipole(double x, double y) {
double r1 = Math.sqrt((x-1)*(x-1)+y*y);
double r2 = Math.sqrt((x+1)*(x+1)+y*y);
if((r1==0)||(r2==0)) {
return 0.0; // potential is undefined at r=0.
}
return 1/r1-1/r2;
}
/**
* A 2D Gaussian distribution function centered at x=0 and y=0 and standard deviation of 1.
*/
public static double gaussian(double x, double y, double sigma) {
double rSqr = x*x+y*y;
return Math.exp(-rSqr/sigma/sigma/2);
}
/**
* Saddle function centered at x=0 and y=0.
*/
public static double saddle(double x, double y) {
double rSqr = x*x-y*y;
return Math.exp(rSqr);
}
/**
* Generate a dipole scalar field.
*/
public static double[][][] dipoleScalarField(GridPointData pointdata) {
double[][][] data = pointdata.getData();
for(int i = 0, mx = data.length; i<mx; i++) {
for(int j = 0, my = data[0].length; j<my; j++) {
data[i][j][2] = dipole(data[i][j][0], data[i][j][1]);
}
}
return data;
}
/**
* Generate a gaussian scalar field.
*/
public static double[][][] gaussianScalarField(GridPointData pointdata) {
double[][][] data = pointdata.getData();
for(int i = 0, mx = data.length; i<mx; i++) {
for(int j = 0, my = data[0].length; j<my; j++) {
data[i][j][2] = gaussian(data[i][j][0], data[i][j][1], 1);
}
}
return data;
}
/**
* Generate a random scalar field.
*/
public static void randomScalarField(GridPointData pointdata) {
double[][][] data = pointdata.getData();
for(int i = 0, mx = data.length; i<mx; i++) {
for(int j = 0, my = data[0].length; j<my; j++) {
data[i][j][2] = Math.random();
}
}
}
/**
* Dipole field with poles at +1 and -1 on the x axis. Field strength is
* proportional to 1/r.
*/
public static double[] dipoleVector(double x, double y) {
double[] vec = new double[2];
double r1 = Math.sqrt((x-1)*(x-1)+y*y);
double r2 = Math.sqrt((x+1)*(x+1)+y*y);
if((r1==0)||(r2==0)) {
return vec; // potential is undefined at r=0.
}
vec[0] = (x-1)/r1/r1-(x+1)/r2/r2;
vec[1] = y/r1/r1-y/r2/r2;
return vec;
}
public static double[][][] circulatingVectorField(int nx, int ny) {
double[][][] data = new double[nx][ny][5]; // x, y, horizontal, vertical, color
double xmin = -10, xmax = 10;
double ymin = -10, ymax = 10;
double x = xmin, y = ymin;
double stepx = (xmax-xmin)/(nx-1);
double stepy = (ymax-ymin)/(ny-1);
for(int i = 0; i<nx; i++) {
for(int j = 0; j<ny; j++) {
data[i][j][0] = x; // x location of field calculation
data[i][j][1] = y; // y location of field calculation
double r = Math.sqrt(x*x+y*y);
data[i][j][2] = -y/r; // x length
data[i][j][3] = x/r; // y length
data[i][j][4] = r; // magnitude of the field
y += stepy;
}
y = ymin;
x += stepx;
}
return data;
}
public static double[][][] dipoleVectorField(int nx, int ny, double xmin, double xmax, double ymin, double ymax) {
double[][][] data = new double[nx][ny][5]; // x, y, horizontal, vertical, magnitude
double x = xmin, y = ymin;
double stepx = (xmax-xmin)/(nx-1);
double stepy = (ymax-ymin)/(ny-1);
for(int i = 0; i<nx; i++) {
for(int j = 0; j<ny; j++) {
data[i][j][0] = x; // x location of field calculation
data[i][j][1] = y; // y location of field calculation
double[] vec = dipoleVector(x, y);
double mag = Math.sqrt(vec[0]*vec[0]+vec[1]*vec[1]);
data[i][j][2] = vec[0]/mag; // x direction cosine
data[i][j][3] = vec[1]/mag; // y direction cosine
data[i][j][4] = mag; // magnitude of the field
y += stepy;
}
y = ymin;
x += stepx;
}
return data;
}
}
/*
* 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
*/