TPCCLIB
Loading...
Searching...
No Matches
simttm.c
Go to the documentation of this file.
1
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <math.h>
14/*****************************************************************************/
15#include "tpcextensions.h"
16#include "tpcift.h"
17#include "tpctac.h"
18#include "tpcli.h"
19#include "tpccm.h"
20#include "tpctacmod.h"
21/*****************************************************************************/
22
23/*****************************************************************************/
24static char *info[] = {
25 "Simulation of input function time lag using n-compartmental transit-time",
26 "model (one-way catenary compartmental model):",
27 " ____ ____ ____ ____ ",
28 " | | k | | k | | k | | k ",
29 " | Ca | ---> | C1 | ---> | C2 | --->...--> | Cn | ---> ",
30 " |____| |____| |____| |____| ",
31 " ",
32 " ",
33 "Usage: @P [Options] tacfile n k [outputfile] ",
34 " ",
35 "Options:",
36 " -stdoptions", // List standard options like --help, -v, etc
37 " ",
38 "If file name for output is not given, then the TAC file is modified.",
39 "The units of k must correspond the time units of the TAC.",
40 "For accurate results, input TAC should have very short sample intervals.",
41 "Frame durations are not used, even if available in the TAC file.",
42 " ",
43 "See also: simdisp, convexpf, fit2dat, simframe, sim_av, tacunit",
44 " ",
45 "Keywords: TAC, input, simulation, dispersion, time delay",
46 0};
47/*****************************************************************************/
48
49/*****************************************************************************/
50/* Turn on the globbing of the command line, since it is disabled by default in
51 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
52 In Unix&Linux wildcard command line processing is enabled by default. */
53/*
54#undef _CRT_glob
55#define _CRT_glob -1
56*/
57int _dowildcard = -1;
58/*****************************************************************************/
59
60/*****************************************************************************/
64int main(int argc, char **argv)
65{
66 int ai, help=0, version=0, verbose=1;
67 char tacfile[FILENAME_MAX], simfile[FILENAME_MAX];
68 double k=nan("");
69 unsigned int n=0;
70
71
72 /*
73 * Get arguments
74 */
75 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
76 tacfile[0]=simfile[0]=(char)0;
77 /* Options */
78 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
79 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
80 char *cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
81 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
82 return(1);
83 } else break; // tac name argument may start with '-'
84
85 TPCSTATUS status; statusInit(&status);
86 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
87 status.verbose=verbose-1;
88
89 /* Print help or version? */
90 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
91 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
92 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
93
94 /* Process other arguments, starting from the first non-option */
95 if(ai<argc) {strlcpy(tacfile, argv[ai++], FILENAME_MAX);}
96 if(ai<argc) {
97 int i;
98 if(atoiCheck(argv[ai], &i) || i<1) {
99 fprintf(stderr, "Error: invalid n '%s'.\n", argv[ai]); return(1);}
100 n=i; ai++;
101 }
102 if(ai<argc) {
103 k=atofVerified(argv[ai]);
104 if(!(k>0.0)) {fprintf(stderr, "Error: invalid k '%s'.\n", argv[ai]); return(1);}
105 ai++;
106 }
107 if(ai<argc) {strlcpy(simfile, argv[ai++], FILENAME_MAX);}
108 if(ai<argc) {
109 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
110 return(1);
111 }
112
113 /* Is something missing? */
114 if(!(k>0.0)) {
115 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
116 return(1);
117 }
118 if(!simfile[0]) strcpy(simfile, tacfile);
119
120 /* In verbose mode print arguments and options */
121 if(verbose>1) {
122 printf("tacfile := %s\n", tacfile);
123 printf("simfile := %s\n", simfile);
124 printf("n := %u\n", n);
125 printf("k := %g\n", k);
126 fflush(stdout);
127 }
128
129
130 /*
131 * Read input TAC
132 */
133 if(verbose>1) {fprintf(stdout, "reading %s\n", tacfile); fflush(stdout);}
134 TAC tac; tacInit(&tac);
135 if(tacRead(&tac, tacfile, &status)!=TPCERROR_OK) {
136 fprintf(stderr, "Error: %s (%s)\n", errorMsg(status.error), tacfile);
137 tacFree(&tac); return(2);
138 }
139 if(verbose>2) {
140 printf("fileformat := %s\n", tacFormattxt(tac.format));
141 printf("tacNr := %d\n", tac.tacNr);
142 printf("sampleNr := %d\n", tac.sampleNr);
143 printf("xunit := %s\n", unitName(tac.tunit));
144 printf("yunit := %s\n", unitName(tac.cunit));
145 if(tacIsWeighted(&tac)) printf("weighting := yes\n");
146 fflush(stdout);
147 }
148 /* Check for missing sample times */
149 if(tacXNaNs(&tac)>0) {
150 fprintf(stderr, "Error: missing frame times.\n");
151 tacFree(&tac); return(2);
152 }
153 /* Check for missing concentrations */
154 if(tacYNaNs(&tac, -1)>0) {
155 fprintf(stderr, "Error: missing concentrations.\n");
156 tacFree(&tac); return(2);
157 }
158 if(tac.sampleNr<3) {
159 fprintf(stderr, "Error: too few samples in input data.\n");
160 tacFree(&tac); return(2);
161 }
162 if(tac.tacNr>1 && verbose>0) {
163 fprintf(stderr, "Warning: TAC contains %d TACs\n", tac.tacNr);
164 fflush(stderr);
165 }
166 if(tacSortByTime(&tac, &status)!=TPCERROR_OK) {
167 fprintf(stderr, "Error: invalid sample times.\n");
168 tacFree(&tac); return(2);
169 }
170 if(tac.isframe!=0 && verbose>0) {
171 if(tacSetX(&tac, &status)!=TPCERROR_OK) { // make sure that frame middle times are set
172 fprintf(stderr, "Error: invalid sample times.\n");
173 tacFree(&tac); return(2);
174 }
175 fprintf(stderr, "Warning: frame durations are ignored.\n");
176 fflush(stderr);
177 }
178
179
180 /*
181 * Simulation
182 */
183 if(verbose>1) {fprintf(stdout, "simulating\n"); fflush(stdout);}
184 for(int ci=0; ci<tac.tacNr; ci++) {
185 double sim[tac.sampleNr];
186 if(simTTM(tac.x, tac.c[ci].y, tac.sampleNr, k, n, sim)!=0) {
187 fprintf(stderr, "Error: invalid data for simulation.\n");
188 tacFree(&tac); return(3);
189 }
190 for(int i=0; i<tac.sampleNr; i++) tac.c[ci].y[i]=sim[i];
191 }
192
193
194 /*
195 * Write the file
196 */
197 {
198 if(verbose>1) printf("writing simulated data in %s\n", simfile);
199 FILE *fp; fp=fopen(simfile, "w");
200 if(fp==NULL) {
201 fprintf(stderr, "Error: cannot open file for writing.\n");
202 tacFree(&tac); return(11);
203 }
204 int ret=tacWrite(&tac, fp, TAC_FORMAT_UNKNOWN, 1, &status);
205 fclose(fp); tacFree(&tac);
206 if(ret!=TPCERROR_OK) {
207 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
208 return(12);
209 }
210 if(verbose>=0) printf("Simulated data saved in %s\n", simfile);
211 }
212
213 return(0);
214}
215/*****************************************************************************/
217/*****************************************************************************/
double atofVerified(const char *s)
Definition decpoint.c:75
int atoiCheck(const char *s, int *v)
Definition intutil.c:25
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:47
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:169
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:339
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:114
int simTTM(double *t, double *c0, const int n, const double k, const int cn, double *cout)
void statusInit(TPCSTATUS *s)
Definition statusmsg.c:104
char * errorMsg(tpcerror e)
Definition statusmsg.c:68
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
double * y
Definition tpctac.h:75
Definition tpctac.h:87
double * x
Definition tpctac.h:97
unit cunit
Definition tpctac.h:105
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
int isframe
Definition tpctac.h:95
TACC * c
Definition tpctac.h:117
unit tunit
Definition tpctac.h:109
int tacNr
Definition tpctac.h:91
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
void tacFree(TAC *tac)
Definition tac.c:106
void tacInit(TAC *tac)
Definition tac.c:24
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
Definition tacio.c:413
char * tacFormattxt(tacformat c)
Definition tacio.c:98
int tacWrite(TAC *tac, FILE *fp, tacformat format, int extra, TPCSTATUS *status)
Definition tacio.c:332
int tacYNaNs(TAC *tac, const int i)
Definition tacnan.c:47
int tacXNaNs(TAC *tac)
Definition tacnan.c:23
int tacSortByTime(TAC *d, TPCSTATUS *status)
Definition tacorder.c:74
int tacIsWeighted(TAC *tac)
Definition tacw.c:24
int tacSetX(TAC *d, TPCSTATUS *status)
Set TAC x values based on x1 and x2 values, or guess x1 and x2 values based on x values.
Definition tacx.c:653
Header file for libtpccm.
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
char * unitName(int unit_code)
Definition units.c:143
Header file for library libtpcift.
Header file for libtpcli.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
Definition tpctac.h:28
Header file for libtpctacmod.