void ps_dump()
{
	DIR *d;
	struct dirent *de;
	char *namefilter = 0;
	int pidfilter = 0;
	int threads = 0;

	d = opendir("/proc");
	if(d == 0) return;

	if(filter_by_system == 1)
	{
	    process_info psinfo;
	    memset(&psinfo, 0, sizeof(process_info));

	    psinfo.uid = 0;
	    strcpy(psinfo.owner, "root");
	    strcpy(psinfo.name, "System");
	    psinfo.status = 'S';
	    ps_list_add(&psinfo);
	}

	while((de = readdir(d)) != 0)
	{
		if(isdigit(de->d_name[0]))
		{
			int pid = atoi(de->d_name);
			ps_instance_dump(pid);
		}
	}
	closedir(d);
}
Beispiel #2
0
int main(int argc, char **argv) {

	int i;
	
	if (2 != argc) {
		printf("test_ls_list infile\n");
		return 1;
	}

	char* infname = argv[1];
	FILE *infile = fopen(infname, "r");
	PS_DATA data = ps_data_read_bin(infile);
	fclose(infile);
	
	PS_SOLUTION *solution;
	
	PS_LIST list = ps_list_create();
	for(i=0; i<5; i++) {
		solution = (PS_SOLUTION*)malloc(sizeof(PS_SOLUTION));
		solution->energy = i;
		solution->wavefunction = ps_data_copy(data);
		ps_list_add(list, solution);
		printf("List Size=%i\n", ps_list_size(list));
	}
	free(data);

	PS_SOLUTION *listnode = ps_list_front(list);
	i = 0;
	while (listnode != NULL) {
		printf("Node %i\n", ++i);
		printf("Energy=%f\n", listnode->energy);
		ps_data_write_ascii(listnode->wavefunction, stdout);
		listnode = ps_list_next(list);
	}
	
	ps_list_destroy_all(list);
	return 0;
}	
void ps_instance_dump(int pid)
{
	char statline[BUFFERSIZE*2];
    struct stat stats;
    process_info psinfo;
    FILE *ps;
    struct passwd *pw;
    int n;

    memset(&psinfo, 0, sizeof(process_info));

    psinfo.pid = pid;

    snprintf(statline, BUFFERSIZE*2, "/proc/%d", pid);
    stat(statline, &stats);

	psinfo.uid = (int) stats.st_uid;

    pw = getpwuid(stats.st_uid);
    if(pw == 0)
    {
    	snprintf(psinfo.owner, 80, "%d", (int)stats.st_uid);
    }
    else
    {
    	strncpy(psinfo.owner, pw->pw_name, 80);
    }


    snprintf(statline, BUFFERSIZE*2, "/proc/%d/stat", pid);
    ps = fopen(statline, "r");
    if (ps != 0)
    {

        /* Scan rest of string. */
        fscanf(ps, "%*d %*s %c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
                     "%lu %lu %*d %*d %*d %*d %d %*lu %lu %*lu %ld",
                     &psinfo.status, &psinfo.delta_utime, &psinfo.delta_stime,
					 &psinfo.threadnum, &psinfo.start_time, &psinfo.rss);

        fclose(ps);
    }

    snprintf(statline, BUFFERSIZE*2, "/proc/%d/cmdline", pid);
    ps = fopen(statline, "r");
    if(ps == 0) {
    	n = 0;
    }
    else
    {
    	n = fread(psinfo.name, 1, 80, ps);
    	fclose(ps);
    	if(n < 0) n = 0;
    }
    psinfo.name[n] = 0;

    if(strlen(psinfo.name) == 0)
    {
        snprintf(statline, BUFFERSIZE*2, "/proc/%d/stat", pid);
        ps = fopen(statline, "r");
        if(ps!= 0)
        {
        	n = fscanf(ps, "%d (%s)", &pid, psinfo.name);
        	fclose(ps);
        	if(n == 2)
        		psinfo.name[strlen(psinfo.name)-1] = 0;
        }
    }

    psinfo.nice = getpriority(PRIO_PROCESS, psinfo.pid);

    if(filter_by_system == 1)
    {
    	if((strcmp(psinfo.owner, "root") != 0) &&
    	   (strstr(psinfo.name, "/system/") == 0) &&
    	   (strstr(psinfo.name, "/sbin/") == 0))
    		ps_list_add(&psinfo);
    	else
    		ps_system_add(&psinfo);

    }
    else
    {
    	ps_list_add(&psinfo);
    }

}
Beispiel #4
0
/*
//
//The 2D version of the solving engine.
//
//intial eqn                 
//-->  Div*F = G/M	
//
//convert to finite diff eqn, use a uniform mesh, Let, dx = dy = d  and Let P[x,y] = G[x,y]/M[x,y] = G*m
//-->  (F[x+d,y]-2F[x,y]+F[x-d,y])/d^2 + (F[x,y+d]-2F[x,y]+F[x,y-d])/d^2 = P[x,y]
//
//rearrange:
//-->  F[x+d,y] + F[x-d,y] + F[x,y+d] + F[x,y-d] - 4F[x,y] = d^2 * P[x,y]
//
//convert to c style: Let x = x0+j*d     and    y = y0+l*d 
//                  where j = 0,1,2...J  and    l = 0,1,2...L
//-->  F[j+1,l] + F[j-1,l] + F[j,l+1] + F[j,l-1] - 4F[j,l] = d^2 * P[j,l]y
//
//Goal: make a linear system of eqns in matrix form -- firstly we make a vector out of F. i.e. map j,l to a linear index 'i'
//     Let, i = j(L+1) + l   ...where j = 0,1,...J and l = 0,1,...L
//-->  F[i+L+1] + F[i-(L+1)] + F[i+1] + F[i-1] - 4F[i] = d^2 * P[i]
//    ...where this eqn holds for the interior points: j = 1,2,...,J and l = 1,2,...,L
//
//On the boundries F should be specified, i.e. where:
//    j = 0    or    i = 0, 1, 2, ..,L
//    j = J    or    i = J(L+1), J(L+1)+1, J(L+1)+2, ...,J(L+1)+L
//    l = 0    or    i = 0, (L+1), 2(L+1), ..., J(L+1)
//    l = L    or    i = L, (L+1)+L, 2(L+1)+L, ..., J(L+1)+L         */
PS_LIST ps_solve_2D(PS_DATA potential, PS_SOLVE_PARAMETERS *params) {
	
	char log_message[256];
	ps_log("PsiShooter -- a shooting method solver for the time independant Schrodinger equation under the effective mass approximation.\n");
	
    // This is a linked list for storing solutions
	PS_LIST solution_list = ps_list_create();

    ps_log("Iterate through Energy Eigenvalues to find the lowest bound state.\n");      
	
	// wavefunction storage.  
	int Nx = potential->xsize; // used for generating strings with sprintf for sending to ps_log
	int Ny = potential->ysize; // used for generating strings with sprintf for sending to ps_log
	
	//	double dx = potential->xstep; //cm, size of differential length 
	//(TODO: Compute dx inside loop in order to support meshes with non-uniform differential steps)
	double dx = ps_data_dx_at(potential, 2); 
	double dy = ps_data_dy_at(potential, 2); // 0 to 1 is wierd, maybe. 
	double dx_dy = dx/dy; 
	double dy_dx = dy/dx;
	//F
	//intial eqn                 -->  Div*F = G * m_eff	
	//convert to finite diff eqn -->  (F[x+dx,y]-F[x,y])/dx + (F[x,y]-F[x,y-dy])/dy = G[x,y]M[x,y]	
	//rearrange                  -->  F[x+dx,y]-F[x,y] + dx*(F[x,y]-F[x,y-dy])/dy = dx*G[x,y]/M[x,y]
	//                           -->  F[x+dx,y] = dx*G[x,y]/M[x,y]+F[x,y]-dx/dy*(F[x,y]-F[x,y-dy])
	//                                What does this mean for our 2D simulations? The more closely spaced the y
	//                                Coordinate mesh points are, the more significant their effects will be. That
	//                                Is pretty terrible for us. 
	//
	//                                We need to decouple the coordinates so we don't need to know future F and G from
	//                                from future calculated y mesh points...
	//                                Is there some other way we can decouple them? We can't just look back instead of forward.
	//
	//                                How about we fill the array with 1D shots across the 1 direction, and then refill
	//                                with the 2D shots? Then the array will have some approximate knowledge of the future
	//                                and the algorithm might work.
	
	//convert to c style         -->  F[x+dx,y] = F[i][j+1]
	// *****README*******    	 --> (NOTE: x is iterated over with j index. x=columns, i=rows) -jns

	//in 2D                      -->  F[i][j+1] = dx*G[i][j]/M[i][j] + F[i][j] - dx/dy*(F[i][j]-F[i-1][j])

	
	//convert to 1D              -->  (F[x+dx]-F[x-dx])/(2dx) = G[x]/M[x]	
	//rearrange                  -->  F[x+dx] = (2dx)*G[x]/M[x] + F[x-dx]
	//convert to c style         -->  F[i+1] = (2dx)*G[i]*m[i] + F[i-1]
	//                                F[i+1] = F_coeff*G[i]*m[i] + F[i-1]
	double F_coeff = dx; //handy prefactor that would otherwise be for every point evaluated
	//from dx to 2*dx jh sometime in may
	
	//G
	//intial eqn                 -->  Div*G = F * 2*(V-E)/h_bar^2
	//convert to finite diff eqn -->  (G[x+dx,y]-G[x-dx,y])/(2dx) + (G[x,y+dy]-G[x,y-dy])/(2dy) = F[x,y]*2*(V[x,y]-E[x,y])/h_bar^2
	//in 2d:                     -->  G[x+dx,y] = (2dx)*F[x,y]*2*(V[x,y]-e[x,y])/h_bar^2+G[x-dx][y]-(2dx)/(2dy)(G[x,y+dy]-G[x,y-dy])
	//                           -->  G[i][j+1] = G_coeff * F[i][j]*(V[i][j]-E) + G[i][j-1] - dx/dy(G[i+1][j] + G[i-1][j])
	//
	// The equivalent FWD DIFFEQ -->  G[x+dx][y] = dx*F[x,y]*2*(V[x,y] - e[x,y])/h_bar^2 + G[x,y] - dx/dy(G[x,y] - G[x,y-dy])
	// 						     -->  G[i][j+1] = dx*F[i][j]*2*(V[i][j] - e[i][j])/h_bar^2 + G[i][j] - dx/dy(G[i][j] - G[i-1][j])


	//convert to 1D              -->  G[x+dx]-G[x-dx]/(2dx) = F[x]*2*(V[x]-E)/h_bar^2	
	//rearrange                  -->  G[x+dx] = (2dx)*F[x]*2*(V[x]-E)/h_bar^2 + G[x-dx]
	//convert to c style         -->  G[i+1] = 4*dx/hbar^2 * F[i]*(V[i]-E) + G[i-1]
	//                                G[i+1] = G_coeff * F[i]*(V[i]-E) + G[i-1]
//	double G_coeff = 2*dx/(HBAR_PLANCK_SQ); //handy prefactor that would otherwise be computed for every point evaluated 
	//from 4*dx to 2*dx jh sometime in may

	double G_coeff = 2*dx*G_COEFF;
	
	//dx = dy here.
	double F[Nx][Ny]; //the envelope function (wavefunction)
	double f_cache[params->n_iter]; // We cache the last value of the envelope for each solution
	double G[Nx][Ny]; //the aux function
	int bound_state_count = 0;	
	int threshold_set_flag = 0; 
	double F_threshold = 0;
	int i, j, iter;
	double E = params->energy_min;
	double Estep = (params->energy_max - params->energy_min)/(params->n_iter); 
	double V; //meV, Current potential
//	double m_eff = MASS_ELECTRON*M_EFF_GAAS;
	double m_eff = M_EFF_GAAS;

	// To Do: make the electron mass be part of the structure that we are simulating. i.e. 
	//in general it can be a position dependant quantity just like the potential 
	//(think heterostructures with different band edge curvatures)
		
	for (iter = 0; iter < params->n_iter; iter++) {

	  //Going to try filling the array with y-direction shots and then running the 2D shots based on those.
		
	  //initial conditions
	  for (j=0; j<Nx; j++){
	    F[0][j] = 0;
	    G[0][j] = 1;
		F[1][j] = 1;
		G[1][j] = 1;
	  }
	
	  for(i=1; i<Ny-1; i++) {

		    for(j=1; j<Nx-1; j++) {
		     	V = ps_data_value(potential, i,j); //V[i][j]
				// 5-point stencil in X and Y
				F[i+1][j] = dx*dx*m_eff*G[i][j]  - F[i][j+1] - F[i][j-1] - F[i-1][j] + 4*F[i][j];
				G[i+1][j] = 2*dx*dx*G_COEFF*(V-E)*F[i][j] - G[i][j+1] - G[i][j-1] - G[i-1][j] + 4*G[i][j];
		    }
			// For First and last point, Compute slope in x direction for F and G directly

			// F[x+3] = F[x] + 3h*F'[x] + (3h)^2/2!*F''[x]  (Taylor Expansion)
			// F'[x] = 1/(12*h)*[-F[x+2] + 8*F[x+1] - 8*F[x-1] + F[x-2]]  (5-point stencil)
			// F''[x] = 1/(12*h*h)*[-F[x+2] + 16*F[x+1] -30*F[x] +16*F[x+1] - F[x-2]] (5-point stencil)
			G[i+1][j] = G[i+1][j-3] + 0.25*(-G[i+1][j-1] + 8*G[i+1][j-2] - 8*G[i+1][j-4] + G[i+1][j-5]) + 9.0/24.0*(-G[i+1][j-5] + 16*G[i+1][j-4] - 30*G[i+1][j-3] + 16*G[i+1][j-2] - G[i+1][j-1]);
			F[i+1][j] = F[i+1][j-3] + 0.25*(-F[i+1][j-1] + 8*F[i+1][j-2] - 8*F[i+1][j-4] + F[i+1][j-5]) + 9.0/24.0*(-F[i+1][j-5] + 16*F[i+1][j-4] - 30*F[i+1][j-3] + 16*F[i+1][j-2] - F[i+1][j-1]);			

			// F[x-3] = F[x] - 3h*F'[x] + (3h)^2/2!*F''[x]  (Taylor Expansion)
			// F'[x] = 1/(12*h)*[-F[x+2] + 8*F[x+1] - 8*F[x-1] + F[x-2]]
			// F''[x] = 1/(12*h*h)*[-F[x+2] + 16*F[x+1] -30*F[x] +16*F[x+1] - F[x-2]]
			G[i+1][0] = G[i+1][3] - 0.25*(-G[i+1][5] + 8*G[i+1][4] - 8*G[i+1][2] + G[i+1][1]) + 9.0/24.0*(-G[i+1][5] + 16*G[i+1][4] - 30*G[i+1][3] + 16*G[i+1][2] - G[i+1][1]);
			F[i+1][0] = F[i+1][3] - 0.25*(-F[i+1][5] + 8*F[i+1][4] - 8*F[i+1][2] + F[i+1][1]) + 9.0/24.0*(-F[i+1][5] + 16*F[i+1][4] - 30*F[i+1][3] + 16*F[i+1][2] - F[i+1][1]);			
	  }
	  //Why not just look at the change on the very last point? This definitely won't be representative, but it
	  //might be useful during debugging.
	  // It needs to check the entire boundary. (jns)
	  f_cache[iter] = F[Nx-1][Ny-1];
		
	  // Consider removing this for speed
	  //         sprintf(log_message, "\tE=%g eV\tF[N-1]=%g\n", E/EV_TO_ERGS, F[N-1]);
	  // ps_log(log_message);
		
	  //Try to detect which solutions are eigen states (bound states)
	  //if((iter > 0) && ((f_cache[iter] > 0 && f_cache[iter-1] < 0) || (f_cache[iter] < 0 && f_cache[iter-1] > 0))) { 
	    //if there was a change in sign between the last point of the prev
	    // and current envelope function then there was a zero crossing and there a solution
	    // test for a sign crossing

	    PS_DATA wavefunction = ps_data_copy(potential); // copy the potential
	    ps_data_set_data(wavefunction, (double*)F); // Overwrite the potential with the wavefunction
			
	    PS_SOLUTION *solution = (PS_SOLUTION*)malloc(sizeof(PS_SOLUTION));
	    solution->energy = E;
	    solution->wavefunction = wavefunction;
			
	    // Add the solution to the list of bound states
	    ps_list_add(solution_list, solution);

		// Save the g solutions
		PS_DATA g = ps_data_copy(potential);
		ps_data_set_data(g, (double*)G);
		PS_SOLUTION *gsolution = (PS_SOLUTION*)malloc(sizeof(PS_SOLUTION));
		gsolution->energy = E;
		gsolution->wavefunction = g;
		ps_list_add(g_solutions, gsolution);
		
	    // print a log message
        sprintf(log_message, "\tBoundstate number %d with E=%e found, F[Nx][Ny]=%e < F_threshold=%e\n", ++bound_state_count, solution->energy/EV_TO_ERGS, F[Nx][Ny], F_threshold);
	    ps_log(log_message);
	    //}

	  // Increment the energy
	  E += Estep;
	}    	
	
	return solution_list;
}
Beispiel #5
0
/*
//
// Solve for the bound energies given by the potential. 
// energies is a buffer of energies to test.
// bound_energies is where the actual bound energies will be stored.  It should be the same size as energies.
// both buffers should be of size buf_size.
// returns the number of bound energies found, 0 or negative for error
//
////////////////////////
//Solving the 1D TISE //
////////////////////////
//
//(0)    H * Psi = E * Psi
//
//E is the eignenvalue
//H is the hamiltonian operator: H = T + V = p^2/(2m) + V  
//    ... where p is the momentum operator, p = h/i * Div
//
//Moving to the effective mass regime we no longer solve for the wavefunction
//but instead we sove for an envelope function. Careful consideration shows that 
//if mass is allowed to vary with positon, m -> m(x), then simple subsitution of 
//m(x) for m in H causes H to become non-Hermitian. 
//Fortunately that can be worked around, the result is:
//
//(1)    T * F = -h_bar^2/2 * Div * ( m_eff(x,y,E)^-1 * Div * F )
//           ...note: F is the envelope function that is replacing the role of Psi
// 
//Put it all together to get the effective mass equation that we are going to
//use:
//    +--------------------------------------------------------------+                               
//(2) |    -h_bar^2/2 * Div * (1/m_eff * Div * F) + V * F = E * F    |  
//    +--------------------------------------------------------------+----> This is the main equation
//
//Note: Skip to equations (14) & (15) to see how we are going to proceed in the actual solver we are implenting.
//
	//Rearranging (2):
	//(3)    Div * (M * Div * F) = 2*(V-E)*F/h_bar^2
	//           ... note: Let 1/m_eff(x,y,E) = M  (For compactness of these notes only, it is still the effective mass that is position dependant)
	//take the coordinate system to be cartesisian and Div becomes: Div = d/dx + d/dy   -->   Div*F = dF/dx + dF/dy
	//(4)    Div * { M(dF/dx) + M(dF/dy) } = 2*(V-E)*F/h_bar^2
	//  
	//The LHS of (4) has a (2D) divergance of 2 terms that each have a product of two functions (M, dF), both of which depend on x and y. 
	//The chain rule will get invoked and there will be quite a few terms after we expand this
	//(5)    Div*(M(dF/dx)) + Div*(M(dF/dy)) = ...     ...note: I am leaving off the RHS for awhile
	//(6)    (d/dx + d/dy)*(M(dF/dx)) + (d/dx + d/dy)*(M(dF/dy)) = ...
	//(7)    (d/dx)*(M(dF/dx)) + (d/dy)*(M(dF/dx)) + (d/dx)*(M(dF/dy)) + (d/dy)*(M(dF/dy))= ...
	//(8)    (M)(d^2F/dx^2) + (dF/dx)(dM/dx)  + (M)(d^2F/(dxdy)) + (dF/dx)(dM/dy) + (M)(d^2F/(dydx)) + (dF/dy)(dM/dx) + (M)(d^2F/dy^2) + (dF/dy)(dM/dy) = ...
	//(9)    (M)(d^2F/dx^2) + (M)(d^2F/dy^2) + 2(M)(d^2F/(dxdy)) + (dF/dx)(dM/dx) + (dF/dx)(dM/dy) + (dF/dy)(dM/dx) + (dF/dy)(dM/dy) = ...
	//
	//The next step is to expand (9) out in terms of finite differences, here are some pieces that will be used:
	//(10)   dF/dx = (F[x+dx]-F[x-dx])/(2dx)
	//(11)   d^2F/dx^2 = ((F[x+dx+dx]-F[x+dx-dx])/(2dx) - (F[x-dx+dx]-F[x-dx-dx])/(2dx)) / (2dx)
	//                 = (F[x+2dx] - 2F[x] + F[x-2dx])*(2dx)^-2
	//(12)   d^2F/(dxdy) = (d/dy) * dF/dx
	//                   = (d/dy) * (F[x+dx]-F[x-dx])/(2dx)
	//                   = ((F[x+dx,y+dy]-F[x-dx,y+dy])/(2dx) - (F[x+dx,y-dy]-F[x-dx,y-dy])/(2dx)) / (2dy)
	//                   = ((F[x+dx,y+dy]-F[x-dx,y+dy]) - (F[x+dx,y-dy]-F[x-dx,y-dy])) / (2dx2dy)
	//
	//Using the finite differencing versions (10)-(12) of the derivatives in (9) and making M & F explcit functions of x & y: 
	//(9)    (M)(d^2F/dx^2) +         --> (13)    M[x,y] * (F[x+2dx,y] - 2F[x,y] + F[x-2dx,y])*(2dx)^-2 +  
	//       (M)(d^2F/dy^2) +         -->         M[x,y] * (F[x,y+2dy] - 2F[x,y] + F[x,y-2dy])*(2dy)^-2 + 
	//       2(M)(d^2F/(dxdy)) +      -->         2*M[x,y] * ((F[x+dx,y+dy]-F[x-dx,y+dy])-(F[x+dx,y-dy]-F[x-dx,y-dy]))/(2dx2dy) +
	//       (dF/dx)(dM/dx) +         -->         (F[x+dx,y]-F[x-dx,y])/(2dx) * (M[x+dx,y]-M[x-dx,y])/(2dx) + 
	//       (dF/dx)(dM/dy) +         -->         (F[x+dx,y]-F[x-dx,y])/(2dx) * (M[x,y+dy]-M[x,y-dy])/(2dy) +
	//       (dF/dy)(dM/dx) +         -->         (F[x,y+dy]-F[x,y-dy])/(2dy) * (M[x+dx,y]-M[x-dx,y])/(2dx) +
	//       (dF/dy)(dM/dy) = ...     -->         (F[x,y+dy]-F[x,y-dy])/(2dy) * (M[x,y+dy]-M[x,y-dy])/(2dy) = 2*(V[x,y]-E)*F[x,y]/h_bar^2
	//
	//The form in (13) can be rearranged so that you can use the shooting method. 
	//
//Instead of proceeding directly with that we are going to first write the 
//orginal 2nd order differential equation (3) as a system of coupled ODEs
//(3)    Div * (1/m_eff * Div * F) = 2*(V-E)*F/h_bar^2
//
//Choose simplifying units, such as: 
//     hbar = 1
//     m_electron = 1/2  ...note: 1/m_eff = 1/(m_electron * m_relative) = 2/m_relative
//
//The equation in (3) becomes simpler:
//    Div * (1/m_r * Div * F) = (V-E)*F
//       ...note that m_relative has been shortened to m_r 
// 
//Let:  
//(14)    G = (1/m_r) * Div * F
//Then (3) becomes:
//(15)   Div * G = (V-E)*F
//
//Rearranging (14) and (15) slightly:
//      +--------------------------------------------------------------+
//(16)  |             Div*F = G * m_r                                  |
//(17)  |             Div*G = F * (V-E)                                |
//      +--------------------------------------------------------------+----> These are the useful equations the solver will implement
//
//Expanding (16) with finite differencing:
//       Div*F = G*m_r
//             = dF/dx + dF/dy = G*m_r
//(18)         = (F[x+dx,y]-F[x-dx,y])/(2dx) + (F[x,y+dy]-F[x,y-dy])/(2dy) = G[x,y]*m_r[x,y]
//
//Expanding (17) with finite differencing:
//       Div*G = F * (V-E)
//             = dG/dx + dG/dy = F * (V-E)
//(19)         = (G[x+dx,y]-G[x-dx,y])/(2dx) + (G[x,y+dy]-G[x,y-dy])/(2dy) = F[x,y]*(V[x,y]-E)
//
//
/////////////////////
//Initial Conditons//
/////////////////////
//
//Take the structure to start in a barrier high enough enough to warrant using:
//(20)   F[0,0] = 0
//
//As for the initial value of G: G is porportional to the slope of F (the envelope function)
//solutions for the envelope function will be approx. exponentials inside barrier regions.
//Exponentials are fast functions so any reasonable value of G will get the value of F going 
//to where it is going to converge too. i.e. the overall tragectory will not be sensitive to
//the intial value of G if we are starting deep enough inside a tall barrier.
//
//(21)   G[0,0] = 1    
//
//additional FD notes:
/////////////////////////////////////////////////////////////////////////////////////////		  
//Consider three points on the x axis separated by distance "h" and indexed by "i" ... i.e. i-1, i, i+1  -->  x-h, x, x+h
//The function F(x) has the values at those positions: F[i-1], F[i], F[i+1]
//Taylor expand F(x) at the i-1 and the i+1 point:
//(22)   F[i-1] = F[i] - dF/dx|i * h + d^2F/dx^2|i * h^2/2! - d^3F/dx^3|i * h^3/3 + ...
//(23)   F[i+1] = F[i] + dF/dx|i * h + d^2F/dx^2|i * h^2/2! + d^3F/dx^3|i * h^3/3 + ...
//        ...where  dF/dx|i means take the derivative with respect to x at point i
//If you add (22) and (23) every other term in the expansions cancel, such that:
//(24)   F[i-1] + F[i+1] = 2*F[i] + d^2F/dx^2|i * h^2 + d^4F/dx^4|i + ...
//Rearrange (24) to put the seconcd derivative of F with respect to x on the LHS  
//(25)   d^2F/dx^2|i = (F[i+1] - 2*F[i] + F[i-1]) / h^2  -  d^4F/dx^4|i + ...
//Drop the higher order corrections b/c a second order finite differencing approx of d^2F/dx^2|i is all we should need
//     +----------------------------------------------------------------+
//(26) |  d^2F/dx^2|i = (F[i+1] - 2*F[i] + F[i-1]) / h^2 + O(h^2)       |
//     +----------------------------------------------------------------+--------> 
//The error contained in O(h^2) is of the order h^2 and therefore will be small.
//BTW, subtracting (22) and (23) you can rearrange to get an accurate approx for dF/dx|i
//     +---------------------------------------------------------+
//(27) |       dF/dx|i = (F[i+1] - F[i-1]) / (2h) + O(h^2)       |
//     +---------------------------------------------------------+--------> 
*/
PS_LIST ps_solve_1D(PS_DATA potential, PS_DATA effective_mass, PS_SOLVE_PARAMETERS *params) {
	char log_message[256];
	ps_log("PsiShooter -- a shooting method solver for the time independant Schrodinger equation under the effective mass approximation.\n");
	ps_log("Iterate through Energy Eigenvalues to find the lowest bound state.\n");      
	
	PS_LIST solution_list = ps_list_create(); // This is a linked list for storing solutions

	// wavefunction storage.  
	int N = potential->xsize; // used for generating strings with sprintf for sending to ps_log
	//	double dx = potential->xstep; //cm, size of differential length (TODO: Compute dx inside loop)
	double dx = ps_data_dx_at(potential, 2); // in case 0 to 1 is wierd, this will do 1 to 2	
	
	//sanity checks, the effective mass should be on the same grid as the potential
	if(N != effective_mass->xsize) {
		//problem
		sprintf(log_message, "\tThat's wierd... there are %d elements in the x axis mesh for the potential, but %d elements in the effective mass x axis mesh. Check to make sure the files for potential and effective mass have the same mesh. \n", N, effective_mass->xsize);
		ps_log(log_message);
		return NULL;
	}
	if(dx != ps_data_dx_at(effective_mass, 2)) {
		//more problems
		sprintf(log_message, "\tThat's wierd... the step size for the potential x axis is %e nm, and the the step size for the effective mass x axis mesh is %e nm. They need the same meshes to work", dx, ps_data_dx_at(effective_mass, 2));
		ps_log(log_message);
		return NULL;
	}
	
	double F[N]; //the envelope function (wavefunction)
	double F_endpoint[params->n_iter]; // We save the last value of the envelope for each solution
	double G[N]; //the aux function
	
	//F
	//intial eqn                 -->  Div*F = G*m_r	
	//convert to finite diff eqn -->  (F[x+dx,y]-F[x-dx,y])/(2dx) + (F[x,y+dy]-F[x,y-dy])/(2dy) = G[x,y]/M[x,y]	
	//convert to 1D              -->  (F[x+dx]-F[x-dx])/(2dx) = G[x]/M[x]	
	//rearrange                  -->  F[x+dx] = (2dx)*G[x]/M[x] + F[x-dx]
	//convert to c style         -->  F[i+1] = (2dx)*G[i]*m[i] + F[i-1]
	//                                F[i+1] = F_coeff*G[i]*m[i] + F[i-1]
	double F_coeff = 2*dx; //handy prefactor that would otherwise be for every point evaluated
	
	//G
	//intial eqn                 -->  Div*G = F * 2*(V-E)/h_bar^2
	//convert to finite diff eqn -->  (G[x+dx,y]-G[x-dx,y])/(2dx) + (G[x,y+dy]-G[x,y-dy])/(2dy) = F[x,y]*2*(V[x,y]-E[x,y])/h_bar^2
	//convert to 1D              -->  G[x+dx]-G[x-dx]/(2dx) = F[x]*2*(V[x]-E)/h_bar^2	
	//rearrange                  -->  G[x+dx] = (2dx)*F[x]*2*(V[x]-E)/h_bar^2 + G[x-dx]
	//convert to c style         -->  G[i+1] = 4*dx/hbar^2 * F[i]*(V[i]-E) + G[i-1]
	//                                G[i+1] = G_coeff * F[i]*(V[i]-E) + G[i-1]
    //double G_coeff = 4*dx/(HBAR_PLANCK_SQ); //handy prefactor that would otherwise be computed for every point evaluated 
	double G_coeff = 4*dx*G_COEFF; // Redefined to use M_ELECTRON/HBAR^2 using assuming units of nm and eV
    
	int bound_state_count = 0;	
	int i, iter;
	double E = params->energy_min;
	double Estep = (params->energy_max - params->energy_min)/(params->n_iter); 
	double V; //eV, Current potential
	double m_eff; 
	
	for (iter = 0; iter < params->n_iter; iter++) {

		//initial conditions
		F[0] = 0;
		G[0] = 1;
		F[1] = F_coeff * G[0] * m_eff + 0;    
		G[1] = G_coeff * F[0] * (V-E) + G[0]; 
		
		for(i=1; i<N; i++) {
			V = ps_data_value(potential, 0,i); //V[i]
			m_eff = ps_data_value(effective_mass, 0,i); //m_eff[i]
			
			//Lets eliminate bad solutions by checking if the forward and backwards solutions overlap
			F[i+1] = F_coeff * G[i] * m_eff + F[i-1];  //subbed in m_eff for m[i], To Do: support a position dependant
			G[i+1] = G_coeff * F[i] * (V-E) + G[i-1];	 // mass by storing different masses at different locations (add to the PS_DATA structure probably)		
		  
		  /*  
		//Mix the bidirectional and forward derivatives to prevent two seperate solutions from forming. This
	    //Makes the upper solutions stable and oscillation free. (Probably only required on the i==1 index -jns) 
		// TODO compare results of this branching statement with IF (1==i) ... ELSE ...
	    if (i%2==1){
	      F[i+1] = F_coeff * G[i] * m_eff + F[i]; 
	      //subbed in m_eff for m[i], To Do: support a position dependant
	      // mass by storing different masses at different locations (add to the PS_DATA structure probably)
	      G[i+1] = G_coeff * F[i] * (V-E) + G[i];
	    }
	    if (i%2==0){
	      F[i+1] = 2*F_coeff * G[i] * m_eff + F[i-1]; 
	      //subbed in m_eff for m[i], To Do: support a position dependant
	      // mass by storing different masses at different locations (add to the PS_DATA structure probably)
	      G[i+1] = 2*G_coeff * F[i] * (V-E) + G[i-1];
	    }
		 */
		}
		F_endpoint[iter] = F[N-1]; //store the last point of the solution in an array, use this to bracket roots by looking for sign changes (zero crossings)
		
		//Try to detect which solutions are eigen states (bound states)
		if((iter > 0) && 
				((F_endpoint[iter] > 0 && F_endpoint[iter-1] < 0) || 
				 (F_endpoint[iter] < 0 && F_endpoint[iter-1] > 0)))
		{ 
			/*
			 
			//if there was a change in sign between the last point of the prev and current envelope function then there probably was a zero crossing and there a solution test for a sign crossing
			//in order to reject solutions that appear to just be amplified/oscillating numerical error run
			//shoot solutions backwards to see if you get basically the same enevelope function. If they really
			//are just amplified error then the shots will look like this:
			// forwards:  ----.'.'.       increasing amplitude in this direcion--> 
			// backwards: .'.'.----    <--increasing amplitude in this direcion
			F[N] = 0;
			G[N] = 1;
			F[N-1] = F_coeff * G[0] * m_eff + 0;   
			G[N-1] = G_coeff * F[0] * (V-E) + G[0];

			//Lets eliminate bad solutions by checking if the forward and backwards solutions overlap
			for(i=N-1; i>0; i--) {
				V = ps_data_value(potential, 0,i); //V[i]
				F[i-1] = F[i+1] - F_coeff * G[i] * m_eff;  //subbed in m_eff for m[i], To Do: support a position dependant
				G[i-1] = G[i+1] + G_coeff * F[i] * (E-V);	 // mass by storing different masses at different locations (add to the PS_DATA structure probably)				
			}
			
			//ToDo:
			//see if the overlap is about unity.
			
			 */
			
			
			
			
			// Add the solution to the list of bound states
			PS_DATA wavefunction = ps_data_copy(potential); // copy the potential to get the x,y coordinate stuff and sizes
			ps_data_set_data(wavefunction, F); // Overwrite the potential with the wavefunction
			PS_SOLUTION *solution = (PS_SOLUTION*)malloc(sizeof(PS_SOLUTION));
			solution->energy = E;
			solution->wavefunction = wavefunction;
			ps_list_add(solution_list, solution);
			bound_state_count++;
			
			// print a log message
			sprintf(log_message, "\tBoundstate number %d with E=%e found, F[N]=%e\n", bound_state_count, solution->energy, F[N]);
			ps_log(log_message);
		}

		// Consider removing this for speed
		//  sprintf(log_message, "\tE=%g eV\tF[N-1]=%g\n", E, F[N-1]);
		// ps_log(log_message);
			  	
		E += Estep; // Increment the energy
	}    	
	
	return solution_list;
}
Beispiel #6
0
void ps_instance_dump(int pid)
{
	__android_log_print(ANDROID_LOG_INFO, "JNIMsg", "run JNI function:  %s ",	"ps_instance_dump");

	char statline[BUFFERSIZE * 2];
	struct stat stats;
	process_info psinfo;
	FILE *ps;
	struct passwd *pw;
	int n;

	memset(&psinfo, 0, sizeof(process_info));

	psinfo.pid = pid;

	snprintf(statline, BUFFERSIZE * 2, "/proc/%d", pid);
	stat(statline, &stats);

	psinfo.uid = (int) stats.st_uid;

	pw = getpwuid(stats.st_uid);
	if (pw == 0)
	{
		snprintf(psinfo.owner, 80, "%d", (int) stats.st_uid);
	}
	else
	{
		strncpy(psinfo.owner, pw->pw_name, 80);
	}

	snprintf(statline, BUFFERSIZE * 2, "/proc/%d/stat", pid);
	ps = fopen(statline, "r");
	if (ps != 0)
	{

		/* Scan rest of string. */
		fscanf(ps, "%*d %*s %c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
				"%lu %lu %*d %*d %*d %*d %d %*d %*d %*lu %ld", &psinfo.status,
				&psinfo.delta_utime, &psinfo.delta_stime, &psinfo.threadnum,
				&psinfo.rss);

		fclose(ps);
	}

	snprintf(statline, BUFFERSIZE * 2, "/proc/%d/cmdline", pid);
	ps = fopen(statline, "r");
	if (ps == 0)
	{
		n = 0;
	}
	else
	{
		n = fread(psinfo.name, 1, 80, ps);
		fclose(ps);
		if (n < 0)
			n = 0;
	}
	psinfo.name[n] = 0;

	if (strlen(psinfo.name) == 0)
	{
		snprintf(statline, BUFFERSIZE * 2, "/proc/%d/stat", pid);
		ps = fopen(statline, "r");
		if (ps != 0)
		{
			n = fscanf(ps, "%d (%s)", &pid, psinfo.name);
			fclose(ps);
			if (n == 2)
				psinfo.name[strlen(psinfo.name) - 1] = 0;
		}
	}

	if ((strcmp(psinfo.owner, "root") != 0)
			&& (strstr(psinfo.name, "/system/") == 0)
			&& (strstr(psinfo.name, "/sbin/") == 0)
			&& (strcmp(psinfo.name, "sh") != 0)
			&& (strcmp(psinfo.name, "logcat") != 0)
			&& (strcmp(psinfo.name, "watchdog") != 0)
			&& (strcmp(psinfo.name, "sleep") != 0)
			&& (strcmp(psinfo.name, "system_server") != 0))
		ps_list_add(&psinfo);
}