C – Translate The Coordinates Along Any Given Axis

Disclaimer

The instructions/steps/scripts/methods given below worked for me running CentOS and various other distributions. It may very well work for you on other linux distributions, Red Hat-like or otherwise. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, sgowtham.com, nor its author is responsible for any/all damage – intellectual or otherwise.

What is C?

In computing, C is a general-purpose, cross-platform, block structured, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. Although C was designed for implementing system software, it is also used for developing application software – on a great many different software platforms and computer architectures, and several popular compilers exist. C has greatly influenced many other popular programming languages, most notably C++, which originally began as an extension to C. C is an imperative (procedural) systems implementation language. It was designed to be compiled using a relatively straightforward compiler, to provide low-level access to memory, to provide language constructs that map efficiently to machine instructions, and to require minimal run-time support. C was therefore useful for many applications that had formerly been coded in assembly language. Despite its low-level capabilities, the language was designed to encourage machine-independent programming. A standards-compliant and portably written C program can be compiled for a very wide variety of computer platforms and operating systems with little or no change to its source code. The language has become available on a very wide range of platforms, from embedded microcontrollers to supercomputers.

The Program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 
This program shifts x, y, z co-ordinates of any system based on
the value of dx, dy, dz and writes the new set of (x,y,z) in a new
file. 
 
INPUT :
 
Input file must have the name 'shift-xyz-input.xyz' and (x,y,z) 
co-ordinates must be in the following format (space or tab separated)
 
First Line   : Number of atoms, N
Next N Lines : x, y, z of each atom
 
DESCRIPTION OF VARIABLES :
 
Nbuf     First line of the input file, stored as string
Abuf     Atomic Number of the atom, stored as string
xbuf     x co-ordinate of the atom, stored as string
ybuf     y co-ordinate of the atom, stored as string
zbuf     z co-ordinate of the atom, stored as string
 
N        Nbuf converted to integer, using atoi()
         Number of atoms
 
Atom[i]  Abuf converted to integer, using atoi()
         One dimensional array for atomic number of N atoms  
 
x[i]     xbuf converted to double precision number, using atof()
         One dimensional array for storing x co-ordinate of N atoms  
 
y[i]     ybuf converted to double precision number, using atof()
         One dimensional array for storing y co-ordinate of N atoms   
 
z[i]     zbuf converted to double precision number, using atof()
         One dimensional array for storing z co-ordinate of N atoms
 
 
COMPILATION/EXECUTION PROCEDURE :
 
gcc -o shift-xyz.x shift-xyz.c -lm
./shift-xyz.x
 
OUTPUT :
 
If the compilation and execution is successful, the program writes an output 
file named in 'shift-xyz-ouput.xyz' format with shifted (x,y,z) co-ordinates
 
Thu Dec 22 21:22:51 EST 2005
 
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
#include 
#include 
#include 
#include 
 
main()
{
 
/* Defines input and output files */
  FILE *input;
  FILE *output;
 
/* Declaration and initialization of variables */
  char   Nbuf[100]={'\0'};
  char   Abuf[100]={'\0'};
  char   xbuf[100]={'\0'};
  char   ybuf[100]={'\0'};
  char   zbuf[100]={'\0'};
 
  int      Atom[100]={0};
  double   x[100]={0};
  double   y[100]={0};
  double   z[100]={0};
 
  int     i, N;
  double  dx, dy, dz;
 
  dx=0.500;  /* Change this value to indicate dx */
  dy=0.500;  /* Change this value to indicate dy */
  dz=0.500;  /* Change this value to indicate dz */
 
/* Opens the input file for reading */
  input = fopen("shift-xyz-input.xyz", "r");
 
  if (input == NULL) {
    printf("\nInput File empty or corrupted! Exiting...\n");
    exit (1);
  }
 
/* Creates the output file and opens it for writing */
  output = fopen("shift-xyz-ouput.xyz", "w");
 
/* Reads the first line of the input file and stores it as N */
  fscanf(input, "%s", Nbuf);
  N = atoi(Nbuf);
 
/* Reads and stores the (x, y, z) for each atom, first as a string (buffer) 
  and later stores it as an array element using appropriate conversion 
  mechanism */
  for(i=0; i<N; i++) {
    fscanf(input, "%s", Abuf);
    Atom[i]= atoi(Abuf);
 
    fscanf(input, "%s", xbuf);
    x[i]= atof(xbuf);
 
    fscanf(input, "%s", ybuf);
    y[i]= atof(ybuf);
 
    fscanf(input, "%s", zbuf);
    z[i]= atof(zbuf);
  }
 
/* Calculates the new x and y co-odrinates of atoms based on the value of
  dx, dy and dz */
  for(i=0; i<N; i++)  {
    x[i] = x[i] + dx;
    y[i] = y[i] + dy;
    z[i] = z[i] + dz;
  }
 
/* Final values are written in the output file */
  fprintf(output, "%d\n", N);
 
  for(i=0; i<N; i++){
    fprintf(output, "%d",       Atom[i]);
    fprintf(output, "\t%lf",    x[i]);
    fprintf(output, "\t%lf",    y[i]);
    fprintf(output, "\t%lf\n",  z[i]);
  }
 
/* Closes the input and output files */
  fclose(input);
  fclose(output);
 
/* Confirmation to the user */
  printf("Please open shift-xyz-output.xyz for results\n\n");
 
/* Program Ends */
}

The Input File Format

1
2
3
4
5
6
7
6
6   1.000000    0.000000        0.000000
6   2.000000  0.000000  0.000000
1   3.000000  0.000000  0.000000
1   4.000000  0.000000  0.000000
8   5.000000  0.000000  0.000000
8   6.000000  0.000000  0.000000

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.