/[ascend]/trunk/ascend/integrator/samplelist.c
ViewVC logotype

Contents of /trunk/ascend/integrator/samplelist.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2018 - (show annotations) (download) (as text)
Wed Apr 29 03:38:10 2009 UTC (15 years, 8 months ago) by jpye
File MIME type: text/x-csrc
File size: 3464 byte(s)
Fixed compile for new header file locations <ascend/compiler/xxx.h> etc.
1 /* ASCEND modelling environment
2 Copyright (C) 2006 Carnegie Mellon University
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18 *//*
19 by John Pye, May 2006.
20 */
21
22 #include "samplelist.h"
23 #include <ascend/utilities/ascMalloc.h>
24 #include <ascend/utilities/error.h>
25
26 /**
27 Free memory allocated by a SampleList.
28 */
29 void samplelist_free(SampleList *l){
30 /* trash the stuff contained inside */
31
32 if(l==NULL){
33 ERROR_REPORTER_HERE(ASC_PROG_ERR,"samplelist already freed!");
34 return;
35 }
36
37 l->ns = 0L;
38 if (l->x != NULL) {
39 ASC_FREE(l->x);
40 l->x = NULL;
41 }
42
43 /* now trash the struct itself */
44 ASC_FREE(l);
45 }
46
47 /**
48 Create an empty samplelist, and allocate memory for the required number
49 of data points.
50
51 @NOTE
52 SampleList is assumed to be the owner of the contained
53 data now, and will destroy the data when samplelist_free is
54 called.
55 */
56 SampleList *samplelist_new(unsigned long n, const dim_type *d){
57 SampleList *l;
58 double *values;
59
60 l = ASC_NEW(SampleList);
61 assert(n>0);
62 values = ASC_NEW_ARRAY_CLEAR(double,n);
63 l->ns = n;
64 if(values==NULL){
65 samplelist_free(l);
66 return NULL;
67 }
68 if(!samplelist_assign(l,n,values,d)){
69 samplelist_free(l);
70 return NULL;
71 }
72 return l;
73 }
74
75 int samplelist_assign(SampleList *l, unsigned long n, double *values, const dim_type *d){
76 /* store d given or copy and store WildDimension */
77 if (d != NULL) {
78 l->d = d;
79
80 #if SAMPLELIST_DEBUG
81 FPRINTF(ASCERR,"sample received dimen\n");
82 PrintDimen(ASCERR,l->d);
83 #endif
84
85 } else {
86 l->d = ASC_NEW(dim_type);
87 if (l->d == NULL) {
88 ERROR_REPORTER_HERE(ASC_PROG_ERR,"Insufficient memory.");
89 return 0; /* out of memory */
90 }
91 # if 0
92 CopyDimensions(WildDimension(),l->d);
93 // copydim is not a reference copy, which is what is needed.
94 // persistent dimen references all come from the global
95 // dim table or Wild or Dimensionless.
96 #else
97 l->d = WildDimension();
98 #endif
99
100 #if SAMPLELIST_DEBUG
101 FPRINTF(ASCERR,"copy of wild dimen looks like\n");
102 PrintDimen(ASCERR,l->d);
103 #endif
104
105 }
106
107 l->ns = n;
108 l->x = values;
109 return 1; /* all ok */
110 }
111
112 long samplelist_length(CONST SampleList *l){
113 return l->ns;
114 }
115
116
117 const dim_type *samplelist_dim(CONST SampleList *l){
118 return l->d;
119 }
120
121 double samplelist_get(CONST SampleList *l, CONST long i){
122 if(i>=0 && i < samplelist_length(l)){
123 return l->x[i];
124 }
125 ERROR_REPORTER_HERE(ASC_PROG_WARNING,"Invalid sample index %ld."
126 " Returning 0.", i);
127 return 0.;
128 }
129
130 void samplelist_set(CONST SampleList *l, CONST long i, CONST double x){
131 if(i>=0 && i < samplelist_length(l)){
132 l->x[i]=x;
133 return;
134 }
135
136 ERROR_REPORTER_HERE(ASC_PROG_WARNING,"Invalid sample index %ld. Ignored (length is %ld)."
137 ,i
138 ,samplelist_length(l)
139 );
140 }
141
142 void samplelist_debug(CONST SampleList *l){
143 long i;
144 CONSOLE_DEBUG("SAMPLELIST (%ld)", l->ns);
145 for(i=0; i < l->ns; ++i){
146 CONSOLE_DEBUG("sample[%ld] = %f", i, l->x[i]);
147 }
148 }

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22