|
ok, so i've been working on this one for quite a while, and the code is very big so i'm just going to give the relevant parts. this is a program that builds polymers (chemical structures of repeated monomers) which are represented as doubly pointed noncomplete binary trees. There are three different types of monomers (hence the three different constructer calling functions) the first one is the "leaves" of the tree, the second adds length between branch points and the third actually branches the tree (making it binary). There are two vectors which hold the reactive species, they are vectors of pointers which only hold the pointer to the last element in the tree (i.e. the last reactive element of the polymer). At the end of the program one of the vectors is empty, and the other one holds the pointers to the last monomer of each polymer formed. The program works most of the time, but when i run it with large numbers of monomers it fails by saying "polymer44.exe has encountered a problem and needs to close. We are sorry for the inconvenience." and gives me the option to debug, or send microsoft an error report. I've spent a lot of debugging time just to realize that this is probably a problem having to do with my vector "shuffling", or my use of new to create the monomers in the first place. so here is a basic outline of what is happening. any suggestions are welcome and I appreciate your time and effort greatly. :) -
-
//initialize the queue to hold pointers to the polymer chain ends
-
vector<monomer*> nucleophiles;
-
//initialize the queue to hold pointer to the reactive monomers
-
vector<monomer*> electrophiles;
-
-
//function to dynamically allocate memory for the "leaf" monomers
-
void initializechains(vector<monomer*>& nucs, int& chainum, int& namenum)
-
{
-
if(chainum > 0)
-
{
-
for(int i=0; i< chainum; i++)
-
{
-
//pointers for the initialization of the correct monomer
-
monomer* pointforward = NULL;
-
-
//creating and initializing the monomer
-
monomer* monomeri = new monomer(pointforward, namenum);
-
monomeri->namenumber = namenum;
-
-
//inserting the monomer onto its vector
-
nucs.insert(nucs.begin(), monomeri);
-
-
//deleting pointers
-
delete pointforward;
-
monomeri = NULL;
-
-
//resetting namenumber
-
namenum = namenum - 1;
-
}
-
-
}
-
else
-
{
-
cout << "there are no original chains to initialize." <<endl;
-
}
-
-
}
-
-
//same thing but for the additive monomers
-
void initializeaddmons(vector<monomer*>& elecs, int& adds, int& namenum)
-
{
-
if(adds > 0)
-
{
-
for(int i =0; i<adds; i++)
-
{
-
//pointers for the initialization of the correct monomer
-
monomer* pointforward = NULL;
-
monomer* pointbackward = NULL;
-
-
//creating and initializing the monomer
-
monomer* addmonomeri = new monomer(pointforward, pointbackward);
-
addmonomeri->namenumber = namenum;
-
-
//inserting the monomer onto its vector
-
elecs.insert(elecs.begin(), addmonomeri);
-
-
//deleting pointers
-
delete pointforward;
-
delete pointbackward;
-
addmonomeri = NULL;
-
-
//resetting namenumber
-
namenum = namenum + 1;
-
}
-
}
-
else
-
{
-
//cout << "there are no additive monomers to initialize." <<endl;
-
}
-
-
}
-
-
//same thing again but for the branching monomers
-
void initializebranchmons(vector<monomer*>& elecs, int& branchers, int& namenum)
-
{
-
if(branchers > 0)
-
{
-
for(int i = 0; i<branchers; i++)
-
{
-
//pointers for the initialization of the correct monomer
-
monomer* pointforward = NULL;
-
monomer* pointbackward = NULL;
-
monomer* pointbranch = NULL;
-
-
//creating and initializing the monomer
-
monomer* branchmoni = new monomer(pointforward, pointbackward, pointbranch);
-
branchmoni->namenumber = namenum;
-
-
//inserting the monomer onto its vector
-
elecs.insert(elecs.begin(), branchmoni);
-
-
//deleting pointers
-
delete pointforward;
-
delete pointbackward;
-
delete pointbranch;
-
branchmoni = NULL;
-
-
//resetting namenumber
-
namenum = namenum + 1;
-
}
-
}
-
else
-
{
-
cout << "there are no branching monomers to initialize." <<endl;
-
}
-
-
}
-
-
void addmonomer(vector<monomer*>& nucs, vector<monomer*>& elecs, int& nucount, int& elecount, double addrate_4, double branchrate1_4, double branchrate2_4, double alpha_6, int iter3)
-
{
-
//checks
-
//int elesizecheck = elecs.size();
-
//int nucsizecheck = nucs.size();
-
-
//declare the iterators
-
vector<monomer*>::iterator nuchooser = nucs.begin();
-
vector<monomer*>::iterator elechooser = elecs.begin();
-
-
//set the iterators
-
for(int nucount2 = 0; nucount2 != nucount; ++nucount2)
-
{
-
++nuchooser;
-
}
-
-
for(int elecount2 = 0; elecount2 != elecount; ++elecount2)
-
{
-
++elechooser;
-
}
-
-
//see if the nucleophile and electrophile are in the same polymer
-
bool loopy = 0;
-
looper(nucs[nucount], loopy, elecs[elecount]->namenumber);
-
if(elecs.size()==1 && nucs.size()==1 && (elecs[elecount]->namenumber == nucs[nucount]->namenumber || loopy ==1))
-
{
-
nucs.erase(nuchooser);
-
}
-
else
-
{
-
-
//protection against a monomer reacting with itself
-
if(nucs.size()==1 && elecs.size()>1 && (elecs[elecount]->namenumber == nucs[nucount]->namenumber || loopy ==1))
-
{
-
while(elecs[elecount]->namenumber == nucs[nucount]->namenumber)
-
{
-
determine_electrophile(elecs, elecount, addrate_4, branchrate1_4, branchrate2_4, alpha_6);
-
}
-
-
//reset the electrophile iterator
-
elechooser = elecs.begin();
-
-
for(int elecount2 = 0; elecount2 != elecount; ++elecount2)
-
{
-
++elechooser;
-
}
-
}
-
else if(elecs.size()==1 && nucs.size()>1 && (elecs[elecount]->namenumber == nucs[nucount]->namenumber || loopy ==1))
-
{
-
while(elecs[elecount]->namenumber == nucs[nucount]->namenumber)
-
{
-
determine_nucleophile(nucs, nucount, alpha_6, iter3);
-
}
-
-
//reset the nucleophile iterator
-
nuchooser = nucs.begin();
-
-
for(int nucount2 = 0; nucount2 != nucount; ++nucount2)
-
{
-
++nuchooser;
-
}
-
}
-
-
//additive monomer reaction
-
if(elecs[elecount]->tag == 2)
-
{
-
//reaction
-
nucs[nucount]->next = &*(elecs[elecount]);
-
elecs[elecount]->previous = &*(nucs[nucount]);
-
-
//reset the molecular weight, number of nodes, number of original chains
-
elecs[elecount]->molweight = elecs[elecount]->molweight + nucs[nucount]->molweight;
-
elecs[elecount]->nodes = elecs[elecount]->nodes + nucs[nucount]->nodes;
-
elecs[elecount]->originalchains = elecs[elecount]->originalchains + nucs[nucount]->originalchains;
-
-
//vector shuffling
-
nuchooser = nucs.erase(nuchooser);
-
nuchooser = nucs.insert(nuchooser, elecs[elecount]);
-
elechooser = elecs.erase(elechooser);
-
}
-
-
//branching monomer reaction
-
else if(elecs[elecount]->tag == 3)
-
{
-
//fast site
-
if(elecs[elecount]->nextbranchtag == 2)
-
{
-
bool loopy2 = 0;
-
looper(nucs[nucount], loopy2, elecs[elecount]->namenumber);
-
-
if(loopy2==0)
-
{
-
//set the monomers fast site as reacted
-
elecs[elecount]->branchfull = 1;
-
-
//reaction
-
nucs[nucount]->next = &*(elecs[elecount]);
-
elecs[elecount]->branch = &*(nucs[nucount]);
-
-
//reset the molecular weight, number of nodes, number of original chains
-
elecs[elecount]->molweight = elecs[elecount]->molweight + nucs[nucount]->molweight;
-
elecs[elecount]->nodes = elecs[elecount]->nodes + nucs[nucount]->nodes;
-
elecs[elecount]->originalchains = elecs[elecount]->originalchains + nucs[nucount]->originalchains;
-
-
//vector shuffling
-
nuchooser = nucs.erase(nuchooser);
-
//if electrophile has no electrophilic sites left
-
if(elecs[elecount]->nextfull)
-
elechooser = elecs.erase(elechooser);
-
}
-
}
-
-
//slow site
-
else if(elecs[elecount]->nextbranchtag == 1)
-
{
-
//set the monomers slowsite as reacted
-
elecs[elecount]->nextfull = 1;
-
-
//reaction
-
elecs[elecount]->previous = &*(nucs[nucount]);
-
nucs[nucount]->next = &*(elecs[elecount]);
-
-
//reset the molecular weight, number of nodes, number of original chains
-
elecs[elecount]->molweight = elecs[elecount]->molweight + nucs[nucount]->molweight;
-
elecs[elecount]->nodes = elecs[elecount]->nodes + nucs[nucount]->nodes;
-
elecs[elecount]->originalchains = elecs[elecount]->originalchains + nucs[nucount]->originalchains;
-
-
//vector shuffling
-
nuchooser = nucs.erase(nuchooser);
-
nuchooser = nucs.insert(nuchooser, elecs[elecount]);
-
//electrophile has no electrophilic sites left
-
if(elecs[elecount]->branchfull)
-
elechooser = elecs.erase(elechooser);
-
-
}
-
}
-
}
-
}
-
-
//the basic run algorithm is as follows
-
while(nucleophiles.size()!=0)
-
{
-
//reinitialize counters
-
nucounter = 0;
-
elecounter = 0;
-
-
if(!electrophiles.empty())//if the elecrophiles queue is not empty there are still more monomers to react
-
{
-
determine_electrophile(electrophiles, elecounter, addprob, branchprob1, branchprob2, alpha);
-
determine_nucleophile(nucleophiles, nucounter, alpha, iter);
-
-
addmonomer(nucleophiles, electrophiles, nucounter, elecounter, addprob, branchprob1, branchprob2, alpha, iter);
-
}
-
else//if the electrophiles queue is empty we add more monomers to react
-
{
-
initializeaddmons(electrophiles, addnum, namenumber);
-
initializebranchmons(electrophiles, branchnum, namenumber);
-
}
-
iter = iter+1;
-
}
-
| |