Output of file : MOHRS.CPP contained in archive : MOHRS.ZIP
//==============================================================================
/*program name - Mohrs.cpp/
/*Randall F. Dartt - 2/4/94*/
/* Solves for roots of general cubic equation for triaxial principle stresses*/
// solution source: Handbook of Engineering Fundamentals,
// Ovid C.Eshbach and Mott Souders,
// Wiley Engineering Handbook Series, John Wiley And Sons,
// New York, 1975
// =============================================================================
#include
#include
#include
#include
#include
#include
#include
//==============================================================================
//function definitions
#ifdef __cplusplus

double max (double value1, double value2);

double max(double value1, double value2)
{
return ( (value1 > value2) ? value1 : value2);
}

#endif
#ifdef __cplusplus

double min (double value1, double value2);

double min(double value1, double value2)
{
return ( (value1 < value2) ? value1 : value2);
}

#endif

//==============================================================================
// root calculation
//==============================================================================
double sx,sy,sz,txy,tyz,tzx;
double sigma1,sigma2,sigma3;
double b,c,d,q,r,s1,s2,y1,y2,y3,crit,w,pwr,sum1,sum2;
complex I,x1,x2,x3;
double max (double,double);
double min (double,double);
void clrscr(void);

double main(void)
{
//==============================================================================
/*INITIALIZE ALL VALUES BEFORE CALCULATION*/
//==============================================================================

double x = -3.0, y =0; /*imaginary and real parts of (-3)*/
sx=0,sy=0, sz=0, txy=0,tyz=0,tzx=0; /*set input values to zero*/

//==============================================================================
//get input stresses from operator
//==============================================================================

printf("enter sx ....");
scanf("%lf",&sx);
printf("enter sy ....");
scanf("%lf",&sy);
printf("enter sz ....");
scanf("%lf",&sz);
printf("enter tau_xy....");
scanf("%lf",&txy);
printf("enter tau_yz....");
scanf("%lf",&tyz);
printf("enter tau_zx....");
scanf("%lf",&tzx);
clrscr();
//==============================================================================
//Initialize remaining variables
//==============================================================================

b=0,c=0,d=0,q=0,r=0; /*equation coeficients*/
sigma1=0,sigma2=0,sigma3=0; /*principle stresses*/
s1=0,s2=0,y1=0,y2=0,y3=0,I=0; /*intermediate calclulation variables*/
w=0,x1=0,x2=0,x3=0;
pwr=1.0/3.0;
sum1=0,sum2=0;
crit=0;

//==============================================================================
//perform root finding calculation
//==============================================================================

complex z = complex(x,y); /*define imaginary value(z) for (-3)*/
I = sqrt(z); /*I=sqrt(-3)*/
b=-(sx+sy+sz)/3; /*calculate coefficients*/
c=(sx*sy+sx*sz+sy*sz-pow(txy,2)-pow(tyz,2)-pow(tzx,2))/3;
d=-(sx*sy*sz+2*txy*tyz*tzx-sx*pow(tyz,2)-sy*pow(tzx,2)-sz*pow(txy,2));
q=c-pow(b,2);
r=(3*b*c-d)/2-pow(b,3);
crit=pow(q,3)+pow(r,2); /*calculate evaluation criteria*/
cout << "q=" << q;
cout << ", r=" << r << "\n\n";
cout << "Evaluation criteria = q^3+r^2 = " << crit << "\n Is criteria >=0 ?\n ANSWER:";

if(crit>=0) /*if criteria >=0, compute roots using*/
{ /*Eshbach algebraic method*/
cout << " Yes, calculation is using algebraic solution. \n\n";
w=sqrt(crit);
sum1=(r+w);
sum2=(r-w);
if(sum1>=0)
s1=pow(sum1,pwr);
else
s1=-(pow(-sum1,pwr));
if(sum2>=0)
s2=pow(sum2,pwr);
else
s2=-(pow(-sum2,pwr));

x1=(s1+s2-b);
x2=-((s1+s2)/2)+(I/2*(s1-s2))-b;
x3=-((s1+s2)/2)-(I/2*(s1-s2))-b;
if(fabs(real(x1))<.000005) /*set very small root values to zero*/
x1=0.0;
if(fabs(real(x2))<.000005)
x2=0.0;
if(fabs(real(x3))<.000005)
x3=0.0;
}

else /*if criteria <0 use,Eshbach trigonometric method*/
{
cout << " No, calculation is using trigonometric solution. \n\n";
if(q<0)
{
if(r<=0)
{
y1=-2*sqrt(-q)*cos((acos(-r/sqrt(pow(-q,3))))/3);
y2=-2*sqrt(-q)*cos((acos(-r/sqrt(pow(-q,3)))+(2*M_PI))/3);
y3=-2*sqrt(-q)*cos((acos(-r/sqrt(pow(-q,3)))+(4*M_PI))/3);
}
else
{
y1=2*sqrt(-q)*cos((acos(r/sqrt(pow(-q,3))))/3);
y2=2*sqrt(-q)*cos((acos(r/sqrt(pow(-q,3)))+(2*M_PI))/3);
y3=2*sqrt(-q)*cos((acos(r/sqrt(pow(-q,3)))+(4*M_PI))/3);
}
}
else
{
cout << "criteria <0 & q>0 \n; solution not valid";
}
x1=y1-b;
x2=y2-b;
x3=y3-b;
if(fabs(real(x1))<0.000005) /*set very small root values to zero*/
x1=0.0;
if(fabs(real(x2))<0.000005)
x2=0.0;
if(fabs(real(x3))<0.000005)
x3=0.0;
}
//==============================================================================
//sort roots in descending order
//==============================================================================

double max (double, double);
double min (double, double);
sigma1=max((real(x1)),(real(x2)));
sigma1=max((sigma1),(real(x3)));
sigma2=min((real(x1)),(real(x2)));
sigma2=max((sigma2),(real(x3)));
sigma3=min((real(x1)),(real(x2)));
sigma3=min((sigma3),(real(x3)));

//==============================================================================
//Output results to screen
//==============================================================================

cout << "Input Values: \n";
cout << " sx = " << sx;
cout << ", sy = " << sy;
cout << ", sz = " << sz;
cout << ", txy = " << txy;
cout << ", tzx = " << tzx;
cout << ", tyz = " << tyz << "\n\n";
cout << "Poloynomial Coefficients:\n b = " < cout << ", c = " << c;
cout << ", d = " << d << "\n\n";
cout << "Intermediate Roots: \n s1 = " << s1;
cout << ", s2 = " << s2 << "\n";
cout << " y1 = " << y1;
cout << ", y2 = " << y2;
cout << ", y3 = " << y3 << "\n\n";
cout << "SOLUTIONS: \n x1 = " << real(x1);
cout << ", x2 = " << real(x2);
cout << " x3 = " << real(x3) << "\n\n";
cout << " SIGMA 1 = " << sigma1;
cout << ", SIGMA 2 = " << sigma2;
cout << ", SIGMA 3 = " << sigma3;
return 0;
}

### 3 Responses to “Category : Science and EducationArchive   : MOHRS.ZIPFilename : MOHRS.CPP”

1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/