Fall Semester 2000
This problem is an introduction to classes in C++.
This exercise is divided into a fair number of parts, but each part requires not more than a few lines of code at most. Hint: Do the Lab exercise on C++ classes before attempting this exercise.
vec3d v; vec3d u(1,2,3);
are both valid instantiations where x has uninitialized members
and u has (x,y,z) set to (1,2,3).
v ^ w * u;
where we know that the cross product should be done before the dot
product (since you aren' allowed to take the cross product of a vector and a
scalar). The compiler does not know this. It looks to the "precedence"
of the "*" and "^" operators and it turns out that "*" has a higher
precedence, just as it has a higher precedence over the "+" and "-"
for scalar math. Thus the dot product will be taken before the
cross product and the above syntax may not get passed the compiler.
To make sure you get the order of the operations correct, when in doubt,
use parentheses: try "(v^w)*u".
vec3d z(1,1,1); cout << z.norm()
should print out the square root of 3, in double precision.
Between the lab exercise and the vec3d_extras.h, you will see examples of how and where to overload the various operators. (Again, please go through the lab exercise AND please look at the header-extras!) One possibility (not necessarily the best for all situations...[long story?]) is to overload all the "op-assign" operators (+=, *=, -=, /=) inside your class def, and to overload others just below the def (+, -, *, /, ^). (Reminder: The mult operator will have to be overloaded three times: for vec3d*double, double*vec3d, and vector-dot-product, vec3d*vec3d). It makes sense to put your member function norm() inside the class def.
Submit your file vec3d.h -- please do not submit a ".c" file! (You may wish to complete the next exercise before handing in your header file, since the next exercise is intended to demonstrate that your header does what it is supposed to....)
In a file called testvec3d.c (just ".c", not ".cc" or ".C"), write code to work with vectors
v = (1,1,1) and u = (-1,2,3).
Put the statement
#include "vec3d.h"near the top of your code by any other "#include <...>" statements you deem necessary (before main(), anyway). This will cause your work from the previous exercise to be read in as if it were part of your new program. Have your code print out something of exactly this form:
v: (1,1,1) u: (-1,2,3) v+u: (XXX,XXX,XXX) v-u: (XXX,XXX,XXX) v*u: XXX 2*v*u: XXX v^u: (XXX,XXX,XXX)where XXX values are to be calculated within your code.
Caution! because of "operator precedence" rules, when you print out the cross product, be sure to use parentheses, "(v^u)", in your "cout" statement. The "^" operator has lower precedence than "<<", so without the parentheses, "^" will be executed after the "<<" -- not what you meant. (A better choice for the cross product operator would have been "%", since it has the same precedence as "*" and "/".)
Write a C++ code, electric.c to calculate the electric field from a set of point charges. Include your header file from exercise 1 just as you did in exercise 2. The point charges should be located at (x,y,z) coordinates
(0.5,0.15,0.1), (0.15,0.5,0.1), (0.5,0.85,0.1), (0.85,0.5,0.1);(for corners of a square lying in the z=0.1 plane). The charges on these points are 1,-1,1,-1, respectively. The units are such that the electric potential from a unit charge is 1/r, where r is the radial separation from the charge.
Have your code loop over a 16x16 grid of points spanning the unit square with corners at (x,y)=(0,0) and (1,1) in the z=0 plane. Print out the x-y coordinates of each grid point and the x-y components of the electric field at that point to cout, so that
g++ electric.c -o electric electric > electric.datwill produce a four column ascii data file electric.dat. (Remember, use the redirect ">!" to overwrite the target file if you run "electric" as above more than once.)
Note that while you are printing out only quantities in the z=0 plane, this is not strictly a 2D problem. The charges themselves do not lie in this plane -- hence there are no singularities to worry about in the field calculations. Also, take full advantage of your vec3d class: for example, to calculate the x-component of a unit vector parallel to some other vector "r", simply write "r.x/r.norm()" (the x-component of "r" divided by the norm of "r").
To view your electric field data, copy the file ~p5720/src/electric.sm to your working directory and "execute electric.sm" within the "sm" plotting utility. Have "sm" dump output to a postscript file, "electric.ps" and invoke the un*x utility
pstojpeg electric.ps
This will create a JPEG file "electric.jpeg", an image which can
be viewed by "gimp", "xv", or your web browser.
Or, perhaps better, try dumping sm output to a pbm (pixel bit-map) file
("device pbm electric.pbm"), then use
pbmtopgm 1 1 electric.pbm | cjpeg > ! electric.jpeg
This may give you a better-looking image.
Please submit your source code, electric.c and the image file electric.jpeg.
Files to be submitted for this assignment:
vec3d.h
testvec.c
electric.c
electric.jpeg