Skip to content

Commit

Permalink
Some changes made so that more things are working
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@732 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
henjo committed Feb 13, 2002
1 parent a6555b2 commit 79809df
Show file tree
Hide file tree
Showing 10 changed files with 720 additions and 129 deletions.
9 changes: 8 additions & 1 deletion mosh/src/Makefile
Expand Up @@ -57,16 +57,22 @@ mosh : $(OBJS)
$(parsergen): expandedexpression_parser.g
java antlr.Tool $(ANTLRFLAGS) expandedexpression_parser.g

$(walkergen): walker.g modelica_expression_parserTokenTypes.txt runtime/modelica_array.hpp runtime/numerical_array.hpp
#$(walkergen): walker.g modelica_expression_parserTokenTypes.txt runtime/modelica_array.hpp runtime/numerical_array.hpp
$(walkergen): walker.g modelica_expression_parserTokenTypes.txt
java antlr.Tool $(ANTLRFLAGS) walker.g
#$<


$(walkerobjs) : runtime/modelica_array.hpp runtime/numerical_array.hpp

expandedexpression_parser.g: expression_parser.g
java antlr.Tool -glib ../../modelica_parser/src/modelica_parser.g expression_parser.g

#modelica_parserTokenTypes.txt : ../../src/modelica_parserTokenTypes.txt
# cp ../../src/modelica_parserTokenTypes.txt .

depend :
gcc -M $(CXXFLAGS) mosh.cpp $(walkersrcs) > Makefile.depend

clean:
-rm -f *.o *~ core *.core mosh
Expand All @@ -77,3 +83,4 @@ reallyclean: clean

mosh.o : mosh.cpp ../../modelica_parser/src/modelica_lexer.hpp ../../modelica_parser/src/modelica_parser.hpp

include Makefile.depend
56 changes: 56 additions & 0 deletions mosh/src/Makefile.single
@@ -0,0 +1,56 @@
# -*- Mode: Makefile -*-
#
# TARGET is specified on the command line
# eg.
# >make -f Makefile.single TARGET=algorithm_for1
#

MODEQ=../../modeq/modeq
CC=gcc

CFLAGS= -W -Wall -ansi -pedantic-errors -Werror -g -I../../c_runtime

LDFLAGS= -L../../c_runtime -lm

CDIR = csrc
ODIR = obj
BDIR = .

MOFILES = $(TARGET:=.mo)
TARGETS = $(MOFILES:%.mo=$(BDIR)/%)
OBJS = $(MOFILES:%.mo=$(ODIR)/%.main.o)
GENC = $(MOFILES:%.mo=$(CDIR)/%.c)


all : $(CDIR) $(ODIR) $(BDIR) $(TARGETS)


$(TARGETS) : $(BDIR)/% : $(ODIR)/%.main.o
rm -f $@
gcc -g -o $@ $(LDFLAGS) $< -lc_runtime

$(OBJS) : $(ODIR)/%.main.o : $(CDIR)/%.c main.c
rm -f $(TARGETS)
@echo compiling $(subst .main.o,,$@)
$(CC) $(CFLAGS) \
-DCFILE_TO_INCLUDE="\"$(subst $(ODIR),$(CDIR),$(subst .main.o,.c,$@))\""\
-DCFUNCTION_TO_CALL="$(subst $(ODIR)/,,$(subst .main.o,,$@))_read_call_write"\
-c -o $@ main.c

$(GENC) : $(CDIR)/%.c : %.mo
@$(MODEQ) $< +d=codegen > $@

$(CDIR) $(ODIR) $(BDIR) : % :
mkdir $@

.SUFFIXES: .mo


.mo.c:
@$(MODEQ) $< +d=codegen > $(<:.mo=.c)

clean :
rm -f $(TARGETS)
rm -f $(OBJS)
rm -f $(GENC)
rm -f *~
266 changes: 242 additions & 24 deletions mosh/src/compiled_function.cpp
Expand Up @@ -36,13 +36,16 @@ value compiled_function::do_apply(value args)
// Execute

// Build executable
std::string build_command = "make -f Makefile.single TARGET="+m_filename+ " clean all";


std::string build_command = "sh -c \"make -f Makefile.single TARGET="+m_filename+ " all 1> cmdoutput.tmp 2>&1\"";
if (system(build_command.c_str()) == -1)
{
system("cat cmdoutput.tmp");
cout << "Failed to build file" << endl;
}

std::string execute_command = m_filename+" mosh_in.dat"+" result.dat";
std::string execute_command = "rm -f result.dat;" + m_filename+" mosh_in.dat"+" result.dat";

if (system(execute_command.c_str())==-1)
{
Expand Down Expand Up @@ -70,41 +73,256 @@ void compiled_function::write_input_file(value args)
cout << "Failed to open mosh_in.dat" << endl;
}

// function_argument* params = args.get_function_arguments();
// function_argument::parameter_iterator it = params->begin();
// for (; it != params->end();++it)
// {
// if (it->first.is_real())
// {
// fprintf(fp,"%e\n",it->first.get_real());
// // cout << "printing real value " << it->f <<endl;
// }
// else if (it->first.is_integer())
// {
// fprintf(fp,"%d\n",it->first.get_integer());
// }
// else
// {
// cout << "tried to print an unknown type" << endl;
// }
// }
function_argument* params = args.get_function_argument();
function_argument::parameter_iterator it = params->begin();
for (; it != params->end();++it)
{
if (it->first.is_real())
{
fprintf(fp,"# r!\n");
fprintf(fp,"%e\n",it->first.get_real());
}
else if (it->first.is_integer())
{
fprintf(fp,"# i!\n");
fprintf(fp,"%d\n",it->first.get_integer());
}
else if (it->first.is_real_array())
{
real_array arr = it->first.get_real_array();
std::vector<int> dims = arr.size();
fprintf(fp,"# r[%d ",dims.size());
for (int i = 0; i < (int)dims.size(); ++i)
{
fprintf(fp,"%d ",dims[i]);
}
fprintf(fp,"\n");
real_array::const_data_iterator it;
for (it = arr.data_begin(); it != arr.data_end(); ++it)
{
fprintf(fp,"%e\n",*it);
}
}
else if (it->first.is_integer_array())
{
integer_array arr = it->first.get_integer_array();
std::vector<int> dims = arr.size();
fprintf(fp,"# i[%d ",dims.size());
for (int i = 0; i < (int)dims.size(); ++i)
{
fprintf(fp,"%d ",dims[i]);
}
fprintf(fp,"\n");
integer_array::const_data_iterator it;
for (it = arr.data_begin(); it != arr.data_end(); ++it)
{
fprintf(fp,"%d\n",*it);
}
}
else
{
cout << "tried to print an unknown type" << endl;
}
}

fclose(fp);
}


struct type_desc_s {
char type;
int ndims;
int *dim_size;
};

typedef struct type_desc_s type_description;

void cleanup_description(type_description* desc)
{
if (desc->ndims > 0)
{
free(desc->dim_size);
}
}

void read_to_eol(FILE* file)
{
int c;
while (((c = fgetc(file)) != '\n') && (c != EOF));
}

int read_type_description(FILE* file, type_description* desc)
{
int c;
int i;
do
{
if ((c = fgetc(file)) == EOF) return 1;
if (c != '#') return 1;
if ((c = fgetc(file)) == EOF) return 1;
if (c != ' ') return 1;
if ((c = fgetc(file)) == EOF) return 1;
switch (c)
{
case 'i': /* integer */
case 'r': /* real */
case 'b': /* boolean */
case 's': /* string */
desc->type = c;
break;
default:
return 1;
}
if ((c = fgetc(file)) == EOF) return 1;
if (c == '!') /* scalar */
{
desc->ndims = 0;
desc->dim_size = 0;
break;
}
if (c != '[') return 1;
/* now is an array dim description */
if (fscanf(file,"%d",&desc->ndims) != 1) return 1;
if (desc->ndims < 0) return 1;
if (desc->ndims > 0)
{
desc->dim_size = (int*)malloc(desc->ndims*sizeof(int));
if (!desc->dim_size) return 1;
}
else
{
desc->dim_size = 0;
}
for (i = 0; i < desc->ndims; ++i)
{
if (fscanf(file,"%d",&desc->dim_size[i]) != 1)
{
free(desc->dim_size);
return 1;
}
}
break;

} while (0);

read_to_eol(file);

return 0;
}

void compiled_function::read_result_file(value* v)
{
FILE* fp = fopen("result.dat","r");

if (fp == NULL)
{
cout << "Failed to open data.out" << endl;
return;
}

std::vector<value> vals;
while (true)
{
type_description desc;
if (read_type_description(fp,&desc))
{
break;
}
if (desc.ndims == 0)
{
if (desc.type == 'r')
{
float f;
if (fscanf(fp,"%e",&f) != 1)
{
cleanup_description(&desc);
break;
}
read_to_eol(fp);
vals.push_back(value((double)f));
}
else if (desc.type == 'i')
{
int i;
if (fscanf(fp,"%d",&i) != 1)
{
cleanup_description(&desc);
break;
}
read_to_eol(fp);
vals.push_back(value(i));
}
else
{
cout << "Unknow result type\n";
break;
}
}
else
{
std::vector<int> dims(desc.dim_size,desc.dim_size+desc.ndims);
if (desc.type == 'r')
{
real_array arr(dims);
real_array::data_iterator it;
bool error = false;
for (it = arr.data_begin(); it != arr.data_end(); ++it)
{
float f;
if (fscanf(fp,"%e",&f) != 1)
{
cleanup_description(&desc);
error = true;
break;
}
*it = f;
}
if (error) break;
read_to_eol(fp);
vals.push_back(value(arr));
}
else if (desc.type == 'i')
{
integer_array arr(dims);
integer_array::data_iterator it;
bool error = false;
for (it = arr.data_begin(); it != arr.data_end(); ++it)
{
int i;
if (fscanf(fp,"%d",&i) != 1)
{
cleanup_description(&desc);
error = true;
break;
}
*it = i;
}
if (error) break;
read_to_eol(fp);
vals.push_back(value(arr));
}
else
{
cout << "Unknow result type\n";
break;
}
}

cleanup_description(&desc);
}
float f;
fscanf(fp,"%e",&f);
// cout << f << endl;
v->set_value(f);


if (vals.size() == 1)
{
*v = vals[0];
}
else
{
v->set_value(vals);
}



fclose(fp);
}

0 comments on commit 79809df

Please sign in to comment.