ftp.nice.ch/pub/next/science/chemistry/fdc-esr.1.13.N.b.tar.gz#/fdc-esr/lineshap/sources/lorentz.c

This is lorentz.c in view mode; [Download] [Up]

/*
	LORENTZ.C
	Bruno Bienfait 1989

*/
/*
	C example (lorentzian) for generating a lineshape file for the FDC-ESR program.
	The output file *.asc is a ASCII file which must be compiled by
	the "COMPILER" program.
*/ 


#include<stdlib.h>
#include<stdio.h>
#include<string.h>

#define ANSI

#ifdef ANSI /* functions prototypes */
	void 	write_string( FILE *fp , const char *string ) ;
	void	write_long( FILE *fp, long number ) ;
	void	write_real( FILE *fp, double value ) ;
	void	show_error_and_exit( const char *message)  ;
#else
	#define const /* */
#endif
	
const char filename[] = "lineshap.asc" ;

const int N = 150 ;
const long number =  16000 ; /* This number will give you a ~150K file */	 
const long width_ratio = 16 ; 
const long max_height = 32767 ; /* May not be larger than 65535 */

void main()
{
	long width = 0 ;
	double X = 0.0, X0 = 0.0 ;
	double Y = 0.0 ;
	
	FILE *fp = NULL ;
	
	printf("\n\tLorentzian lineshape ASCII file generator\n");
	printf("for the FDC-ESR program\n");
	printf("\n\tPlease wait\n") ;

/* create a new file called 'filename' :*/	
	if( (fp = fopen( filename, "w" )) == NULL )
		show_error_and_exit("I cannot create the new file") ;
	

/* <1>  write name of the lineshape ( string of maximun N characters ).
	It will be displayed in FDC-ESR while loading the data from disk */		
	write_string( fp, "Lorentzian" ) ;
	printf("\tcalculating the %s lineshape\n", "Lorentzian" ) ;

/* <2> write comment for the linewidth ( string of maximun N characters).
	It will be displayed in FDC-ESR when entering the line width */
	write_string( fp , "half height linewidth" ) ;
	
/* <3> write total number of element in the lineshape */
	write_long( fp, number ) ;

/* <4> write width of the lineshape ( corresponding to comment at <2> ) */
	width = number / width_ratio ;
	write_long( fp, width ) ;

/* <5> write symmetry of the lineshape */
	write_string( fp, "SYMETRIC") ;	/* For the 1st derivative, it would be "ANTISYMETRIC" */ 

/* <6> write the largest unsigned integer value for the digital lineshape ,
		all data values (see <7> and <8> )will be scaled and rounded to a 0 - max_height
		range by the COMPILER program.
	low : decrease precision but decrease overflow risk
	high : increase precision but increase overflow risk
*/		
	if( max_height > 0 && max_height <= 65535U )
		write_long( fp, max_height ) ;
	else
		show_error_and_exit("max_height is not correct") ;


/* <7> The first calculated value comes here */
/* lorentzian function : Y = A*W*W / (W*W + 4*(X0-X)*(X0-X) ) */
/* absolute height is not important ( A*W*W is a constant ) 
		=>  Y = constant / (W*W + 4*(X0-X)*(X0-X) ) */

#define constant 100000.0 
#define W (double)width

	X0 = number / 2 ;
	for( X = 0 ; X < number ; X++ )
	{
		Y = constant / ( W*W + 4*(X0-X)*(X0-X) ) ;
		/*printf(" %6ld\t\t%lf\n", (long)X, Y ) ; */		/* for debugging */
		write_real( fp, Y ) ; /* SHAPE-BIN accept also data in integer format */
	}
	
/* <8> end of the program */
	fclose( fp ) ;
}

/***************************************************************************/

/***************************************************************************/

static char buffer[1000] ;

void
write_string( fp , string )
	FILE *fp ;
	const char *string ;
{
	int length = strlen( string ) ;
	
	if( length > N )
	{
		printf(" String is longer than %d character\n", N );
		show_error_and_exit("") ;
	}
	
	if( fprintf( fp, "%s\n", string ) < 0 )
		show_error_and_exit( " Problem while writing data" );
}
/***************************************************************************/
void
write_long( fp, number ) 
	FILE *fp ;
	long number ;
{
	sprintf( buffer, "%ld\n", number ) ;
 	if( fprintf( fp, "%s", buffer ) < 0 )
		show_error_and_exit( " Problem while writing data" );
}
/***************************************************************************/
void
write_real( fp, value ) 
	FILE *fp ;
	double value ;
{
	sprintf( buffer, "%lf\n", value ) ;
 	if( fprintf( fp, "%s", buffer ) < 0 )
		show_error_and_exit( " Problem while writing data" );
}
/***************************************************************************/
void
show_error_and_exit( message) 
	const char *message ;
{
	printf("\n%s\nPress return to continue\n", message ) ;
	getchar() ;
	exit(0) ;
}
/***************************************************************************/

/* EOF */
	
	
	
	

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.