⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cdalloclt.c

📁 一个很好的分子动力学程序
💻 C
字号:
/*************************************************************************File Includes*************************************************************************/#include <stdlib.h>#include <stdio.h>#include <string.h>#include "particle.h"#include "cdhouse.h"#include "cdalloc.h"#ifndef XMDVIEW#define XMDVIEW#endif/*************************************************************************Macros*************************************************************************//*  Macros used in CopyParticle()  */#define COPY_COORD(ARRAY,TARGET,SOURCE) \	if (ARRAY != NULL) \		memcpy  \		(&ARRAY[NDIR*TARGET], &ARRAY[NDIR*SOURCE], NDIR*sizeof(ARRAY[0]));#define COPY_ELEM(ARRAY,TARGET,SOURCE) \	if (ARRAY != NULL) \		memcpy (&ARRAY[TARGET], &ARRAY[SOURCE], sizeof(ARRAY[0]));/*************************************************************************Global Variables*************************************************************************//*************************************************************************Local Function Prototypes*************************************************************************/void ReAllocateExternalVectorList(ExternalVector_t ***List,int NumPart,int OldNumPart);#ifdef XMDVIEWvoid InitializeDepthIndex (WORD *DepthIndex, int NumIndex);#endif/*************************************************************************Exported Functions*************************************************************************//*Re-allocate or Newly Allocate particle coordinates andall arrays whose size depends on number of particles.(if they are already in use, determined if pointer not NULL)Arrays such as neighbor list arrays are not affected.*/void ReallocateParticle (Particle_t *a, int NewNumPart)	{	int OldNumCoord;	int NewNumCoord;	int OldNumPart;	int ipart;	OldNumPart = a->np;	NewNumCoord = NDIR * NewNumPart;	OldNumCoord = NDIR * OldNumPart;	/*  Test for NewNumPart exceeding Maximum nuber of particles  */#if 0	if (NewNumPart > MaxNumPart_g)		{		printf ("ERROR (ReallocateParticle):  Request allocation (%i) ",			NewNumPart);		printf ("exceeds maximum specified (%i) in ALLOCATE command.\n",			MaxNumPart_g);		CleanAfterError();		}#endif	/*	NOTE:		The arrays (cur, ngh, v, eatom, sel, type, mass, eatom)		are allocated the maximum size (MaxNewNumPart_g) at the start of program		(in function palloc() above).  There is no need to change their		allocations.	*/	if (a->ngh   != NULL)   REALLOC(a->ngh,  double,OldNumCoord,NewNumCoord)	if (a->ref   != NULL)   REALLOC(a->ref,  double,OldNumCoord,NewNumCoord)	if (a->v     != NULL)   REALLOC(a->v  ,  double,OldNumCoord,NewNumCoord)	if (a->f     != NULL)   REALLOC(a->f  ,  double,OldNumCoord,NewNumCoord)	if (a->c2    != NULL)   REALLOC(a->c2 ,  double,OldNumCoord,NewNumCoord)	if (a->c3    != NULL)   REALLOC(a->c3 ,  double,OldNumCoord,NewNumCoord)	if (a->c4    != NULL)   REALLOC(a->c4 ,  double,OldNumCoord,NewNumCoord)	if (a->c5    != NULL)   REALLOC(a->c5 ,  double,OldNumCoord,NewNumCoord)	if (a->disp  != NULL)   REALLOC(a->disp, double,OldNumCoord,NewNumCoord)	if (a->Damp  != NULL)   REALLOC(a->Damp, double,OldNumCoord,NewNumCoord)#ifdef XMDVIEW	if (a->cur   != NULL)   REALLOC(a->cur,  double,OldNumCoord,NewNumCoord)	if (a->type  != NULL)   REALLOC(a->type, BYTE,  OldNumPart, NewNumPart )	if (a->DepthIndex != NULL)		{	   REALLOC(a->DepthIndex, WORD ,OldNumCoord,NewNumCoord)		InitializeDepthIndex (a->DepthIndex, NewNumCoord);		}#endif	/*  Reallocate other arrays  */	if (a->rtype != NULL)   REALLOC(a->rtype,BYTE,OldNumPart,NewNumPart)	if (a->tag   != NULL)   REALLOC(a->tag  ,BYTE,OldNumPart,NewNumPart)	/*  For constraint array, free unneeded nodes if array will shrink  */	if (NewNumPart < OldNumPart && a->cons != NULL)		for (ipart=NewNumPart; ipart<OldNumPart; ipart++)			{			FREE (a->cons[ipart])			}	/*  Now reallocate cons array  */	if (a->cons  != NULL)			REALLOC (a->cons,Constraint_t*, OldNumPart, NewNumPart)	/*	Reallocate External Vector arrays (SpringPtrList and ForcePtrList)	*/#if 0	ReAllocateExternalVectorList (&a->SpringPtrList, NewNumPart, OldNumPart);	ReAllocateExternalVectorList (&a->ForcePtrList,  NewNumPart, OldNumPart);#endif	/*  Save new number of particles  */	a->np = NewNumPart;	}/*Allocate particle required arrays.Set optional array pointers to NULL*/Particle_t *palloc (int InputNumPart, int InputNumNeigh)	{	int i;	Particle_t *a = NULL;	/*  Test for program limit to number of particles	*/	/*	NOTE:		This limit is imposed by a->NeighborList[], which is of type	unsigned short int (which is defined as USHORT in the program).	a->NeighborList[] contains the indices of particles, and since	USHORT can is only two bytes, the largest number it can hold	is 65535 (0xffff).		To increase the maximum number of particles, change USHORT	to unsigned int (or under DOS, unsigned long).	*/#if 0	/*	Use unsigned int variable TestWord	to avoid Borland C++ Compiler error	*/	{	unsigned int TestWord;	TestWord = InputMaxNumPart;	if (TestWord > 0xffff)		{		printf ("ERROR (palloc):  Number of particles cannot exceed %u\n",			0xffff);		CleanAfterError();		}	}#endif	/*  MAKE SURE COORD ARRAYS ARE NULL FOR CRDALLOC TO WORK PROPERLY  */	ALLOCATE (a,							  Particle_t,		  1			  )	ALLOCATE (a->cur,                  double,      NDIR*InputNumPart)	ALLOCATE (a->type,					  BYTE,				  InputNumPart)#ifdef XMDVIEW	ALLOCATE (a->DepthIndex,           WORD,             InputNumPart)	InitializeDepthIndex (a->DepthIndex, InputNumPart);#endif#if 0	ALLOCATE (a->NeighborListIndex,	  int,				  InputNumPart)	ALLOCATE (a->NeighborListLength,   int,				  InputNumPart)	ALLOCATE (a->NeighborList, 		  USHORT,			  InputNumNeigh)	ALLOCATE (a->Selection,            USHORT,           InputNumPart)	ALLOCATE (a->mass,					  double,			  InputNumPart)	ALLOCATE (a->eatom,					  double,			  InputNumPart)#endif	a->NeighborListIndex = NULL;	a->NeighborListLength = NULL;	a->NeighborList = NULL;	a->Selection = NULL;	a->mass = NULL;	a->eatom = NULL;	/* Set optional array pointers to NULL  */	a->ref	 = NULL;	a->ngh	 = NULL;	a->v		 = NULL;	a->c2 	 = NULL;	a->c3 	 = NULL;	a->c4 	 = NULL;	a->c5 	 = NULL;	a->rtype  = NULL;	a->cons	 = NULL;	a->disp	 = NULL;	a->Damp   = NULL;	/*	Set counters	*/	a->nfix	 = 0;	/* INITIALIZE FLAGS */	a->coordflag = FALSE;	a->boxflag	 = FALSE;	a->InvalidNeighborList = TRUE;	for (i=0;i<3;i++)		a->surf[i] = FALSE;	/*  Initialize counts and averages	*/	a->ebath   =  0.0;	a->np 	  =  0;	a->ng 	  =  0;	a->nsel	  =  0;	a->nbin	  = 100;	a->step	  =  0;	a->run	  =  0;	/*  Intialize title to default string *title*  */	LOOP (i, 8)		strcpy (a->title[i], "*title*");	/*  Initialize Damping   */	a->UseDamp = FALSE;	/*  Initialize Cavity  */	a->UseCavity = FALSE;	/*  Initialize volume settings  */	a->BoxMotionAlgorithm = BMA_NONE;	a->BoxMotionGeometry  = BMG_ISOTROPIC;	/*  Initialize Repeating Boundary Geometry  */	a->BoundaryType = BD_ORTHONORMAL;	/*  RETURN POINTER  */	return a;	}#ifdef XMDVIEWvoid InitializeDepthIndex (WORD *DepthIndex, int NumIndex)	{	int i;	LOOP (i, NumIndex)		DepthIndex[i] = i;	}#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -