ECE 4122/6122 Lab 1: Electric Field Produced by Array of Point Charges


Category: You will Instantly receive a download link for .zip solution file upon Payment


5/5 - (1 vote)

Category: Custom Classes and Multithreading


To understand and apply the principles of class creation and inheritance in C++ while leveraging
std::thread for parallel calculations in a computationally-intensive problem related to the Electric
Field calculations.


Given an 2D grid of N x M point charges, all with the same charge q, in the x-y plane, centered
around the origin, your task is to compute the magnitude and direction of the electric field at a specific
point (x, y, z) in space due to all the charges.

You will use the formula below to calculate the electric field vector:
𝐸𝐸�⃗ = π‘˜π‘˜ βˆ™ π‘žπ‘ž
π‘Ÿπ‘Ÿ2 π‘Ÿπ‘ŸΜ‚
𝐸𝐸�⃗ is the electric field vector,
π‘žπ‘ž is magnitude of the charge of the point,
π‘˜π‘˜ is Coulomb’s constant = 9Γ—109 Nm2
π‘Ÿπ‘Ÿ is the radial distance between the point charge and the (x, y, z) location in space,
π‘Ÿπ‘ŸΜ‚ is the unit vector from the point charge to the (x, y, z) location in space.

Your code should include the following:
Base Class: ECE_PointCharge in files ECE_PointCharge.h & ECE_PointCharge.cpp
Protected members variables:
β€’ double x; // x-coordinate.
β€’ double y; // y-coordinate.
β€’ double z; // z-coordinate.
β€’ double q; // charge of the point.

Public member functions:
β€’ A constructor that initializes the above member variables.
β€’ A function void setLocation (double x, double y, double z);
β€’ A function void setCharge (double q);

Derived Class: ECE_ElectricField in files ECE_ElectricField.h & ECE_ElectricField.cpp
Inherits from: ECE_PointCharge
Protected additional member variables:
β€’ double Ex; // Electric field in the x-direction.
β€’ double Ey; // Electric field in the y-direction.
β€’ double Ez; // Electric field in the z-direction.

Public member functions:
β€’ A constructor that initializes base class member variables.
β€’ void computeFieldAt(double x, double y, double y);
o Calculates the electric field at the point (x, y, z) due to the charge using the above
formula. Updates the Ex, Ey, Ez member variables.

β€’ void getElectricField(double &Ex, double &Ey, double Ez);


Create an application that uses the above classes and multithreading to calculate the electric field at a point in

β€’ Your program should notify the user how many threads can run concurrently when doing the
β€’ Your program needs to query the user for the size of the N x M array and the charge q.

β€’ Your program should continuously prompt the user if a new calculation is wanted.
β€’ Your program should use multithreading to reduce the processing time taken.
β€’ After calculating the resultant electric field your program should output the Ex, Ey, Ez values and the
resultant electric field strength.

β€’ Your program should also output how long it takes in microseconds from the time the user inputs the
xyz location to just before the results are output to the screen.
β€’ Spaces separate the values when entering multiple inputs.

β€’ Make sure you check for valid inputs
o N and M should be natural numbers.
o Separation distances should be > 0.0 and be valid numerical values
o Charge should be a valid numerical value.
o Point location should be made up of valid numerical values.

β€’ Use multithreading to breakup the array so that each thread does the calculations for part of the array.
β€’ Make sure that your code checks for a location that may be the same as a point charge location.

Sample Program Flow:
οƒ˜ Your computer supports ## concurrent threads.
οƒ˜ Please enter the number of rows and columns in the N x M array: 100 100
οƒ˜ Please enter the x and y separation distances in meters: 0.01 0.03
οƒ˜ Please enter the common charge on the points in micro C: 0.02

οƒ˜ Please enter the location in space to determine the electric field (x y z) in meters: 1.0 2.0 3.0
οƒ˜ The electric field at (1.0, 2.0, 3.0) in V/m is
οƒ˜ Ex = x.xxxx * 10^y
οƒ˜ Ey = x.xxxx * 10^y
οƒ˜ Ez = x.xxxx * 10^y
οƒ˜ |E| = x.xxxx * 10^y

οƒ˜ The calculation took x.xxxx microsec!
οƒ˜ Do you want to enter a new location (Y/N)? Y
οƒ˜ Please enter the location in space to determine the electric field (x y z) in meters: 2.0 2.0 2.0
οƒ˜ The electric field at (2.0, 2.0, 2.0) in V/m is
οƒ˜ Ex = x.xxxx * 10^y
οƒ˜ Ey = x.xxxx * 10^y
οƒ˜ Ez = x.xxxx * 10^y
οƒ˜ |E| = x.xxxx * 10^y

οƒ˜ The calculation took x.xxxx microsec!
οƒ˜ Do you want to enter a new location (Y/N)? N
οƒ˜ Bye!

Turn-In Instructions
Place your files in a zip file called and upload this zip file on the assignment section of Canvas.
Grading Rubric:
If a student’s program runs correctly and produces the desired output, the student has the potential to get a 100
on his or her homework; however, TA’s will look through your code for other elements needed to meet the lab
requirements. The table below shows typical deductions that could occur.

Element Percentage


Does Not Compile 40% Code does not compile on PACE-ICE!
Does Not Match Output Up to 90% The code compiles but does not produce correct outputs.
Runtime and efficiency of
code setup
Up to 20% The code generates the correct output but runs slower than expected.
Clear Self-Documenting

Coding Styles
Up to 25% This can include incorrect indentation, using unclear variable names,
unclear/missing comments, or compiling with warnings. (See
Appendix A)

Element Percentage Deduction Details
Late Deduction Function score – 0.5 * H H = number of hours (ceiling function) passed
Appendix A: Coding Standards

When using if/for/while statements, make sure you indent 4 spaces for the content inside those. Also make
sure that you use spaces to make the code more readable.

For example:
for (int i; i < 10; i++)
j = j + i;

If you have nested statements, you should use multiple indentions. Each { should be on its own line (like the
for loop) If you have else or else if statements after your if statement, they should be on their own line.
for (int i; i < 10; i++)
if (i < 5)
k -= i;
k +=1;
j += i;

Camel Case:
This naming convention has the first letter of the variable be lower case, and the first letter in each new word
be capitalized (e.g. firstSecondThird).

This applies for functions and member functions as well!

The main exception to this is class names, where the first letter should also be capitalized.

Variable and Function Names:
Your variable and function names should be clear about what that variable or function represents. Do not use
one letter variables, but use abbreviations when it is appropriate (for example: β€œimag” instead of
β€œimaginary”). The more descriptive your variable and function names are, the more readable your code will
be. This is the idea behind self-documenting code.

File Headers:
Every file should have the following header at the top
Author: your name
Class: ECE4122 or ECE6122 (section)
Last Date Modified: date

What is the purpose of this file?

Code Comments:
1. Every function must have a comment section describing the purpose of the function, the input and
output parameters, the return value (if any).
2. Every class must have a comment section to describe the purpose of the class.
3. Comments need to be placed inside of functions/loops to assist in the understanding of the flow of
the code.