Initial equation called in each time step
File.write is called 1003 times if the when-equation doesn't stop us. With the when-equation, it is called 113 times.
With -lv LOG_INIT I can tell that initialization finishes before the first call. So it is very much not an initial equation. The flat Modelica seems to suggest it is a front-end issue...
package File
class File
extends ExternalObject;
function constructor
output File file;
external "C" file=om_file_new() annotation(Include="
#ifndef __OMC_FILE_NEW
#define __OMC_FILE_NEW
#include <stdio.h>
#include <gc.h>
static inline void* om_file_new()
{
return GC_malloc(sizeof(FILE*));
}
#endif
");
end constructor;
function destructor
input File file;
external "C" om_file_free(file) annotation(Include="
#ifndef __OMC_FILE_FREE
#define __OMC_FILE_FREE
#include <stdio.h>
static inline void om_file_free(FILE **file)
{
if (*file) {
fclose(*file);
*file = 0;
}
}
#endif
");
end destructor;
end File;
type Mode = enumeration(Read,Write);
function open
input File file;
input String filename;
input Mode mode := Mode.Read;
output Boolean success;
external "C" success = om_file_open(file,filename,mode) annotation(Include="
#ifndef __OMC_FILE_OPEN
#define __OMC_FILE_OPEN
#include <stdio.h>
static inline int om_file_open(FILE **file,const char *filename,int mode)
{
if (*file) {
fclose(*file);
}
*file = fopen(filename, mode == 1 ? \"rb\" : \"wb\");
fflush(NULL);
fprintf(stderr, \"opening file...\n\");
return *file != 0;
}
#endif
");
end open;
function write
input File file;
input String data;
output Boolean success;
external "C" success = om_file_write(file,data) annotation(Include="
#ifndef __OMC_FILE_WRITE
#define __OMC_FILE_WRITE
#include <stdio.h>
static inline int om_file_write(FILE **file,const char *data)
{
if (!*file) {
return 0;
}
fflush(NULL);
fprintf(stderr, \"writing...\n\");
return 1 == fputs(data,*file);
}
#endif
");
end write;
end File;
model M
File.File file = File.File();
initial equation
File.open(file,"abc.txt",File.Mode.Write);
File.write(file,"def.fafaf\n");
equation
when time>0.1 then
terminate("time>0.1\n");
end when;
end M;
I can fix the front-end stuff first ;)