Thread: Error when trying to read and process larger files

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    28

    Error when trying to read and process larger files

    Hi all,

    I am facing some weird (at least that seems to me) phenomenon in running my code.
    I have a code, which needs to read inputs from files, processes the data and output it into a file as well. I have been trying with smaller files and that seems to work. So, I was thinking to do an actual run where the files are bigger and it is unable to proceed.

    I tried it with Code:Blocks and its default compiler, and also with Windows Visual Studio 2010 (copy paste my whole code and keep the <stdafx.h> header). I even try to run the files only with one of the smaller functions I have.

    It feels to me that it has something to do with memory? So, should I look up malloc etc. (which I tried to look into before but end up not getting it at all)?
    I am from an engineering background and am not too familiar with all of the custom functions.

    I have attached my main.c as well as the two files I want it to read. The code could be a bit messy at the moment but it is working with smaller files.

    Would really be glad to learn the reason and also the solution of this going wrong!
    Last edited by tanjinjack; 03-24-2011 at 10:11 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please post your code inside code tags.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What is this weird phenomenon? What does "unable to proceed" mean? Also, I don't see any attached files.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    28
    Sorry, I didn't find out that the .zip isn't attached successfully.

    Here, the .zip file, containing my main.c and also the two inputs files I want to read.

    Quote Originally Posted by anduril462 View Post
    What is this weird phenomenon? What does "unable to proceed" mean? Also, I don't see any attached files.
    As I mentioned earlier, the code works as I like when I am using a smaller file. It breaks when I try with the bigger file which I have no idea why is that. I have changed the necessary variables but it still doesn't work right.

    Attachment please check #14.
    Last edited by tanjinjack; 03-25-2011 at 06:44 AM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    When I run your code through the debugger it segfaults in your void thermoData(void) function at this line:
    Code:
    				a[k][14]=atof(buff2);
    The value of k at this point was 774976307 which is clearly out of bounds for this array.

    What does the smaller file that worked actually contain?

    What are you trying to write to your Sthermo.dat output file in this function?

    Jim

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Good lord... I had a look at your code... Who on earth taught you to write code like that?

    1) Try to minimize the use of global variables... that's way ridiculous.
    2) Every function is void function (void) which is just crap lazy coding.
    3) The layout of your source is practically unreadable... learn proper formatting and indentation
    4) The only place you didn't use void was the one place you should it's... int main (void)

    Really... You gotta clean that up....

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    28
    Quote Originally Posted by jimblumberg View Post
    When I run your code through the debugger it segfaults in your void thermoData(void) function at this line:
    Code:
    				a[k][14]=atof(buff2);
    The value of k at this point was 774976307 which is clearly out of bounds for this array.

    What does the smaller file that worked actually contain?

    What are you trying to write to your Sthermo.dat output file in this function?

    Jim
    The smaller file has the same format, only difference is that it contains less data. I want to output a bunch of numbers to my Sthermo.dat and that works for the smaller input file.
    I have uploaded a thermo30.txt, which is the "smaller input file" I use in developing my code. It's originally in .dat but I can't seem to be able to attach .dat, so I change it to .txt. Feel free to change it to .dat to suit the code or change the name of the file to be read inside the code. (When using notepad to open that, it's not organised. But it looks nice if you open via notepad++)

    I tried to go sections by sections to find out where did the code stop. It stops right before it enters the processRxn(). However, some of the output files in the fetchRxn() doesn't output as expected. What could be the problem?

    Quote Originally Posted by CommonTater View Post
    Good lord... I had a look at your code... Who on earth taught you to write code like that?

    1) Try to minimize the use of global variables... that's way ridiculous.
    2) Every function is void function (void) which is just crap lazy coding.
    3) The layout of your source is practically unreadable... learn proper formatting and indentation
    4) The only place you didn't use void was the one place you should it's... int main (void)

    Really... You gotta clean that up....
    Yes, it indeed needs some cleaning up. I am sorry for the mess.
    Some of the global variables are obsolete as I manage to find a better way for my purposes, which I could clean up later. Could the whole load of global variables be a problem as well?

    Is there any reasons that I should avoid using void?

    Here's the link to the thermo30.dat file, without any change of the extension.
    http://www.me.berkeley.edu/gri_mech/...0/thermo30.dat
    Last edited by tanjinjack; 03-24-2011 at 11:13 AM.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Compile with warnings cranked up to the max, then make sure you fix them all. I got the following, which is definitely a problem:
    file.c:731: warning: function returns address of local variable
    That's in your str_replace function. You can't return "buffer", since that's basically a pointer to a local variable. Once the function returns, you have no idea what is actually at that address.

    Tater is right, your code is quite a mess. I know you don't have a heavy programming background, so don't take it too hard, but you need some clean up.

    • Read about why globals are evil here.
    • Also, you shouldn't use goto. At least use break or continue to get out of a loop early or skip to the next iteration.
    • You only have 2 functions under 100 lines. Try to split those massive functions up into smaller chunks that make it easier to read.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    28
    Quote Originally Posted by anduril462 View Post
    Compile with warnings cranked up to the max, then make sure you fix them all. I got the following, which is definitely a problem:

    That's in your str_replace function. You can't return "buffer", since that's basically a pointer to a local variable. Once the function returns, you have no idea what is actually at that address.

    Tater is right, your code is quite a mess. I know you don't have a heavy programming background, so don't take it too hard, but you need some clean up.

    • Read about why globals are evil here.
    • Also, you shouldn't use goto. At least use break or continue to get out of a loop early or skip to the next iteration.
    • You only have 2 functions under 100 lines. Try to split those massive functions up into smaller chunks that make it easier to read.
    Thanks for the comment. The str_replace function is actually a function I copy off from a website, of which I have credited in my code along with the link which it could be found. I do not have issues using it though. I tried to return *buffer though, but it breaks.

    I tried to avoid using "goto" at first but I don't seem to get it work the way I want using break and continue, sort of a last resort. After I got everything working finally though I may go back to fix that up. Thought to breaking them out into smaller chunks as well, but as I got it working the way I want, I don't want to change it for the time being.

    Other than the warnings and the messy code of which I will look them up to try to make it better, do you all face what I face, (i.e. the program breaks when I try to run it - it doesn't reach "press any key to continue." at all when I read the larger files but it does for the smaller files)? Could it be my weaker computer?

    It's late here and I am about to sleep, will check back this thread the next day and also will try some cleaning up, too!

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Could it be my weaker computer?
    NO, you have only allocated memory for an array size of 160
    Code:
    #define nSpecies 160
    yet the larger file contains at least 500.

    And as I stated in my previous post I got a segfault at about line 632 in your thermoData function so there is a problem in your loop in that function.

    You should probably be handling any lines that start with a '!' as comments and the thermo line and the next line should be processed before your loop.


    Jim
    Last edited by jimblumberg; 03-24-2011 at 12:00 PM.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tanjinjack View Post
    Yes, it indeed needs some cleaning up. I am sorry for the mess.
    Some of the global variables are obsolete as I manage to find a better way for my purposes, which I could clean up later. Could the whole load of global variables be a problem as well?

    Is there any reasons that I should avoid using void?
    What you should do is try to minimize the use of global variables into non-existence.

    Define a minimum set of variables at the top of each function.

    Each function should perform a specific single task on variables passed in and return the results on it's way out... Here's a trivial example...
    Code:
     
    #include <stdio.h>
    
    // calculate loop load
    int LoopMultiply(int Bias, int Preload)
       { int Result = 0;  // return value
          int x;          // loop counter
          for (x = 0; x < Bias; x++)
             { Result += x * Preload; }
          return Result; }
    
    // entry point
    int main (void)
      {  int Loops = 5;       // active loop count
          int Prefactor = 6;  // preload factor
          int Load = 0;       // calculation result
         
          Load = LoopMultiply(Loops,Prefactor);
          printf("System load = %d", Load);
          
         return 0; }
    The design of the lanugage is such that raw values go int the top of your function and results come out the bottom. Your programs run by passing values around and operating on them.

    The problem with using void function (void) for everything is that all those global variables are at risk of being modified in ways you won't understand... a buffer overflow, an integer overflow, bad memory management etc. can all affect the validity of global values. The more encapsulated your code is the less the risk of catastrophic failure for untraceable reasons.

    The extensive use of global variables has been frowned upon since GWBASIC was largely replaced by Pascal... in the late 1970s.

    Moreover... indentation, a simple text setup operation --typing skill-- can go a very long way towards making code more readable. It is a worthwhile skill and should be a habit... especially if it's likely you're going to have to come back to this code in a couple of years and update it.

    Indent style - Wikipedia, the free encyclopedia

    The same is true of commenting... don't be afraid to type a line or two to say what a section of code is doing. *Especially* if it's effect is not entirely obvious.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Never in my life have I seen so many arrays. Why do I think you really need to make use of some structs here?

    The code, indented:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define nSpecies  160
    #define numForwardRxn 1540
    
    void fetchRxn(void);
    void processRxn(void);
    void thermoData(void);
    char *replace_str(char *str, char *orig); //credit to itsme86 from linuxquestions.org "http://goo.gl/F3YpH"//
    
    char speciesName[nSpecies][25];
    double ArrhFac[numForwardRxn];
    double tempN[numForwardRxn];
    double energyAct[numForwardRxn];
    double thermo[nSpecies][15];
    int numRxnThisSpc[nSpecies];
    int rxnID_SpcN[nSpecies][numForwardRxn];
    int stoichioNum[nSpecies][numForwardRxn];
    int numReactSpc[numForwardRxn];
    int numProdtSpc[numForwardRxn];
    int reactSpcIDNum[numForwardRxn][5];
    int prodtSpcIDNum[numForwardRxn][5];
    double cpCoeff4EqmConst[nSpecies][15];
    int numPressuDpdRxn=0;
    int rxnID_PrDpdRxn[numForwardRxn];
    double koa[numForwardRxn];
    double kotn[numForwardRxn];
    double koe[numForwardRxn];
    double alfa[numForwardRxn];
    double tts[numForwardRxn];
    double ts[numForwardRxn];
    double tss[numForwardRxn];
    
    int nn[numForwardRxn];
    char speciesTBSR[numForwardRxn][nSpecies][30];
    double lpl[numForwardRxn][3];
    double troe[numForwardRxn][4];
    char rxnFormula[numForwardRxn][110];
    
    char buff[256];
    
    FILE *fp;
    
    
    int main()
    {
        fetchRxn();
        puts("past here?");
        processRxn();
    
        thermoData();
    
        return 0;
    }
    
    
    void fetchRxn(void)
    {
        int i=0,j[numForwardRxn],k=0, TBSRcount[numForwardRxn], LPLcount[numForwardRxn],
            n=0,numTBSRonly=0, numLPLonly=0,
            b=0,c=0,LPLcheck=0,
            e=0;
        int RxnTBSR[numForwardRxn];
        FILE *fRxn, *fatne,*frcpm, *ftbsr;
        int RxnLPL[numForwardRxn];
    
    
        double coeffTBSR[numForwardRxn][nSpecies];
        char dummy[30];
        for(c=0;c<numForwardRxn;c++)
        {
            j[c]=0;
            RxnTBSR[c]=0;
            LPLcount[c]=0;
            TBSRcount[c]=0;
            for(e=0;e<nSpecies;e++)
                coeffTBSR[c][e]=0;
    
        }
    
    
        fp=fopen("try2.out","r");
        while ( fgets( buff, sizeof(buff)-1, fp ) != NULL )
        {
    
    
            if(sscanf(buff,"%d. %s %lf %lf %lf",&nn[i],rxnFormula[i],&ArrhFac[i],&tempN[i],&energyAct[i])==5)
            {
    
                sprintf(rxnFormula[i],"%s",(replace_str((replace_str(rxnFormula[i], "(+M)")),"(+M)")));
                sprintf(rxnFormula[i],"%s",(replace_str((replace_str(rxnFormula[i], "+M")),"+M")));
                // sprintf(rxnFormula[i],"%s",(replace_str((replace_str(rxnFormula[i], "<")),"<")));
                // sprintf(rxnFormula[i],"%s",(replace_str((replace_str(rxnFormula[i], ">")),">")));
    
                // printf("%2d - %25s - %10.3e %10.3e %10.3e\n",nn[i],rxnFormula[i],ArrhFac[i],tempN[i],energyAct[i]);
                // system("PAUSE");
                i++;
                j[i]=0;
                LPLcheck=0;
    
            }
    
            else if(strstr(buff,"Enhanced")!=NULL)
            {
                if(j[i]==0)//&&LPLcheck==0)
                {
    
                    RxnTBSR[numTBSRonly]=i;
    //printf("%d,%d\n",numTBSRonly,RxnTBSR[numTBSRonly]);
                    numTBSRonly++;
                    //printf("%d:TBSR includes %d\n",numTBSRonly-1,RxnTBSR[numTBSRonly-1]);
                }
                //else if(j==0&&LPLcheck==1)
    
                sscanf(buff,"%s %*s %*s %s",speciesTBSR[i][j[i]],dummy); //coeffTBSR[RxnID][j]
                {
                    coeffTBSR[i][j[i]]=atof(dummy);
                    //if(LPLcheck==0)
                    //printf("3rd body surface rxn for rxn %d, %s species with enhancement of %e\n",RxnTBSR[numTBSRonly-1],speciesTBSR[i][j],coeffTBSR[i][j]);
    
                    //if(LPLcheck==0)
    
                    j[i]++;
                    TBSRcount[numTBSRonly-1]=j[i];
                    // if(i==43)
                    //printf("j %d in TBSRRxn %d\n",TBSRcount[numTBSRonly-1],numTBSRonly-1);
    
                }
    
    
            }
            else if(strstr(buff,"Low pressure limit")!=NULL)
            {
                sscanf(buff,"%*s %*s %*s\t%lf\t%lf\t%lf",&lpl[i][0],&lpl[i][1],&lpl[i][2]);
                //printf("Low pressure limit: %e %e %e\n",lpl[i][0],lpl[i][1],lpl[i][2]);
    //tell "Enhanced by" to delete the LPL off//
    //LPLcheck=1;
                {
                    RxnLPL[numLPLonly]=i;
                    // LPLcount[numLPLonly]=TBSRcount[numTBSRonly-1];
                    LPLcount[numLPLonly]=j[i];
                    numTBSRonly--;
    
                    numLPLonly++;
    
    //            goto skip;
                }
    
    
            }
    
    
            else if(strstr(buff,"TROE centering")!=NULL)
            {
    
                numLPLonly--;
                sscanf(buff,"%*s %*s\t%lf\t%lf\t%lf\t%lf",&troe[i][0],&troe[i][1],&troe[i][2],&troe[i][3]);
                // printf("%E %E %E %E\n",troe[i][0],troe[i][1],troe[i][2],troe[i][3]);
                //printf("Low pressure limit: %e %e %e\n",lpl[i][0],lpl[i][1],lpl[i][2]);
                koa[numPressuDpdRxn]=lpl[i][0]; lpl[i][0]=0; //zero to avoid messing up
                kotn[numPressuDpdRxn]=lpl[i][1]; lpl[i][1]=0;
                koe[numPressuDpdRxn]=lpl[i][2]; lpl[i][2]=0;
                alfa[numPressuDpdRxn]=troe[i][0];
                tts[numPressuDpdRxn]=troe[i][1];
                ts[numPressuDpdRxn]=troe[i][2];
                tss[numPressuDpdRxn]=troe[i][3];
                rxnID_PrDpdRxn[numPressuDpdRxn]=(i); //first i is zero, may need i instead of (i-1)//
                // printf("%d. pressure dependent rxn is %d\n",numPressuDpdRxn,i);
                numPressuDpdRxn++;
    
            }
    
        }
    
        remove("Reaction.txt");
        fRxn=fopen("Reaction.txt","a+");
        for(i=0;i<numForwardRxn;i++)  fprintf(fRxn,"%s\n",rxnFormula[i]);
        fclose(fRxn);
        remove("atne.txt");
        fatne=fopen("atne.txt","a+");
        for(i=0;i<numForwardRxn;i++) fprintf(fatne,"%E\n",ArrhFac[i]);
        for(i=0;i<numForwardRxn;i++) fprintf(fatne,"%E\n",tempN[i]);
        for(i=0;i<numForwardRxn;i++) fprintf(fatne,"%E\n",energyAct[i]);
        fclose(fatne);
    
        remove("rcpm.txt");
        frcpm=fopen("rcpm.txt","a+");
        for(i=0;i<numPressuDpdRxn;i++) fprintf(frcpm,"%d\n",rxnID_PrDpdRxn[i]);
        fclose(frcpm);
        remove("Pm.txt");
        frcpm=fopen("Pm.txt","a+");
        for(i=0;i<numPressuDpdRxn;i++) fprintf(frcpm,"%E\n",koa[i]);
        for(i=0;i<numPressuDpdRxn;i++) fprintf(frcpm,"%E\n",kotn[i]);
        for(i=0;i<numPressuDpdRxn;i++) fprintf(frcpm,"%E\n",koe[i]);
        for(i=0;i<numPressuDpdRxn;i++) fprintf(frcpm,"%E\n",alfa[i]);
        for(i=0;i<numPressuDpdRxn;i++) fprintf(frcpm,"%E\n",tts[i]);
        for(i=0;i<numPressuDpdRxn;i++) fprintf(frcpm,"%E\n",ts[i]);
        for(i=0;i<numPressuDpdRxn;i++) fprintf(frcpm,"%E\n",tss[i]);
    
        remove("tbsr.txt");
        ftbsr=fopen("tbsr.txt","a+");
        for(i=0;i<numTBSRonly;i++)
        {
            fprintf(ftbsr,"%d\n",RxnTBSR[i]);
    //printf("TBSR includes %d \n",RxnTBSR[i]);
    
        }
        fclose(ftbsr);
        remove("tbsrcount.txt");
        ftbsr=fopen("tbsrcount.txt","a+");
        for(i=0;i<numTBSRonly;i++)
        {
            fprintf(ftbsr,"%d\n",j[RxnTBSR[i]]);
        }
        remove("Stbsr.txt");
        ftbsr=fopen("Stbsr.txt","a+");
        for(i=0;i<numTBSRonly;i++)
        {
            //for(j=0;j<TBSRcount[i];j++)
            //printf("i=%d, TBSRcount=%d\n",RxnTBSR[i],TBSRcount[i]);
            for(k=0;k<TBSRcount[i];k++) fprintf(ftbsr,"%s\n",speciesTBSR[RxnTBSR[i]][k]);
        }
        for(i=0;i<numTBSRonly;i++)
        {
    
            for(k=0;k<TBSRcount[i];k++) fprintf(ftbsr,"%E\n",coeffTBSR[RxnTBSR[i]][k]);
        }
        fclose(ftbsr);
    
        FILE *flpl;
        remove("lpl.txt");
        remove("lplcount.txt");
        remove("Slpl.txt");
        flpl=fopen("lpl.txt","a+");
        for(i=0;i<numLPLonly;i++)
        {
            fprintf(flpl,"%d\n",RxnLPL[i]);
            //printf("%d,LPLcount = %d\n",RxnLPL[i],LPLcount[numLPLonly]);
        }
        fclose(flpl);
        flpl=fopen("lplcount.txt","a+");
        for(i=0;i<numLPLonly;i++)
            fprintf(flpl,"%d\n",j[RxnLPL[i]]);
        fclose(flpl);
        flpl=fopen("Slpl.txt","a+");
        for(i=0;i<numLPLonly;i++)
        {
            //for(k=0;j<LPLcount[i];k++)
            //printf("i=%d, LPLcount=%d\n",RxnLPL[i],LPLcount[i]);
            for(k=0;k<LPLcount[i];k++) fprintf(flpl,"%s\n",speciesTBSR[RxnLPL[i]][k]);
        }
        for(i=0;i<numLPLonly;i++)
        {
    
            for(k=0;k<LPLcount[i];k++) fprintf(flpl,"%E\n",coeffTBSR[RxnLPL[i]][k]);
        }
        fclose(flpl);
        remove("coefflpl.txt");
        flpl=fopen("coefflpl.txt","a+");
        for(i=0;i<numLPLonly;i++) fprintf(flpl,"%E\n",lpl[RxnLPL[i]][0]);
        for(i=0;i<numLPLonly;i++) fprintf(flpl,"%E\n",lpl[RxnLPL[i]][1]);
        for(i=0;i<numLPLonly;i++) fprintf(flpl,"%E\n",lpl[RxnLPL[i]][2]);
        fclose(flpl);
    
    }
    
    
    void processRxn(void)
    {
        puts("here?");
        int hh[nSpecies];
        int i=0,j=0,k=0,
            n=0,p=0,a=0,
            b=0,c=0,d=0,
            e=0;
        char buffer[80];
        char temp[2*numForwardRxn][60];
        char *sep;
        char *sep2;
        FILE *fp,*fp2;
        remove("Species.txt");
        remove("nva.dat");
        remove("va1.dat");
        remove("va2.dat");
        remove("sre.dat");
        remove("spr.dat");
        int countspecies[nSpecies];
        int countspecies2[nSpecies];
        int va1[nSpecies][2*numForwardRxn];
        int va2[nSpecies][2*numForwardRxn];
        int va2temp;
        double rateRxnOA[numForwardRxn];
        double concS[nSpecies];
        double kRxn[2*numForwardRxn];
        char rxnstring[2*numForwardRxn];
        int srepr[2*numForwardRxn];
        int sre[numForwardRxn];
        int spr[numForwardRxn];
        int srepr2[2*numForwardRxn];
        int sre2[3*numForwardRxn];
        int spr2[3*numForwardRxn];
    
        for(i=0;i<nSpecies;i++)
        {
            countspecies[i]=0;
            countspecies2[i]=countspecies[i];
            hh[i]=0;
    
            for(j=0;j<numForwardRxn;j++)
            {
                rxnID_SpcN[i][j]=0;
                stoichioNum[i][j]=0;
    
            }
        }
    
        for(i=0;i<2*numForwardRxn;i++)
            srepr[i]=0;
        j=0;
        fp=fopen("Reaction.txt","r");
        while ((fgets(buffer,sizeof(buffer)-1,fp))!=NULL)
        {
    
            //printf("something: %s\n",rxnFormula[i]);
            //system("PAUSE");
            sep=strtok(buffer,"<=>\n"); //seperate the strings with delimiter, '=' and '\n'//
    
    //assign buffer into temporary array//
            while(sep!=NULL)
            {
    
                strcpy(temp[n],sep);
    //printf("%3d)%s\n",n,temp[n]);
                n++;
                sep=strtok(NULL,"<=>\n");
            }
    
        }
        fclose(fp);
    //splitting temporary array into species//
        for(p=0;p<n;p++)
        {
    //printf("splitting <%s>\n",temp[p]);
    
            sep2=strtok(temp[p],"+");
            while(sep2!=NULL)
            {
                va2temp=1;
                if(sscanf(sep2,"%d%s", &va2temp,sep2)==2)
                {
                    //printf("va2temp %d\t",va2temp);
                    //printf("va2 correct\n");
                }
                //else
                {
    
                }
    
                //check for duplicate//
                for(i=0;i<j;i++)
                {
    
    
                    if(strcmp(sep2,speciesName[i])==0) //compare strings,0 indicates same strings//
                    {
    
                        //add counter to know how many times the species participate in the mechanism//
                        countspecies[i]++;
                        //printf("in strcmp: j=%3d,%7s participate in rxn ID: %d\n",i,species[i],p);
                        //assign this duplicate species[i] into va1[i][h[i]]//
                        va1[i][hh[i]]=p;
    
                        //if (va2temp!=1)
                        {
                            va2[i][hh[i]]=va2temp;
                        }
                        //else va2[i][h[i]]=1;
    
                        for(k=0;k<hh[i];k++)
                        {
                            if(va1[i][hh[i]]==va1[i][k])
                            {
                                goto skipwriteva1;
                            }
                        }
    
                        hh[i]++;
                      skipwriteva1:
    
                        //printf(">>>>duplicate found<<<<\n");
                        //printf("add one counter to %s\n",species[i]);
    
                        // srepr2[c++]=i;
    
                        goto skipcopy; //skip writing in the species if there is duplicate//
                    }
    
                }
                //check for duplicate end//
    
                strcpy(speciesName[j],sep2);  //assign the splitted token as species after checking for duplicate//
    
                countspecies[j]++;
    
                //assign this species[j] into the va1[species j][h[j]]//
                //printf("           j=%3d,%7s participate in rxn ID: %d\n",j,species[j],p);
                va1[j][hh[j]]=p;
    
                //if (va2temp!=1)
                {
                    va2[i][hh[j]]=va2temp;
                }
                //else va2[i][h[j]]=1;
    
                //srepr2[c++]=j;
                hh[j]++;
                j++;
              skipcopy:
    
    //assign va2 value//
    
    //for sre and spr, count how many reactants/products//
    
    
    
    
                if(va2temp!=1)
                {
                    srepr[p]+=(va2temp-1);
    
    
                }
                srepr[p]++;
    
                sep2=strtok(NULL,"+");
    
            }
    
    
    
    //ensure all counter will add up to ONE only//
            for(i=0;i<j;i++)
            {
                if(countspecies[i]>(countspecies2[i]+1))
                {
                    countspecies[i]=countspecies2[i]+1;
                }
                countspecies2[i]=countspecies[i];
    
            }
    
        }
    
    //printf("\n");
    
    //print out the species//
        fp=fopen("Species.txt","a+");
        for (i=0;i<j;i++)
        {
            fprintf(fp,"%s\n",speciesName[i]);
            //printf("species %2d: <%s>\n",i+1,speciesName[i]);
        }
        fclose(fp);
    //printf("\n");
    //print out the number of times each species occurs in//
    
        fp=fopen("nva.dat","a+");
        for(i=0;i<j;i++)
        {
            fprintf(fp,"%d\n",countspecies[i]);
    //printf("%3d: %7s found %3d times in total\n",i+1,speciesName[i],countspecies[i]);
            numRxnThisSpc[i]=countspecies[i];
        }
        fclose(fp);
    
    //print va1//
        fp=fopen("va1.dat","a+");
    //adjust va1, from say 1,2,3,4,5,6,7,8,9,10 into 1,3,5,7,9,2,4,6,8,10//
        for(i=0;i<j;i++)
        {
            for(k=0;k<hh[i];k++)
            {
                va1[i][k]=va1[i][k]+1;
                if((va1[i][k])%2==0)
                {
                    //even//
    
                    (va1[i][k])=(va1[i][k]+1)/2+numForwardRxn;
                }
                else
                {
                    //odd//
                    (va1[i][k])=va1[i][k]/2+1;
                }
                rxnID_SpcN[i][k]=va1[i][k];
    //printf("%4d",va1[i][k]);
                fprintf(fp,"%d\t",va1[i][k]);
            }
            fprintf(fp,"\n");
    //printf("\n");
        }
        fclose(fp);
    //printf("\n\n");
        fp=fopen("va2.dat","a+");
        for(i=0;i<j;i++)
        {
            for(k=0;k<hh[i];k++)
            {
                if(va1[i][k]<=numForwardRxn) //adjust, give negative for those at the LHS of a reaction//
                    va2[i][k]=-va2[i][k];
                stoichioNum[i][k]=va2[i][k];
                //printf("%4d",va2[i][k]);
                fprintf(fp,"%d\t",va2[i][k]);
            }
            fprintf(fp,"\n");
            //printf("\n");
        }
        fclose(fp);
    
    //building the rateRxnOA=kRxn*conc-kRxn*conc
        for(i=0;i<2*numForwardRxn;i++)
        {
            // printf("species involved in rxn ID %d includes:\t",i+1);
    
            for(k=0;k<nSpecies;k++)
            {
                //while (va1[k][a]!=NULL)
                for(a=0;a<hh[k];a++)
                {
                    if(va1[k][a]==i+1)
                    {
                        c=0;
                        while(c<abs(va2[k][a]))
                        {
                            //printf("%s\t",speciesName[k]);
                            c++;
    
                            if(i<numForwardRxn)
                                sre2[d++]=k;
                            else
                                spr2[e++]=k;
                        }
                    }
                }
            }
            //printf("\n");
        }
    
    
    
        b=0;
    //fixing spr and sre, 1st part//
        fp=fopen("sre.dat","a+");
        fp2=fopen("spr.dat","a+");
        for(i=0;i<numForwardRxn;i++)
        {
            sre[i]=srepr[b]; //here got problem!!, or up there for srepr there//
            spr[i]=srepr[b+1];
            b+=2;
    
            // printf("sre:%d\tspre:%d\n",sre[i],spr[i]);
            numReactSpc[i]=sre[i];
            numProdtSpc[i]=spr[i];
            fprintf(fp,"%d\n",sre[i]);
            //printf("%d\t",spr[i]);
            fprintf(fp2,"%d\n",spr[i]);
    
        }
    
    //printf("\n\n");
    
    
        for(i=0;i<d;i++)
    //printf("%d\t",sre2[i]+1)
            ;
        k=0;
        for(i=0;i<numForwardRxn;i++)
            for(j=0;j<numReactSpc[i];j++)
            {
    			reactSpcIDNum[i][j]=(sre2[k++]+1);
    			//printf("%d\t",reactSpcIDNum[i][j]);
                fprintf(fp,"%d\n",reactSpcIDNum[i][j]);
            }
    //printf("\n\n");
    
        for(i=0;i<numForwardRxn;i++)
            for(j=0;j<numProdtSpc[i];j++)
            {
    			prodtSpcIDNum[i][j]=(spr2[k++]+1);
                fprintf(fp2,"%d\n",prodtSpcIDNum[i][j]);
            }
        fclose(fp);
        fclose(fp2);
    
    
    }
    //processRxn end.
    
    
    
    
    void thermoData(void)
    {
    
        char buff[82];
        char buff2[30];
        char tspecies[nSpecies][10];
        int n=0,m=0;
        int i=0,k=0;
        double a[nSpecies][15];
    
        int len;
        //FILE *fp;
        fp=fopen("thermo30.dat","r");
    
        while ( fgets( buff, sizeof(buff)-1, fp ) != NULL)
        {
    //printf("something %d\n",m++);
            len=strlen(buff);
            if(len>75)
            {
                if(i%4==0)
                {
                    sscanf(buff,"%s",tspecies[k]);
                    //printf("%s\n",temp[k]);
                    strncpy(buff2,&buff[67],11);
                    a[k][14]=atof(buff2);
    
                }
    
    
    
                if(i%4==1)
                {
    //puts(buff);
    
                    {
    
                        for(n=0;n<5;n++)
                        {
                            sscanf(&buff[n*15],"%lf",&a[k][n]);
                            //  printf("%e\n",a[k][n]);
                        }
    
                    }
    
    //printf("\n\n");
                }
    
    
    
                if(i%4==2)
                {
    //puts(buff);
    
                    {
    
                        for(n=0;n<5;n++)
                        {
                            sscanf(&buff[n*15],"%lf",&a[k][n+5]);
                            // printf("%e\n",a[k][n+5]);
                        }
    
                    }
    
    //printf("\n\n");
                }
    
    
    
    
                if(i%4==3)
                {
    //puts(buff);
                    for(n=0;n<4;n++)
                    {
                        sscanf(&buff[n*15],"%lf",&a[k][n+10]);
                        //printf("%e\n",a[k][n+10]);
                    }
                    k++;
    //printf("\n\n");
                }
                i++;
    
            }
    
        }
    
        fclose(fp);
        remove("Sthermo.dat");
        fp=fopen("Sthermo.dat","a+");
        for(m=0;m<nSpecies;m++)
        {
            for(i=0;i<nSpecies;i++)
                if(strcmp(speciesName[m],tspecies[i])==0)
                {
                    for(k=0;k<15;k++)
                        thermo[m][k]=a[i][k];
                }
    //printf("%s has the following coeffs:\n",speciesName[m]);
            for(k=0;k<15;k++)
    //printf("%e\n", thermo[m][k]);
                if(k<14)
                    fprintf(fp,"%.8E\n",thermo[m][k]);
            cpCoeff4EqmConst[m][k]=thermo[m][k];
        }
        for(m=0;m<nSpecies;m++)
            fprintf(fp,"%.8E\n",thermo[m][14]);
        fclose(fp);
    
    
    
    }
    
    
    char *replace_str(char *str, char *orig)
    {
        char buffer[4096];
        char *pp;
    
        if(!(pp = strstr(str, orig)))  // Is 'orig' even in 'str'?
            return str;
    
        strncpy(buffer, str, pp-str); // Copy characters from 'str' start to 'orig' st$
        buffer[pp-str] = '\0';
    
        sprintf(buffer+(pp-str), "%s", pp+strlen(orig));
    
        return buffer;
    }

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jimblumberg View Post
    NO, you have only allocated memory for an array size of 160
    Code:
    #define nSpecies 160
    yet the larger file contains at least 500.

    And as I stated in my previous post I got a segfault at about line 632 in your thermoData function so there is a problem in your loop in that function.

    You should probably be handling any lines that start with a '!' as comments and the thermo line and the next line should be processed before your loop.


    Jim
    Yeah from the look of that file, it might be smarter to use a linked list of structs for the species array... then he can have any number of species the file dictates, up to the limit of memory.
    Last edited by CommonTater; 03-24-2011 at 12:18 PM.

  14. #14
    Registered User
    Join Date
    Jul 2010
    Posts
    28
    Hi all,

    Thanks to rags_to_riches, I now have a much cleaner code (attached). Also moved the global variables to the respective function leaving the necessary one only.

    By trial and errors, I found that my code will quit if my nSpecies and numForwardRxn goes too big. It seems that whatever value under 90 and 580 will be fine. Why would it so?

    Quote Originally Posted by jimblumberg View Post
    NO, you have only allocated memory for an array size of 160
    Code:
    #define nSpecies 160
    yet the larger file contains at least 500.

    And as I stated in my previous post I got a segfault at about line 632 in your thermoData function so there is a problem in your loop in that function.

    You should probably be handling any lines that start with a '!' as comments and the thermo line and the next line should be processed before your loop.


    Jim
    It's really 160 and 1540 only.

    As for the line 632 (now 626 in my latest main.c), I tried to change the way it reads (a line above), not sure if it works now. sscanf directly into double doesn't work, I don't know why.

    Quote Originally Posted by CommonTater View Post
    Yeah from the look of that file, it might be smarter to use a linked list of structs for the species array... then he can have any number of species the file dictates, up to the limit of memory.
    Thanks for the suggestion. I will appreciate if you try to shed some lights to me on that as I have no experience in structs. I'll include that if it's permissible within my deadline.

    My desired input files here (but didn't work). nSpecies 160 and numForwardRxn 1540
    http://dl.dropbox.com/u/21915500/nhe...20-%20Copy.out
    http://dl.dropbox.com/u/21915500/nheptanethermo.txt

    My input files that works. nSpecies 53 and numForwardRxn 325
    http://dl.dropbox.com/u/21915500/try2.out
    http://www.me.berkeley.edu/gri-mech/...0/thermo30.dat
    Last edited by tanjinjack; 03-25-2011 at 06:52 AM.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I really think you need to further simplify this code. You should think about using structures to hold your data.

    Also since no one but you probably understands exactly what you are trying to do you need to comment your code, start using meaningful variable names, eliminate the rest of your global variables, and eliminate the goto's.

    C2H4 L 1/91C 2H 4 G 200.000 3500.000 1000.000 1
    2.03611116E+00 1.46454151E-02-6.71077915E-06 1.47222923E-09-1.25706061E-13 2
    4.93988614E+03 1.03053693E+01 3.95920148E+00-7.57052247E-03 5.70990292E-05 3
    -6.91588753E-08 2.69884373E-11 5.08977593E+03 4.09733096E+00 4
    It would help us to under stand your code if you could tell us how each of the variables above are named in your program. This is where a structure would really make sense.
    Code:
    struct Chemical
    {
       char name[30];
       // the other variables for the first line here.
       // The next  three lines contain a series of numbers that could be in an array.
       double coef[15]
       // I don't know if you need to hold the last variable or not but..
       int line[4];
    };
    The above code is not complete and may not compile.

    Also please explain exactly what these 2 defines are to be used for
    Code:
    define nSpecies  90
    #define numForwardRxn 580
    EDIT: I forget to mention, you should also always check if the opening of your files succeeded.

    Jim
    Last edited by jimblumberg; 03-25-2011 at 11:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open a file, read it, display it and process char.
    By Devenc in forum C Programming
    Replies: 6
    Last Post: 04-19-2010, 11:29 PM
  2. Read communication data of process by port number
    By ajay009ajay in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2010, 09:18 AM
  3. Replies: 2
    Last Post: 03-16-2010, 07:15 PM
  4. Reading Process Memory
    By pobri19 in forum Windows Programming
    Replies: 4
    Last Post: 02-12-2010, 08:20 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM