Cleaned up omxRAMObjective, added a few new helper functions to prettify things....
[openmx:openmx.git] / src / omxState.c
1 /*
2  *  Copyright 2007-2009 The OpenMx Project
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 /***********************************************************
18
19 *  omxState.cc
20 *
21 *  Created: Timothy R. Brick    Date: 2009-06-05
22 *
23 *       omxStates carry the current optimization state
24 *
25 **********************************************************/
26
27 #include "omxState.h"
28
29 /* Initialize and Destroy */
30         void omxInitState(omxState* state) {
31                 state->numMats = 0;
32                 state->numAlgs = 0;
33                 state->numData = 0;
34                 state->matrixList = NULL;       
35                 state->algebraList = NULL;
36                 state->dataList = NULL;
37                 state->objectiveMatrix = NULL;
38                 state->conList = NULL;
39                 state->freeVarList = NULL;
40                 state->optimizerState = NULL;
41                 state->optimalValues = NULL;
42                 state->optimum = 9999999999;
43
44                 state->computeCount = -1;
45                 state->currentRow = -1;
46
47                 state->statusCode = 0;
48                 strncpy(state->statusMsg, "", 1);
49         }
50
51         void omxFillState(omxState* state, /*omxOptimizer *oo,*/ omxMatrix** matrixList, omxMatrix** algebraList, omxData** dataList, omxMatrix* objective) {
52                 error("NYI: Can't fill a state from outside yet.  Besides, do you really need a single function to do this?");
53         }
54         
55         void omxFreeState(omxState *oo) {
56                 int k;
57                 if(OMX_DEBUG) { Rprintf("Freeing %d Algebras.\n", oo->numAlgs);}
58                 for(k = 0; k < oo->numAlgs; k++) {
59                         if(OMX_DEBUG) { Rprintf("Freeing Algebra %d at 0x%x.\n", k, oo->algebraList[k]); }
60                         omxFreeAllMatrixData(oo->algebraList[k]);
61                 }
62
63                 if(OMX_DEBUG) { Rprintf("Freeing %d Matrices.\n", oo->numMats);}
64                 for(k = 0; k < oo->numMats; k++) {
65                         if(OMX_DEBUG) { Rprintf("Freeing Matrix %d at 0x%x.\n", k, oo->matrixList[k]); }
66                         omxFreeAllMatrixData(oo->matrixList[k]);
67                 }
68                 
69                 if(OMX_DEBUG) { Rprintf("Freeing %d Data Sets.\n", oo->numData);}
70                 for(k = 0; k < oo->numData; k++) {
71                         if(OMX_DEBUG) { Rprintf("Freeing Data Set %d at 0x%x.\n", k, oo->dataList[k]); }
72                         omxFreeData(oo->dataList[k]);
73                 }
74                 
75                 if(OMX_DEBUG) { Rprintf("State Freed.\n");}
76         }
77         
78         void omxRaiseError(omxState *oo, int errorCode, char* errorMsg) {
79                 oo->statusCode = errorCode;
80                 strncpy(oo->statusMsg, errorMsg, 249);
81                 oo->statusMsg[249] = '\0';
82         }
83         
84         void omxStateNextRow(omxState *oo) { 
85                 oo->currentRow++; 
86         };
87         void omxStateNextEvaluation(omxState *oo) { 
88                 oo->currentRow = 0;
89                 oo->computeCount++; 
90         };
91