- #1
madtraveller
- 28
- 0
Homework Statement
Dear physics forum,
I'm start learning C++ and I'm trying to program a space transform function for linearized convection-diffusion problem using Code:block
Everything was fine until I wrote calculated result into a file. When I run the program, some error message popped up which related to "memory could not be read" and std:stream::sentry::sentry() in debug windows
Code:
#include <iostream>
#include <fstream> // for working with file
#include <cmath>
#include <iomanip>
#include <stdio.h>
#include <math.h>
#include <cstdlib> // for exit function
using namespace std;
// Function for calculating exponental part of transfer function //
double expo(double dx, double celerity, double time, double dif)
{
double result;
result = -(dx - celerity * time );
result = exp ( pow (result, 2.0) / ( 4.0 * dif * time ));
return (result);
}
// Main program
int main ()
{
// Creat a file to store the result
ofstream outdata;
outdata.open ("Result.txt", ios::out);
if( !outdata ) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);
}
outdata << "OK??" << endl;
// Declare variables
double PI = 3.14159,
u = 1.5,
c,
D = 50.0,
diffu = 0.0,
t,
t0 = 0.0,
deltat = 1.0,
deltax = 0.5;
// Introduce number of interval
int n = 50;
// Create an array to store results of transfer function
double transfer[50] = {0.0};
// Approximate c by u
c = 5.0/3.0 * u;
cout << "Time" << setw (21) << "Transfer function" << endl;
// Loop for calculating transfer function
for (int i = 1; i <= n; i++) {
t = t0 + deltat * i;
transfer[i] = deltax / ( 2.0 * sqrt( PI * D * t )); // Calculate convection part
diffu = expo(deltax, c, t, D); // Calculate diffusion part
transfer[i] = transfer [i] * diffu; // Put 2 parts together
cout << setw(2) << t << setw(18) << transfer[i] << endl; // Print out results
}
for (int i = 1; i <= n; ++i) {
outdata << transfer[i] << endl;
}
outdata.close();
// Terminate the program
return 0;
}
Homework Equations
T(t,deltax) = [tex]\frac{deltax}{2\sqrt{\Pi*D*t^{3}}}*e^{-\frac{(deltax - c*t)^2}{4*D*t}[/tex]
Plz help me with this. Thank you very much for your time
Regards,
madtraveller