1 /*
2   Teem: Tools to process and visualize scientific data and images             .
3   Copyright (C) 2012, 2011, 2010, 2009  University of Chicago
4   Copyright (C) 2008, 2007, 2006, 2005  Gordon Kindlmann
5   Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998  University of Utah
6 
7   This library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Lesser General Public License
9   (LGPL) as published by the Free Software Foundation; either
10   version 2.1 of the License, or (at your option) any later version.
11   The terms of redistributing and/or modifying this software also
12   include exceptions to the LGPL that facilitate static linking.
13 
14   This library is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   Lesser General Public License for more details.
18 
19   You should have received a copy of the GNU Lesser General Public License
20   along with this library; if not, write to Free Software Foundation, Inc.,
21   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 */
23 
24 #ifndef ALAN_HAS_BEEN_INCLUDED
25 #define ALAN_HAS_BEEN_INCLUDED
26 
27 #include <stdio.h>
28 #include <math.h>
29 
30 #include <teem/air.h>
31 #include <teem/biff.h>
32 #include <teem/ell.h>
33 #include <teem/nrrd.h>
34 
35 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(TEEM_STATIC)
36 #  if defined(TEEM_BUILD) || defined(alan_EXPORTS) || defined(teem_EXPORTS)
37 #    define ALAN_EXPORT extern __declspec(dllexport)
38 #  else
39 #    define ALAN_EXPORT extern __declspec(dllimport)
40 #  endif
41 #else /* TEEM_STATIC || UNIX */
42 #  define ALAN_EXPORT extern
43 #endif
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 #define ALAN alanBiffKey
50 #define ALAN_THREAD_MAX 256
51 
52 enum {
53   alanTextureTypeUnknown,
54   alanTextureTypeTuring,
55   alanTextureTypeGrayScott,
56   alanTextureTypeLast
57 };
58 
59 enum {
60   alanParmUnknown,
61   alanParmVerbose,
62   alanParmTextureType,
63   alanParmNumThreads,
64   alanParmFrameInterval,
65   alanParmHomogAniso,
66   alanParmSaveInterval,
67   alanParmMaxIteration,
68   alanParmRandRange,
69   alanParmDeltaT,
70   alanParmDeltaX,
71   alanParmDiffA,
72   alanParmDiffB,
73   alanParmReact,
74   alanParmK,
75   alanParmF,
76   alanParmMinAverageChange,
77   alanParmMaxPixelChange,
78   alanParmAlpha,
79   alanParmBeta,
80   alanParmConstantFilename,
81   alanParmWrapAround,
82   alanParmLast
83 };
84 
85 enum {
86   alanStopUnknown=0,
87   alanStopNot,          /* 1 */
88   alanStopMaxIteration, /* 2 */
89   alanStopNonExist,     /* 3 */
90   alanStopConverged,    /* 4 */
91   alanStopDiverged,     /* 5 */
92   alanStopLast
93 };
94 #define ALAN_STOP_MAX      5
95 
96 /* all morphogen values are stored as
97 ** 1: floats
98 ** 0: doubles
99 */
100 #if 1
101 typedef float alan_t;
102 #  define alan_nt nrrdTypeFloat
103 #  define ALAN_FLOAT 1
104 #else
105 typedef double alan_t;
106 #  define alan_nt nrrdTypeDouble
107 #  define ALAN_FLOAT 0
108 #endif
109 
110 typedef struct alanContext_t {
111   /* INPUT ----------------------------- */
112   unsigned int
113     dim,              /* either 2 or 3 */
114     size[3];          /* number of texels in X, Y, (Z) */
115   int verbose,
116     wrap,             /* do toroidal boundary wrapping */
117     textureType,      /* what kind are we (from alanTextureType* enum) */
118     oversample,       /* oversampling of tensors to texels */
119     homogAniso,       /* homogenous anisotropy approximation */
120     numThreads,       /* # of threads, if airThreadCapable */
121     frameInterval,    /* # of iterations between which to an image */
122     saveInterval,     /* # of iterations between which to save all state */
123     maxIteration,     /* cap on # of iterations, or 0 if there is no limit */
124     constFilename;    /* always use the same filename when saving frames */
125   alan_t K, F,        /* simulation variables */
126     deltaX,           /* size of spatial grid discretization */
127     minAverageChange, /* min worthwhile "avergageChange" value (see below),
128                          assume convergence if it falls below this */
129     maxPixelChange,   /* maximum allowed change in the first morphogen (on
130                          any single pixels), assume unstable divergence if
131                          this is exceeded */
132     alpha, beta,      /* variables for turing */
133     react,            /* additional scaling of reaction term */
134     deltaT,           /* euler integration step size */
135     initA, initB,     /* initial (constant) values for each morphogen */
136     diffA, diffB,     /* base diffusion rates for each morphogen */
137     randRange;        /* amplitude of noise to destabalize Turing */
138   Nrrd *nten;         /* tensors guiding texture.  May have 1+3 or 1+6 values
139                          per sample, depending on dim */
140   /* if non-NULL, this is called once per iteration, at its completion */
141   int (*perIteration)(struct alanContext_t *, int iter);
142 
143   /* INTERNAL -------------------------- */
144   int iter;           /* current iteration */
145   Nrrd *_nlev[2],     /* levels of morphogens, alternating buffers */
146     *nlev;            /* pointer to last iterations output */
147   Nrrd *nparm;        /* alpha, beta values for all texels */
148   alan_t
149     averageChange;    /* average amount of "change" in last iteration */
150   int changeCount;    /* # of contributions to averageChange */
151                       /* to control update of averageChange and changeCount */
152   airThreadMutex *changeMutex;
153                       /* to synchronize separate iterations of simulation */
154   airThreadBarrier *iterBarrier;
155 
156   /* OUTPUT ---------------------------- */
157   int stop;          /* why we stopped */
158 } alanContext;
159 
160 /* methodsAlan.c */
161 ALAN_EXPORT const int alanPresent;
162 ALAN_EXPORT const char *alanBiffKey;
163 ALAN_EXPORT alanContext *alanContextNew(void);
164 ALAN_EXPORT alanContext *alanContextNix(alanContext *actx);
165 ALAN_EXPORT int alanDimensionSet(alanContext *actx, int dim);
166 ALAN_EXPORT int alan2DSizeSet(alanContext *actx, int sizeX, int sizeY);
167 ALAN_EXPORT int alan3DSizeSet(alanContext *actx,
168                               int sizeX, int sizeY, int sizeZ);
169 ALAN_EXPORT int alanTensorSet(alanContext *actx, Nrrd *nten, int oversample);
170 ALAN_EXPORT int alanParmSet(alanContext *actx, int whichParm, double parm);
171 
172 /* enumsAlan.c */
173 ALAN_EXPORT const airEnum *const alanStop;
174 
175 /* coreAlan.c */
176 ALAN_EXPORT int alanUpdate(alanContext *actx);
177 ALAN_EXPORT int alanInit(alanContext *actx,
178                          const Nrrd *nlevInit, const Nrrd *nparmInit);
179 ALAN_EXPORT int alanPriorityParm(alanContext *actx, const Nrrd *npri);
180 ALAN_EXPORT int alanRun(alanContext *actx);
181 
182 #ifdef __cplusplus
183 }
184 #endif
185 
186 #endif /* ALAN_HAS_BEEN_INCLUDED */
187