Skip to content

Commit 40af863

Browse files
farhiEmmanuel FARHIwillend
authored
4 mcxtrace fluo fix again (#2045)
* mcxtrace: reflectivity: pretty printing * mcxtrace: attempt to fix variable scatt and SPLIT * mcxtrace: Test Fluo: use Lorentzian * mcxtrace: Add LiNbO3 as CIF example * mcxtrace: Fluo: fix stability issue and SPLIT * mcxtrace: test Fluo: set test value * mcxtrace: fix FluoPowder stability issue, SPLIT and test value * mcxtrace: FluoSX: could NOT fix SPLIT - and no LUCIA fix in the end * mcxtrace: fluo: avoid use of rand in SHARE * mcxtrace: fluo: detector use SHARE from fluo sample * mcxtrace: fluo: fix test value after rand01 move to TRACE * Add low-key event monitor ported from McStas * Add protection for iarr/darr allocations * Take into account hints from cppcheck in SOLEIL_LUCIA and merge with @farhi changes * Pedantically following all hints from cppcheck... * Sync mcstas <-> mcxtrace Event_monitor_simple, more malloc checks in read_table-lib.c * mcxtrace: fluo: seem to find error: mc_trans correction ? * mcxtrace: more fixes in fluo: set zero XS at start, too many i_Z+1, properly set sigma_barn * mcxtrace: fluo: extend array copy bound * mcxtrace: fluo: forward Fluorescence fixes to Powder and SX versions * mcxtrace: examples: fix test values for fluo samples --------- Co-authored-by: Emmanuel FARHI <emmanuel.farhi.1@gmail.com> Co-authored-by: Peter Willendrup <pkwi@fysik.dtu.dk>
1 parent e17e1c8 commit 40af863

18 files changed

Lines changed: 597 additions & 511 deletions

File tree

common/lib/share/interoff-lib.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,10 @@ long off_init( char *offfile, double xwidth, double yheight, double zdepth,
792792
{
793793
int nbVertex=faceArray[i];//nb of vertices of this polygon
794794
double *vertices=malloc(3*nbVertex*sizeof(double));
795+
if (!vertices) {
796+
fprintf(stderr,"Error allocating vertex array sized %i\n",nbVertex);
797+
exit(-1);
798+
}
795799
int j;
796800

797801
for (j=0; j<nbVertex; ++j)
@@ -868,6 +872,10 @@ int n2 = r - m;
868872
intersection *L, *R;
869873
L = (intersection *)malloc(sizeof(intersection) * n1);
870874
R = (intersection *)malloc(sizeof(intersection) * n2);
875+
if (!L||!R) {
876+
fprintf(stderr,"Error allocating intersection arrays\n");
877+
exit(-1);
878+
}
871879
/* Copy data to temp arrays L[] and R[] */
872880
#pragma acc loop independent
873881
for (i = 0; i < n1; i++)

common/lib/share/mccode-r.c

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,27 +183,42 @@ int any_set(int n, ...){
183183

184184
/* SECTION: Dynamic Arrays ================================================== */
185185
IArray1d create_iarr1d(int n){
186-
IArray1d arr2d;
187-
arr2d = calloc(n, sizeof(int));
188-
return arr2d;
186+
IArray1d arr1d;
187+
arr1d = calloc(n, sizeof(int));
188+
if (!arr1d) {
189+
fprintf(stderr, "Error allocating IArray1d of dimension %i\n",n);
190+
exit(-1);
191+
}
192+
return arr1d;
189193
}
194+
190195
void destroy_iarr1d(IArray1d a){
191196
free(a);
192197
}
193198

194199
IArray2d create_iarr2d(int nx, int ny){
195200
IArray2d arr2d;
196201
arr2d = calloc(nx, sizeof(int *));
202+
if (!arr2d) {
203+
fprintf(stderr, "Error allocating IArray2d of dimension %i x %i\n",nx,ny);
204+
exit(-1);
205+
}
197206

198207
int *p1;
199208
p1 = calloc(nx*ny, sizeof(int));
200209

210+
if (!p1) {
211+
fprintf(stderr, "Error allocating int* array of dimension %i\n",nx*ny);
212+
exit(-1);
213+
}
214+
201215
int i;
202216
for (i=0; i<nx; i++){
203217
arr2d[i] = &(p1[i*ny]);
204218
}
205219
return arr2d;
206220
}
221+
207222
void destroy_iarr2d(IArray2d a){
208223
free(a[0]);
209224
free(a);
@@ -215,18 +230,31 @@ IArray3d create_iarr3d(int nx, int ny, int nz){
215230

216231
// 1d
217232
arr3d = calloc(nx, sizeof(int **));
233+
if (!arr3d) {
234+
fprintf(stderr, "Error allocating IArray3d of dimension %i x %i x %i\n",nx,ny,nz);
235+
exit(-1);
236+
}
218237

219238
// d2
220239
int **p1;
221240
p1 = calloc(nx*ny, sizeof(int *));
222241

242+
if (!p1) {
243+
fprintf(stderr, "Error allocating int** array of dimension %i\n",nx*ny);
244+
exit(-1);
245+
}
246+
223247
for (i=0; i<nx; i++){
224248
arr3d[i] = &(p1[i*ny]);
225249
}
226250

227251
// 3d
228252
int *p2;
229253
p2 = calloc(nx*ny*nz, sizeof(int));
254+
if (!p2) {
255+
fprintf(stderr, "Error allocating int* array of dimension %i\n",nx*ny*nz);
256+
exit(-1);
257+
}
230258
for (i=0; i<nx; i++){
231259
for (j=0; j<ny; j++){
232260
arr3d[i][j] = &(p2[(i*ny+j)*nz]);
@@ -242,9 +270,13 @@ void destroy_iarr3d(IArray3d a){
242270
}
243271

244272
DArray1d create_darr1d(int n){
245-
DArray1d arr2d;
246-
arr2d = calloc(n, sizeof(double));
247-
return arr2d;
273+
DArray1d arr1d;
274+
arr1d = calloc(n, sizeof(double));
275+
if (!arr1d) {
276+
fprintf(stderr, "Error allocating DArray1d of dimension %i\n",n);
277+
exit(-1);
278+
}
279+
return arr1d;
248280
}
249281

250282
void destroy_darr1d(DArray1d a){
@@ -254,10 +286,16 @@ void destroy_darr1d(DArray1d a){
254286
DArray2d create_darr2d(int nx, int ny){
255287
DArray2d arr2d;
256288
arr2d = calloc(nx, sizeof(double *));
257-
289+
if (!arr2d) {
290+
fprintf(stderr, "Error allocating DArray2d of dimension %i x %i\n",nx,ny);
291+
exit(-1);
292+
}
258293
double *p1;
259294
p1 = calloc(nx*ny, sizeof(double));
260-
295+
if (!p1) {
296+
fprintf(stderr, "Error allocating double* array of dimension %i\n",nx*ny);
297+
exit(-1);
298+
}
261299
int i;
262300
for (i=0; i<nx; i++){
263301
arr2d[i] = &(p1[i*ny]);
@@ -277,18 +315,28 @@ DArray3d create_darr3d(int nx, int ny, int nz){
277315

278316
// 1d
279317
arr3d = calloc(nx, sizeof(double **));
280-
318+
if (!arr3d) {
319+
fprintf(stderr, "Error allocating DArray3d of dimension %i x %i x %i\n",nx,ny,nz);
320+
exit(-1);
321+
}
281322
// d2
282323
double **p1;
283324
p1 = calloc(nx*ny, sizeof(double *));
284-
325+
if (!p1) {
326+
fprintf(stderr, "Error allocating double** array of dimension %i\n",nx*ny);
327+
exit(-1);
328+
}
285329
for (i=0; i<nx; i++){
286330
arr3d[i] = &(p1[i*ny]);
287331
}
288332

289333
// 3d
290334
double *p2;
291335
p2 = calloc(nx*ny*nz, sizeof(double));
336+
if (!p2) {
337+
fprintf(stderr, "Error allocating double* array of dimension %i\n",nx*ny*nz);
338+
exit(-1);
339+
}
292340
for (i=0; i<nx; i++){
293341
for (j=0; j<ny; j++){
294342
arr3d[i][j] = &(p2[(i*ny+j)*nz]);
@@ -3041,7 +3089,10 @@ void mcdis_polygon(int count, ...){
30413089
x=malloc(count*sizeof(double));
30423090
y=malloc(count*sizeof(double));
30433091
z=malloc(count*sizeof(double));
3044-
3092+
if (!x || !y || !z) {
3093+
fprintf(stderr,"Error initializing polygon set size %i\n",count);
3094+
exit(-1);
3095+
}
30453096
va_start(ap, count);
30463097
// Fallback for trace==1 is multiline, one rank higher
30473098
if (mcdotrace==1) {

common/lib/share/metadata-r.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,17 @@ char * metadata_table_key_component(char* key){
99
char sep[2] = ":\0"; // matches any number of repeated colons
1010
// look for the separator in the provided key; strtok is allowed to modify the string, so copy it
1111
char * tok = malloc((strlen(key) + 1) * sizeof(char));
12+
if (!tok) {
13+
fprintf(stderr,"Error allocating token\n");
14+
exit(-1);
15+
}
1216
strcpy(tok, key);
1317
char * pch = strtok(tok, sep); // this *is* the component name (if provided) -- but we need to move the pointer
1418
char * comp = malloc((1 + strlen(pch)) * sizeof(char));
19+
if (!comp) {
20+
fprintf(stderr,"Error allocating comp\n");
21+
exit(-1);
22+
}
1523
strcpy(comp, pch);
1624
if (tok) free(tok);
1725
return comp;
@@ -20,12 +28,20 @@ char * metadata_table_key_literal(char * key){
2028
if (strlen(key) == 0) return NULL;
2129
char sep[3] = ":\0";
2230
char * tok = malloc((strlen(key) + 1 ) * sizeof(char));
31+
if (!tok) {
32+
fprintf(stderr,"Error allocating token\n");
33+
exit(-1);
34+
}
2335
strcpy(tok, key);
2436
char * pch = strtok(tok, sep); // this *is* the component name (if provided)
2537
if (pch) pch = strtok(NULL, sep); // either NULL or the literal name
2638
char * name = NULL;
2739
if (pch) {
2840
name = malloc((1 + strlen(pch)) * sizeof(char));
41+
if (!name) {
42+
fprintf(stderr,"Error allocating name\n");
43+
exit(-1);
44+
}
2945
strcpy(name, pch);
3046
}
3147
if (tok) free(tok);
@@ -63,6 +79,10 @@ char * metadata_table_name(int no, metadata_table_t * tab, char *key){
6379
for (int i=0; i<no; ++i){
6480
if (!strcmp(comp, tab[i].source)){
6581
name = malloc((strlen(tab[i].name) + 1) * sizeof(char));
82+
if (!name) {
83+
fprintf(stderr,"Error allocating metadata_table_name\n");
84+
exit(-1);
85+
}
6686
strcpy(name, tab[i].name);
6787
break;
6888
}
@@ -131,6 +151,10 @@ void metadata_table_print_all_keys(int no, metadata_table_t * tab){
131151
int metadata_table_print_all_components(int no, metadata_table_t * tab){
132152
int count = 0;
133153
char ** known = malloc(no * sizeof(char*));
154+
if (!known) {
155+
fprintf(stderr,"Error allocating table of known metadata\n");
156+
exit(-1);
157+
}
134158
for (int i=0; i<no; ++i){
135159
int unknown = 1;
136160
for (int j=0; j<count; ++j) if (!strcmp(tab[i].source, known[j])) unknown = 0;
@@ -140,6 +164,10 @@ int metadata_table_print_all_components(int no, metadata_table_t * tab){
140164
for (int i=0; i<count; ++i) nchar += strlen(known[i]) + 1;
141165
char * line = malloc((nchar + 1) * sizeof(char));
142166
char * linetmp = malloc((nchar + 1) * sizeof(char));
167+
if (!line || !linetmp) {
168+
fprintf(stderr,"Error allocating metadata print arrays\n");
169+
exit(-1);
170+
}
143171
line[0] = '\0';
144172
for (int i=0; i<count; ++i) sprintf(linetmp, "%s%s ", line, known[i]);
145173
line=linetmp;

common/lib/share/read_table-lib.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@ void *Table_File_List_store(t_Table *tab){
427427
double *dataf;
428428
s = (float*)data;
429429
dataf = (double*)malloc(sizeof(double)*nelements);
430+
if (!dataf) {
431+
fprintf(stderr, "Could not allocate data block of size %i\n", nelements);
432+
exit(-1);
433+
}
430434
for (i=0; i<nelements; i++)
431435
dataf[i]=s[i];
432436
free(data);
@@ -496,6 +500,10 @@ void *Table_File_List_store(t_Table *tab){
496500
char *line=malloc(1024*CHAR_BUF_LENGTH*sizeof(char));
497501
long back_pos=0; /* ftell start of line */
498502

503+
if (!line) {
504+
fprintf(stderr,"Could not allocate line buffer\n");
505+
exit(-1);
506+
}
499507
back_pos = ftell(hfile);
500508
if (fgets(line, 1024*CHAR_BUF_LENGTH, hfile) != NULL) { /* analyse line */
501509
/* first skip blank and tabulation characters */
@@ -714,7 +722,10 @@ void *Table_File_List_store(t_Table *tab){
714722
return(Table->rows*Table->columns);
715723
}
716724
New_Table = (double*)malloc(Length_Table*Table->columns*sizeof(double));
717-
725+
if (!New_Table) {
726+
fprintf(stderr,"Could not allocate New_Table of size %i x %i\n", Length_Table, Table->columns);
727+
exit(-1);
728+
}
718729
for (i=0; i < Length_Table; i++)
719730
{
720731
long j;

mccode/xlib/share/mcxtrace-r.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ paraboloid_intersect(double *l0, double *l1, double x, double y, double z,
408408
double kx, double ky, double kz, double a, double b, int sign)
409409
{
410410
double A,B,C,D,k;
411-
double a2i,b2i;
411+
double a2i=0,b2i=0;
412412
int retval=0;
413413
if(a!=0 && b!=0){
414414
a2i=1.0/(a*a);b2i=1.0/(b*b);

mcstas-comps/monitors/Event_monitor_simple.comp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ DEFINE COMPONENT Event_monitor_simple
2929

3030
SETTING PARAMETERS (nevents=1e6)
3131

32-
/* Neutron parameters: (x,y,z,vx,vy,vz,t,sx,sy,sz,p) */
3332
DECLARE
3433
%{
3534
DArray2d Events;
@@ -88,14 +87,18 @@ SAVE
8887
FILE * fp;
8988
printf("Storing event logfile --> %s\n",fullfile);
9089
fp = fopen(fullfile, "w+");
90+
if (!fp) {
91+
fprintf(stderr,"Could not open file %s in w+ mode\n",fullfile);
92+
exit(-1);
93+
}
9194
fprintf(fp,"# Event-file %s, contains %lu events\n",fullfile,Nevents);
9295
fprintf(fp,"########################################### START EVENT LIST ##############################################\n");
9396
fprintf(fp,"# id x y z vx vy vz t sx sy sz p # \n");
9497
fprintf(fp,"###########################################################################################################\n");
9598
unsigned long i;
9699
int j;
97100
for (i=0; i<Nevents; i++) {
98-
fprintf(fp,"%6i ",i);
101+
fprintf(fp,"%6lu ",i);
99102
for (j=0; j<11; j++) {
100103
fprintf(fp,"%8.2g ", Events[i][j]);
101104
}

mcxtrace-comps/data/LiNbO3.cif

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#------------------------------------------------------------------------------
2+
#$Date: 2016-02-13 21:28:24 +0200 (Sat, 13 Feb 2016) $
3+
#$Revision: 176429 $
4+
#$URL: svn://www.crystallography.net/cod/cif/1/52/17/1521772.cif $
5+
#------------------------------------------------------------------------------
6+
#
7+
# This file is available in the Crystallography Open Database (COD),
8+
# http://www.crystallography.net/
9+
#
10+
# All data on this site have been placed in the public domain by the
11+
# contributors.
12+
#
13+
data_1521772
14+
loop_
15+
_publ_author_name
16+
'Postnikov, A.V.'
17+
'Caciuc, V.'
18+
'Borstel, G.'
19+
_publ_section_title
20+
;
21+
Structure optimization and frozen phonons in Li Nb O3
22+
;
23+
_journal_name_full 'Journal of Physics and Chemistry of Solids'
24+
_journal_page_first 295
25+
_journal_page_last 299
26+
_journal_volume 61
27+
_journal_year 2000
28+
_chemical_formula_sum 'Li Nb O3'
29+
_chemical_name_systematic 'Li (Nb O3)'
30+
_space_group_IT_number 161
31+
_symmetry_space_group_name_Hall 'R 3 -2"c'
32+
_symmetry_space_group_name_H-M 'R 3 c :H'
33+
_cell_angle_alpha 90
34+
_cell_angle_beta 90
35+
_cell_angle_gamma 120
36+
_cell_formula_units_Z 6
37+
_cell_length_a 5.1378
38+
_cell_length_b 5.1378
39+
_cell_length_c 13.4987
40+
_cell_volume 308.587
41+
_citation_journal_id_ASTM JPCSAW
42+
_cod_data_source_file Postnikov_JPCSAW_2000_1632.cif
43+
_cod_data_source_block Li1Nb1O3
44+
_cod_original_cell_volume 308.5865
45+
_cod_original_formula_sum 'Li1 Nb1 O3'
46+
_cod_database_code 1521772
47+
loop_
48+
_symmetry_equiv_pos_as_xyz
49+
x,y,z
50+
-y,x-y,z
51+
-x+y,-x,z
52+
-y,-x,z+1/2
53+
x,x-y,z+1/2
54+
-x+y,y,z+1/2
55+
x+2/3,y+1/3,z+1/3
56+
-y+2/3,x-y+1/3,z+1/3
57+
-x+y+2/3,-x+1/3,z+1/3
58+
-y+2/3,-x+1/3,z+5/6
59+
x+2/3,x-y+1/3,z+5/6
60+
-x+y+2/3,y+1/3,z+5/6
61+
x+1/3,y+2/3,z+2/3
62+
-y+1/3,x-y+2/3,z+2/3
63+
-x+y+1/3,-x+2/3,z+2/3
64+
-y+1/3,-x+2/3,z+7/6
65+
x+1/3,x-y+2/3,z+7/6
66+
-x+y+1/3,y+2/3,z+7/6
67+
loop_
68+
_atom_site_label
69+
_atom_site_type_symbol
70+
_atom_site_fract_x
71+
_atom_site_fract_y
72+
_atom_site_fract_z
73+
_atom_site_occupancy
74+
_atom_site_U_iso_or_equiv
75+
O1 O-2 0.041 0.3333 0.0833 1 0.0
76+
Nb1 Nb+5 0 0 0 1 0.0
77+
Li1 Li+1 0 0 0.25 1 0.0

0 commit comments

Comments
 (0)