2016-04-18 11 views
1

私はC++の新しい学習者です。実際には、各時間ステップ(dt = 0.00001)で力、速度、位置、合計時間を調べ、単純な方程式からmyfile1、myfile2、myfile3、myfile4を保存しようとします。私は正確な値を見つけるために問題に直面しています。プログラムを実行すると、セグメンテーションフォールトエラーが検出されます。どうすれば問題を解決できますか?私は以下のプログラムを添付しています:誰でも私を助けることができますか?方程式を解く方法と正しい方法で保存するには?

//C++ programming for selection random number 
#include <cstdlib> 
#include <iostream> 
#include <fstream> 
#include <ctime> 
#include <sstream> 
#include <string> 
#include <iomanip> 
#include <cmath> 


//%stifness 
double kp=1000; 
//%gravitational 
double g=-9.81; 


    double x[10000], y[10000], z[10000]; 
    double Fx[10000],Fy[10000], Fz[10000]; 
    double vx[10000],vy[10000], vz[10000] ; 
    double ax[10000],ay[10000], az[10000] ; 
    double force[10000]; 
    const double PI=4.0*atan(1.0); 

    using namespace std; 
int main() 
{ 
    srand((unsigned) time(NULL)); 
//open output file 
    ofstream myfile1; 
    myfile1.open("1.dat"); 
    ofstream myfile2; 
    myfile2.open("2.dat"); 
    ofstream myfile3; 
    myfile3.open("3.dat"); 
    ofstream myfile4; 
    myfile4.open("4.dat"); 


// %particle parameter 

double dt=1e-5; 
double Ntimestep=1000; 

//particle properties 
    double R=0.003; 
    double Dens=2600; 
     int npart=1; 

//Particle Creation 
     double Mass=(Dens*4*PI*(pow(R,3))/3); //m=(4/3)* Dens*Pi*R^3 
     cout<<"Mass="<< Mass<<endl; 

//initial position of the particle 

     x[0]=0; 
      y[0] =0.01; 
      z[0] =0; 

// movement of the particle 
// Particle initial velocity and forces 
    Fx[0]=0; 
    Fy[0]=0; 
    Fz[0]=0; 
    vx[0]=0; 
    vy[0]=-0.5; 
    vz[0]=0; 
    force[0]=0; 

// Relation between dashport coefficient and coefficient of restitution 
    double exp=0.9; 
    double lnexp=0.10536; 
    double Eta= ((2*sqrt(Mass*kp)*lnexp)/(sqrt(pow(PI,2)+pow(lnexp,2)))); 

//Time step 
    int t=0; 
for (int i=0;i<Ntimestep;i++) 
{ 

// calculate particle to wall contact force 

     if (y[i]<R) 
      { 
       Fy[i]=(-kp*(y[i]-R))-Eta*vy[i]; 
      } 

// Calculate initial acceleration 

     ay[i]=(Fy[i]/Mass)+g; 
//force[i]+=force[1]; 
     force[i+1]=ay[i]*Mass; 
     //cout<<"Total_Force="<<force<<endl; 

// update valocity and displacement/location 

    vy[i+1]=(vy[i]+(ay[i]*dt)); 
    y[i+1]=y[i]+vy[i]*dt+0.5*ay[i]*pow(dt,2); 
    Fy[i]=0; 

    t=t+1; 

    double time=t*dt; 



//...............output/save file.............................. 

cout<<"Total force="<<force<<endl; 
myfile1<<"Total force="<<force<<endl; 

cout<<"velocity="<<vy<<endl; 
myfile2<<"velocity="<<vy<<endl; 

cout<<"location="<<y<<endl; 
myfile3<<"location="<<y<<endl; 

cout<<"Total time"<<time<<endl; 
myfile4<<"Total time="<<time<<endl; 

} 


//system ("PAUSE"); 
    cin.ignore(); 
    cin.get(); 
    myfile1.close(); 
    myfile2.close(); 
    myfile3.close(); 
    myfile4.close(); 



    return 0; 
} 
+2

のため申し訳ありません:std::vector<double>

PSより良い解決策になることができますか?私のシステムで問題なく動作します。ところで、あまりにも多くの値と結果を出力コンソールに出力するのは良い考えではありません。 –

+0

'force [i]'、 'vy [i]'と 'y [i]'を出力したいと思うかもしれません。 –

+1

デバッガをステップ実行するとどうなりますか? –

答えて

1

私はNtimestepのためのあなたの元の値が実際、(あなたのCスタイルの配列の次元xyzFxFyFzvxvyvzforce10000ではなかったと仮定1000

実際にはNtimestep = 1000では、セグメント違反の機会はありません。しかしNtimestep = 10000で、forサイクルのときiは、あなたの学習次の手順

force[i+1]=ay[i]*Mass; 
vy[i+1]=(vy[i]+(ay[i]*dt)); 
y[i+1]=y[i]+vy[i]*dt+0.5*ay[i]*pow(dt,2); 

に(あなたがvyに位置10000でyで、forceに書き込み、これは悪いです、9999(最後の反復)でありますあなたはX x[n]の配列を0からn-1に読み書きすることができ、x[n]のアクセスはアクセス不可能であることを知っていると思います。無particolarため

1)インデントの世話をする中

いくつかのアドバイス、。あなたのコードは)

2を読んで回避し、可能な場合は、グローバル変数 )を使用し、することは困難であるときにすることができ、定数(constexpr、可能な限り、あなたが使用している場合はC++ 11/C++ 14 );大域定数は良好で、配列の大きさを定義することができます。例では、配列に別の位置を与え、上記の問題を回避するために

double x[numSteps+1], y[numSteps+1], z[numSteps+1]; 
double Fx[numSteps+1],Fy[numSteps+1], Fz[numSteps+1]; 
double vx[numSteps+1],vy[numSteps+1], vz[numSteps+1] ; 
double ax[numSteps+1],ay[numSteps+1], az[numSteps+1] ; 
double force[numSteps+1]; 

+1ことができます(私はお勧め、main()でローカル)グローバル定数

const int numSteps = 10000; 

と配列の定義を定義することができます)とforサイクルはと(std::vector<double>を使用することを考慮して仮説を取る)

for (int i=0;i<numSteps;i++) 

4であることができます)をCスタイルの配列の代わりに使用します。 std::vectorは、代わりに[i]at()を使用して、あなたは例外を持っているだろうし、あなたが使用している場合は、バインドされたエラー

4ビス)のすぐ外に出ていただろうC++ 11/C++ 14、std::array<double, numSteps+1>私の悪い英語のエラーの詳細を提供することができ

+0

ありがとうございましたgood advise max – mizan

関連する問題