コード例 #1
0
ファイル: lsfdemo.cpp プロジェクト: nsgarv/PHYS103
void main() {

  //* Initialize data to be fit. Data is quadratic plus random number.
  Matrix c(3);
  cout << "Curve fit data is created using the quadratic" << endl;
  cout << "  y(x) = c(1) + c(2)*x + c(3)*x^2" << endl;
  cout << "Enter the coefficients:" << endl;
  cout << "c(1) = "; cin >> c(1);
  cout << "c(2) = "; cin >> c(2);
  cout << "c(3) = "; cin >> c(3);  
  double alpha;
  cout << "Enter estimated error bar: "; cin >> alpha;
  int i, N = 50;        // Number of data points
  long seed = 1234;     // Seed for random number generator
  Matrix x(N), y(N), sigma(N);
  for( i=1; i<=N; i++ ) {
    x(i) = i;                  // x = [1, 2, ..., N]
    y(i) = c(1) + c(2)*x(i) + c(3)*x(i)*x(i) + alpha*randn(seed);
    sigma(i) = alpha;          // Constant error bar
  }
  
  //* Fit the data to a straight line or a more general polynomial
  cout << "Enter number of fit parameters (=2 for line): ";
  int M;  cin >> M;
  Matrix a_fit(M), sig_a(M), yy(N);	 double chisqr;
  if( M == 2 )  //* Linear regression (Straight line) fit   
    linreg( x, y, sigma, a_fit, sig_a, yy, chisqr);
  else          //* Polynomial fit
    pollsf( x, y, sigma, M, a_fit, sig_a, yy, chisqr);

  //* Print out the fit parameters, including their error bars.
  cout << "Fit parameters:" << endl;
  for( i=1; i<=M; i++ )
    cout << " a(" << i << ") = " << a_fit(i) << 
            " +/- " << sig_a(i) << endl;

  cout << "Chi square = " << chisqr << "; N-M = " << N-M << endl;
  
  //* Print out the plotting variables: x, y, sigma, yy
  ofstream xOut("x.txt"), yOut("y.txt"), 
	       sigmaOut("sigma.txt"), yyOut("yy.txt");
  for( i=1; i<=N; i++ ) {
    xOut << x(i) << endl;
    yOut << y(i) << endl;
    sigmaOut << sigma(i) << endl;
    yyOut << yy(i) << endl;
  }
}
コード例 #2
0
void InstalledProgramsForm::on_pushButton_clicked()
{
    QString xFilePath = QFileDialog::getSaveFileName(this, tr("Сохранить файл"),
                                                     mUI->CompName->text() + ".html",
                                                     tr("Файлы (*.html)"));

    QFile xFile(xFilePath);
    if(xFile.exists() == true) xFile.remove();
    xFile.open(QFile::WriteOnly);

    QTextStream xOut(&xFile);
    xOut<<"<html><body><h4>"<<tr("Oтчет о программном обеспечении")<<"</h4>"
            <<tr("Имя компьютера: ")
            <<mUI->CompName->text()
            <<"<br>"<<tr("IP-адресс: ")
            <<mUI->CompIPAddress->text()<<endl;
    xOut<<"<table border=1 cellpadding=5px style='border:solid; border-collapse:collapse;' >"<<endl;

    xOut<<"<tr>";
    xOut<<"<td>"<<tr("Имя программы");
    xOut<<"<td>"<<tr("Отображаемая версия");
    xOut<<"<td>"<<tr("Разработчик");
    xOut<<"<td>"<<tr("Дата установки");
    xOut<<"<td>"<<tr("Путь установки");
    xOut<<"<td>"<<tr("Ключ в реестре");
    xOut<<endl;

    for(int i = 0; i < mUI->ProgramsList->rowCount(); i++)
    {
        xOut<<"<tr>";
        xOut<<"<td>"<<mUI->ProgramsList->item(i,0)->text();
        xOut<<"<td>"<<mUI->ProgramsList->item(i,1)->text();
        xOut<<"<td>"<<mUI->ProgramsList->item(i,2)->text();
        xOut<<"<td>"<<mUI->ProgramsList->item(i,3)->text();
        xOut<<"<td>"<<mUI->ProgramsList->item(i,4)->text();
        xOut<<"<td>"<<mUI->ProgramsList->item(i,5)->text();
        xOut<<endl;
    }

    xOut<<"</table>"<<endl;
    xOut<<"</body></html>";
    xFile.flush();
    xFile.close();
}
コード例 #3
0
ファイル: schro.cpp プロジェクト: nsgarv/PHYS103
void main() {

  //* Initialize parameters (grid spacing, time step, etc.)
  cout << "Enter number of grid points: "; int N; cin >> N;
  double L = 100;        // System extends from -L/2 to L/2
  double h = L/(N-1);    // Grid size
  double h_bar = 1;  double mass = 1; // Natural units
  cout << "Enter time step: "; double tau; cin >> tau;
  Matrix x(N);
  int i, j, k;
  for( i=1; i<=N; i++ )
    x(i) = h*(i-1) - L/2;  // Coordinates  of grid points

  //* Set up the Hamiltonian operator matrix
  Matrix eye(N,N), ham(N,N);
  eye.set(0.0);  // Set all elements to zero
  for( i=1; i<=N; i++ ) // Identity matrix
    eye(i,i) = 1.0;
  ham.set(0.0);  // Set all elements to zero
  double coeff = -h_bar*h_bar/(2*mass*h*h);
  for( i=2; i<=(N-1); i++ ) {
    ham(i,i-1) = coeff;
    ham(i,i) = -2*coeff;  // Set interior rows
    ham(i,i+1) = coeff;
  }
  // First and last rows for periodic boundary conditions
  ham(1,N) = coeff;   ham(1,1) = -2*coeff; ham(1,2) = coeff;
  ham(N,N-1) = coeff; ham(N,N) = -2*coeff; ham(N,1) = coeff;

  //* Compute the Crank-Nicolson matrix
  Matrix RealA(N,N), ImagA(N,N), RealB(N,N), ImagB(N,N);
  for( i=1; i<=N; i++ )
   for( j=1; j<=N; j++ ) {
     RealA(i,j) = eye(i,j);
     ImagA(i,j) = 0.5*tau/h_bar*ham(i,j);
     RealB(i,j) = eye(i,j);
     ImagB(i,j) = -0.5*tau/h_bar*ham(i,j);
   }
  Matrix RealAi(N,N), ImagAi(N,N);
  cout << "Computing matrix inverse ... " << flush;
  cinv( RealA, ImagA, RealAi, ImagAi );  // Complex matrix inverse
  cout << "done" << endl;
  Matrix RealD(N,N), ImagD(N,N); // Crank-Nicolson matrix
  for( i=1; i<=N; i++ )
   for( j=1; j<=N; j++ ) {
    RealD(i,j) = 0.0;           // Matrix (complex) multiplication
    ImagD(i,j) = 0.0;
    for( k=1; k<=N; k++ ) {
      RealD(i,j) += RealAi(i,k)*RealB(k,j) - ImagAi(i,k)*ImagB(k,j);
      ImagD(i,j) += RealAi(i,k)*ImagB(k,j) + ImagAi(i,k)*RealB(k,j);
    }
   }
			 
  //* Initialize the wavefunction 
  const double pi = 3.141592654;
  double x0 = 0;          // Location of the center of the wavepacket
  double velocity = 0.5;  // Average velocity of the packet
  double k0 = mass*velocity/h_bar;       // Average wavenumber
  double sigma0 = L/10;   // Standard deviation of the wavefunction
  double Norm_psi = 1/(sqrt(sigma0*sqrt(pi)));  // Normalization
  Matrix RealPsi(N), ImagPsi(N), rpi(N), ipi(N);
  for( i=1; i<=N; i++ ) {
    double expFactor = exp(-(x(i)-x0)*(x(i)-x0)/(2*sigma0*sigma0));
    RealPsi(i) = Norm_psi * cos(k0*x(i)) * expFactor;
    ImagPsi(i) = Norm_psi * sin(k0*x(i)) * expFactor;
    rpi(i) = RealPsi(i);    // Record initial wavefunction
    ipi(i) = ImagPsi(i);    // for plotting
  }

  //* Initialize loop and plot variables 
  int nStep = (int)(L/(velocity*tau));  // Particle should circle system
  int nplots = 20;                      // Number of plots to record
  double plotStep = nStep/nplots;       // Iterations between plots          
  Matrix p_plot(N,nplots+2);
  for( i=1; i<=N; i++ )      // Record initial condition
    p_plot(i,1) = RealPsi(i)*RealPsi(i) + ImagPsi(i)*ImagPsi(i);
  int iplot = 1;

  //* Loop over desired number of steps (wave circles system once)
  int iStep;
  Matrix RealNewPsi(N), ImagNewPsi(N);
  for( iStep=1; iStep<=nStep; iStep++ )	{
	
    //* Compute new wave function using the Crank-Nicolson scheme
	RealNewPsi.set(0.0);  ImagNewPsi.set(0.0);
    for( i=1; i<=N; i++ )        // Matrix multiply D*psi
      for( j=1; j<=N; j++ ) {
        RealNewPsi(i) += RealD(i,j)*RealPsi(j) - ImagD(i,j)*ImagPsi(j);
        ImagNewPsi(i) += RealD(i,j)*ImagPsi(j) + ImagD(i,j)*RealPsi(j);
      }
    RealPsi = RealNewPsi;  // Copy new values into Psi
    ImagPsi = ImagNewPsi;
        
    //* Periodically record values for plotting
    if( fmod(iStep,plotStep) < 1 ) {  
      iplot++;
      for( i=1; i<=N; i++ ) 
        p_plot(i,iplot) = RealPsi(i)*RealPsi(i) + ImagPsi(i)*ImagPsi(i);
      cout << "Finished " << iStep << " of " << nStep << " steps" << endl; 
    }
  }
  // Record final probability density
  iplot++;
  for( i=1; i<=N; i++ ) 
    p_plot(i,iplot) = RealPsi(i)*RealPsi(i) + ImagPsi(i)*ImagPsi(i); 
  nplots = iplot;   // Actual number of plots recorded
  
  //* Print out the plotting variables:  x, rpi, ipi, p_plot
  ofstream xOut("x.txt"), rpiOut("rpi.txt"), ipiOut("ipi.txt"), 
	       p_plotOut("p_plot.txt");
  for( i=1; i<=N; i++ ) {
    xOut << x(i) << endl;
    rpiOut << rpi(i) << endl;
    ipiOut << ipi(i) << endl;
    for( j=1; j<nplots; j++ )
      p_plotOut << p_plot(i,j) << ", ";
    p_plotOut << p_plot(i,nplots) << endl;
  }
}
コード例 #4
0
ファイル: fftpoi.cpp プロジェクト: AlejGarcia/NM4P
void main() {

  //* Initialize parameters (system size, grid spacing, etc.)
  double eps0 = 8.8542e-12;   // Permittivity (C^2/(N m^2))
  int N = 64;   // Number of grid points on a side (square grid)
  double L = 1;    // System size
  double h = L/N;  // Grid spacing for periodic boundary conditions
  Matrix x(N), y(N);
  int i,j;
  for( i=1; i<=N; i++ )
    x(i) = (i-0.5)*h;  // Coordinates of grid points
  y = x;               // Square grid
  cout << "System is a square of length " << L << endl;

  //* Set up charge density rho(i,j)
  Matrix rho(N,N);
  rho.set(0.0);     // Initialize charge density to zero
  cout << "Enter number of line charges: "; int M; cin >> M;
  for( i=1; i<=M; i++ ) {
    cout << "For charge #" << i << endl;
    cout << "Enter x coordinate: "; double xc; cin >> xc;
    cout << "Enter y coordinate: "; double yc; cin >> yc;
    int ii = (int)(xc/h) + 1;    // Place charge at nearest
    int jj = (int)(yc/h) + 1;    // grid point
    cout << "Enter charge density: "; double q; cin >> q;
    rho(ii,jj) += q/(h*h);
  }

  //* Compute matrix P
  const double pi = 3.141592654;
  Matrix cx(N), cy(N);
  for( i=1; i<=N; i++ )
    cx(i) = cos((2*pi/N)*(i-1));
  cy = cx;
  Matrix RealP(N,N), ImagP(N,N);
  double numerator = -h*h/(2*eps0);
  double tinyNumber = 1e-20;  // Avoids division by zero
  for( i=1; i<=N; i++ )
   for( j=1; j<=N; j++ )
     RealP(i,j) = numerator/(cx(i)+cy(j)-2+tinyNumber);
  ImagP.set(0.0);

  //* Compute potential using MFT method
  Matrix RealR(N,N), ImagR(N,N), RealF(N,N), ImagF(N,N);
  for( i=1; i<=N; i++ )
   for( j=1; j<=N; j++ ) {
     RealR(i,j) = rho(i,j);
     ImagR(i,j) = 0.0;       // Copy rho into R for input to fft2
   }
  fft2(RealR,ImagR);   // Transform rho into wavenumber domain
  // Compute phi in the wavenumber domain
  for( i=1; i<=N; i++ )
   for( j=1; j<=N; j++ ) {
    RealF(i,j) = RealR(i,j)*RealP(i,j) - ImagR(i,j)*ImagP(i,j);
    ImagF(i,j) = RealR(i,j)*ImagP(i,j) + ImagR(i,j)*RealP(i,j);
   }
  Matrix phi(N,N);
  ifft2(RealF,ImagF);    // Inv. transf. phi into the coord. domain
  for( i=1; i<=N; i++ )
   for( j=1; j<=N; j++ )
     phi(i,j) = RealF(i,j);

  //* Print out the plotting variables: x, y, phi
  ofstream xOut("x.txt"), yOut("y.txt"), phiOut("phi.txt");
  for( i=1; i<=N; i++ ) {
    xOut << x(i) << endl;
    yOut << y(i) << endl;
    for( j=1; j<N; j++ )
      phiOut << phi(i,j) << ", ";
    phiOut << phi(i,N) << endl;
  }
}