/[ascend]/branches/georgy/ascend/integrator/samplelist.c
ViewVC logotype

Contents of /branches/georgy/ascend/integrator/samplelist.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3105 - (show annotations) (download) (as text)
Tue May 24 01:31:12 2016 UTC (8 years, 1 month ago) by jpye
File MIME type: text/x-csrc
File size: 3395 byte(s)
Creating new branch for [[User:Georgy]] for [[GSOC2016]], from trunk r3104.

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, see <http://www.gnu.org/licenses/>.
16 *//*
17 by John Pye, May 2006.
18 */
19
20 #include "samplelist.h"
21 #include <ascend/general/ascMalloc.h>
22 #include <ascend/utilities/error.h>
23
24 /**
25 Free memory allocated by a SampleList.
26 */
27 void samplelist_free(SampleList *l){
28 /* trash the stuff contained inside */
29
30 if(l==NULL){
31 ERROR_REPORTER_HERE(ASC_PROG_ERR,"samplelist already freed!");
32 return;
33 }
34
35 l->ns = 0L;
36 if (l->x != NULL) {
37 ASC_FREE(l->x);
38 l->x = NULL;
39 }
40
41 /* now trash the struct itself */
42 ASC_FREE(l);
43 }
44
45 /**
46 Create an empty samplelist, and allocate memory for the required number
47 of data points.
48
49 @NOTE
50 SampleList is assumed to be the owner of the contained
51 data now, and will destroy the data when samplelist_free is
52 called.
53 */
54 SampleList *samplelist_new(unsigned long n, const dim_type *d){
55 SampleList *l;
56 double *values;
57
58 l = ASC_NEW(SampleList);
59 assert(n>0);
60 values = ASC_NEW_ARRAY_CLEAR(double,n);
61 l->ns = n;
62 if(values==NULL){
63 samplelist_free(l);
64 return NULL;
65 }
66 if(!samplelist_assign(l,n,values,d)){
67 samplelist_free(l);
68 return NULL;
69 }
70 return l;
71 }
72
73 int samplelist_assign(SampleList *l, unsigned long n, double *values, const dim_type *d){
74 /* store d given or copy and store WildDimension */
75 if (d != NULL) {
76 l->d = d;
77
78 #if SAMPLELIST_DEBUG
79 FPRINTF(ASCERR,"sample received dimen\n");
80 PrintDimen(ASCERR,l->d);
81 #endif
82
83 } else {
84 l->d = ASC_NEW(dim_type);
85 if (l->d == NULL) {
86 ERROR_REPORTER_HERE(ASC_PROG_ERR,"Insufficient memory.");
87 return 0; /* out of memory */
88 }
89 # if 0
90 CopyDimensions(WildDimension(),l->d);
91 // copydim is not a reference copy, which is what is needed.
92 // persistent dimen references all come from the global
93 // dim table or Wild or Dimensionless.
94 #else
95 l->d = WildDimension();
96 #endif
97
98 #if SAMPLELIST_DEBUG
99 FPRINTF(ASCERR,"copy of wild dimen looks like\n");
100 PrintDimen(ASCERR,l->d);
101 #endif
102
103 }
104
105 l->ns = n;
106 l->x = values;
107 return 1; /* all ok */
108 }
109
110 long samplelist_length(CONST SampleList *l){
111 return l->ns;
112 }
113
114
115 const dim_type *samplelist_dim(CONST SampleList *l){
116 return l->d;
117 }
118
119 double samplelist_get(CONST SampleList *l, CONST long i){
120 if(i>=0 && i < samplelist_length(l)){
121 return l->x[i];
122 }
123 ERROR_REPORTER_HERE(ASC_PROG_WARNING,"Invalid sample index %ld."
124 " Returning 0.", i);
125 return 0.;
126 }
127
128 void samplelist_set(CONST SampleList *l, CONST long i, CONST double x){
129 if(i>=0 && i < samplelist_length(l)){
130 l->x[i]=x;
131 return;
132 }
133
134 ERROR_REPORTER_HERE(ASC_PROG_WARNING,"Invalid sample index %ld. Ignored (length is %ld)."
135 ,i
136 ,samplelist_length(l)
137 );
138 }
139
140 void samplelist_debug(CONST SampleList *l){
141 long i;
142 CONSOLE_DEBUG("SAMPLELIST (%ld)", l->ns);
143 for(i=0; i < l->ns; ++i){
144 CONSOLE_DEBUG("sample[%ld] = %f", i, l->x[i]);
145 }
146 }

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