1675313205ba0f23066185f672fe672711780f38
[blender.git] / source / blender / editors / physics / ed_fluidsim.c
1 /**
2  * fluidsim.c
3  * 
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) Blender Foundation
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32
33
34 #include <math.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/stat.h>
38
39 #ifdef WIN32    /* Windos */
40 #ifndef snprintf
41 #define snprintf _snprintf
42 #endif
43 #endif
44
45 #include "MEM_guardedalloc.h"
46
47 /* types */
48 #include "DNA_curve_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_object_fluidsim.h"        
51 #include "DNA_key_types.h"
52 #include "DNA_mesh_types.h"
53 #include "DNA_meshdata_types.h"
54 #include "DNA_lattice_types.h"
55 #include "DNA_scene_types.h"
56 #include "DNA_camera_types.h"
57 #include "DNA_screen_types.h"
58 #include "DNA_space_types.h"
59 #include "DNA_userdef_types.h"
60 #include "DNA_ipo_types.h"
61 #include "DNA_key_types.h" 
62
63 #include "BLI_blenlib.h"
64 #include "BLI_threads.h"
65 #include "BLI_arithb.h"
66
67 #include "BKE_blender.h"
68 #include "BKE_context.h"
69 #include "BKE_customdata.h"
70 #include "BKE_displist.h"
71 #include "BKE_effect.h"
72 #include "BKE_fluidsim.h"
73 #include "BKE_global.h"
74 #include "BKE_modifier.h"
75 #include "BKE_main.h"
76 #include "BKE_key.h"
77 #include "BKE_scene.h"
78 #include "BKE_object.h"
79 #include "BKE_softbody.h"
80 #include "BKE_DerivedMesh.h"
81 #include "BKE_ipo.h"
82
83 #include "PIL_time.h"
84
85 #include "LBM_fluidsim.h"
86
87 #include "BIF_gl.h"
88
89 #include "ED_anim_api.h"
90 #include "ED_fluidsim.h"
91
92 /* XXX */
93 /* from header info.c */
94 static int start_progress_bar(void) {return 0;};
95 static void end_progress_bar(void) {};
96 static void waitcursor() {};
97 static int progress_bar(float done, char *busy_info) {return 0;}
98 static int pupmenu() {return 0;}
99 /* XXX */
100
101
102 double fluidsimViscosityPreset[6] = {
103         -1.0,   /* unused */
104         -1.0,   /* manual */
105         1.0e-6, /* water */
106         5.0e-5, /* some (thick) oil */
107         2.0e-3, /* ca. honey */
108         -1.0    /* end */
109 };
110
111 char* fluidsimViscosityPresetString[6] = {
112         "UNUSED",       /* unused */
113         "UNUSED",       /* manual */
114         "  = 1.0 * 10^-6", /* water */
115         "  = 5.0 * 10^-5", /* some (thick) oil */
116         "  = 2.0 * 10^-3", /* ca. honey */
117         "INVALID"       /* end */
118 };
119
120 /* enable/disable overall compilation */
121 #ifndef DISABLE_ELBEEM
122
123
124 /* ********************** fluid sim settings struct functions ********************** */
125
126 /* helper function */
127 void fluidsimGetGeometryObjFilename(struct Object *ob, char *dst) { //, char *srcname) {
128         //snprintf(dst,FILE_MAXFILE, "%s_cfgdata_%s.bobj.gz", srcname, ob->id.name);
129         snprintf(dst,FILE_MAXFILE, "fluidcfgdata_%s.bobj.gz", ob->id.name);
130 }
131
132
133
134
135 /* ******************************************************************************** */
136 /* ********************** fluid sim channel helper functions ********************** */
137 /* ******************************************************************************** */
138
139 // no. of entries for the two channel sizes
140 #define CHANNEL_FLOAT 1
141 #define CHANNEL_VEC   3
142
143 #define FS_FREE_ONECHANNEL(c,str) { \
144         if(c){ MEM_freeN(c); c=NULL; } \
145 } // end ONE CHANN, debug: fprintf(stderr,"freeing " str " \n"); 
146
147 #define FS_FREE_CHANNELS { \
148         FS_FREE_ONECHANNEL(timeAtIndex,"timeAtIndex");\
149         FS_FREE_ONECHANNEL(timeAtFrame,"timeAtFrame");\
150         FS_FREE_ONECHANNEL(channelDomainTime,"channelDomainTime"); \
151         FS_FREE_ONECHANNEL(channelDomainGravity,"channelDomainGravity");\
152         FS_FREE_ONECHANNEL(channelDomainViscosity,"channelDomainViscosity");\
153         for(i=0;i<256;i++) { \
154                 FS_FREE_ONECHANNEL(channelObjMove[i][0],"channelObjMove0"); \
155                 FS_FREE_ONECHANNEL(channelObjMove[i][1],"channelObjMove1"); \
156                 FS_FREE_ONECHANNEL(channelObjMove[i][2],"channelObjMove2"); \
157                 FS_FREE_ONECHANNEL(channelObjInivel[i],"channelObjInivel"); \
158                 FS_FREE_ONECHANNEL(channelObjActive[i],"channelObjActive"); \
159                 FS_FREE_ONECHANNEL(channelAttractforceStrength[i],"channelAttractforceStrength"); \
160                 FS_FREE_ONECHANNEL(channelAttractforceRadius[i],"channelAttractforceRadius"); \
161                 FS_FREE_ONECHANNEL(channelVelocityforceStrength[i],"channelVelocityforceStrength"); \
162                 FS_FREE_ONECHANNEL(channelVelocityforceRadius[i],"channelVelocityforceRadius"); \
163         }  \
164 } // end FS FREE CHANNELS
165
166
167 // simplify channels before printing
168 // for API this is done anyway upon init
169 #if 0
170 static void fluidsimPrintChannel(FILE *file, float *channel, int paramsize, char *str, int entries) 
171
172         int i,j; 
173         int channelSize = paramsize; 
174
175         if(entries==3) {
176                 elbeemSimplifyChannelVec3( channel, &channelSize); 
177         } else if(entries==1) {
178                 elbeemSimplifyChannelFloat( channel, &channelSize); 
179         } else {
180                 // invalid, cant happen?
181         }
182
183         fprintf(file, "      CHANNEL %s = \n", str); 
184         for(i=0; i<channelSize;i++) { 
185                 fprintf(file,"        ");  
186                 for(j=0;j<=entries;j++) {  // also print time value
187                         fprintf(file," %f ", channel[i*(entries+1)+j] ); 
188                         if(j==entries-1){ fprintf(file,"  "); }
189                 } 
190                 fprintf(file," \n");  
191         } 
192
193         fprintf(file,  "      ; \n" ); 
194 }
195 #endif
196
197 static void fluidsimInitChannel(Scene *scene, float **setchannel, int size, float *time, 
198                 int *icuIds, float *defaults, Ipo* ipo, int entries) 
199 {
200 #if 0
201         /* goes away completely */
202         int i,j;
203         IpoCurve* icus[3];
204         char *cstr = NULL;
205         float *channel = NULL;
206         float aniFrlen = scene->r.framelen;
207         int current_frame = scene->r.cfra;
208         if((entries<1) || (entries>3)) {
209                 printf("fluidsimInitChannel::Error - invalid no. of entries: %d\n",entries);
210                 entries = 1;
211         }
212
213         cstr = "fluidsiminit_channelfloat";
214         if(entries>1) cstr = "fluidsiminit_channelvec";
215         channel = MEM_callocN( size* (entries+1)* sizeof(float), cstr );
216         
217         if(ipo) {
218                 for(j=0; j<entries; j++) icus[j]  = find_ipocurve(ipo, icuIds[j] );
219         } else {
220                 for(j=0; j<entries; j++) icus[j]  = NULL; 
221         }
222         
223         for(j=0; j<entries; j++) {
224                 if(icus[j]) { 
225                         for(i=1; i<=size; i++) {
226                                 /* Bugfix to make python drivers working
227                                 // which uses Blender.get("curframe") 
228                                 */
229                                 scene->r.cfra = floor(aniFrlen*((float)i));
230                                 
231                                 // XXX calc_icu(icus[j], aniFrlen*((float)i) );
232                                 channel[(i-1)*(entries+1) + j] = icus[j]->curval;
233                         }
234                 }  else {
235                         for(i=1; i<=size; i++) { channel[(i-1)*(entries+1) + j] = defaults[j]; }
236                 }
237                 //printf("fluidsimInitChannel entry:%d , ",j); for(i=1; i<=size; i++) { printf(" val%d:%f ",i, channel[(i-1)*(entries+1) + j] ); } printf(" \n"); // DEBUG
238         }
239         // set time values
240         for(i=1; i<=size; i++) {
241                 channel[(i-1)*(entries+1) + entries] = time[i];
242         }
243         scene->r.cfra = current_frame;
244         *setchannel = channel;
245 #endif
246 }
247
248 static void fluidsimInitMeshChannel(bContext *C, float **setchannel, int size, Object *obm, int vertices, 
249                                                                         float *time, int modifierIndex) 
250 {
251         Scene *scene= CTX_data_scene(C);
252         float *channel = NULL;
253         int mallsize = size* (3*vertices+1);
254         int frame,i;
255         int numVerts=0, numTris=0;
256         int setsize = 3*vertices+1;
257
258         channel = MEM_callocN( mallsize* sizeof(float), "fluidsim_meshchannel" );
259
260         //fprintf(stderr,"\n\nfluidsimInitMeshChannel size%d verts%d mallsize%d \n\n\n",size,vertices,mallsize);
261         for(frame=1; frame<=size; frame++) {
262                 float *verts=NULL;
263                 int *tris=NULL;
264                 scene->r.cfra = frame;
265                 ED_update_for_newframe(C, 0);
266
267                 initElbeemMesh(scene, obm, &numVerts, &verts, &numTris, &tris, 1, modifierIndex);
268                 //fprintf(stderr,"\nfluidsimInitMeshChannel frame%d verts%d/%d \n\n",frame,vertices,numVerts);
269                 for(i=0; i<3*vertices;i++) {
270                         channel[(frame-1)*setsize + i] = verts[i];
271                         //fprintf(stdout," frame%d vert%d=%f \n",frame,i,verts[i]);
272                         //if(i%3==2) fprintf(stdout,"\n");
273                 }
274                 channel[(frame-1)*setsize + setsize-1] = time[frame];
275
276                 MEM_freeN(verts);
277                 MEM_freeN(tris);
278         }
279         *setchannel = channel;
280 }
281
282
283 /* ******************************************************************************** */
284 /* ********************** simulation thread             ************************* */
285 /* ******************************************************************************** */
286
287 static volatile int     globalBakeState = 0; // 0 everything ok, -1 abort simulation, -2 sim error, 1 sim done
288 static volatile int     globalBakeFrame = 0;
289 static volatile int g_break= 0;
290
291 // run simulation in seperate thread
292 static void *fluidsimSimulateThread(void *unused) { // *ptr) {
293         //char* fnameCfgPath = (char*)(ptr);
294         int ret=0;
295         
296         ret = elbeemSimulate();
297         BLI_lock_thread(LOCK_CUSTOM1);
298         if(globalBakeState==0) {
299                 if(ret==0) {
300                         // if no error, set to normal exit
301                         globalBakeState = 1;
302                 } else {
303                         // simulation failed, display error
304                         globalBakeState = -2;
305                 }
306         }
307         BLI_unlock_thread(LOCK_CUSTOM1);
308         return NULL;
309 }
310
311
312 int runSimulationCallback(void *data, int status, int frame) {
313         //elbeemSimulationSettings *settings = (elbeemSimulationSettings*)data;
314         //printf("elbeem blender cb s%d, f%d, domainid:%d \n", status,frame, settings->domainId ); // DEBUG
315         int state = 0;
316         if(status==FLUIDSIM_CBSTATUS_NEWFRAME) {
317                 BLI_lock_thread(LOCK_CUSTOM1);
318                 globalBakeFrame = frame-1;
319                 BLI_unlock_thread(LOCK_CUSTOM1);
320         }
321         
322         //if((frameCounter==3) && (!frameStop)) { frameStop=1; return 1; }
323                 
324         BLI_lock_thread(LOCK_CUSTOM1);
325         state = globalBakeState;
326         BLI_unlock_thread(LOCK_CUSTOM1);
327         
328         if(state!=0) {
329                 return FLUIDSIM_CBRET_ABORT;
330         }
331         
332         return FLUIDSIM_CBRET_CONTINUE;
333 }
334
335
336 /* ******************************************************************************** */
337 /* ********************** write fluidsim config to file ************************* */
338 /* ******************************************************************************** */
339
340 void fluidsimBake(bContext *C, struct Object *ob)
341 {
342         Scene *scene= CTX_data_scene(C);
343         FILE *fileCfg;
344         int i;
345         struct Object *fsDomain = NULL;
346         FluidsimSettings *domainSettings;
347         struct Object *obit = NULL; /* object iterator */
348         Base *base;
349         int origFrame = scene->r.cfra;
350         char debugStrBuffer[256];
351         int dirExist = 0;
352         int gridlevels = 0;
353         int simAborted = 0; // was the simulation aborted by user?
354         int  doExportOnly = 0;
355         char *exportEnvStr = "BLENDER_ELBEEMEXPORTONLY";
356         const char *strEnvName = "BLENDER_ELBEEMDEBUG"; // from blendercall.cpp
357         //char *channelNames[3] = { "translation","rotation","scale" };
358
359         char *suffixConfig = "fluidsim.cfg";
360         char *suffixSurface = "fluidsurface";
361         char newSurfdataPath[FILE_MAXDIR+FILE_MAXFILE]; // modified output settings
362         char targetDir[FILE_MAXDIR+FILE_MAXFILE];  // store & modify output settings
363         char targetFile[FILE_MAXDIR+FILE_MAXFILE]; // temp. store filename from targetDir for access
364         int  outStringsChanged = 0;             // modified? copy back before baking
365         int  haveSomeFluid = 0;                 // check if any fluid objects are set
366
367         // config vars, inited before either export or run...
368         double calcViscosity = 0.0;
369         int noFrames;
370         double aniFrameTime;
371         float aniFrlen;
372         int   channelObjCount;
373         float *bbStart = NULL;
374         float *bbSize = NULL;
375         float domainMat[4][4];
376         float invDomMat[4][4];
377         // channel data
378         int   allchannelSize; // fixed by no. of frames
379         int   startFrame = 1;  // dont use scene->r.sfra here, always start with frame 1
380         // easy frame -> sim time calc
381         float *timeAtFrame=NULL, *timeAtIndex=NULL;
382         // domain
383         float *channelDomainTime = NULL;
384         float *channelDomainViscosity = NULL; 
385         float *channelDomainGravity = NULL;
386         // objects (currently max. 256 objs)
387         float *channelObjMove[256][3]; // object movments , 0=trans, 1=rot, 2=scale
388         float *channelObjInivel[256];    // initial velocities
389         float *channelObjActive[256];    // obj active channel
390         
391         /* fluid control channels */
392         float *channelAttractforceStrength[256];
393         float *channelAttractforceRadius[256];
394         float *channelVelocityforceStrength[256];
395         float *channelVelocityforceRadius[256];
396         FluidsimModifierData *fluidmd = NULL;
397         Mesh *mesh = NULL;
398         
399         if(getenv(strEnvName)) {
400                 int dlevel = atoi(getenv(strEnvName));
401                 elbeemSetDebugLevel(dlevel);
402                 snprintf(debugStrBuffer,256,"fluidsimBake::msg: Debug messages activated due to envvar '%s'\n",strEnvName); 
403                 elbeemDebugOut(debugStrBuffer);
404         }
405         if(getenv(exportEnvStr)) {
406                 doExportOnly = atoi(getenv(exportEnvStr));
407                 snprintf(debugStrBuffer,256,"fluidsimBake::msg: Exporting mode set to '%d' due to envvar '%s'\n",doExportOnly, exportEnvStr); 
408                 elbeemDebugOut(debugStrBuffer);
409         }
410
411         // make sure it corresponds to startFrame setting
412         // old: noFrames = scene->r.efra - scene->r.sfra +1;
413         noFrames = scene->r.efra - 0;
414         if(noFrames<=0) {
415                 pupmenu("Fluidsim Bake Error%t|No frames to export - check your animation range settings. Aborted%x0");
416                 return;
417         }
418
419         /* no object pointer, find in selected ones.. */
420         if(!ob) {
421                 for(base=scene->base.first; base; base= base->next) {
422                         if ((base)->flag & SELECT) 
423                         {
424                                 FluidsimModifierData *fluidmdtmp = (FluidsimModifierData *)modifiers_findByType(base->object, eModifierType_Fluidsim);
425                                 
426                                 if(fluidmdtmp && (base->object->type==OB_MESH)) 
427                                 {
428                                         if(fluidmdtmp->fss->type == OB_FLUIDSIM_DOMAIN) 
429                                         {
430                                                 ob = base->object;
431                                                 break;
432                                         }
433                                 }
434                         }
435                 }
436                 // no domains found?
437                 if(!ob) return;
438         }
439         
440         channelObjCount = 0;
441         for(base=scene->base.first; base; base= base->next) 
442         {
443                 FluidsimModifierData *fluidmdtmp = (FluidsimModifierData *)modifiers_findByType(base->object, eModifierType_Fluidsim);
444                 obit = base->object;
445                 if( fluidmdtmp && 
446                         (obit->type==OB_MESH) &&
447                         (fluidmdtmp->fss->type != OB_FLUIDSIM_DOMAIN) &&  // if has to match 3 places! // CHECKMATCH
448                         (fluidmdtmp->fss->type != OB_FLUIDSIM_PARTICLE) ) 
449                 {
450                         channelObjCount++;
451                 }
452         }
453         
454         if (channelObjCount>=255) {
455                 pupmenu("Fluidsim Bake Error%t|Cannot bake with more then 256 objects");
456                 return;
457         }
458
459         /* check if there's another domain... */
460         for(base=scene->base.first; base; base= base->next) 
461         {
462                 FluidsimModifierData *fluidmdtmp = (FluidsimModifierData *)modifiers_findByType(base->object, eModifierType_Fluidsim);
463                 obit = base->object;
464                 if( fluidmdtmp &&(obit->type==OB_MESH)) 
465                 {
466                         if(fluidmdtmp->fss->type == OB_FLUIDSIM_DOMAIN) 
467                         {
468                                 if(obit != ob) 
469                                 {
470                                         pupmenu("Fluidsim Bake Error%t|There should be only one domain object! Aborted%x0");
471                                         return;
472                                 }
473                         }
474                 }
475         }
476         
477         // check if theres any fluid
478         // abort baking if not...
479         for(base=scene->base.first; base; base= base->next) 
480         {
481                 FluidsimModifierData *fluidmdtmp = (FluidsimModifierData *)modifiers_findByType(base->object, eModifierType_Fluidsim);
482                 obit = base->object;
483                 if( fluidmdtmp && 
484                         (obit->type==OB_MESH) && 
485                         ((fluidmdtmp->fss->type == OB_FLUIDSIM_FLUID) ||
486                         (fluidmdtmp->fss->type == OB_FLUIDSIM_INFLOW) ))
487                 {
488                         haveSomeFluid = 1;
489                         break;
490                 }
491         }
492         if(!haveSomeFluid) {
493                 pupmenu("Fluidsim Bake Error%t|No fluid objects in scene... Aborted%x0");
494                 return;
495         }
496         
497         /* these both have to be valid, otherwise we wouldnt be here */
498         /* dont use ob here after...*/
499         fsDomain = ob;
500         fluidmd = (FluidsimModifierData *)modifiers_findByType(ob, eModifierType_Fluidsim);
501         domainSettings = fluidmd->fss;
502         ob = NULL;
503         mesh = fsDomain->data;
504         
505         // calculate bounding box
506         fluid_get_bb(mesh->mvert, mesh->totvert, fsDomain->obmat, domainSettings->bbStart, domainSettings->bbSize);
507         
508         // reset last valid frame
509         domainSettings->lastgoodframe = -1;
510         
511         /* rough check of settings... */
512         if(domainSettings->previewresxyz > domainSettings->resolutionxyz) {
513                 snprintf(debugStrBuffer,256,"fluidsimBake::warning - Preview (%d) >= Resolution (%d)... setting equal.\n", domainSettings->previewresxyz ,  domainSettings->resolutionxyz); 
514                 elbeemDebugOut(debugStrBuffer);
515                 domainSettings->previewresxyz = domainSettings->resolutionxyz;
516         }
517         // set adaptive coarsening according to resolutionxyz
518         // this should do as an approximation, with in/outflow
519         // doing this more accurate would be overkill
520         // perhaps add manual setting?
521         if(domainSettings->maxRefine <0) {
522                 if(domainSettings->resolutionxyz>128) {
523                         gridlevels = 2;
524                 } else
525                 if(domainSettings->resolutionxyz>64) {
526                         gridlevels = 1;
527                 } else {
528                         gridlevels = 0;
529                 }
530         } else {
531                 gridlevels = domainSettings->maxRefine;
532         }
533         snprintf(debugStrBuffer,256,"fluidsimBake::msg: Baking %s, refine: %d\n", fsDomain->id.name , gridlevels ); 
534         elbeemDebugOut(debugStrBuffer);
535
536         // prepare names...
537         strncpy(targetDir, domainSettings->surfdataPath, FILE_MAXDIR);
538         strncpy(newSurfdataPath, domainSettings->surfdataPath, FILE_MAXDIR);
539         BLI_convertstringcode(targetDir, G.sce); // fixed #frame-no 
540
541         strcpy(targetFile, targetDir);
542         strcat(targetFile, suffixConfig);
543         if(!doExportOnly) { strcat(targetFile,".tmp"); }  // dont overwrite/delete original file
544         // make sure all directories exist
545         // as the bobjs use the same dir, this only needs to be checked
546         // for the cfg output
547         BLI_make_existing_file(targetFile);
548
549         // check selected directory
550         // simply try to open cfg file for writing to test validity of settings
551         fileCfg = fopen(targetFile, "w");
552         if(fileCfg) { 
553                 dirExist = 1; fclose(fileCfg); 
554                 // remove cfg dummy from  directory test
555                 if(!doExportOnly) { BLI_delete(targetFile, 0,0); }
556         }
557
558         if((strlen(targetDir)<1) || (!dirExist)) {
559                 char blendDir[FILE_MAXDIR+FILE_MAXFILE], blendFile[FILE_MAXDIR+FILE_MAXFILE];
560                 // invalid dir, reset to current/previous
561                 strcpy(blendDir, G.sce);
562                 BLI_splitdirstring(blendDir, blendFile);
563                 if(strlen(blendFile)>6){
564                         int len = strlen(blendFile);
565                         if( (blendFile[len-6]=='.')&& (blendFile[len-5]=='b')&& (blendFile[len-4]=='l')&&
566                                         (blendFile[len-3]=='e')&& (blendFile[len-2]=='n')&& (blendFile[len-1]=='d') ){
567                                 blendFile[len-6] = '\0';
568                         }
569                 }
570                 // todo... strip .blend ?
571                 snprintf(newSurfdataPath,FILE_MAXFILE+FILE_MAXDIR,"//fluidsimdata/%s_%s_", blendFile, fsDomain->id.name);
572
573                 snprintf(debugStrBuffer,256,"fluidsimBake::error - warning resetting output dir to '%s'\n", newSurfdataPath);
574                 elbeemDebugOut(debugStrBuffer);
575                 outStringsChanged=1;
576         }
577
578         // check if modified output dir is ok
579         if(outStringsChanged) {
580                 char dispmsg[FILE_MAXDIR+FILE_MAXFILE+256];
581                 int  selection=0;
582                 strcpy(dispmsg,"Output settings set to: '");
583                 strcat(dispmsg, newSurfdataPath);
584                 strcat(dispmsg, "'%t|Continue with changed settings%x1|Discard and abort%x0");
585
586                 // ask user if thats what he/she wants...
587                 selection = pupmenu(dispmsg);
588                 if(selection<1) return; // 0 from menu, or -1 aborted
589                 strcpy(targetDir, newSurfdataPath);
590                 strncpy(domainSettings->surfdataPath, newSurfdataPath, FILE_MAXDIR);
591                 BLI_convertstringcode(targetDir, G.sce); // fixed #frame-no 
592         }
593         
594         // --------------------------------------------------------------------------------------------
595         // dump data for start frame 
596         // CHECK more reasonable to number frames according to blender?
597         // dump data for frame 0
598         scene->r.cfra = startFrame;
599         ED_update_for_newframe(C, 0);
600         
601         // init common export vars for both file export and run
602         for(i=0; i<256; i++) {
603                 channelObjMove[i][0] = channelObjMove[i][1] = channelObjMove[i][2] = NULL;
604                 channelObjInivel[i] = NULL;
605                 channelObjActive[i] = NULL;
606                 channelAttractforceStrength[i] = NULL;
607                 channelAttractforceRadius[i] = NULL;
608                 channelVelocityforceStrength[i] = NULL;
609                 channelVelocityforceRadius[i] = NULL;
610         }
611         allchannelSize = scene->r.efra; // always use till last frame
612         aniFrameTime = (domainSettings->animEnd - domainSettings->animStart)/(double)noFrames;
613         // blender specific - scale according to map old/new settings in anim panel:
614         aniFrlen = scene->r.framelen;
615         if(domainSettings->viscosityMode==1) {
616                 /* manual mode, visc=value/(10^-vexp) */
617                 calcViscosity = (1.0/pow(10.0,domainSettings->viscosityExponent)) * domainSettings->viscosityValue;
618         } else {
619                 calcViscosity = fluidsimViscosityPreset[ domainSettings->viscosityMode ];
620         }
621
622         bbStart = domainSettings->bbStart;
623         bbSize = domainSettings->bbSize;
624
625         // always init
626         { int timeIcu[1] = { FLUIDSIM_TIME };
627                 float timeDef[1] = { 1. };
628                 int gravIcu[3] = { FLUIDSIM_GRAV_X, FLUIDSIM_GRAV_Y, FLUIDSIM_GRAV_Z };
629                 float gravDef[3];
630                 int viscIcu[1] = { FLUIDSIM_VISC };
631                 float viscDef[1] = { 1. };
632
633                 gravDef[0] = domainSettings->gravx;
634                 gravDef[1] = domainSettings->gravy;
635                 gravDef[2] = domainSettings->gravz;
636
637                 // time channel is a bit special, init by hand...
638                 timeAtIndex = MEM_callocN( (allchannelSize+1)*1*sizeof(float), "fluidsiminit_timeatindex");
639                 for(i=0; i<=scene->r.efra; i++) {
640                         timeAtIndex[i] = (float)(i-startFrame);
641                 }
642                 fluidsimInitChannel(scene, &channelDomainTime, allchannelSize, timeAtIndex, timeIcu,timeDef, domainSettings->ipo, CHANNEL_FLOAT ); // NDEB
643                 // time channel is a multiplicator for aniFrameTime
644                 if(channelDomainTime) {
645                         for(i=0; i<allchannelSize; i++) { 
646                                 channelDomainTime[i*2+0] = aniFrameTime * channelDomainTime[i*2+0]; 
647                                 if(channelDomainTime[i*2+0]<0.) channelDomainTime[i*2+0] = 0.;
648                         }
649                 }
650                 timeAtFrame = MEM_callocN( (allchannelSize+1)*1*sizeof(float), "fluidsiminit_timeatframe");
651                 timeAtFrame[0] = timeAtFrame[1] = domainSettings->animStart; // start at index 1
652                 if(channelDomainTime) {
653                         for(i=2; i<=allchannelSize; i++) {
654                                 timeAtFrame[i] = timeAtFrame[i-1]+channelDomainTime[(i-1)*2+0];
655                         }
656                 } else {
657                         for(i=2; i<=allchannelSize; i++) { timeAtFrame[i] = timeAtFrame[i-1]+aniFrameTime; }
658                 }
659
660                 fluidsimInitChannel(scene, &channelDomainViscosity, allchannelSize, timeAtFrame, viscIcu,viscDef, domainSettings->ipo, CHANNEL_FLOAT ); // NDEB
661                 if(channelDomainViscosity) {
662                         for(i=0; i<allchannelSize; i++) { channelDomainViscosity[i*2+0] = calcViscosity * channelDomainViscosity[i*2+0]; }
663                 }
664                 fluidsimInitChannel(scene, &channelDomainGravity, allchannelSize, timeAtFrame, gravIcu,gravDef, domainSettings->ipo, CHANNEL_VEC );
665         } // domain channel init
666         
667         // init obj movement channels
668         channelObjCount=0;
669         for(base=scene->base.first; base; base= base->next) 
670         {
671                 FluidsimModifierData *fluidmdtmp = (FluidsimModifierData *)modifiers_findByType(base->object, eModifierType_Fluidsim);
672                 obit = base->object;
673                 
674                 if( fluidmdtmp && 
675                         (obit->type==OB_MESH) &&
676                         (fluidmdtmp->fss->type != OB_FLUIDSIM_DOMAIN) &&  // if has to match 3 places! // CHECKMATCH
677                         (fluidmdtmp->fss->type != OB_FLUIDSIM_PARTICLE) ) {
678
679                         //  cant use fluidsimInitChannel for obj channels right now, due
680                         //  to the special DXXX channels, and the rotation specialities
681                         IpoCurve *icuex[3][3];
682                         //IpoCurve *par_icuex[3][3];
683 #if 0
684                         int icuIds[3][3] = { 
685                                 {OB_LOC_X,  OB_LOC_Y,  OB_LOC_Z},
686                                 {OB_ROT_X,  OB_ROT_Y,  OB_ROT_Z},
687                                 {OB_SIZE_X, OB_SIZE_Y, OB_SIZE_Z} 
688                         };
689                         int icudIds[3][3] = { 
690                                 {OB_DLOC_X,  OB_DLOC_Y,  OB_DLOC_Z},
691                                 {OB_DROT_X,  OB_DROT_Y,  OB_DROT_Z},
692                                 {OB_DSIZE_X, OB_DSIZE_Y, OB_DSIZE_Z} 
693                         };
694 #endif
695                         // relative ipos
696                         IpoCurve *icudex[3][3];
697                         //IpoCurve *par_icudex[3][3];
698                         int j,k;
699                         float vals[3] = {0.0,0.0,0.0}; 
700                         int o = channelObjCount;
701                         int   inivelIcu[3] =  { FLUIDSIM_VEL_X, FLUIDSIM_VEL_Y, FLUIDSIM_VEL_Z };
702                         float inivelDefs[3];
703                         int   activeIcu[1] =  { FLUIDSIM_ACTIVE };
704                         float activeDefs[1] = { 1 }; // default to on
705
706                         inivelDefs[0] = fluidmdtmp->fss->iniVelx;
707                         inivelDefs[1] = fluidmdtmp->fss->iniVely;
708                         inivelDefs[2] = fluidmdtmp->fss->iniVelz;
709
710                         // check & init loc,rot,size
711                         for(j=0; j<3; j++) {
712                                 for(k=0; k<3; k++) {
713                                         // XXX icuex[j][k]  = find_ipocurve(obit->ipo, icuIds[j][k] );
714                                         // XXX icudex[j][k] = find_ipocurve(obit->ipo, icudIds[j][k] );
715                                         // XXX lines below were already disabled!
716                                         //if(obit->parent) {
717                                                 //par_icuex[j][k]  = find_ipocurve(obit->parent->ipo, icuIds[j][k] );
718                                                 //par_icudex[j][k] = find_ipocurve(obit->parent->ipo, icudIds[j][k] );
719                                         //}
720                                 }
721                         }
722
723                         for(j=0; j<3; j++) {
724                                 channelObjMove[o][j] = MEM_callocN( allchannelSize*4*sizeof(float), "fluidsiminit_objmovchannel");
725                                 for(i=1; i<=allchannelSize; i++) {
726
727                                         for(k=0; k<3; k++) {
728                                                 if(icuex[j][k]) { 
729                                                         // IPO exists, use it ...
730                                                         // XXX calc_icu(icuex[j][k], aniFrlen*((float)i) );
731                                                         vals[k] = icuex[j][k]->curval; 
732                                                         if(obit->parent) {
733                                                                 // add parent transform, multiply scaling, add trafo&rot
734                                                                 //calc_icu(par_icuex[j][k], aniFrlen*((float)i) );
735                                                                 //if(j==2) { vals[k] *= par_icuex[j][k]->curval; }
736                                                                 //else { vals[k] += par_icuex[j][k]->curval; }
737                                                         }
738                                                 } else {
739                                                         // use defaults from static values
740                                                         float setval=0.0;
741                                                         if(j==0) { 
742                                                                 setval = obit->loc[k];
743                                                                 if(obit->parent){ setval += obit->parent->loc[k]; }
744                                                         } else if(j==1) { 
745                                                                 setval = ( 180.0*obit->rot[k] )/( 10.0*M_PI );
746                                                                 if(obit->parent){ setval = ( 180.0*(obit->rot[k]+obit->parent->rot[k]) )/( 10.0*M_PI ); }
747                                                         } else { 
748                                                                 setval = obit->size[k]; 
749                                                                 if(obit->parent){ setval *= obit->parent->size[k]; }
750                                                         }
751                                                         vals[k] = setval;
752                                                 }
753                                                 if(icudex[j][k]) { 
754                                                         // XXX calc_icu(icudex[j][k], aniFrlen*((float)i) );
755                                                         //vals[k] += icudex[j][k]->curval; 
756                                                         // add transform, multiply scaling, add trafo&rot
757                                                         if(j==2) { vals[k] *= icudex[j][k]->curval; }
758                                                         else { vals[k] += icudex[j][k]->curval; }
759                                                         if(obit->parent) {
760                                                                 // add parent transform, multiply scaling, add trafo&rot
761                                                                 //calc_icu(par_icuex[j][k], aniFrlen*((float)i) );
762                                                                 //if(j==2) { vals[k] *= par_icudex[j][k]->curval; }
763                                                                 //else { vals[k] += par_icudex[j][k]->curval; }
764                                                         }
765                                                 } 
766                                         } // k
767
768                                         for(k=0; k<3; k++) {
769                                                 float set = vals[k];
770                                                 if(j==1) { // rot is downscaled by 10 for ipo !?
771                                                         set = 360.0 - (10.0*set);
772                                                 }
773                                                 channelObjMove[o][j][(i-1)*4 + k] = set;
774                                         } // k
775                                         channelObjMove[o][j][(i-1)*4 + 3] = timeAtFrame[i];
776                                 }
777                         }
778                         
779                         {
780                                 int   attrFSIcu[1] =  { FLUIDSIM_ATTR_FORCE_STR };
781                                 int   attrFRIcu[1] =  { FLUIDSIM_ATTR_FORCE_RADIUS };
782                                 int   velFSIcu[1] =  { FLUIDSIM_VEL_FORCE_STR };
783                                 int   velFRIcu[1] =  { FLUIDSIM_VEL_FORCE_RADIUS };
784
785                                 float attrFSDefs[1];
786                                 float attrFRDefs[1];
787                                 float velFSDefs[1];
788                                 float velFRDefs[1];
789                                 
790                                 attrFSDefs[0] = fluidmdtmp->fss->attractforceStrength;
791                                 attrFRDefs[0] = fluidmdtmp->fss->attractforceRadius;
792                                 velFSDefs[0] = fluidmdtmp->fss->velocityforceStrength;
793                                 velFRDefs[0] = fluidmdtmp->fss->velocityforceRadius;
794                                 
795                                 fluidsimInitChannel(scene, &channelAttractforceStrength[o], allchannelSize, timeAtFrame, attrFSIcu,attrFSDefs, fluidmdtmp->fss->ipo, CHANNEL_FLOAT );
796                                 fluidsimInitChannel(scene, &channelAttractforceRadius[o], allchannelSize, timeAtFrame, attrFRIcu,attrFRDefs, fluidmdtmp->fss->ipo, CHANNEL_FLOAT );
797                                 fluidsimInitChannel(scene, &channelVelocityforceStrength[o], allchannelSize, timeAtFrame, velFSIcu,velFSDefs, fluidmdtmp->fss->ipo, CHANNEL_FLOAT );
798                                 fluidsimInitChannel(scene, &channelVelocityforceRadius[o], allchannelSize, timeAtFrame, velFRIcu,velFRDefs, fluidmdtmp->fss->ipo, CHANNEL_FLOAT );
799                         }
800                         
801                         fluidsimInitChannel(scene, &channelObjInivel[o], allchannelSize, timeAtFrame, inivelIcu,inivelDefs, fluidmdtmp->fss->ipo, CHANNEL_VEC );
802                         fluidsimInitChannel(scene, &channelObjActive[o], allchannelSize, timeAtFrame, activeIcu,activeDefs, fluidmdtmp->fss->ipo, CHANNEL_FLOAT );
803                 
804
805                         channelObjCount++;
806
807                 }
808         }
809
810         // init trafo matrix
811         Mat4CpyMat4(domainMat, fsDomain->obmat);
812         if(!Mat4Invert(invDomMat, domainMat)) {
813                 snprintf(debugStrBuffer,256,"fluidsimBake::error - Invalid obj matrix?\n"); 
814                 elbeemDebugOut(debugStrBuffer);
815                 // FIXME add fatal msg
816                 FS_FREE_CHANNELS;
817                 return;
818         }
819
820
821         // --------------------------------------------------------------------------------------------
822         // start writing / exporting
823         strcpy(targetFile, targetDir);
824         strcat(targetFile, suffixConfig);
825         if(!doExportOnly) { strcat(targetFile,".tmp"); }  // dont overwrite/delete original file
826         // make sure these directories exist as well
827         if(outStringsChanged) {
828                 BLI_make_existing_file(targetFile);
829         }
830
831         if(!doExportOnly) {
832                 ListBase threads;
833
834                 // perform simulation with El'Beem api and threads
835                 elbeemSimulationSettings fsset;
836                 elbeemResetSettings(&fsset);
837                 fsset.version = 1;
838
839                 // setup global settings
840                 for(i=0 ; i<3; i++) fsset.geoStart[i] = bbStart[i];
841                 for(i=0 ; i<3; i++) fsset.geoSize[i] = bbSize[i];
842                 
843                 // simulate with 50^3
844                 fsset.resolutionxyz = (int)domainSettings->resolutionxyz;
845                 fsset.previewresxyz = (int)domainSettings->previewresxyz;
846                 // 10cm water domain
847                 fsset.realsize = domainSettings->realsize;
848                 fsset.viscosity = calcViscosity;
849                 // earth gravity
850                 fsset.gravity[0] = domainSettings->gravx;
851                 fsset.gravity[1] = domainSettings->gravy;
852                 fsset.gravity[2] = domainSettings->gravz;
853                 // simulate 5 frames, each 0.03 seconds, output to ./apitest_XXX.bobj.gz
854                 fsset.animStart = domainSettings->animStart;
855                 fsset.aniFrameTime = aniFrameTime;
856                 fsset.noOfFrames = noFrames; // is otherwise subtracted in parser
857                 strcpy(targetFile, targetDir);
858                 strcat(targetFile, suffixSurface);
859                 // defaults for compressibility and adaptive grids
860                 fsset.gstar = domainSettings->gstar;
861                 fsset.maxRefine = domainSettings->maxRefine; // check <-> gridlevels
862                 fsset.generateParticles = domainSettings->generateParticles; 
863                 fsset.numTracerParticles = domainSettings->generateTracers; 
864                 fsset.surfaceSmoothing = domainSettings->surfaceSmoothing; 
865                 fsset.surfaceSubdivs = domainSettings->surfaceSubdivs; 
866                 fsset.farFieldSize = domainSettings->farFieldSize; 
867                 strcpy( fsset.outputPath, targetFile);
868
869                 // domain channels
870                 fsset.channelSizeFrameTime = 
871                 fsset.channelSizeViscosity = 
872                 fsset.channelSizeGravity =  allchannelSize;
873                 fsset.channelFrameTime = channelDomainTime;
874                 fsset.channelViscosity = channelDomainViscosity;
875                 fsset.channelGravity = channelDomainGravity;
876
877                 fsset.runsimCallback = &runSimulationCallback;
878                 fsset.runsimUserData = &fsset;
879
880                 if(     (domainSettings->typeFlags&OB_FSBND_NOSLIP))   fsset.domainobsType = FLUIDSIM_OBSTACLE_NOSLIP;
881                 else if((domainSettings->typeFlags&OB_FSBND_PARTSLIP)) fsset.domainobsType = FLUIDSIM_OBSTACLE_PARTSLIP;
882                 else if((domainSettings->typeFlags&OB_FSBND_FREESLIP)) fsset.domainobsType = FLUIDSIM_OBSTACLE_FREESLIP;
883                 fsset.domainobsPartslip = domainSettings->partSlipValue;
884                 fsset.generateVertexVectors = (domainSettings->domainNovecgen==0);
885
886                 // init blender trafo matrix
887                 // fprintf(stderr,"elbeemInit - mpTrafo:\n");
888                 { int j; 
889                 for(i=0; i<4; i++) {
890                         for(j=0; j<4; j++) {
891                                 fsset.surfaceTrafo[i*4+j] = invDomMat[j][i];
892                                 // fprintf(stderr,"elbeemInit - mpTrafo %d %d = %f (%d) \n", i,j, fsset.surfaceTrafo[i*4+j] , (i*4+j) );
893                         }
894                 } }
895
896           // init solver with settings
897                 elbeemInit();
898                 elbeemAddDomain(&fsset);
899                 
900                 // init objects
901                 channelObjCount = 0;
902                 for(base=scene->base.first; base; base= base->next) {
903                         FluidsimModifierData *fluidmdtmp = (FluidsimModifierData *)modifiers_findByType(base->object, eModifierType_Fluidsim);
904                         obit = base->object;
905                         //{ snprintf(debugStrBuffer,256,"DEBUG object name=%s, type=%d ...\n", obit->id.name, obit->type); elbeemDebugOut(debugStrBuffer); } // DEBUG
906                         if( fluidmdtmp &&  // if has to match 3 places! // CHECKMATCH
907                                 (obit->type==OB_MESH) &&
908                                 (fluidmdtmp->fss->type != OB_FLUIDSIM_DOMAIN) &&
909                                 (fluidmdtmp->fss->type != OB_FLUIDSIM_PARTICLE)) 
910                         {
911                                 float *verts=NULL;
912                                 int *tris=NULL;
913                                 int numVerts=0, numTris=0;
914                                 int o = channelObjCount;
915                                 int     deform = (fluidmdtmp->fss->domainNovecgen); // misused value
916                                 // todo - use blenderInitElbeemMesh
917                                 int modifierIndex = modifiers_indexInObject(obit, (ModifierData *)fluidmdtmp);
918                                 
919                                 elbeemMesh fsmesh;
920                                 elbeemResetMesh( &fsmesh );
921                                 fsmesh.type = fluidmdtmp->fss->type;
922                                 // get name of object for debugging solver
923                                 fsmesh.name = obit->id.name; 
924
925                                 initElbeemMesh(scene, obit, &numVerts, &verts, &numTris, &tris, 0, modifierIndex);
926                                 fsmesh.numVertices   = numVerts;
927                                 fsmesh.numTriangles  = numTris;
928                                 fsmesh.vertices      = verts;
929                                 fsmesh.triangles     = tris;
930
931                                 fsmesh.channelSizeTranslation  = 
932                                 fsmesh.channelSizeRotation     = 
933                                 fsmesh.channelSizeScale        = 
934                                 fsmesh.channelSizeInitialVel   = 
935                                 fsmesh.channelSizeActive       = allchannelSize;
936
937                                 fsmesh.channelTranslation      = channelObjMove[o][0];
938                                 fsmesh.channelRotation         = channelObjMove[o][1];
939                                 fsmesh.channelScale            = channelObjMove[o][2];
940                                 fsmesh.channelActive           = channelObjActive[o];
941                                 if( (fsmesh.type == OB_FLUIDSIM_FLUID) ||
942                                 (fsmesh.type == OB_FLUIDSIM_INFLOW)) {
943                                         fsmesh.channelInitialVel       = channelObjInivel[o];
944                                         fsmesh.localInivelCoords = ((fluidmdtmp->fss->typeFlags&OB_FSINFLOW_LOCALCOORD)?1:0);
945                                 } 
946
947                                 if(     (fluidmdtmp->fss->typeFlags&OB_FSBND_NOSLIP))   fsmesh.obstacleType = FLUIDSIM_OBSTACLE_NOSLIP;
948                                 else if((fluidmdtmp->fss->typeFlags&OB_FSBND_PARTSLIP)) fsmesh.obstacleType = FLUIDSIM_OBSTACLE_PARTSLIP;
949                                 else if((fluidmdtmp->fss->typeFlags&OB_FSBND_FREESLIP)) fsmesh.obstacleType = FLUIDSIM_OBSTACLE_FREESLIP;
950                                 fsmesh.obstaclePartslip = fluidmdtmp->fss->partSlipValue;
951                                 fsmesh.volumeInitType = fluidmdtmp->fss->volumeInitType;
952                                 fsmesh.obstacleImpactFactor = fluidmdtmp->fss->surfaceSmoothing; // misused value
953                                 
954                                 if(fsmesh.type == OB_FLUIDSIM_CONTROL)
955                                 {
956                                         // control fluids will get exported as whole
957                                         deform = 1;
958                                         
959                                         fsmesh.cpsTimeStart = fluidmdtmp->fss->cpsTimeStart;
960                                         fsmesh.cpsTimeEnd = fluidmdtmp->fss->cpsTimeEnd;
961                                         fsmesh.cpsQuality = fluidmdtmp->fss->cpsQuality;
962                                         fsmesh.obstacleType = (fluidmdtmp->fss->flag & OB_FLUIDSIM_REVERSE);
963                                         
964                                         fsmesh.channelSizeAttractforceRadius = 
965                                         fsmesh.channelSizeVelocityforceStrength = 
966                                         fsmesh.channelSizeVelocityforceRadius = 
967                                         fsmesh.channelSizeAttractforceStrength = allchannelSize;
968                                         
969                                         fsmesh.channelAttractforceStrength = channelAttractforceStrength[o];
970                                         fsmesh.channelAttractforceRadius = channelAttractforceRadius[o];
971                                         fsmesh.channelVelocityforceStrength = channelVelocityforceStrength[o];
972                                         fsmesh.channelVelocityforceRadius = channelVelocityforceRadius[o];
973                                 }
974                                 else 
975                                 {
976                                         // set channels to 0
977                                         fsmesh.channelAttractforceStrength =
978                                         fsmesh.channelAttractforceRadius = 
979                                         fsmesh.channelVelocityforceStrength = 
980                                         fsmesh.channelVelocityforceRadius = NULL; 
981                                 }
982
983                                 // animated meshes
984                                 if(deform) {
985                                         fsmesh.channelSizeVertices = allchannelSize;
986                                         fluidsimInitMeshChannel(C, &fsmesh.channelVertices, allchannelSize, obit, numVerts, timeAtFrame, modifierIndex);
987                                         scene->r.cfra = startFrame;
988                                         ED_update_for_newframe(C, 0);
989                                         // remove channels
990                                         fsmesh.channelTranslation      = 
991                                         fsmesh.channelRotation         = 
992                                         fsmesh.channelScale            = NULL; 
993                                 } 
994
995                                 elbeemAddMesh(&fsmesh);
996
997                                 if(verts) MEM_freeN(verts);
998                                 if(tris) MEM_freeN(tris);
999                                 if(fsmesh.channelVertices) MEM_freeN(fsmesh.channelVertices);
1000                                 channelObjCount++;
1001                         } // valid mesh
1002                 } // objects
1003                 //domainSettings->type = OB_FLUIDSIM_DOMAIN; // enable for bake display again
1004                 //fsDomain->fluidsimFlag = OB_FLUIDSIM_ENABLE; // disable during bake
1005                 
1006                 // set to neutral, -1 means user abort, -2 means init error
1007                 globalBakeState = 0;
1008                 globalBakeFrame = 0;
1009                 BLI_init_threads(&threads, fluidsimSimulateThread, 1);
1010                 BLI_insert_thread(&threads, targetFile);
1011                 
1012                 {
1013                         int done = 0;
1014                         float noFramesf = (float)noFrames;
1015                         float percentdone = 0.0;
1016                         int lastRedraw = -1;
1017                         
1018                         g_break= 0;
1019                         G.afbreek= 0;   /* blender_test_break uses this global */
1020                         
1021                         start_progress_bar();
1022
1023                         while(done==0) {
1024                                 char busy_mess[80];
1025                                 
1026                                 waitcursor(1);
1027                                 
1028                                 // lukep we add progress bar as an interim mesure
1029                                 percentdone = globalBakeFrame / noFramesf;
1030                                 sprintf(busy_mess, "baking fluids %d / %d       |||", globalBakeFrame, (int) noFramesf);
1031                                 progress_bar(percentdone, busy_mess );
1032                                 
1033                                 // longer delay to prevent frequent redrawing
1034                                 PIL_sleep_ms(2000);
1035                                 
1036                                 BLI_lock_thread(LOCK_CUSTOM1);
1037                                 if(globalBakeState != 0) done = 1; // 1=ok, <0=error/abort
1038                                 BLI_unlock_thread(LOCK_CUSTOM1);
1039
1040                                 if (!G.background) {
1041                                         g_break= blender_test_break();
1042                                         
1043                                         if(g_break)
1044                                         {
1045                                                 // abort...
1046                                                 BLI_lock_thread(LOCK_CUSTOM1);
1047                                                 
1048                                                 if(domainSettings)
1049                                                         domainSettings->lastgoodframe = startFrame+globalBakeFrame;
1050                                                 
1051                                                 done = -1;
1052                                                 globalBakeFrame = 0;
1053                                                 globalBakeState = -1;
1054                                                 simAborted = 1;
1055                                                 BLI_unlock_thread(LOCK_CUSTOM1);
1056                                                 break;
1057                                         }
1058                                 } 
1059
1060                                 // redraw the 3D for showing progress once in a while...
1061                                 if(lastRedraw!=globalBakeFrame) {
1062 #if 0                                   
1063                                         ScrArea *sa;
1064                                         scene->r.cfra = startFrame+globalBakeFrame;
1065                                         lastRedraw = globalBakeFrame;
1066                                         ED_update_for_newframe(C, 0);
1067                                         sa= G.curscreen->areabase.first;
1068                                         while(sa) {
1069                                                 if(sa->spacetype == SPACE_VIEW3D) { scrarea_do_windraw(sa); }
1070                                                 sa= sa->next;   
1071                                         } 
1072                                         screen_swapbuffers();
1073 #endif
1074                                 } // redraw
1075                         }
1076                         end_progress_bar();
1077                 }
1078                 BLI_end_threads(&threads);
1079         } // El'Beem API init, thread creation 
1080         // --------------------------------------------------------------------------------------------
1081         else
1082         { // write config file to be run with command line simulator
1083                 pupmenu("Fluidsim Bake Message%t|Config file export not supported.%x0");
1084         } // config file export done!
1085
1086         // --------------------------------------------------------------------------------------------
1087         FS_FREE_CHANNELS;
1088
1089         // go back to "current" blender time
1090         waitcursor(0);
1091         
1092         if(globalBakeState >= 0)
1093         {
1094                 if(domainSettings)
1095                         domainSettings->lastgoodframe = startFrame+globalBakeFrame;
1096         }
1097         
1098         scene->r.cfra = origFrame;
1099         ED_update_for_newframe(C, 0);
1100
1101         if(!simAborted) {
1102                 char fsmessage[512];
1103                 char elbeemerr[256];
1104                 strcpy(fsmessage,"Fluidsim Bake Error: ");
1105                 // check if some error occurred
1106                 if(globalBakeState==-2) {
1107                         strcat(fsmessage,"Failed to initialize [Msg: ");
1108
1109                         elbeemGetErrorString(elbeemerr);
1110                         strcat(fsmessage,elbeemerr);
1111
1112                         strcat(fsmessage,"] |OK%x0");
1113                         pupmenu(fsmessage);
1114                 } // init error
1115         }
1116         
1117         // elbeemFree();
1118 }
1119
1120 void fluidsimFreeBake(struct Object *ob)
1121 {
1122         /* not implemented yet */
1123 }
1124
1125
1126 #else /* DISABLE_ELBEEM */
1127
1128 /* compile dummy functions for disabled fluid sim */
1129
1130 FluidsimSettings *fluidsimSettingsNew(struct Object *srcob) {
1131         return NULL;
1132 }
1133
1134 void fluidsimSettingsFree(FluidsimSettings *fss) {
1135 }
1136
1137 FluidsimSettings* fluidsimSettingsCopy(FluidsimSettings *fss) {
1138         return NULL;
1139 }
1140
1141 /* only compile dummy functions */
1142 void fluidsimBake(bContext *C, struct Object *ob) {
1143 }
1144
1145 void fluidsimFreeBake(struct Object *ob) {
1146 }
1147
1148 #endif /* DISABLE_ELBEEM */
1149