Untitled

mail@pastecode.io avatar
unknown
c_cpp
5 months ago
3.1 kB
2
Indexable
#include<iostream>
#include<cstdlib> 
#include<fstream>
#include<cmath>
#include<ctime> 
using namespace std;

const int x_unit = 10;
const int y_unit = 10;
const int z_unit = 1;
const int N = 100;
string folder = "substrate/random_move(single_atom)/";
string folder_cl = folder;

int cubic[x_unit+2][y_unit+2][z_unit] = {};

string trans(int num)
{
	string s_inversed="";
	string s="";
	while((num/10)!=0)
	{
		s_inversed+=char(num%10+48);
		num/=10;
	}
	s_inversed+=char(num%10+48);
	for(int i=s_inversed.size()-1;i>=0;i--)
		s+=s_inversed[i];
	
	return s;
}

int counter()
{
	int atoms=0;
	int p[x_unit*y_unit*z_unit][3]={};
	for(int i=1;i<=x_unit;i++)
		for(int j=1;j<=y_unit;j++)
			for(int k=0;k<z_unit;k++)
				if(cubic[i][j][k]==1) atoms++;
	return atoms;
}

void clean()
{
	for(int i=0;i<=x_unit+1;i++)
		for(int j=0;j<=y_unit+1;j++)
			for(int k=0;k<z_unit;k++)
				if(i>x_unit||i<1||j>y_unit||j<1) cubic[i][j][k]=0;
}

void replica()
{				
	for(int i=0;i<=x_unit+1;i++)
		for(int j=0;j<=y_unit+1;j++)
			for(int k=0;k<z_unit;k++)
				{
					if(i<=1&&cubic[i][j][k]==1) cubic[i+x_unit][j][k]=1;
					else if(i>=x_unit&&cubic[i][j][k]==1) cubic[i-x_unit][j][k]=1;
					
					if(j<=1&&cubic[i][j][k]==1) cubic[i][j+y_unit][k]=1;
					else if(j>=y_unit&&cubic[i][j][k]==1) cubic[i][j-y_unit][k]=1;
				}
}


void output(int c)
{
	int a=0;
	int p[x_unit*y_unit*z_unit][3]={};
	for(int i=1;i<=x_unit;i++)
		for(int j=1;j<=y_unit;j++)
			for(int k=0;k<z_unit;k++)
				if(cubic[i][j][k]==1)
				{
					p[a][0]=i-1;
					p[a][1]=j-1;
					p[a][2]=k;
					a++; 
				}
	
	folder=folder_cl;
	string file="model_consequences_";
	file+=trans(c);
	file+=".txt";
	folder+=file;
	
	ofstream outputFile(folder.c_str());
	outputFile << "LAMMPS data file via write_data, version 23 Jun 2022, timestep = 0" << endl << endl;
	
	outputFile << counter() << " atoms" << endl << "1 atom types" << endl << endl;
	
	outputFile << "0 " << x_unit-1 << " xlo xhi" << endl;
	outputFile << "0 " << y_unit-1 << " ylo yhi" << endl;
	outputFile << "0 " << z_unit-1 << " zlo zhi" << endl << endl;
	outputFile << "Masses" << endl << endl << "1 12.011" << endl << endl;
	outputFile << "Atoms # molecular" << endl << endl;
	
	for(int i=0;i<a;i++)
		outputFile << i+1 << " 1 " << p[i][0] << " " << p[i][1] << " " << p[i][2] << endl;
	
	outputFile.close();
}

int main()
{
	srand(time(NULL));
	int r_x = rand()%(x_unit)+1;
	int r_y = rand()%(y_unit)+1;
	cubic[r_x][r_y][0]=1;
	replica();
	for(int count=0;count<N;count++)
	{
		for(int i=1;i<=x_unit;i++)
			for(int j=1;j<=y_unit;j++)
				for(int k=0;k<z_unit;k++)
					if(cubic[i][j][k]==1) 
					{
						r_x = i;
						r_y = j;
					}
		cubic[r_x][r_y][0]=0;
		int move;
		move = rand()%100;
		
		if(move<20) r_x++;
		else if(move>=20&&move<40) r_x--;
		else if(move>=40&&move<60) r_y--;
		else r_y++;
		
		
		clean();
		cubic[r_x][r_y][0]=1;
		replica();
		output(count);
	
	}
	
	return 0;
}
Leave a Comment