现有组件功能与Amesim组件比较对齐,补全功能;仿真服务启动环境检测,旧版json工程文件适配读取
This commit is contained in:
1 parent
b6c22a54f8
commit
6fc9afe41d
119 files changed
+260114
-351
No files matched your search
+76
-3
@@ -98,12 +98,32 @@ static double locate(int idx, double bound, int lower, double left, double right
|
||||
return right;
|
||||
}
|
||||
|
||||
#if NFRICTIONS
|
||||
static int friction_drives(NativeRun *r,double t,const double *state,double *drives) {
|
||||
r->nfev++;
|
||||
return model_friction_drives(t,state,drives);
|
||||
}
|
||||
|
||||
static double locate_breakaway(NativeRun *r,int index,double left,double right,
|
||||
NativeDense dense,void *context) {
|
||||
double state[NSTATES],drives[NFRICTIONS];
|
||||
for(int i=0;i<60;i++) {
|
||||
double mid=left+.5*(right-left);
|
||||
if(mid<=left || mid>=right) break;
|
||||
if(!dense(context,mid,state) || !friction_drives(r,mid,state,drives)) return NAN;
|
||||
if(fabs(drives[index])>model_frictions[index].breakaway_force) right=mid;
|
||||
else left=mid;
|
||||
}
|
||||
return right;
|
||||
}
|
||||
#endif
|
||||
|
||||
int native_accept(NativeRun *r, double t, double next, const double *old,
|
||||
const double *trial, NativeDense dense, void *context,
|
||||
double *accepted_time, double *accepted_state) {
|
||||
double when[2*(NSTOPS+1)], bounds[2*(NSTOPS+1)];
|
||||
double restitution[2*(NSTOPS+1)], thresholds[2*(NSTOPS+1)];
|
||||
int indices[2*(NSTOPS+1)], count=0;
|
||||
double when[2*(NSTOPS+NFRICTIONS+1)], bounds[2*(NSTOPS+NFRICTIONS+1)];
|
||||
double restitution[2*(NSTOPS+NFRICTIONS+1)], thresholds[2*(NSTOPS+NFRICTIONS+1)];
|
||||
int indices[2*(NSTOPS+NFRICTIONS+1)], friction[2*(NSTOPS+NFRICTIONS+1)], count=0;
|
||||
for (int j=0;j<NSTOPS;j++) {
|
||||
NativeStop s=model_stops[j]; int v=s.velocity_index, x=v+1;
|
||||
double vt0=1e-12*fmax(fabs(old[v]),1), vt1=1e-12*fmax(fabs(trial[v]),1);
|
||||
@@ -120,12 +140,38 @@ int native_accept(NativeRun *r, double t, double next, const double *old,
|
||||
}
|
||||
if (isfinite(at)) {
|
||||
when[count]=at; bounds[count]=bound; indices[count]=v;
|
||||
friction[count]=-1;
|
||||
restitution[count]=lower?s.lower_restitution:s.upper_restitution;
|
||||
thresholds[count]=lower?s.lower_threshold:s.upper_threshold;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
#if NFRICTIONS
|
||||
double before[NFRICTIONS],after[NFRICTIONS];
|
||||
if(!friction_drives(r,t,old,before) || !friction_drives(r,next,trial,after)) return -1;
|
||||
for(int j=0;j<NFRICTIONS;j++) {
|
||||
NativeFriction f=model_frictions[j];
|
||||
double mode=old[f.mode_index],at=INFINITY;
|
||||
if(fabs(mode)<.5) {
|
||||
if(fabs(before[j])>f.breakaway_force) at=t;
|
||||
else if(fabs(after[j])>f.breakaway_force)
|
||||
at=locate_breakaway(r,j,t,next,dense,context);
|
||||
} else {
|
||||
double direction=mode>0?1:-1;
|
||||
if(direction*old[f.velocity_index]>f.velocity_threshold &&
|
||||
direction*trial[f.velocity_index]<=f.velocity_threshold)
|
||||
at=locate(f.velocity_index,direction*f.velocity_threshold,mode>0,t,next,dense,context);
|
||||
else if(direction*old[f.velocity_index]>=0 && direction*trial[f.velocity_index]<0)
|
||||
at=locate(f.velocity_index,0,mode>0,t,next,dense,context);
|
||||
}
|
||||
if(isnan(at)) return -1;
|
||||
if(isfinite(at)) {
|
||||
when[count]=at;indices[count]=f.velocity_index;friction[count]=j;
|
||||
bounds[count]=restitution[count]=thresholds[count]=0;count++;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
double stop=next;
|
||||
for (int i=0;i<count;i++) stop=fmin(stop,when[i]);
|
||||
if (r->options.record_samples) {
|
||||
@@ -139,10 +185,28 @@ int native_accept(NativeRun *r, double t, double next, const double *old,
|
||||
if (count) {
|
||||
if (!dense(context,stop,accepted_state)) return -1;
|
||||
for (int i=0;i<count;i++) if (fabs(when[i]-stop)<=1e-12*fmax(fabs(stop),1)) {
|
||||
if(friction[i]>=0) continue;
|
||||
double incoming=accepted_state[indices[i]];
|
||||
accepted_state[indices[i]]=fabs(incoming)<=thresholds[i]?0:-restitution[i]*incoming;
|
||||
accepted_state[indices[i]+1]=bounds[i];
|
||||
}
|
||||
#if NFRICTIONS
|
||||
/* Stop/reverse only at an accepted event. The discrete mode travels
|
||||
* with saved states so replayed results and Jacobian trials are pure. */
|
||||
double drives[NFRICTIONS];
|
||||
for(int i=0;i<count;i++) if(friction[i]<0 && fabs(when[i]-stop)<=1e-12*fmax(fabs(stop),1)) {
|
||||
for(int j=0;j<NFRICTIONS;j++) if(model_frictions[j].velocity_index==indices[i]) {
|
||||
double velocity=accepted_state[indices[i]];
|
||||
accepted_state[model_frictions[j].mode_index]=velocity>0?1:(velocity<0?-1:0);
|
||||
}
|
||||
}
|
||||
if(!friction_drives(r,stop,accepted_state,drives)) return -1;
|
||||
for(int i=0;i<count;i++) if(friction[i]>=0 && fabs(when[i]-stop)<=1e-12*fmax(fabs(stop),1)) {
|
||||
int j=friction[i];NativeFriction f=model_frictions[j];
|
||||
accepted_state[f.velocity_index]=0;
|
||||
accepted_state[f.mode_index]=fabs(drives[j])>f.breakaway_force ? (drives[j]>0?1:-1) : 0;
|
||||
}
|
||||
#endif
|
||||
if (!native_append(r,stop,accepted_state)) return -1;
|
||||
while (r->options.start+r->sample_index*r->options.sample_step<=stop) r->sample_index++;
|
||||
r->events++;
|
||||
@@ -155,6 +219,15 @@ int native_accept(NativeRun *r, double t, double next, const double *old,
|
||||
int native_solve(NativeRun *r) {
|
||||
double y[NSTATES];
|
||||
if (!model_init(y)) { r->status=2; r->message="Native model initialization failed."; return 0; }
|
||||
#if NFRICTIONS
|
||||
double drives[NFRICTIONS];
|
||||
if(!friction_drives(r,r->options.start,y,drives)) {r->status=2;r->message="Cannot initialize friction.";return 0;}
|
||||
for(int j=0;j<NFRICTIONS;j++) {
|
||||
NativeFriction f=model_frictions[j];
|
||||
if(fabs(y[f.mode_index])<.5 && fabs(drives[j])>f.breakaway_force)
|
||||
y[f.mode_index]=drives[j]>0?1:-1;
|
||||
}
|
||||
#endif
|
||||
r->sample_index=1;
|
||||
if (!native_append(r,r->options.start,y)) { r->status=2; r->message="Cannot allocate samples."; return 0; }
|
||||
r->wall_start=native_wall_time(); r->cpu_start=native_cpu_time();
|
||||
|
||||
Reference in new issue
Block a user