I have written some code for a photon simulation involving different skin types.
I am using windows visual studio (visual C++) for windows 8 (x86). The code is quite long and it build fine (compiles) but when I run I get an assertion error.
I went to line 1796 in my very long code but I have nothing there but:
1796- cauchyY[i] = p1.getCauchy(yVal[i], getMedianY, 1.0);
(these are vectors that store doubles)
The only other place in my code where I think my cause an issue is where I have used functions involving vectors to do integration (which is used to calculate power).
Function regarding Trapezoid Rule:
double functIntegralTrap() {
int i, j;
double sum = 0;
double integral = 0;
cout << "Please enter the number of subintervals for integration ";
cin >> subIntervals;
vector <double> x(subIntervals + 1);
vector <double> result(subIntervals + 1);
timeDiff = static_cast<double>(getTimeForPower());
termCurr = termVolt / resistance;
phaseDiff = thetaVol - thetaCurr;
intermediary = (termVolt*termCurr) / 2.0;
deltax = timeDiff / static_cast<double>(subIntervals);
for (i = 0; i < subIntervals + 1; i++) {
x.at(i) = lowerTime + i*deltax;
result.at(i) = x.at(i)*intermediary*cos(phaseDiff*PI / 180) + intermediary*cos(phaseDiff*PI / 180)*cos(2.0*getangularfreq()*x.at(i)) - intermediary*sin(phaseDiff*PI / 180)*sin(2.0*getangularfreq()*x.at(i));
}
for (i = 1; i < subIntervals; i++) {
sum = sum + deltax*result.at(i); //need to be careful of this section regarding result.at.i
}
integral = (deltax / 2.0)*(result.at(0) + result.at(subIntervals)) + sum;
cout << "The definite integral is " << integral << endl;
return integral;
}
Function Involving Riemann sums:
double sumRiemannIntegral() {
double cumsum = 0.0;
int i, j;
double integral = 0.0;
cout << "Please enter the number of subintervals (rectangles):";
cin >> subIntervals;
vector <double> x(subIntervals);
vector <double> result(subIntervals);
vector <double> rectangArea(subIntervals);
timeDiff = static_cast<double>(getTimeForPower());
termVolt = 20.0;
resistance = 6.0;
termCurr = termVolt / resistance;
phaseDiff = thetaVol - thetaCurr;
intermediary = (termVolt*termCurr) / 2.0;
deltax = timeDiff / static_cast<double>(subIntervals);
In debug mode the vector class will throw an out_of_range error when your code uses the subscript operator[] to access an element outside the valid range i.e. 0 - size() -1 1796- cauchyY[i] = p1.getCauchy(yVal[i], getMedianY, 1.0);
if cauchyY or yVal are vectors you need to check the value of i and the size of cauchyY and yVal
for (int i = 0; i < p1.current_Phot; i++) {//opening for
xVal[i] = x;
yVal[i] = y;
zVal[i] = z;
}//closing for
for (int i = 0; i < cr1.photonN; i++) {
cauchyY[i] = p1.getCauchy(yVal[i], getMedianY, 1.0);
proposalY[i] = yVal[i] + unifNum1; //unifNum1 is a random number
}
getmedianPropY = p1.CalcMedian(proposalY, cr1.photonN);
for (int i = 0; i < cr1.photonN; i++) {
ratioCauchyY[i] = p1.getCauchy(proposalY[i], getmedianPropY, 1.0) / cauchyY[i];
if (unifNum2 < ratioCauchyY[i]) {
acceptY[i] = yVal[i + 1];
}//if for y
else {
acceptY[i] = yVal[i];
}//else for y
return (1 / PI)*(scaleParam / (pow(Val - median, 2.0)) + pow(scaleParam, 2.0));
} //Here the median acts as the location parameter
double CalcMedian(vector <double> v1, int size) {//function start -(IMPORTANT)
// Allocate an array of the same size and sort it.
vector<double> doub_sorted;
doub_sorted.reserve(sizeof v1);
// double* doub_sorted = new double[size];IMPORTANT (ALLOCATE MEMORY FOR NEW 1D ARRAY THAT STORES DOUBLES)
for (int i = 0; i < size; ++i) {
doub_sorted[i] = v1[i];
}
for (int i = size - 1; i > 0; --i) {
for (int j = 0; j < i; ++j) {
if (doub_sorted[j] > doub_sorted[j + 1]) {
double store = doub_sorted[j];
doub_sorted[j] = doub_sorted[j + 1];//THIS IS TO GET THE NUMBERS OF THE ARRAY IN NUMERICAL (ASCENDING ORDER) ORDER WHICH IS NEEDED TO CALCUALTE THE MEDIAN
doub_sorted[j + 1] = store;
}//end if
}//end for
}//end for
// Middle or average of middle values in the sorted array.
double median_m = 0.0;
if ((size % 2) == 0) {
median_m = (doub_sorted[size / 2] + doub_sorted[(size / 2) - 1]) / 2.0;
}
else {
median_m = doub_sorted[(int)size / 2];//IMPORTANT: CALCULATING THE MEDIAN DEPENDENT ON WHETHER WE HAVE ODD OR EVEN NUMBERS (eg if odd and you have 5 numbers size/2= 2); if even you value
//above and below half-way.
} //IMPORTANT (WHEN CREATING MEMORY DYNAMICALLY WITH NEW YOU NEED TO DELETE THE ARRAY AFTERWARDS (AS FAR AS I KNOW WITHIN METHODS)
return median_m;
}//function end
-END OF CODE BLOCK
JUST SOME MORE INFORMATION IF ANYONE HAS ANY OTHER HELP TO GIVE ME
Consider using the debugger to look at the values you have when running the program, it will save you days of staring at code wondering why it doesn't work.
So you have:
1 2 3 4
for (int i = 0; i < cr1.photonN; i++) {
cauchyY[i] = p1.getCauchy(yVal[i], getMedianY, 1.0);
proposalY[i] = yVal[i] + unifNum1; //unifNum1 is a random number
}
What is the value of cr1.photonN, this is the limit of the for loop, so it's important. Is it bigger than the size of yVal ?
Value of cr1.photonN is 10000 it is to be used as part of a loop to track photon positions in 3D (X, Y AND Z) and in the azimuthal and zenith angles theta and psi.
appears to be referring to the "vector" include file, not your source files. You probably need to back trace with your debugger to somewhere in your code.
I hope it does not mean the vector include file was corrupted during installation of visual studio 2017 community version at the path mentioned on the c drive. Sorry for the slight typo error in spelling... it is Debug Assertion Failed!.
I hope it does not mean the vector include file was corrupted during installation of visual studio 2017 community version at the path mentioned on the c drive.
Not at all, the bug is only in your program. The code in the <vector> justs reports that you did sth. wrong.
No, that is just where the program crashed the actual problem is somewhere before the line indicated. You need to run the program with your debugger. The debugger should be able to let you backtrack from the crash location to a point in your code. Then you can step through the problematic area watching the variables as you step.
If you use STL containers and range based for loops, I suspect a lot of the problems will go away.
Another thing:
1 2 3 4 5 6
for (int i = 0; i < p1.current_Phot; i++) {//opening for
xVal[i] = x;
yVal[i] = y;
zVal[i] = z;
}//closing for
This shows you have 3 separate vectors for the xyz coordinates of points, consider creating a struct to represent a Point. Then have a std::vector of Point.
In C++ a struct is almost the same as a class (it's members are public by default), so you could add constructors, operators etc if you want.
That way you could have one ranged based for loop to process a container, not 3, and not 3 sets of containers everywhere. Also the functions that operate on points can all be in one place - in the struct or class. I am guessing the whole code will be shorter, easier to understand and less error prone.
I know what you mean I read some more on the STL and went on the GeeksForGeeks website where they have a nice STL section involving all the sets, arrays, maps vectors and the functions that come under the include <algorithm> header. The link for range based for loop was good too for getting the indexing right. I am making the modifications too my long code (2000 lines) and will enquire for further information if needed. Thanks for the help from you all.