file:/local_home/local_home/hugo/neurospaces_project/heccer/source/c/snapshots/0/perfectclamp.c (Mon Jun 16 00:04:26 2008
) HOME
1: //
2: // Heccer : a compartmental solver that implements efficient Crank-Nicolson
3: // integration for neuronal models.
4: //
5:
6: //////////////////////////////////////////////////////////////////////////////
7: //'
8: //' Heccer : testbed C implementation
9: //'
10: //' Copyright (C) 2006-2008 Hugo Cornelis
11: //'
12: //' functional ideas .. Hugo Cornelis, hugo.cornelis@gmail.com
13: //'
14: //' coding ............ Hugo Cornelis, hugo.cornelis@gmail.com
15: //'
16: //////////////////////////////////////////////////////////////////////////////
17:
18:
19: #include <math.h>
20: #include <stdio.h>
21: #include <stdlib.h>
22: #include <string.h>
23:
24:
25: #include "heccer/perfectclamp.h"
26:
27:
28: /// **************************************************************************
29: ///
30: /// SHORT: PerfectClampAddVariable()
31: ///
32: /// ARGS.:
33: ///
34: /// ppc.........: voltage clamper.
35: /// pvVoltage...: pointer to the voltage variable, assumed is double *
36: ///
37: /// RTN..: int
38: ///
39: /// success of operation.
40: ///
41: /// DESCR: Clamp the given voltage variable.
42: ///
43: /// **************************************************************************
44:
45: int
46: PerfectClampAddVariable
47: (struct PerfectClamp * ppc, void *pvVoltage)
48: {
49: //- set default result: ok
50:
51: int iResult = 1;
52:
53: if (ppc->iClampsActive >= 1)
54: {
55: return(0);
56: }
57:
58: //- set next variable
59:
60: ppc->pdVoltage = (double *)pvVoltage;
61:
62: ppc->iClampsActive++;
63:
64: //- return result
65:
66: return(iResult);
67: }
68:
69:
70: /// **************************************************************************
71: ///
72: /// SHORT: PerfectClampFinish()
73: ///
74: /// ARGS.:
75: ///
76: /// ppc...: voltage clamper.
77: ///
78: /// RTN..: int
79: ///
80: /// success of operation.
81: ///
82: /// DESCR: Free the voltage clamper.
83: ///
84: /// **************************************************************************
85:
86: int PerfectClampFinish(struct PerfectClamp * ppc)
87: {
88: //- set default result: ok
89:
90: int iResult = 1;
91:
92: //- close files
93:
94: if (ppc->pfile)
95: {
96: fclose(ppc->pfile);
97: }
98:
99: if (ppc->pcFilename)
100: {
101: free(ppc->pcFilename);
102: }
103:
104: //- free all allocated memory
105:
106: free(ppc->pcName);
107:
108: free(ppc);
109:
110: //- return result
111:
112: return(iResult);
113: }
114:
115:
116: /// **************************************************************************
117: ///
118: /// SHORT: PerfectClampInitiate()
119: ///
120: /// ARGS.:
121: ///
122: /// ppc...: voltage clamper.
123: ///
124: /// RTN..: int
125: ///
126: /// success of operation.
127: ///
128: /// DESCR: Initiate the voltage clamper.
129: ///
130: /// **************************************************************************
131:
132: int PerfectClampInitiate(struct PerfectClamp * ppc)
133: {
134: //- set default result: ok
135:
136: int iResult = 1;
137:
138: //- return result
139:
140: return(iResult);
141: }
142:
143:
144: /// **************************************************************************
145: ///
146: /// SHORT: PerfectClampNew()
147: ///
148: /// ARGS.:
149: ///
150: /// pcName..: name of this object.
151: ///
152: /// RTN..: struct PerfectClamp *
153: ///
154: /// voltage clamper, NULL for failure.
155: ///
156: /// DESCR: voltage clamper.
157: ///
158: /// **************************************************************************
159:
160: struct PerfectClamp * PerfectClampNew(char *pcName)
161: {
162: //- set default result: failure
163:
164: struct PerfectClamp * ppcResult = NULL;
165:
166: //- allocate voltage clamper
167:
168: ppcResult = (struct PerfectClamp *)calloc(1, sizeof(struct PerfectClamp));
169:
170: if (!ppcResult)
171: {
172: return(NULL);
173: }
174:
175: //- set name
176:
177: ppcResult->pcName = calloc(1 + strlen(pcName), sizeof(char));
178:
179: strcpy(ppcResult->pcName, pcName);
180:
181: //- return result
182:
183: return(ppcResult);
184: }
185:
186:
187: /// **************************************************************************
188: ///
189: /// SHORT: PerfectClampSetFields()
190: ///
191: /// ARGS.:
192: ///
193: /// dCommand.....: command voltage, ignored if a filename is given.
194: /// pcFilename...: filename, file contains sequence of command voltages.
195: ///
196: /// RTN..: int
197: ///
198: /// success of operation.
199: ///
200: /// DESCR: set operation fields of voltage clamper.
201: ///
202: /// **************************************************************************
203:
204: int
205: PerfectClampSetFields
206: (struct PerfectClamp * ppc,
207: double dCommand,
208: char *pcFilename)
209: {
210: //- set default result: ok
211:
212: int iResult = 1;
213:
214: //- set fields
215:
216: ppc->dCommand = dCommand;
217:
218: //- if a filename has been given
219:
220: if (pcFilename)
221: {
222: //- allocate filename
223:
224: ppc->pcFilename = calloc(1 + strlen(pcFilename), sizeof(char));
225:
226: strcpy(ppc->pcFilename, pcFilename);
227:
228: //- open file
229:
230: //! I need the qualification service overhere.
231:
232: ppc->pfile = fopen(pcFilename, "r");
233:
234: if (!ppc->pfile)
235: {
236: fprintf(stderr, "warning: PerfectClampSetFields() cannot open command file\n");
237:
238: PerfectClampFinish(ppc);
239:
240: return(0);
241: }
242:
243: //- file stays open during simulation
244:
245: //! we could also cache the file in memory, depending on an option or so.
246: }
247:
248: //- return result
249:
250: return(iResult);
251: }
252:
253:
254: /// **************************************************************************
255: ///
256: /// SHORT: PerfectClampSingleStep()
257: ///
258: /// ARGS.:
259: ///
260: /// ppc...: voltage clamper.
261: /// dTime.: current simulation time.
262: ///
263: /// RTN..: int
264: ///
265: /// success of operation.
266: ///
267: /// DESCR: Compute new currents to correct voltages.
268: ///
269: /// NOTE.:
270: ///
271: /// Old current values are overwritten.
272: ///
273: /// **************************************************************************
274:
275: int PerfectClampSingleStep(struct PerfectClamp * ppc, double dTime)
276: {
277: //- set default result: ok
278:
279: int iResult = 1;
280:
281: //- if running with a file open
282:
283: if (ppc->pfile)
284: {
285: //- read a command voltage from the file
286:
287: char pcStep[100];
288:
289: char pcCommand[100];
290:
291: int iEOF = fscanf(ppc->pfile, " %[^:]: %s", pcStep, pcCommand);
292:
293: ppc->dCommand = strtod(pcCommand, (char **)NULL);
294:
295: //- if end of file
296:
297: if (iEOF == EOF)
298: {
299: //- rewind the file
300:
301: if (fseek(ppc->pfile, 0L, SEEK_SET) != 0)
302: {
303: fprintf(stderr, "warning: PerfectClampSingleStep() cannot rewind the command file\n");
304: }
305: }
306: else if (iEOF != 2)
307: {
308: fprintf(stderr, "warning: PerfectClampSingleStep() read %i values from the command file (instead of 2)\n", iEOF);
309: }
310:
311: //- set the output
312:
313: *ppc->pdVoltage = ppc->dCommand;
314: }
315: else
316: {
317: //- set the output
318:
319: *ppc->pdVoltage = ppc->dCommand;
320: }
321:
322: //- return result
323:
324: return(iResult);
325: }
326:
327:
328:
Generated by Xrefactory version 2.0.14 on Thu Jul 24 22:41:20 2008