PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/python/examples/java/x09.java

#
Java | 436 lines | 246 code | 70 blank | 120 comment | 21 complexity | f2dcd9a653f35d92827c1abc4496f5b8 MD5 | raw file
Possible License(s): LGPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, Apache-2.0, GPL-2.0
  1. //---------------------------------------------------------------------------//
  2. // $Id: x09.java 6815 2006-07-19 07:13:56Z andrewross $
  3. //---------------------------------------------------------------------------//
  4. //---------------------------------------------------------------------------//
  5. // Copyright (C) 2001, 2002 Geoffrey Furnish
  6. // Copyright (C) 2002, 2003 Alan W. Irwin
  7. // Copyright (C) 2004 Andrew Ross
  8. //
  9. // This file is part of PLplot.
  10. //
  11. // PLplot is free software; you can redistribute it and/or modify
  12. // it under the terms of the GNU Library General Public License as published by
  13. // the Free Software Foundation; version 2 of the License.
  14. //
  15. // PLplot is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU Library General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU Library General Public License
  21. // along with PLplot; if not, write to the Free Software
  22. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. //---------------------------------------------------------------------------//
  24. //---------------------------------------------------------------------------//
  25. // Implementation of PLplot example 9 in Java.
  26. //---------------------------------------------------------------------------//
  27. package plplot.examples;
  28. import plplot.core.*;
  29. import java.lang.Math;
  30. class x09 {
  31. static final int XPTS = 35;
  32. static final int YPTS = 46;
  33. static final double XSPA = 2./(XPTS-1);
  34. static final double YSPA = 2./(YPTS-1);
  35. // polar plot data
  36. static final int PERIMETERPTS = 100;
  37. static final int RPTS = 40;
  38. static final int THETAPTS = 40;
  39. // potential plot data
  40. static final int PPERIMETERPTS = 100;
  41. static final int PRPTS = 40;
  42. static final int PTHETAPTS = 64;
  43. static final int PNLEVEL = 20;
  44. final double clevel[] = {-1., -.8, -.6, -.4, -.2, 0, .2, .4, .6, .8, 1.};
  45. // Transformation function
  46. final double tr[] = {XSPA, 0.0, -1.0, 0.0, YSPA, -1.0};
  47. PLStream pls = new PLStream();
  48. // State data used by f2mnmx
  49. double fmin, fmax;
  50. // Does a large series of unlabelled and labelled contour plots.
  51. public static void main( String[] args )
  52. {
  53. new x09( args );
  54. }
  55. public x09( String[] args )
  56. {
  57. int i, j;
  58. double[][] xg0 = new double[XPTS][YPTS];
  59. double[][] yg0 = new double[XPTS][YPTS];
  60. double[][] xg1 = new double[XPTS][YPTS];
  61. double[][] yg1 = new double[XPTS][YPTS];
  62. double[][] xg2 = new double[XPTS][YPTS];
  63. double[][] yg2 = new double[XPTS][YPTS];
  64. double[][] z = new double[XPTS][YPTS];
  65. double[][] w = new double[XPTS][YPTS];
  66. double xx, yy, argx, argy, distort;
  67. final int[] mark = {1500};
  68. final int[] space = {1500};
  69. final int[] mark0 = {};
  70. final int[] space0 = {};
  71. // Parse and process command line arguments.
  72. pls.parseopts( args, PLStream.PL_PARSE_FULL | PLStream.PL_PARSE_NOPROGRAM );
  73. /* Initialize plplot */
  74. pls.init();
  75. /* Set up function arrays */
  76. for (i = 0; i < XPTS; i++) {
  77. xx = (double) (i - (XPTS / 2)) / (double) (XPTS / 2);
  78. for (j = 0; j < YPTS; j++) {
  79. yy = (double) (j - (YPTS / 2)) / (double) (YPTS / 2) - 1.0;
  80. z[i][j] = xx * xx - yy * yy;
  81. w[i][j] = 2 * xx * yy;
  82. }
  83. }
  84. /* Set up grids */
  85. for (i = 0; i < XPTS; i++) {
  86. for (j = 0; j < YPTS; j++) {
  87. // Replacement for mypltr of x09c.c
  88. xx = tr[0] * i + tr[1] * j + tr[2];
  89. yy = tr[3] * i + tr[4] * j + tr[5];
  90. argx = xx * Math.PI/2;
  91. argy = yy * Math.PI/2;
  92. distort = 0.4;
  93. // Note these are one-dimensional because of arrangement of
  94. // zeros in the final tr definition above.
  95. // But I haven't found out yet, how with swig to overload
  96. // one- and two-dimensional array arguments so for now make
  97. // xg0 --> yg1 two-dimensional.
  98. xg0[i][j] = xx;
  99. yg0[i][j] = yy;
  100. xg1[i][j] = xx + distort * Math.cos(argx);
  101. yg1[i][j] = yy - distort * Math.cos(argy);
  102. xg2[i][j] = xx + distort * Math.cos(argx) * Math.cos(argy);
  103. yg2[i][j] = yy - distort * Math.cos(argx) * Math.cos(argy);
  104. }
  105. }
  106. // Plot using scaled identity transform used to create xg0 and yg0
  107. /* pls.setcontlabelparam(0.006, 0.3, 0.1, 0);
  108. pls.env(-1.0, 1.0, -1.0, 1.0, 0, 0);
  109. pls.col0(2);
  110. pls.cont( z, 1, XPTS, 1, YPTS, clevel, xg0, yg0 );
  111. pls.styl(mark, space);
  112. pls.col0(3);
  113. pls.cont(w, 1, XPTS, 1, YPTS, clevel, xg0, yg0 );
  114. pls.styl(mark0, space0);
  115. pls.col0(1);
  116. pls.lab("X Coordinate", "Y Coordinate", "Streamlines of flow");
  117. */
  118. pls.setcontlabelparam(0.006, 0.3, 0.1, 1);
  119. pls.env(-1.0, 1.0, -1.0, 1.0, 0, 0);
  120. pls.col0(2);
  121. pls.cont(z, 1, XPTS, 1, YPTS, clevel, xg0, yg0 );
  122. pls.styl(mark, space);
  123. pls.col0(3);
  124. pls.cont(w, 1, XPTS, 1, YPTS, clevel, xg0, yg0 );
  125. pls.styl(mark0, space0);
  126. pls.col0(1);
  127. pls.lab("X Coordinate", "Y Coordinate", "Streamlines of flow");
  128. pls.setcontlabelparam(0.006, 0.3, 0.1, 0);
  129. // Plot using 1d coordinate transform
  130. pls.env(-1.0, 1.0, -1.0, 1.0, 0, 0);
  131. pls.col0(2);
  132. pls.cont(z, 1, XPTS, 1, YPTS, clevel, xg1, yg1 );
  133. pls.styl(mark, space);
  134. pls.col0(3);
  135. pls.cont(w, 1, XPTS, 1, YPTS, clevel, xg1, yg1) ;
  136. pls.styl(mark0, space0);
  137. pls.col0(1);
  138. pls.lab("X Coordinate", "Y Coordinate", "Streamlines of flow");
  139. /* pls.setcontlabelparam(0.006, 0.3, 0.1, 1);
  140. pls.env(-1.0, 1.0, -1.0, 1.0, 0, 0);
  141. pls.col0(2);
  142. pls.cont(z, 1, XPTS, 1, YPTS, clevel, xg1, yg1 );
  143. pls.styl(mark, space);
  144. pls.col0(3);
  145. pls.cont(w, 1, XPTS, 1, YPTS, clevel, xg1, yg1 );
  146. pls.styl(mark0, space0);
  147. pls.col0(1);
  148. pls.lab("X Coordinate", "Y Coordinate", "Streamlines of flow");
  149. pls.setcontlabelparam(0.006, 0.3, 0.1, 0);
  150. */
  151. // Plot using 2d coordinate transform
  152. pls.env(-1.0, 1.0, -1.0, 1.0, 0, 0);
  153. pls.col0(2);
  154. pls.cont(z, 1, XPTS, 1, YPTS, clevel, xg2, yg2 );
  155. pls.styl(mark, space);
  156. pls.col0(3);
  157. pls.cont(w, 1, XPTS, 1, YPTS, clevel, xg2, yg2 );
  158. pls.styl(mark0, space0);
  159. pls.col0(1);
  160. pls.lab("X Coordinate", "Y Coordinate", "Streamlines of flow");
  161. /* pls.setcontlabelparam(0.006, 0.3, 0.1, 1);
  162. pls.env(-1.0, 1.0, -1.0, 1.0, 0, 0);
  163. pls.col0(2);
  164. pls.cont(z, 1, XPTS, 1, YPTS, clevel, xg2, yg2 );
  165. pls.styl(mark, space);
  166. pls.col0(3);
  167. pls.cont(w, 1, XPTS, 1, YPTS, clevel, xg2, yg2 );
  168. pls.styl(mark0, space0);
  169. pls.col0(1);
  170. pls.lab("X Coordinate", "Y Coordinate", "Streamlines of flow");
  171. pls.setcontlabelparam(0.006, 0.3, 0.1, 0);
  172. */
  173. polar();
  174. /* pls.setcontlabelparam(0.006, 0.3, 0.1, 1);
  175. polar();
  176. pls.setcontlabelparam(0.006, 0.3, 0.1, 0);
  177. */
  178. potential();
  179. /* pls.setcontlabelparam(0.006, 0.3, 0.1, 1);
  180. potential();
  181. pls.setcontlabelparam(0.006, 0.3, 0.1, 0);
  182. */
  183. pls.end();
  184. }
  185. void polar()
  186. // polar contour plot example.
  187. {
  188. int i,j;
  189. double[] px = new double[PERIMETERPTS];
  190. double[] py = new double[PERIMETERPTS];
  191. double[][] xg = new double[RPTS][THETAPTS];
  192. double[][] yg = new double[RPTS][THETAPTS];
  193. double[][] z = new double[RPTS][THETAPTS];
  194. double t, r, theta;
  195. double [] lev = new double[10];
  196. pls.env(-1., 1., -1., 1., 0, -2);
  197. pls.col0(1);
  198. // Perimeter
  199. for (i = 0; i < PERIMETERPTS; i++) {
  200. t = (2.*Math.PI/(PERIMETERPTS-1))*(double)i;
  201. px[i] = Math.cos(t);
  202. py[i] = Math.sin(t);
  203. }
  204. pls.line(px, py);
  205. // Create data to be contoured.
  206. for (i = 0; i < RPTS; i++) {
  207. r = i/(double)(RPTS-1);
  208. for (j = 0; j < THETAPTS; j++) {
  209. theta = (2.*Math.PI/(double)(THETAPTS-1))*(double)j;
  210. xg[i][j] = r*Math.cos(theta);
  211. yg[i][j] = r*Math.sin(theta);
  212. z[i][j] = r;
  213. }
  214. }
  215. for (i = 0; i < 10; i++) {
  216. lev[i] = 0.05 + 0.10*(double) i;
  217. }
  218. pls.col0(2);
  219. pls.cont( z, 1, RPTS, 1, THETAPTS, lev, xg, yg );
  220. pls.col0(1);
  221. pls.lab("", "", "Polar Contour Plot");
  222. }
  223. // Compute min and max value of a 2-d array.
  224. void f2mnmx( double[][] f, int nx, int ny )
  225. {
  226. fmax = f[0][0];
  227. fmin = fmax;
  228. for( int i=0; i < nx; i++ )
  229. for( int j=0; j < ny; j++ ) {
  230. if (f[i][j] < fmin) fmin = f[i][j];
  231. if (f[i][j] > fmax) fmax = f[i][j];
  232. }
  233. }
  234. final void potential()
  235. // Shielded potential contour plot example.
  236. {
  237. int i,j;
  238. double rmax, xmin, xmax, x0, ymin, ymax, y0, zmin, zmax;
  239. double peps, xpmin, xpmax, ypmin, ypmax;
  240. double eps, q1, d1, q1i, d1i, q2, d2, q2i, d2i;
  241. double div1, div1i, div2, div2i;
  242. double [][] xg = new double[PRPTS][PTHETAPTS] ;
  243. double [][] yg = new double[PRPTS][PTHETAPTS] ;
  244. double [][] z = new double[PRPTS][PTHETAPTS] ;
  245. int nlevelneg, nlevelpos;
  246. double dz, clev;
  247. double [] clevelneg_store = new double[PNLEVEL];
  248. double [] clevelpos_store = new double[PNLEVEL];
  249. int ncollin, ncolbox, ncollab;
  250. double [] px = new double[PPERIMETERPTS];
  251. double [] py = new double[PPERIMETERPTS];
  252. double t, r, theta;
  253. // Create data to be contoured.
  254. //java wants r unambiguously initialized for rmax below.
  255. r = 0.;
  256. for (i = 0; i < PRPTS; i++) {
  257. r = 0.5 + (double) i;
  258. for (j = 0; j < PTHETAPTS; j++) {
  259. theta = (2.*Math.PI/(double)(PTHETAPTS-1))*(0.5 + (double) j);
  260. xg[i][j] = r*Math.cos(theta);
  261. yg[i][j] = r*Math.sin(theta);
  262. }
  263. }
  264. rmax = r;
  265. f2mnmx( xg, PRPTS, PTHETAPTS );
  266. xmin = fmin;
  267. xmax = fmax;
  268. f2mnmx( yg, PRPTS, PTHETAPTS );
  269. ymin = fmin;
  270. ymax = fmax;
  271. x0 = (xmin + xmax)/2.;
  272. y0 = (ymin + ymax)/2.;
  273. // Expanded limits
  274. peps = 0.05;
  275. xpmin = xmin - Math.abs(xmin)*peps;
  276. xpmax = xmax + Math.abs(xmax)*peps;
  277. ypmin = ymin - Math.abs(ymin)*peps;
  278. ypmax = ymax + Math.abs(ymax)*peps;
  279. // Potential inside a conducting cylinder (or sphere) by method of images.
  280. // Charge 1 is placed at (d1, d1), with image charge at (d2, d2).
  281. // Charge 2 is placed at (d1, -d1), with image charge at (d2, -d2).
  282. // Also put in smoothing term at small distances.
  283. eps = 2.;
  284. q1 = 1.;
  285. d1 = rmax/4.;
  286. q1i = - q1*rmax/d1;
  287. d1i = Math.pow(rmax,2)/d1;
  288. q2 = -1.;
  289. d2 = rmax/4.;
  290. q2i = - q2*rmax/d2;
  291. d2i = Math.pow(rmax,2)/d2;
  292. for (i = 0; i < PRPTS; i++) {
  293. for (j = 0; j < PTHETAPTS; j++) {
  294. div1 = Math.sqrt(Math.pow(xg[i][j]-d1,2) + Math.pow(yg[i][j]-d1,2) + Math.pow(eps,2));
  295. div1i = Math.sqrt(Math.pow(xg[i][j]-d1i,2) + Math.pow(yg[i][j]-d1i,2) + Math.pow(eps,2));
  296. div2 = Math.sqrt(Math.pow(xg[i][j]-d2,2) + Math.pow(yg[i][j]+d2,2) + Math.pow(eps,2));
  297. div2i = Math.sqrt(Math.pow(xg[i][j]-d2i,2) + Math.pow(yg[i][j]+d2i,2) + Math.pow(eps,2));
  298. z[i][j] = q1/div1 + q1i/div1i + q2/div2 + q2i/div2i;
  299. }
  300. }
  301. f2mnmx( z, PRPTS, PTHETAPTS );
  302. zmin = fmin;
  303. zmax = fmax;
  304. /* printf("%.15g %.15g %.15g %.15g %.15g %.15g %.15g %.15g \n",
  305. q1, d1, q1i, d1i, q2, d2, q2i, d2i);
  306. System.out.println(xmin);
  307. System.out.println(xmax);
  308. System.out.println(ymin);
  309. System.out.println(ymax);
  310. System.out.println(zmin);
  311. System.out.println(zmax); */
  312. // Positive and negative contour levels.
  313. dz = (zmax-zmin)/(double) PNLEVEL;
  314. nlevelneg = 0;
  315. nlevelpos = 0;
  316. for (i = 0; i < PNLEVEL; i++) {
  317. clev = zmin + ((double) i + 0.5)*dz;
  318. if (clev <= 0.)
  319. clevelneg_store[nlevelneg++] = clev;
  320. else
  321. clevelpos_store[nlevelpos++] = clev;
  322. }
  323. // Colours!
  324. ncollin = 11;
  325. ncolbox = 1;
  326. ncollab = 2;
  327. // Finally start plotting this page!
  328. pls.adv(0);
  329. pls.col0(ncolbox);
  330. pls.vpas(0.1, 0.9, 0.1, 0.9, 1.0);
  331. pls.wind(xpmin, xpmax, ypmin, ypmax);
  332. pls.box("", 0., 0, "", 0., 0);
  333. pls.col0(ncollin);
  334. if(nlevelneg >0) {
  335. // Negative contours
  336. pls.lsty(2);
  337. // The point here is to copy results into an array of the correct size
  338. // which is essential for the java wrapper of plplot to work correctly.
  339. double [] clevelneg = new double[nlevelneg];
  340. System.arraycopy(clevelneg_store, 0, clevelneg, 0, nlevelneg);
  341. pls.cont( z, 1, PRPTS, 1, PTHETAPTS, clevelneg, xg, yg );
  342. }
  343. if(nlevelpos >0) {
  344. // Positive contours
  345. pls.lsty(1);
  346. double [] clevelpos = new double[nlevelpos];
  347. // The point here is to copy results into an array of the correct size
  348. // which is essential for the java wrapper of plplot to work correctly.
  349. System.arraycopy(clevelpos_store, 0, clevelpos, 0, nlevelpos);
  350. pls.cont( z, 1, PRPTS, 1, PTHETAPTS, clevelpos, xg, yg );
  351. }
  352. // Draw outer boundary
  353. for (i = 0; i < PPERIMETERPTS; i++) {
  354. t = (2.*Math.PI/(PPERIMETERPTS-1))*(double)i;
  355. px[i] = x0 + rmax*Math.cos(t);
  356. py[i] = y0 + rmax*Math.sin(t);
  357. }
  358. pls.col0(ncolbox);
  359. pls.line(px, py);
  360. pls.col0(ncollab);
  361. pls.lab("", "", "Shielded potential of charges in a conducting sphere");
  362. }
  363. }
  364. //---------------------------------------------------------------------------//
  365. // End of x09.java
  366. //---------------------------------------------------------------------------//