1 /*//////////////////////////////////////////////////////////////////////////////
2 //
10 //
11 // See the README.txt file in this directory for copyright and license
12 // information.
13 //
14 //
15 //
16 // Example to demonstrate how to use libnucnet routines to create a
17 // Libnucnet__Nuc structure of nuclear species, add species to the
18 // structure, remove species from the structure, retrieve data about
19 // each species in the structure, and clear the structure and free the
20 // allocated memory.
21 //
22 //
23 //
24 //////////////////////////////////////////////////////////////////////////////*/
25
26 #include
27
28 void print_nuclei( Libnucnet__Nuc * );
29
30 int print_species( Libnucnet__Species *, void * );
31
32 int
33 main( int argc, char **argv ) {
34
35 Libnucnet__Nuc *p_my_nuclei;
36 Libnucnet__Species *p_species;
37 size_t i, i_partf_count = 10;
38 unsigned i_z, i_a;
39 int i_state;
40 double d_mass_excess, d_spin;
41 double a_t9[10] = {0.1, 0.2, 0.3, 0.5, 1.0, 2.0, 3.0, 5.0, 7.0, 10.};
42 double a_log10_partf[10] = {1.,2.,3.,4.,5.,6.,7.,8.,9.,10.};
43 gsl_vector *p_t9, *p_log10_partf;
44
45 /*============================================================================
46 // Check input.
47 //==========================================================================*/
48
49 if ( argc != 1 ) {
50 fprintf(
51 stderr, "\nUsage: %s\n\n", argv[0]
52 );
53 return EXIT_FAILURE;
54 }
55
56 /*============================================================================
57 // Allocate and assign gsl vectors.
58 //==========================================================================*/
59
60 p_t9 = gsl_vector_alloc( i_partf_count );
61 p_log10_partf = gsl_vector_alloc( i_partf_count );
62
63 for( i = 0; i < i_partf_count; i++ ) {
64 gsl_vector_set( p_t9, i, a_t9[i] );
65 gsl_vector_set( p_log10_partf, i, a_log10_partf[i] );
66 }
67
68 /*============================================================================
69 // Create collection of nuclear species.
70 //==========================================================================*/
71
72 p_my_nuclei = Libnucnet__Nuc__new( );
73
74 print_nuclei( p_my_nuclei );
75
76 /*============================================================================
77 // Add neutrons.
78 //==========================================================================*/
79
80 fprintf( stdout, "\nAdd neutrons:\n\n" );
81
82 i_z = 0;
83 i_a = 1;
84 i_state = 0;
85 d_mass_excess = 8.071;
86 d_spin = 0.5;
87
88 p_species =
89 Libnucnet__Species__new(
90 i_z,
91 i_a,
92 "example 1",
93 i_state,
94 NULL,
95 d_mass_excess,
96 d_spin,
97 p_t9,
98 p_log10_partf
99 );
100
101 Libnucnet__Nuc__addSpecies( p_my_nuclei, p_species );
102
103 print_nuclei( p_my_nuclei );
104
105 /*============================================================================
106 // Add protons.
107 //==========================================================================*/
108
109 fprintf( stdout, "\nAdd protons:\n\n" );
110
111 i_z = 1;
112 i_a = 1;
113 i_state = 0;
114 d_mass_excess = 7.289;
115 d_spin = 0.5;
116
117 p_species =
118 Libnucnet__Species__new(
119 i_z,
120 i_a,
121 "example 1",
122 i_state,
123 NULL,
124 d_mass_excess,
125 d_spin,
126 p_t9,
127 p_log10_partf
128 );
129
130 Libnucnet__Nuc__addSpecies( p_my_nuclei, p_species );
131
132 /*============================================================================
133 // Print out data about the species.
134 //==========================================================================*/
135
136 print_nuclei( p_my_nuclei );
137
138 /*============================================================================
139 // Remove neutrons
140 //==========================================================================*/
141
142 fprintf( stdout, "\nRemove neutrons:\n\n" );
143
144 Libnucnet__Nuc__removeSpecies(
145 p_my_nuclei,
146 Libnucnet__Nuc__getSpeciesByName( p_my_nuclei, "n" )
147 );
148
149 print_nuclei( p_my_nuclei );
150
151 /*============================================================================
152 // Free the collection.
153 //==========================================================================*/
154
155 printf( "\nFree the collection:\n\n" );
156
157 Libnucnet__Nuc__free( p_my_nuclei );
158
159 /*============================================================================
160 // Recreate the collection.
161 //==========================================================================*/
162
163 p_my_nuclei = Libnucnet__Nuc__new();
164
165 /*============================================================================
166 // Add protons and print out data.
167 //==========================================================================*/
168
169 fprintf( stdout, "\nAdd protons again:\n\n" );
170
171 i_z = 1;
172 i_a = 1;
173 i_state = 0;
174 d_mass_excess = 7.289;
175 d_spin = 0.5;
176
177 p_species =
178 Libnucnet__Species__new(
179 i_z,
180 i_a,
181 "example 1",
182 i_state,
183 NULL,
184 d_mass_excess,
185 d_spin,
186 p_t9,
187 p_log10_partf
188 );
189
190 Libnucnet__Nuc__addSpecies( p_my_nuclei, p_species );
191
192 print_nuclei( p_my_nuclei );
193
194 /*============================================================================
195 // Final clean up
196 //==========================================================================*/
197
198 gsl_vector_free( p_t9 );
199 gsl_vector_free( p_log10_partf );
200
201 Libnucnet__Nuc__free( p_my_nuclei );
202
203 /*============================================================================
204 // Done!
205 //==========================================================================*/
206
207 return EXIT_SUCCESS;
208
209 }
210
211 /*##############################################################################
212 // print_nuclei()
213 //############################################################################*/
214
215 void print_nuclei( Libnucnet__Nuc * self ) {
216
217 /*============================================================================
218 // Print out header information.
219 //==========================================================================*/
220
221 fprintf(
222 stdout,
223 "\n Index Z A Name Mass Excess (MeV) Spin Data Source\n"
224 );
225 fprintf(
226 stdout,
227 " _____ ___ ___ ______ ___________________ ____ __________\n\n"
228 );
229
230 /*============================================================================
231 // Iterate to print species.
232 //==========================================================================*/
233
234 Libnucnet__Nuc__iterateSpecies(
235 self,
236 (Libnucnet__Species__iterateFunction) print_species,
237 NULL
238 );
239
240 /*============================================================================
241 // Print out the number of species.
242 //==========================================================================*/
243
244 fprintf(
245 stdout,
246 "\nThe collection has a total of %lu species.\n\n",
247 (unsigned long) Libnucnet__Nuc__getNumberOfSpecies( self )
248 );
249
250 return;
251
252 }
253
254 /*##############################################################################
255 // print_species()
256 //############################################################################*/
257
258 int
259 print_species(
260 Libnucnet__Species *p_species, void *p_user_data
261 )
262 {
263
264 if( p_user_data ) {
265 fprintf( stderr, "No user data should be passed to this function.\n" );
266 return 0;
267 }
268
269 fprintf(
270 stdout,
271 "%5d %4d %4d %5s %13.4f %13.2f %s\n",
272 Libnucnet__Species__getIndex( p_species ),
273 Libnucnet__Species__getZ( p_species ),
274 Libnucnet__Species__getA( p_species ),
275 Libnucnet__Species__getName( p_species ),
276 Libnucnet__Species__getMassExcess( p_species ),
277 Libnucnet__Species__getSpin( p_species ),
278 Libnucnet__Species__getSource( p_species )
279 );
280
281 return 1;
282
283 }
284